-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashmap.c
231 lines (204 loc) · 5.66 KB
/
hashmap.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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
//
// hashmap.c
// hashmap
//
// Created by XuNing on 2021/10/31.
//
#include "hashmap.h"
#include <errno.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
typedef struct hashmap_entry hashmap_entry;
struct hashmap_entry {
void *key;
void *value;
int hash;
hashmap_entry *next;
};
struct hashmap {
hashmap_entry **buckets;
size_t bucket_count;
size_t size;
int (*hash)(void *key);
bool (*is_keys_equal)(void *key1, void *key2);
pthread_mutex_t lock;
};
static inline int default_hash_key(void *key) {
char *data = (char *)key;
size_t key_size = strlen(data);
int h = (int)key_size;
for (size_t i = 0; i < key_size; i++) {
h = 31 * h + *data;
data++;
}
return h;
}
static inline bool default_is_keys_equal(void *key1, void *key2) {
return strcmp((const char *)key1, (const char *)key2) == 0;
}
hashmap *hashmap_create(size_t initial_capacity, int (*hash)(void *key),
bool (*is_keys_equal)(void *key1, void *key2)) {
hashmap *map = (hashmap *)malloc(sizeof(hashmap));
if (map == NULL) {
return NULL;
}
// 0.75 load factor
size_t min_bucket_count = initial_capacity * 4 / 3;
map->bucket_count = 1;
while (map->bucket_count < min_bucket_count) {
// Bucket count must be power of 2
map->bucket_count <<= 1;
}
map->buckets =
(hashmap_entry **)(calloc(map->bucket_count, sizeof(hashmap_entry *)));
if (map->buckets == NULL) {
free(map);
return NULL;
}
map->size = 0;
map->hash = (hash == NULL ? default_hash_key : hash);
map->is_keys_equal =
(is_keys_equal == NULL ? default_is_keys_equal : is_keys_equal);
pthread_mutex_init(&map->lock, NULL);
return map;
}
void hashmap_destroy(hashmap *map) {
for (size_t i = 0; i < map->bucket_count; i++) {
hashmap_entry *e = map->buckets[i];
while (e != NULL) {
hashmap_entry *next = e->next;
free(e);
e = next;
}
}
free(map->buckets);
map->buckets = NULL;
pthread_mutex_destroy(&map->lock);
}
static inline int hash_key(hashmap *map, void *key) {
int h = map->hash(key);
return h;
}
static inline bool is_keys_equal(void *key1, int hash1, void *key2, int hash2,
bool (*equals)(void *, void *)) {
if (key1 == key2) {
return 1;
}
if (hash1 != hash2) {
return 0;
}
return equals(key1, key2);
}
static inline size_t calculate_index(size_t bucket_count, int hash) {
return ((size_t)hash) & (bucket_count - 1);
}
static hashmap_entry *create_entry(void *key, void *value, int hash) {
hashmap_entry *e = (hashmap_entry *)malloc(sizeof(hashmap_entry));
if (e == NULL) {
return NULL;
}
e->key = key;
e->value = value;
e->hash = hash;
e->next = NULL;
return e;
}
static void expand_if_needed(hashmap *map) {
if (map->size > (map->bucket_count * 3 / 4)) {
size_t new_bucket_count = map->bucket_count << 1;
hashmap_entry **new_buckets =
(hashmap_entry **)calloc(new_bucket_count, sizeof(hashmap_entry *));
if (new_buckets == NULL) {
return;
}
// Move over existing entries.
for (size_t i = 0; i < map->bucket_count; i++) {
hashmap_entry *e = map->buckets[i];
while (e != NULL) {
hashmap_entry *next = e->next;
size_t index = calculate_index(new_bucket_count, e->hash);
e->next = new_buckets[index];
new_buckets[index] = e;
e = next;
}
}
free(map->buckets);
map->buckets = new_buckets;
map->bucket_count = new_bucket_count;
}
}
void *hashmap_set(hashmap *map, void *key, void *value) {
int hash = map->hash(key);
size_t index = calculate_index(map->bucket_count, hash);
hashmap_entry **p = &(map->buckets[index]);
while (true) {
hashmap_entry *current = *p;
// Add a new hashmap_entry
if (current == NULL) {
*p = create_entry(key, value, hash);
if (*p == NULL) {
errno = ENOMEM;
return NULL;
}
map->size++;
expand_if_needed(map);
return NULL;
}
// Replace existing hashmap_entry
if (is_keys_equal(current->key, current->hash, key, hash,
map->is_keys_equal)) {
void *old_value = current->value;
current->value = value;
return old_value;
}
p = ¤t->next;
}
}
void *hashmap_get(hashmap *map, void *key) {
int hash = hash_key(map, key);
size_t index = calculate_index(map->bucket_count, hash);
hashmap_entry *e = map->buckets[index];
while (e != NULL) {
if (is_keys_equal(e->key, e->hash, key, hash, map->is_keys_equal)) {
return e->value;
}
e = e->next;
}
return NULL;
}
void *hashmap_remove(hashmap *map, void *key) {
int hash = hash_key(map, key);
size_t index = calculate_index(map->bucket_count, hash);
hashmap_entry **p = &(map->buckets[index]);
hashmap_entry *current;
while ((current = *p) != NULL) {
if (is_keys_equal(current->key, current->hash, key, hash,
map->is_keys_equal)) {
void *value = current->value;
*p = current->next;
free(current);
map->size--;
return value;
}
p = ¤t->next;
}
return NULL;
}
void hashmap_iterate(hashmap *map,
bool (*callback)(void *key, void *value, void *context),
void *context) {
for (size_t i = 0; i < map->bucket_count; i++) {
hashmap_entry *e = map->buckets[i];
while (e != NULL) {
hashmap_entry *next = e->next;
if (!callback(e->key, e->value, context)) {
return;
}
e = next;
}
}
}
size_t hashmap_count(hashmap *map) { return map->size; }
void hashmap_lock(hashmap *map) { pthread_mutex_lock(&map->lock); }
void hashmap_unlock(hashmap *map) { pthread_mutex_unlock(&map->lock); }