-
Notifications
You must be signed in to change notification settings - Fork 0
/
clientHash.c
50 lines (37 loc) · 1 KB
/
clientHash.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "clientHash.h"
clientHash *ch_create() {
clientHash *hashmap = (clientHash *)malloc(sizeof(clientHash));
hashmap->max_size = CLIENT_HASH_SIZE;
hashmap->curr_size = 0;
hashmap->map = (clientList **)malloc(CLIENT_HASH_SIZE * sizeof(clientList *));
return hashmap;
}
client *ch_get(clientHash *hashmap, int id) {
int index = ch_hash(id);
clientList *list = hashmap->map[index];
return cl_find(list, id);
}
void ch_put(clientHash *hashmap, client *c) {
int index = ch_hash(c->id);
if (hashmap->map[index] == NULL) hashmap->map[index] = cl_create();
cl_add(hashmap->map[index], c);
hashmap->curr_size++;
}
void ch_remove(clientHash *hashmap, int id) {
client *client = ch_get(hashmap, id);
if (client != NULL) {
cl_remove(client);
hashmap->curr_size--;
}
}
void ch_clear(clientHash *hashmap) {
int i;
for (i = 0; i < hashmap->max_size; i++) {
if (hashmap->map[i] != NULL)
cl_clear(hashmap->map[i]->head);
}
free(hashmap->map);
}
int ch_hash(int id) {
return id % CLIENT_HASH_SIZE;
}