-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhash.h
More file actions
533 lines (466 loc) · 19.9 KB
/
Copy pathhash.h
File metadata and controls
533 lines (466 loc) · 19.9 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
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
#ifndef MODULE_HASH
#define MODULE_HASH
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
typedef int64_t isize;
typedef void* (*Allocator)(void* alloc, int mode, int64_t new_size, void* old_ptr, int64_t old_size, int64_t align, void* other);
typedef struct Hash_Entry Hash_Entry;
//Growing hash table like primitive mapping 64 bit keys to 64 bit values.
//Can be used to implement more fully fledged hash tables.
typedef struct Hash {
Allocator* allocator;
Hash_Entry* entries;
uint32_t count;
uint32_t capacity;
uint32_t gravestone_count;
uint32_t rehashed_times;
uint64_t empty_value;
//entries which have value = empty_value are considered empty
//entries which have value = empty_value + 1 are considered gravestone
} Hash;
typedef struct Hash_Entry {
uint64_t hash;
//the value can be anything as long as it fits into 64 bits.
union {
uint64_t value;
uint64_t value_u64;
uint32_t value_u32;
int64_t value_i64;
int32_t value_i32;
double value_f32;
float value_f64;
void* value_ptr;
struct {
uint32_t value_lo32;
uint32_t value_hi32;
};
};
} Hash_Entry;
//Iterator of entries with the same hash
typedef struct Hash_Iter {
uint32_t index;
uint32_t iter;
Hash_Entry* entry;
} Hash_Iter;
#ifndef EXTERNAL
#define EXTERNAL
#endif
EXTERNAL void hash_init(Hash* table, Allocator* allocator, uint64_t empty_value);
EXTERNAL void hash_deinit(Hash* table);
EXTERNAL void hash_clear(Hash* to_table);
EXTERNAL bool hash_find(const Hash*, uint64_t hash, isize* index);
EXTERNAL bool hash_find_or_insert(Hash* table, uint64_t hash, uint64_t value, isize* index);
EXTERNAL bool hash_iterate(const Hash* table, uint64_t hash, Hash_Iter* it);
EXTERNAL isize hash_insert(Hash* table, uint64_t hash, uint64_t value);
EXTERNAL isize hash_set(Hash* table, uint64_t hash, uint64_t value);
EXTERNAL void hash_reserve(Hash* table, isize to_size);
EXTERNAL void hash_rehash_in_place(Hash* table, isize to_size, Allocator* temp);
EXTERNAL void hash_copy_rehash(Hash* to_table, const Hash* from_table, isize to_size);
EXTERNAL void hash_copy_simple(Hash* to_table, const Hash* from_table);
EXTERNAL bool hash_remove(Hash* table, isize found_index);
EXTERNAL isize hash_remove_with_hash(Hash* table, uint64_t hash);
EXTERNAL isize hash_remove_with_value(Hash* table, uint64_t hash, uint64_t value);
EXTERNAL bool hash_find_with_value(const Hash* table, uint64_t hash, uint64_t value, isize* index);
EXTERNAL void hash_test_consistency(const Hash* table, bool slow_check);
EXTERNAL void _hash_hacky_insert(Hash* table, isize index, uint64_t hash, uint64_t value);
static inline bool hash_entry_is_used(const Hash* table, Hash_Entry* entry)
{
return entry->value - table->empty_value > 1;
}
//Backlink interface
//
// This is a solution to a rather niche problem. Consider an array of items and a hash accelerating searches into it.
// Lets say we want to remove an item at particular array index. We want to also remove the item from the accelerating hash, for which we would
// need to first find it. This requires computing its hash and then doing normal hash_find or similar. When hash is actually a multihash this can
// be quite expensive operation, thus it would be nice if we had some way to simply obtain the corresponding hash index without this lookup.
// This is precisely what backlinks are - simply an index from the items back into the hash.
//
// We create backlinks by manually inserting the index of the hash entry to the given item. These backlinks remain valid until the next rehash
// upon which the Hash_Entries get essentially randomly shuffled. Because of this we give specialized routines that also restore the backlinks
// as a part of the rehashing. These assume that the Hash stores {hash, index of item} or {hash, pointer of item} entries (thus both can be read as value)
// and calculate the address uint32_t backlink as: (uint8_t*) items_base + entry.value*item_size + item_backlink_offset.
// When we are storing pointers to indices we set item_size=1, items_base=NULL. When we are storing indices we use the pointer to the array and sizeof item.
//
// Be careful that normal rehash might be called when hash_insert, hash_find_or_insert require to grow the hash. If you want to prevent that, then simply call
// hash_backlink_reserve(table, table.count + 1ll, ...) before the call.
EXTERNAL void hash_backlink_reserve(Hash* table, isize to_size, void* items_base, isize item_size, isize item_backlink_offset);
EXTERNAL void hash_backlink_rehash_in_place(Hash* table, isize to_size, Allocator* temp, void* items_base, isize item_size, isize item_backlink_offset);
EXTERNAL void hash_backlink_copy_rehash(Hash* to_table, const Hash* from_table, isize to_size, void* items_base, isize item_size, isize item_backlink_offset);
#endif
#if (defined(MODULE_IMPL_ALL) || defined(MODULE_IMPL_HASH)) && !defined(MODULE_HAS_IMPL_HASH)
#define MODULE_HAS_IMPL_HASH
#ifndef PROFILE_START
#define PROFILE_START(...)
#define PROFILE_STOP(...)
#endif
#ifndef ATTRIBUTE_INLINE_NEVER
#define ATTRIBUTE_INLINE_NEVER
#endif
#ifndef ASSERT
#include <assert.h>
#define ASSERT(x, ...) assert(x)
#endif
#ifndef TEST
#include <stdio.h>
#define TEST(x, ...) (!(x) ? (fprintf(stderr, "TEST(" #x ") failed. " __VA_ARGS__), abort()) : (void) 0)
#endif
#ifndef INTERNAL
#define INTERNAL inline static
#endif
INTERNAL void _hash_check_consistency(const Hash* table)
{
#ifndef HASH_DEBUG
#if defined(DO_ASSERTS_SLOW)
#define HASH_DEBUG 2
#elif !defined(NDEBUG)
#define HASH_DEBUG 1
#else
#define HASH_DEBUG 0
#endif
#endif
(void) table;
#if MAP_DEBUG > 0
hash_test_consistency(table, HASH_DEBUG > 1);
#endif
}
INTERNAL Hash_Iter _hash_it_make(const Hash* table, uint64_t hash)
{
Hash_Iter it = {hash & (table->capacity - 1), 1};
return it;
}
INTERNAL bool _hash_find_next(const Hash* table, uint64_t hash, Hash_Iter* it)
{
if(table->count > 0)
{
uint64_t empty = table->empty_value;
uint64_t removed = table->empty_value + 1;
uint64_t mask = (uint64_t) table->capacity - 1;
for(;;) {
it->entry = &table->entries[it->index];
if(it->entry->value == empty)
break;
if(it->entry->hash == hash)
if(it->entry->value != removed)
return true;
ASSERT(it->iter <= table->capacity && "must not be completely full!");
it->index = (it->index + (uint64_t) it->iter) & mask;
it->iter += 1;
}
}
it->entry = NULL;
return false;
}
//lowlevel insert into a slot without any guarantee that its the right. (well, except consistency)
//Sometimes this comes in handy
EXTERNAL void _hash_hacky_insert(Hash* table, isize index, uint64_t hash, uint64_t value)
{
_hash_check_consistency(table);
uint64_t empty = table->empty_value;
uint64_t removed = table->empty_value + 1;
Hash_Entry* entry = &table->entries[index];
ASSERT(0 <= index && index < table->capacity);
ASSERT(value != empty && value != removed);
ASSERT(entry->value == empty && entry->value != removed);
table->gravestone_count -= entry->value == removed;
table->count += 1;
entry->value = value;
entry->hash = hash;
_hash_check_consistency(table);
}
INTERNAL bool _hash_find_or_insert(Hash* table, uint64_t hash, uint64_t value, bool insert_only, isize* index)
{
hash_reserve(table, table->count + 1);
uint64_t empty = table->empty_value;
uint64_t removed = table->empty_value + 1;
ASSERT(value != empty && value != removed);
uint64_t mask = (uint64_t) table->capacity - 1;
uint64_t i = hash & mask;
uint64_t empty_index = (uint64_t) -1;
for(uint64_t it = 1;; it++) {
if(insert_only)
{
if(table->entries[i].value - empty <= 1)
break;
}
else
{
if(table->entries[i].value == empty) {
if(empty_index != (uint64_t) -1)
i = empty_index;
break;
}
if(table->entries[i].value == removed)
empty_index = i;
else if(table->entries[i].hash == hash) {
*index = i;
return false;
}
}
ASSERT(it <= table->capacity && "must not be completely full!");
i = (i + it) & mask;
}
//If writing over a gravestone reduce the gravestone counter
table->gravestone_count -= table->entries[i].value == removed;
//Push the entry
table->entries[i].value = value;
table->entries[i].hash = hash;
table->count += 1;
*index = i;
_hash_check_consistency(table);
return true;
}
EXTERNAL void hash_clear(Hash* to_table)
{
for(uint32_t i = 0; i < to_table->capacity; i++)
{
to_table->entries[i].hash = 0;
to_table->entries[i].value = to_table->empty_value;
}
to_table->gravestone_count = 0;
to_table->count = 0;
_hash_check_consistency(to_table);
}
INTERNAL void* _hash_alloc(Allocator* alloc, int64_t new_size, void* old_ptr, int64_t old_size, int64_t align)
{
#ifndef USE_MALLOC
ASSERT(alloc);
return (*alloc)(alloc, 0, new_size, old_ptr, old_size, align, NULL);
#else
if(new_size != 0) {
void* out = realloc(old_ptr, new_size);
TEST(out);
return out;
}
else
free(old_ptr);
#endif
}
EXTERNAL void hash_deinit(Hash* table)
{
if(table->allocator != NULL)
_hash_alloc(table->allocator, 0, table->entries, table->capacity*sizeof(Hash_Entry), sizeof(Hash_Entry));
memset(table, 0, sizeof *table);
}
EXTERNAL void hash_init(Hash* table, Allocator* allocator, uint64_t empty_value)
{
hash_deinit(table);
table->allocator = allocator;
table->empty_value = empty_value;
}
INTERNAL void _hash_copy_rehash(Hash* to_table, const Hash* from_table, void* items_base, isize item_size, isize item_backlink_offset)
{
hash_clear(to_table);
uint8_t* base = (uint8_t*) items_base + item_backlink_offset;
uint32_t mask = to_table->capacity - 1;
for(uint32_t j = 0; j < from_table->capacity; j++)
{
Hash_Entry entry = from_table->entries[j];
if(entry.value - from_table->empty_value > 1)
{
uint32_t i = (uint32_t) entry.hash & mask;
for(uint32_t it = 1;; it++) {
if(to_table->entries[i].value == to_table->empty_value) {
to_table->entries[i] = entry;
//do backlinks if given
if(item_size > 0)
memcpy(entry.value*item_size + base, &i, sizeof i);
break;
}
i = (i + it) & mask;
}
}
}
to_table->count = from_table->count;
to_table->rehashed_times += 1;
}
ATTRIBUTE_INLINE_NEVER
EXTERNAL void hash_backlink_copy_rehash(Hash* to_table, const Hash* from_table, isize to_size, void* items_base, isize item_size, isize item_backlink_offset)
{
PROFILE_START();
_hash_check_consistency(to_table);
_hash_check_consistency(from_table);
isize required = from_table->gravestone_count + from_table->count;
if(from_table->gravestone_count > from_table->count)
required = from_table->count;
if(required < to_size)
required = to_size;
isize rehash_to = 16;
while(rehash_to*3/4 < required)
rehash_to *= 2;
TEST(rehash_to <= UINT32_MAX);
//we can call the rehash with to_table and from_table being the same
// thing. We should handle those cases gracefully.
if(to_table->entries == from_table->entries)
{
Hash old_copy = *from_table;
to_table->entries = (Hash_Entry*) _hash_alloc(to_table->allocator, rehash_to*sizeof(Hash_Entry), NULL, 0, sizeof(Hash_Entry));
to_table->capacity = (int32_t) rehash_to;
_hash_copy_rehash(to_table, &old_copy, items_base, item_size, item_backlink_offset);
hash_deinit(&old_copy);
}
else
{
if(rehash_to > to_table->capacity)
{
to_table->entries = (Hash_Entry*) _hash_alloc(to_table->allocator, rehash_to*sizeof(Hash_Entry), to_table->entries, to_table->capacity*sizeof(Hash_Entry), sizeof(Hash_Entry));
to_table->capacity = (int32_t) rehash_to;
}
_hash_copy_rehash(to_table, from_table, items_base, item_size, item_backlink_offset);
}
_hash_check_consistency(to_table);
_hash_check_consistency(from_table);
PROFILE_STOP();
}
ATTRIBUTE_INLINE_NEVER
EXTERNAL void hash_copy_rehash(Hash* to_table, const Hash* from_table, isize to_size)
{
hash_backlink_copy_rehash(to_table, from_table, to_size, 0, 0, 0);
}
EXTERNAL void hash_copy_simple(Hash* to_table, const Hash* from_table)
{
PROFILE_START();
_hash_check_consistency(to_table);
_hash_check_consistency(from_table);
if(to_table->entries == from_table->entries)
return;
if(to_table->capacity != from_table->capacity) {
to_table->entries = (Hash_Entry*) _hash_alloc(to_table->allocator, from_table->capacity*sizeof(Hash_Entry), to_table->entries, to_table->capacity*sizeof(Hash_Entry), sizeof(Hash_Entry));
to_table->capacity = (int32_t) from_table->capacity;
}
memcpy(to_table->entries, from_table->entries, from_table->capacity*sizeof(Hash_Entry));
to_table->gravestone_count = from_table->gravestone_count;
to_table->empty_value = from_table->empty_value;
_hash_check_consistency(to_table);
_hash_check_consistency(from_table);
PROFILE_STOP();
}
EXTERNAL void hash_backlink_rehash_in_place(Hash* table, isize to_size, Allocator* temp_alloc, void* items_base, isize item_size, isize item_backlink_offset)
{
Hash temp = {0};
hash_init(&temp, temp_alloc, table->empty_value);
hash_copy_simple(&temp, table);
hash_backlink_copy_rehash(table, &temp, to_size, items_base, item_size, item_backlink_offset);
hash_deinit(&temp);
}
EXTERNAL void hash_rehash_in_place(Hash* table, isize to_size, Allocator* temp_alloc)
{
hash_backlink_rehash_in_place(table, to_size, temp_alloc, 0, 0, 0);
}
EXTERNAL void hash_reserve(Hash* table, isize to_size)
{
_hash_check_consistency(table);
if(table->capacity*3/4 <= to_size + table->gravestone_count)
hash_copy_rehash(table, table, to_size);
}
EXTERNAL void hash_backlink_reserve(Hash* table, isize to_size, void* items_base, isize item_size, isize item_backlink_offset)
{
_hash_check_consistency(table);
if(table->capacity*3/4 <= to_size + table->gravestone_count)
hash_backlink_copy_rehash(table, table, to_size, items_base, item_size, item_backlink_offset);
}
EXTERNAL bool hash_find(const Hash* table, uint64_t hash, isize* index)
{
_hash_check_consistency(table);
Hash_Iter it = _hash_it_make(table, hash);
bool out = _hash_find_next(table, hash, &it);
if(index)
*index = it.index;
return out;
}
EXTERNAL bool hash_iterate(const Hash* table, uint64_t hash, Hash_Iter* it)
{
_hash_check_consistency(table);
if(it->iter == 0)
*it = _hash_it_make(table, hash);
else {
it->index = (it->index + (uint64_t) it->iter) & (table->capacity - 1);
it->iter += 1;
}
return _hash_find_next(table, hash, it);
}
EXTERNAL isize hash_remove_with_hash(Hash* table, uint64_t hash)
{
isize count = 0;
for(Hash_Iter it = _hash_it_make(table, hash); _hash_find_next(table, hash, &it); count++)
hash_remove(table, it.index);
return count;
}
EXTERNAL isize hash_remove_with_value(Hash* table, uint64_t hash, uint64_t value)
{
isize count = 0;
for(Hash_Iter it = _hash_it_make(table, hash); _hash_find_next(table, hash, &it); )
if(it.entry->value == value)
count += hash_remove(table, it.index);
return count;
}
EXTERNAL bool hash_find_with_value(const Hash* table, uint64_t hash, uint64_t value, isize* index)
{
for(Hash_Iter it = _hash_it_make(table, hash); _hash_find_next(table, hash, &it); )
if(it.entry->value == value)
{
if(index) *index = it.index;
return true;
}
return false;
}
EXTERNAL bool hash_find_or_insert(Hash* table, uint64_t hash, uint64_t value, isize* index)
{
return _hash_find_or_insert(table, hash, value, false, index);
}
EXTERNAL isize hash_insert(Hash* table, uint64_t hash, uint64_t value)
{
isize index = 0;
_hash_find_or_insert(table, hash, value, true, &index);
return index;
}
EXTERNAL isize hash_set(Hash* table, uint64_t hash, uint64_t value)
{
isize index = 0;
if(_hash_find_or_insert(table, hash, value, false, &index) == false)
table->entries[index].value = value;
return index;
}
EXTERNAL bool hash_remove(Hash* table, isize found)
{
if((uint64_t) found < table->capacity)
{
ASSERT(table->count > 0);
table->entries[found].value = table->empty_value + 1;
table->count -= 1;
table->gravestone_count += 1;
return true;
}
return false;
}
EXTERNAL void hash_test_consistency(const Hash* table, bool slow_check)
{
PROFILE_START();
TEST((table->entries == NULL) == (table->capacity == 0));
TEST((table->count >= 0 && table->capacity >= 0 && table->gravestone_count >= 0));
TEST(((uint64_t) table->capacity & ((uint64_t) table->capacity-1)) == 0); // capacity needs to be power of two or zero
TEST(table->capacity*3/4 >= table->count + table->gravestone_count);
if(table->entries != NULL)
TEST(table->allocator != NULL);
if(slow_check)
{
uint32_t used_count = 0;
uint32_t gravestone_count = 0;
for(uint32_t i = 0; i < table->capacity; i++)
{
Hash_Entry entry = table->entries[i];
if(hash_entry_is_used(table, &entry)) {
Hash_Iter it = _hash_it_make(table, entry.hash);
TEST(_hash_find_next(table, entry.hash, &it));
used_count += 1;
}
else if(entry.value == table->empty_value + 1)
gravestone_count += 1;
}
TEST(used_count == table->count);
TEST(gravestone_count == table->gravestone_count);
}
PROFILE_STOP();
}
#endif