-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmap.c
422 lines (356 loc) · 9.15 KB
/
map.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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
//
// map.h
//
// Created by Mashpoe on 1/15/21.
//
#include "map.h"
#include <stdlib.h>
#include <string.h>
//#include <stdio.h>
#define HASHMAP_DEFAULT_CAPACITY 20
#define HASHMAP_MAX_LOAD 0.75f
#define HASHMAP_RESIZE_FACTOR 2
struct bucket
{
// `next` must be the first struct element.
// changing the order will break multiple functions
struct bucket* next;
// key, key size, key hash, and associated value
const void* key;
size_t ksize;
uint32_t hash;
uintptr_t value;
};
struct hashmap
{
struct bucket* buckets;
int capacity;
int count;
#ifdef __HASHMAP_REMOVABLE
// "tombstones" are empty buckets from removing elements
int tombstone_count;
#endif
// a linked list of all valid entries, in order
struct bucket* first;
// lets us know where to add the next element
struct bucket* last;
};
hashmap* hashmap_create(void)
{
hashmap* m = malloc(sizeof(hashmap));
if (m == NULL)
{
return NULL;
}
m->capacity = HASHMAP_DEFAULT_CAPACITY;
m->count = 0;
#ifdef __HASHMAP_REMOVABLE
m->tombstone_count = 0;
#endif
m->buckets = calloc(HASHMAP_DEFAULT_CAPACITY, sizeof(struct bucket));
if (m->buckets == NULL) {
free(m);
return NULL;
}
m->first = NULL;
// this prevents branching in hashmap_set.
// m->first will be treated as the "next" pointer in an imaginary bucket.
// when the first item is added, m->first will be set to the correct address.
m->last = (struct bucket*)&m->first;
return m;
}
void hashmap_free(hashmap* m)
{
free(m->buckets);
free(m);
}
// puts an old bucket into a resized hashmap
static struct bucket* resize_entry(hashmap* m, struct bucket* old_entry)
{
uint32_t index = old_entry->hash % m->capacity;
for (;;)
{
struct bucket* entry = &m->buckets[index];
if (entry->key == NULL)
{
*entry = *old_entry; // copy data from old entry
return entry;
}
index = (index + 1) % m->capacity;
}
}
static int hashmap_resize(hashmap* m)
{
int old_capacity = m->capacity;
struct bucket* old_buckets = m->buckets;
m->capacity *= HASHMAP_RESIZE_FACTOR;
// initializes all bucket fields to null
m->buckets = calloc(m->capacity, sizeof(struct bucket));
if (m->buckets == NULL) {
m->capacity = old_capacity;
m->buckets = old_buckets;
return -1;
}
// same trick; avoids branching
m->last = (struct bucket*)&m->first;
#ifdef __HASHMAP_REMOVABLE
m->count -= m->tombstone_count;
m->tombstone_count = 0;
#endif
// assumes that an empty map won't be resized
do
{
#ifdef __HASHMAP_REMOVABLE
// skip entry if it's a "tombstone"
struct bucket* current = m->last->next;
if (current->key == NULL)
{
m->last->next = current->next;
// skip to loop condition
continue;
}
#endif
m->last->next = resize_entry(m, m->last->next);
m->last = m->last->next;
} while (m->last->next != NULL);
free(old_buckets);
return 0;
}
#define HASHMAP_HASH_INIT 2166136261u
// FNV-1a hash function
static inline uint32_t hash_data(const unsigned char* data, size_t size)
{
size_t nblocks = size / 8;
uint64_t hash = HASHMAP_HASH_INIT;
for (size_t i = 0; i < nblocks; ++i)
{
hash ^= (uint64_t)data[0] << 0 | (uint64_t)data[1] << 8 |
(uint64_t)data[2] << 16 | (uint64_t)data[3] << 24 |
(uint64_t)data[4] << 32 | (uint64_t)data[5] << 40 |
(uint64_t)data[6] << 48 | (uint64_t)data[7] << 56;
hash *= 0xbf58476d1ce4e5b9;
data += 8;
}
uint64_t last = size & 0xff;
switch (size % 8)
{
case 7:
last |= (uint64_t)data[6] << 56; /* fallthrough */
case 6:
last |= (uint64_t)data[5] << 48; /* fallthrough */
case 5:
last |= (uint64_t)data[4] << 40; /* fallthrough */
case 4:
last |= (uint64_t)data[3] << 32; /* fallthrough */
case 3:
last |= (uint64_t)data[2] << 24; /* fallthrough */
case 2:
last |= (uint64_t)data[1] << 16; /* fallthrough */
case 1:
last |= (uint64_t)data[0] << 8;
hash ^= last;
hash *= 0xd6e8feb86659fd93;
}
// compress to a 32-bit result.
// also serves as a finalizer.
return (uint32_t)(hash ^ hash >> 32);
}
static struct bucket* find_entry(hashmap* m, const void* key, size_t ksize, uint32_t hash)
{
uint32_t index = hash % m->capacity;
for (;;)
{
struct bucket* entry = &m->buckets[index];
#ifdef __HASHMAP_REMOVABLE
// compare sizes, then hashes, then key data as a last resort.
// check for tombstone
if ((entry->key == NULL && entry->value == 0) ||
// check for valid matching entry
(entry->key != NULL &&
entry->ksize == ksize &&
entry->hash == hash &&
memcmp(entry->key, key, ksize) == 0))
{
// return the entry if a match or an empty bucket is found
return entry;
}
#else
// kind of a thicc condition;
// I didn't want this to span multiple if statements or functions.
if (entry->key == NULL ||
// compare sizes, then hashes, then key data as a last resort.
(entry->ksize == ksize &&
entry->hash == hash &&
memcmp(entry->key, key, ksize) == 0))
{
// return the entry if a match or an empty bucket is found
return entry;
}
#endif
//printf("collision\n");
index = (index + 1) % m->capacity;
}
}
int hashmap_set(hashmap* m, const void* key, size_t ksize, uintptr_t val)
{
if (m->count + 1 > HASHMAP_MAX_LOAD * m->capacity) {
if (hashmap_resize(m) == -1)
return -1;
}
uint32_t hash = hash_data(key, ksize);
struct bucket* entry = find_entry(m, key, ksize, hash);
if (entry->key == NULL)
{
m->last->next = entry;
m->last = entry;
entry->next = NULL;
++m->count;
entry->key = key;
entry->ksize = ksize;
entry->hash = hash;
}
entry->value = val;
return 0;
}
int hashmap_get_set(hashmap* m, const void* key, size_t ksize, uintptr_t* out_in)
{
if (m->count + 1 > HASHMAP_MAX_LOAD * m->capacity) {
if (hashmap_resize(m) == -1)
return -1;
}
uint32_t hash = hash_data(key, ksize);
struct bucket* entry = find_entry(m, key, ksize, hash);
if (entry->key == NULL)
{
m->last->next = entry;
m->last = entry;
entry->next = NULL;
++m->count;
entry->value = *out_in;
entry->key = key;
entry->ksize = ksize;
entry->hash = hash;
return 0;
}
*out_in = entry->value;
return 1;
}
int hashmap_set_free(hashmap* m, const void* key, size_t ksize, uintptr_t val, hashmap_callback c, void* usr)
{
if (m->count + 1 > HASHMAP_MAX_LOAD * m->capacity) {
if (hashmap_resize(m) == -1)
return -1;
}
uint32_t hash = hash_data(key, ksize);
struct bucket *entry = find_entry(m, key, ksize, hash);
if (entry->key == NULL)
{
m->last->next = entry;
m->last = entry;
entry->next = NULL;
++m->count;
entry->key = key;
entry->ksize = ksize;
entry->hash = hash;
entry->value = val;
// there was no overwrite, exit the function.
return 0;
}
// allow the callback to free entry data.
// use old key and value so the callback can free them.
// the old key and value will be overwritten after this call.
int error = c(entry->key, ksize, entry->value, usr);
// overwrite the old key pointer in case the callback frees it.
entry->key = key;
entry->value = val;
return error;
}
int hashmap_get(hashmap* m, const void* key, size_t ksize, uintptr_t* out_val)
{
uint32_t hash = hash_data(key, ksize);
struct bucket* entry = find_entry(m, key, ksize, hash);
// if there is no match, output val will just be NULL
*out_val = entry->value;
return entry->key != NULL ? 1 : 0;
}
#ifdef __HASHMAP_REMOVABLE
// doesn't "remove" the element per se, but it will be ignored.
// the element will eventually be removed when the map is resized.
void hashmap_remove(hashmap* m, const void* key, size_t ksize)
{
uint32_t hash = hash_data(key, ksize);
struct bucket* entry = find_entry(m, key, ksize, hash);
if (entry->key != NULL)
{
// "tombstone" entry is signified by a NULL key with a nonzero value
// element removal is optional because of the overhead of tombstone checks
entry->key = NULL;
entry->value = 0xDEAD; // I mean, it's a tombstone...
++m->tombstone_count;
}
}
void hashmap_remove_free(hashmap* m, const void* key, size_t ksize, hashmap_callback c, void* usr)
{
uint32_t hash = hash_data(key, ksize);
struct bucket* entry = find_entry(m, key, ksize, hash);
if (entry->key != NULL)
{
c(entry->key, entry->ksize, entry->value, usr);
// "tombstone" entry is signified by a NULL key with a nonzero value
// element removal is optional because of the overhead of tombstone checks
entry->key = NULL;
entry->value = 0xDEAD; // I mean, it's a tombstone...
++m->tombstone_count;
}
}
#endif
int hashmap_size(hashmap* m)
{
#ifdef __HASHMAP_REMOVABLE
return m->count - m->tombstone_count;
#else
return m->count;
#endif
}
int hashmap_iterate(hashmap* m, hashmap_callback c, void* user_ptr)
{
// loop through the linked list of valid entries
// this way we can skip over empty buckets
struct bucket* current = m->first;
int error = 0;
while (current != NULL)
{
#ifdef __HASHMAP_REMOVABLE
// "tombstone" check
if (current->key != NULL)
#endif
error = c(current->key, current->ksize, current->value, user_ptr);
if (error == -1)
break;
current = current->next;
}
return error;
}
/*void bucket_dump(hashmap* m)
{
for (int i = 0; i < m->capacity; i++)
{
if (m->buckets[i].key == NULL)
{
if (m->buckets[i].value != 0)
{
printf("x");
}
else
{
printf("0");
}
}
else
{
printf("1");
}
}
printf("\n");
fflush(stdout);
}*/