-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashmap.c
More file actions
106 lines (86 loc) · 2.05 KB
/
Hashmap.c
File metadata and controls
106 lines (86 loc) · 2.05 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include "Hashmap.h"
int hashmap_add(Hashmap *hash, char *key, void *value) {
HashmapItem *item;
unsigned int hashval;
if (hashmap_get(hash, key) != NULL)
return 0;
hashval = hashmap_hash(hash, key);
item = malloc(sizeof(HashmapItem));
if (item == NULL)
return 0;
item->key = strdup(key);
item->value = value;
item->next = hash->table[hashval];
hash->table[hashval] = item;
hash->usage += 1;
return 1;
}
void hashmap_delete(Hashmap *hash, void (*valuefreefunc)(void *)) {
HashmapItem *item, *temp;
int i;
if (hash == NULL)
return;
for (i = 0; i < hash->size; i++) {
item = hash->table[i];
while (item != NULL) {
temp = item;
item = item->next;
free(temp->key);
if (valuefreefunc != NULL)
valuefreefunc(temp->value);
free(temp);
}
}
free(hash->table);
free(hash);
}
void *hashmap_get(Hashmap *hash, char *key) {
HashmapItem *item;
unsigned int hashval = hashmap_hash(hash, key);
for (item = hash->table[hashval]; item != NULL; item = item->next) {
if (strcmp(key, item->key) == 0)
return item->value;
}
return NULL;
}
unsigned int hashmap_hash(Hashmap *hash, char *key) {
unsigned int hashval = 0;
for (; *key != '\0'; key++)
hashval = *key + (hashval << 5) - hashval;
return hashval % hash->size;
}
char **hashmap_keys(Hashmap *hash) {
char **keys;
HashmapItem *item;
int i, j;
keys = malloc(sizeof(char *) * hash->usage);
j = 0;
for (i = 0; i < hash->size; i++) {
item = hash->table[i];
while (item != NULL) {
keys[j++] = item->key;
item = item->next;
}
}
return keys;
}
Hashmap *hashmap_new(int size) {
Hashmap *hash;
int i;
if (size < 1)
return NULL;
hash = malloc(sizeof(HashmapItem));
if (hash == NULL)
return NULL;
hash->table = malloc(sizeof(HashmapItem *) * size);
if (hash->table == NULL)
return NULL;
for (i = 0; i < size; i++)
hash->table[i] = NULL;
hash->size = size;
hash->usage = 0;
return hash;
}
int hashmap_size(Hashmap *hash) {
return hash->usage;
}