forked from blechschmidt/massdns
-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
hashmap.h
339 lines (328 loc) · 9.41 KB
/
hashmap.h
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#ifndef MASSDNS_HASHMAP_H
#define MASSDNS_HASHMAP_H
/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <sys/types.h>
// http://www.cse.yorku.ca/~oz/hash.html
unsigned long hash_djb2(unsigned char *str)
{
unsigned long hash = 5381;
int c;
while ((c = *str++) != 0)
{
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
}
return hash;
}
int hash_string(void *str)
{
return (int) hash_djb2((unsigned char *) str);
}
typedef struct Entry Entry;
struct Entry {
void* key;
int hash;
void* value;
Entry* next;
};
typedef struct Hashmap {
Entry** buckets;
size_t bucketCount;
int (*hash)(void* key);
bool (*equals)(void* keyA, void* keyB);
size_t size;
} Hashmap;
Hashmap* hashmapCreate(size_t initialCapacity,
int (*hash)(void* key), bool (*equals)(void* keyA, void* keyB)) {
assert(hash != NULL);
assert(equals != NULL);
Hashmap* map = malloc(sizeof(Hashmap));
if (map == NULL) {
return NULL;
}
// 0.75 load factor.
size_t minimumBucketCount = initialCapacity * 4 / 3;
map->bucketCount = 1;
while (map->bucketCount <= minimumBucketCount) {
// Bucket count must be power of 2.
map->bucketCount <<= 1;
}
map->buckets = calloc(map->bucketCount, sizeof(Entry*));
if (map->buckets == NULL) {
free(map);
return NULL;
}
map->size = 0;
map->hash = hash;
map->equals = equals;
return map;
}
/**
* Hashes the given key.
*/
#ifdef __clang__
__attribute__((no_sanitize("integer")))
#endif
static inline int hashKey(Hashmap* map, void* key) {
int h = map->hash(key);
// We apply this secondary hashing discovered by Doug Lea to defend
// against bad hashes.
h += ~(h << 9);
h ^= (((unsigned int) h) >> 14);
h += (h << 4);
h ^= (((unsigned int) h) >> 10);
return h;
}
size_t hashmapSize(Hashmap* map) {
return map->size;
}
static inline size_t calculateIndex(size_t bucketCount, int hash) {
return ((size_t) hash) & (bucketCount - 1);
}
static void expandIfNecessary(Hashmap* map) {
// If the load factor exceeds 0.75...
if (map->size > (map->bucketCount * 3 / 4)) {
// Start off with a 0.33 load factor.
size_t newBucketCount = map->bucketCount << 1;
Entry** newBuckets = calloc(newBucketCount, sizeof(Entry*));
if (newBuckets == NULL) {
// Abort expansion.
return;
}
// Move over existing entries.
size_t i;
for (i = 0; i < map->bucketCount; i++) {
Entry* entry = map->buckets[i];
while (entry != NULL) {
Entry* next = entry->next;
size_t index = calculateIndex(newBucketCount, entry->hash);
entry->next = newBuckets[index];
newBuckets[index] = entry;
entry = next;
}
}
// Copy over internals.
free(map->buckets);
map->buckets = newBuckets;
map->bucketCount = newBucketCount;
}
}
void hashmapFree(Hashmap* map) {
size_t i;
for (i = 0; i < map->bucketCount; i++) {
Entry* entry = map->buckets[i];
while (entry != NULL) {
Entry* next = entry->next;
free(entry);
entry = next;
}
}
free(map->buckets);
free(map);
}
#ifdef __clang__
__attribute__((no_sanitize("integer")))
#endif
/* FIXME: relies on signed integer overflow, which is undefined behavior */
int hashmapHash(void* key, size_t keySize) {
int h = keySize;
char* data = (char*) key;
size_t i;
for (i = 0; i < keySize; i++) {
h = h * 31 + *data;
data++;
}
return h;
}
static Entry* createEntry(void* key, int hash, void* value) {
Entry* entry = malloc(sizeof(Entry));
if (entry == NULL) {
return NULL;
}
entry->key = key;
entry->hash = hash;
entry->value = value;
entry->next = NULL;
return entry;
}
static inline bool equalKeys(void* keyA, int hashA, void* keyB, int hashB,
bool (*equals)(void*, void*)) {
if (keyA == keyB) {
return true;
}
if (hashA != hashB) {
return false;
}
return equals(keyA, keyB);
}
void* hashmapPut(Hashmap* map, void* key, void* value) {
int hash = hashKey(map, key);
size_t index = calculateIndex(map->bucketCount, hash);
Entry** p = &(map->buckets[index]);
while (true) {
Entry* current = *p;
// Add a new entry.
if (current == NULL) {
*p = createEntry(key, hash, value);
if (*p == NULL) {
errno = ENOMEM;
return NULL;
}
map->size++;
expandIfNecessary(map);
return NULL;
}
// Replace existing entry.
if (equalKeys(current->key, current->hash, key, hash, map->equals)) {
void* oldValue = current->value;
current->value = value;
return oldValue;
}
// Move to next entry.
p = ¤t->next;
}
}
void* hashmapGet(Hashmap* map, void* key) {
int hash = hashKey(map, key);
size_t index = calculateIndex(map->bucketCount, hash);
Entry* entry = map->buckets[index];
while (entry != NULL) {
if (equalKeys(entry->key, entry->hash, key, hash, map->equals)) {
return entry->value;
}
entry = entry->next;
}
return NULL;
}
void* hashmapGetWithKey(Hashmap* map, void* key, void **originalKey) {
int hash = hashKey(map, key);
size_t index = calculateIndex(map->bucketCount, hash);
Entry* entry = map->buckets[index];
while (entry != NULL) {
if (equalKeys(entry->key, entry->hash, key, hash, map->equals)) {
*originalKey = entry->key;
return entry->value;
}
entry = entry->next;
}
return NULL;
}
bool hashmapContainsKey(Hashmap* map, void* key) {
int hash = hashKey(map, key);
size_t index = calculateIndex(map->bucketCount, hash);
Entry* entry = map->buckets[index];
while (entry != NULL) {
if (equalKeys(entry->key, entry->hash, key, hash, map->equals)) {
return true;
}
entry = entry->next;
}
return false;
}
void* hashmapMemoize(Hashmap* map, void* key,
void* (*initialValue)(void* key, void* context), void* context) {
int hash = hashKey(map, key);
size_t index = calculateIndex(map->bucketCount, hash);
Entry** p = &(map->buckets[index]);
while (true) {
Entry* current = *p;
// Add a new entry.
if (current == NULL) {
*p = createEntry(key, hash, NULL);
if (*p == NULL) {
errno = ENOMEM;
return NULL;
}
void* value = initialValue(key, context);
(*p)->value = value;
map->size++;
expandIfNecessary(map);
return value;
}
// Return existing value.
if (equalKeys(current->key, current->hash, key, hash, map->equals)) {
return current->value;
}
// Move to next entry.
p = ¤t->next;
}
}
void* hashmapRemove(Hashmap* map, void* key) {
int hash = hashKey(map, key);
size_t index = calculateIndex(map->bucketCount, hash);
// Pointer to the current entry.
Entry** p = &(map->buckets[index]);
Entry* current;
while ((current = *p) != NULL) {
if (equalKeys(current->key, current->hash, key, hash, map->equals)) {
void* value = current->value;
*p = current->next;
free(current);
map->size--;
return value;
}
p = ¤t->next;
}
return NULL;
}
void hashmapForEach(Hashmap* map,
bool (*callback)(void* key, void* value, void* context),
void* context) {
size_t i;
for (i = 0; i < map->bucketCount; i++) {
Entry* entry = map->buckets[i];
while (entry != NULL) {
Entry *next = entry->next;
if (!callback(entry->key, entry->value, context)) {
return;
}
entry = next;
}
}
}
size_t hashmapCurrentCapacity(Hashmap* map) {
size_t bucketCount = map->bucketCount;
return bucketCount * 3 / 4;
}
size_t hashmapCountCollisions(Hashmap* map) {
size_t collisions = 0;
size_t i;
for (i = 0; i < map->bucketCount; i++) {
Entry* entry = map->buckets[i];
while (entry != NULL) {
if (entry->next != NULL) {
collisions++;
}
entry = entry->next;
}
}
return collisions;
}
int hashmapIntHash(void* key) {
// Return the key value itself.
return *((int*) key);
}
bool hashmapIntEquals(void* keyA, void* keyB) {
int a = *((int*) keyA);
int b = *((int*) keyB);
return a == b;
}
#endif