-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashmap.h
More file actions
889 lines (871 loc) · 26.9 KB
/
hashmap.h
File metadata and controls
889 lines (871 loc) · 26.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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
/* hashmap.h - A basic hashmap implementation for C89 */
#ifndef HASHMAP_H
#define HASHMAP_H
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Library to generate type-safe hashmap types.
*
* This library is portable (tested on GCC/Clang/MSVC/ICX, x86/x86_64/ARM64,
* all warnings and pedantic) and is C89 compatible.
*
* To generate hashmaps, use the macros HASHMAP_DECLARE() to generate the
* header, and HASHMAP_DEFINE() to generate the source. For string hashmaps, use
* the macros HASHMAP_DECLARE_STRING() and HASHMAP_DEFINE_STRING(). It is
* recommended to place them in their respective files. Generate as many
* different types of hashmaps as you want.
*
* HASHMAP_DECLARE() takes six arguments: the struct name, the function prefix,
* the key type, the value type, an optional hash function (NULL for default
* FNV-1a), and an optional key comparison function (NULL for memcmp).
*
* HASHMAP_DECLARE_STRING() takes three arguments: the struct name, the function
* prefix, and the value type. The key type is automatically set to const char *,
* the hash function to FNV-1a (reads string content instead of raw pointer),
* and the comparison function to strcmp.
*
* Note that with HASHMAP_DECLARE() and HASHMAP_DEFINE() by default, the key
* value itself is compared and hashed. For instance, if the key is a char * and
* the hash and comparison functions are set to NULL, the pointer itself will be
* hashed and compared (not the string it points to).
*
* This means two different strings with the same contents won't be equal or
* produce the same hash. To fix this, pass hashing and comparison functions
* that read the pointer's content, or use HASHMAP_DECLARE_STRING() and
* HASHMAP_DEFINE_STRING() if your keys are const char *.
*
* This library is not thread safe.
*
* It is safe to cast uninitialized hashmaps to any other hashmap type.
*
* It is safe to cast initalized hashmaps to other hashmaps as long as their
* element sizes are equal. e.g. MapVoidPtrToU32 -> MapCharPtrToI32 is supported
* (both 8/4 bytes), but MapCharToU32 -> MapCharPtrToI32 is undefined behavior
* (1/4 bytes vs 8/4 bytes).
*
* This library uses separate chaining with linked lists for collision
* resolution.
*
* Load factor is set at 0.75, with capacity growing by powers of 2.
*
* To configure this library #define the symbols before including the library.
* This is usually done in the header file where HASHMAP_DECLARE() is called.
*
* Configuration options:
*
* - HASHMAP_NO_PANIC_ON_NULL (default 0): if true (1), does not panic upon
* passing NULL to hashmap functions. Otherwise, panic.
*
* - HASHMAP_REALLOC (default realloc(3)): specify the allocator. If using
* a custom allocator, must also specify HASHMAP_FREE.
*
* - HASHMAP_FREE (default free(3)): specify the deallocator. If using a
* custom deallocator, must also specify HASHMAP_REALLOC.
*
* - HASHMAP_LONG_JUMP_NO_ABORT (default undefined): for testing only. Jump to
* externally defined "jmp_buf abort_jmp" instead of panicking. Unlike other
* configuration options, must be defined before including the library.
*
*
* API Functions:
*
* The following documentation takes this generated hashmap for instance:
* HASHMAP_DECLARE(Hashmap, hashmap, const char *, int, fnv1a_32_str, strcmp)
*
* All functions panic if map is NULL (unless HASHMAP_NO_PANIC_ON_NULL).
*
* void hashmap_init(Hashmap *map)
* Initialize empty hashmap with default capacity. Leaks memory if initializes
* an already initialized hashmap.
*
* void hashmap_free(Hashmap *map)
* Deallocate hashmap memory. Safe to call on already-freed hashmaps.
*
* void hashmap_grow(Hashmap *map)
* Increase capacity to next power of 2. Auto-initializes empty hashmaps.
* No-op if growth would cause overflow.
*
* int hashmap_insert(Hashmap *map, const char *key, int value)
* Insert or update key-value pair. Auto-initializes empty hashmaps.
* Grows capacity if load factor exceeds 0.75. Returns 1 if key existed
* and value was overwritten, 0 if new key was inserted.
*
* int hashmap_remove(Hashmap *map, const char *key, int *out)
* Remove key-value pair. If out is non-NULL, stores removed value.
* Returns 1 if key was found and removed, 0 otherwise.
*
* int hashmap_get(const Hashmap *map, const char *key, int *out)
* Get value for key. If out is non-NULL, stores value.
* Returns 1 if key found, 0 otherwise.
*
* int hashmap_has(const Hashmap *map, const char *key)
* Check if key exists in hashmap. Returns 1 if found, 0 otherwise.
*
* size_t hashmap_size(const Hashmap *map)
* Return the amount of elements stored in the hashmap. Same as map.size.
*
* void hashmap_iterate(Hashmap *map, void *context)
* Iterate over all key-value pairs using map->iteration_callback.
* Callback signature: int callback(const char *key, int value, void *context).
* Callback should return 1 to continue iteration, 0 to stop.
* No-op if iteration_callback is NULL.
*
* void hashmap_duplicate(Hashmap *dest, const Hashmap *src)
* Copy src hashmap to dest. dest must be uninitialized. Overwrites existing
* dest data without freeing it.
*
* void hashmap_clear(Hashmap *map)
* Remove all elements without deallocating capacity. Safe to call on
* uninitialized hashmaps.
*
*
* Example:
* int main(void)
* {
* Hashmap map = {0};
* int value;
*
* hashmap_insert(&map, "hello", 10);
* hashmap_insert(&map, "world", 20);
* hashmap_insert(&map, "test", 30);
*
* if (hashmap_get(&map, "hello", &value)) {
* printf("Found: %d\n", value);
* }
*
* if (hashmap_has(&map, "world")) {
* hashmap_remove(&map, "world", NULL);
* }
*
* hashmap_insert(&map, "hello", 99); // Overwrites existing value
*
* hashmap_clear(&map);
*
* hashmap_free(&map);
*
* return 0;
* }
*/
#if defined(HASHMAP_REALLOC) && !defined(HASHMAP_FREE) || \
!defined(HASHMAP_REALLOC) && defined(HASHMAP_FREE)
#error "You must define both HASHMAP_REALLOC and HASHMAP_FREE, or neither."
#endif
#if !defined(HASHMAP_REALLOC) && !defined(HASHMAP_FREE)
#define HASHMAP_REALLOC(p, s) (realloc((p), (s)))
#define HASHMAP_FREE(p) (free((p)))
#endif
#ifndef HASHMAP_NO_PANIC_ON_NULL
#define HASHMAP_NO_PANIC_ON_NULL 0
#endif
#ifdef HASHMAP_LONG_JUMP_NO_ABORT
#include <setjmp.h>
extern jmp_buf abort_jmp;
#endif
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L
#define HASHMAP_NORETURN [[noreturn]]
#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
#define HASHMAP_NORETURN _Noreturn
#elif defined(__GNUC__) || defined(__clang__)
#define HASHMAP_NORETURN __attribute__((noreturn))
#else
#define HASHMAP_NORETURN
#endif
#ifndef __STDC_VERSION__
#define RESTRICT
#else
#define RESTRICT restrict
#endif
#define HASHMAP_LOAD_FACTOR 0.75f
enum { HASHMAP_DEFAULT_CAPACITY = 8, HASHMAP_GROWTH_FACTOR = 2 };
#define HASHMAP_DECLARE_STRING(Struct_Name_, Functions_Prefix_, \
Custom_Value_Type_) \
HASHMAP_DECLARE(Struct_Name_, Functions_Prefix_, const char *, \
Custom_Value_Type_, Functions_Prefix_##_fnv1a_32_str, \
strcmp)
#define HASHMAP_DEFINE_STRING(Struct_Name_, Functions_Prefix_, \
Custom_Value_Type_) \
HASHMAP_DEFINE(Struct_Name_, Functions_Prefix_, const char *, \
Custom_Value_Type_, Functions_Prefix_##_fnv1a_32_str, \
strcmp)
#define HASHMAP_DECLARE(Struct_Name_, Functions_Prefix_, Custom_Key_Type_, Custom_Value_Type_, Custom_Hash_Func_, Custom_Comparison_Func_)\
\
struct Struct_Name_##ListNode {\
struct Struct_Name_##ListNode *next;\
Custom_Key_Type_ key;\
Custom_Value_Type_ value;\
};\
\
typedef struct Struct_Name_ {\
struct Struct_Name_##ListNode **buckets;\
int (*iteration_callback)(Custom_Key_Type_ key, Custom_Value_Type_ value,\
void *context);\
size_t size;\
size_t capacity;\
size_t buckets_filled;\
} Struct_Name_;\
\
/* API functions */\
void Functions_Prefix_##_init(Struct_Name_ *map);\
void Functions_Prefix_##_grow(Struct_Name_ *map);\
int Functions_Prefix_##_insert(Struct_Name_ *map, Custom_Key_Type_ key, Custom_Value_Type_ value);\
int Functions_Prefix_##_remove(Struct_Name_ *RESTRICT map, Custom_Key_Type_ key,\
Custom_Value_Type_ *RESTRICT out);\
int Functions_Prefix_##_get(const Struct_Name_ *RESTRICT map, Custom_Key_Type_ key,\
Custom_Value_Type_ *RESTRICT out);\
int Functions_Prefix_##_has(const Struct_Name_ *map, Custom_Key_Type_ key);\
size_t Functions_Prefix_##_size(const Struct_Name_ *map);\
void Functions_Prefix_##_free(Struct_Name_ *map);\
void Functions_Prefix_##_iterate(Struct_Name_ *map, void *context);\
void Functions_Prefix_##_duplicate(Struct_Name_ *RESTRICT dest, Struct_Name_ *RESTRICT src);\
void Functions_Prefix_##_clear(Struct_Name_ *map);\
\
/* Internal functions */\
void Functions_Prefix_##_assert(const Struct_Name_ *map);\
void Functions_Prefix_##_assert_internal(const struct Struct_Name_ *map);\
struct Struct_Name_##ListNode *Functions_Prefix_##_list_new(struct Struct_Name_##ListNode *next,\
Custom_Key_Type_ key, Custom_Value_Type_ value);\
int (*Functions_Prefix_##_compare_comparison_callback(void))(Custom_Key_Type_,\
Custom_Key_Type_);\
int Functions_Prefix_##_grow_internal(Custom_Key_Type_ key, Custom_Value_Type_ value, void *new_map);\
int Functions_Prefix_##_compare_keys(Custom_Key_Type_ key1, Custom_Key_Type_ key2);\
int Functions_Prefix_##_list_insert(struct Struct_Name_##ListNode *head, Custom_Key_Type_ key,\
Custom_Value_Type_ value);\
int Functions_Prefix_##_list_find(struct Struct_Name_##ListNode *RESTRICT head, Custom_Key_Type_ key,\
Custom_Value_Type_ *RESTRICT out);\
int Functions_Prefix_##_list_remove(struct Struct_Name_##ListNode **RESTRICT list, Custom_Key_Type_ key,\
Custom_Value_Type_ *RESTRICT out);\
int Functions_Prefix_##_list_iterate(struct Struct_Name_##ListNode *head,\
int (*callback)(Custom_Key_Type_ key, Custom_Value_Type_ value,\
void *context),\
void *context);\
void Functions_Prefix_##_list_free(struct Struct_Name_##ListNode *head);\
struct Struct_Name_##ListNode *Functions_Prefix_##_list_duplicate(struct Struct_Name_##ListNode *head);\
unsigned long (*Functions_Prefix_##_compare_hash_callback(void))(Custom_Key_Type_);\
size_t Functions_Prefix_##_hash_index(const Struct_Name_ *map, Custom_Key_Type_ key);\
unsigned long Functions_Prefix_##_fnv1a_32_buf(const void *buf, size_t len);\
unsigned long Functions_Prefix_##_fnv1a_32_str(const char *str);
#ifdef HASHMAP_LONG_JUMP_NO_ABORT
#define HASHMAP_DEFINE_PANIC(Function_Prefix_) \
HASHMAP_NORETURN void Function_Prefix_##_panic(const char *message) \
{ \
assert(message); \
longjmp(abort_jmp, 1); \
}
#else
#define HASHMAP_DEFINE_PANIC(Function_Prefix_) \
HASHMAP_NORETURN void Function_Prefix_##_panic(const char *message) \
{ \
assert(message); \
(void)fprintf(stderr, "%s\n", message); \
abort(); \
}
#endif
#define HASHMAP_DEFINE(Struct_Name_, Functions_Prefix_, Custom_Key_Type_, Custom_Value_Type_, Custom_Hash_Func_, Custom_Comparison_Func_)\
struct Struct_Name_;\
struct Struct_Name_##ListNode;\
HASHMAP_DEFINE_PANIC(Functions_Prefix_)\
\
void Functions_Prefix_##_assert(const struct Struct_Name_ *map)\
{\
/* If buckets is NULL, map should be in initial/freed state */\
if (map->buckets == NULL) {\
assert(map->size == 0);\
assert(map->buckets_filled == 0);\
assert(map->capacity == 0);\
return;\
}\
\
Functions_Prefix_##_assert_internal(map);\
}\
\
/* Separated into its own function to avoid Clang complexity warning */\
void Functions_Prefix_##_assert_internal(const struct Struct_Name_ *map)\
{\
/* Capacity must be a power of 2 and non-zero */\
assert(map->capacity > 0);\
assert((map->capacity & (map->capacity - 1)) == 0);\
\
/* Size invariants */\
assert(map->size >= map->buckets_filled);\
assert(map->buckets_filled <= map->capacity);\
}\
\
struct Struct_Name_##ListNode *Functions_Prefix_##_list_new(struct Struct_Name_##ListNode *next,\
Custom_Key_Type_ key, Custom_Value_Type_ value)\
{\
struct Struct_Name_##ListNode *ret = (struct Struct_Name_##ListNode *)HASHMAP_REALLOC(\
NULL, sizeof(struct Struct_Name_##ListNode));\
if (ret == NULL) {\
Functions_Prefix_##_panic("Out of memory. Panic.");\
}\
\
ret->next = next;\
ret->key = key;\
ret->value = value;\
\
return ret;\
}\
\
void Functions_Prefix_##_init(struct Struct_Name_ *map)\
{\
if (map == NULL) {\
if (HASHMAP_NO_PANIC_ON_NULL) {\
return;\
}\
Functions_Prefix_##_panic(\
"Null passed to "#Functions_Prefix_"_init but non-null argument expected.");\
}\
\
memset((void *)map, 0, sizeof(struct Struct_Name_));\
\
map->buckets = (struct Struct_Name_##ListNode **)HASHMAP_REALLOC(\
NULL,\
HASHMAP_DEFAULT_CAPACITY * sizeof(struct Struct_Name_##ListNode *));\
\
if (map->buckets == NULL) {\
Functions_Prefix_##_panic("Out of memory. Panic.");\
}\
\
map->capacity = HASHMAP_DEFAULT_CAPACITY;\
assert(map->capacity > 0);\
\
memset((void *)map->buckets, 0,\
map->capacity * sizeof(struct Struct_Name_##ListNode *));\
\
Functions_Prefix_##_assert(map);\
}\
\
int (*Functions_Prefix_##_compare_comparison_callback(void))(Custom_Key_Type_,\
Custom_Key_Type_)\
{\
return Custom_Comparison_Func_;\
}\
\
int Functions_Prefix_##_compare_keys(Custom_Key_Type_ key1, Custom_Key_Type_ key2)\
{\
int (*callback)(Custom_Key_Type_, Custom_Key_Type_) =\
Functions_Prefix_##_compare_comparison_callback();\
\
if (callback == NULL) {\
return memcmp((void *)&key1, (void *)&key2, sizeof(Custom_Key_Type_));\
}\
return callback(key1, key2);\
}\
\
/* Assume the first node isn't NULL */\
int Functions_Prefix_##_list_insert(struct Struct_Name_##ListNode *head, Custom_Key_Type_ key,\
Custom_Value_Type_ value)\
{\
struct Struct_Name_##ListNode *prev = NULL;\
size_t iter = 0;\
\
assert(head != NULL);\
\
for (; head != NULL; prev = head, head = head->next, iter++) {\
/* Debug test for infinite loops */\
assert(iter < 0xFFFFFFFFUL);\
\
if (Functions_Prefix_##_compare_keys(key, head->key) == 0) {\
/* Override existing value */\
head->value = value;\
return 1;\
}\
}\
\
/* Append to end of list */\
prev->next = Functions_Prefix_##_list_new(NULL, key, value);\
return 0;\
}\
\
int Functions_Prefix_##_list_find(struct Struct_Name_##ListNode *RESTRICT head, Custom_Key_Type_ key,\
Custom_Value_Type_ *RESTRICT out)\
{\
size_t iter = 0;\
\
for (iter = 0; head != NULL; head = head->next, iter++) {\
/* Debug test for infinite loops */\
assert(iter < 0xFFFFFFFFUL);\
\
if (Functions_Prefix_##_compare_keys(head->key, key) == 0) {\
if (out != NULL) {\
*out = head->value;\
}\
return 1;\
}\
}\
\
return 0;\
}\
\
int Functions_Prefix_##_list_remove(struct Struct_Name_##ListNode **RESTRICT list, Custom_Key_Type_ key,\
Custom_Value_Type_ *RESTRICT out)\
{\
struct Struct_Name_##ListNode *head = NULL;\
struct Struct_Name_##ListNode *prev = NULL;\
size_t iter = 0;\
\
if (list == NULL) {\
return 0;\
}\
\
head = *list;\
\
for (iter = 0; head != NULL; prev = head, head = head->next, iter++) {\
/* Debug test for infinite loops */\
assert(iter < 0xFFFFFFFFUL);\
\
if (Functions_Prefix_##_compare_keys(head->key, key) != 0) {\
continue;\
}\
\
if (out != NULL) {\
*out = head->value;\
}\
\
if (prev == NULL) {\
*list = head->next;\
} else {\
prev->next = head->next;\
}\
\
HASHMAP_FREE(head);\
return 1;\
}\
\
return 0;\
}\
\
int Functions_Prefix_##_list_iterate(struct Struct_Name_##ListNode *head,\
int (*callback)(Custom_Key_Type_ key, Custom_Value_Type_ value,\
void *context),\
void *context)\
{\
size_t iter = 0;\
\
assert(callback != NULL);\
\
for (iter = 0; head != NULL; head = head->next, iter++) {\
/* Debug test for infinite loops */\
assert(iter < 0xFFFFFFFFUL);\
\
if (callback(head->key, head->value, context) == 0) {\
return 0;\
}\
}\
\
return 1;\
}\
\
void Functions_Prefix_##_list_free(struct Struct_Name_##ListNode *head)\
{\
struct Struct_Name_##ListNode *next = NULL;\
size_t iter = 0;\
\
for (iter = 0; head != NULL; iter++) {\
/* Debug test for infinite loops */\
assert(iter < 0xFFFFFFFFUL);\
\
next = head->next;\
HASHMAP_FREE(head);\
head = next;\
}\
}\
\
struct Struct_Name_##ListNode *Functions_Prefix_##_list_duplicate(struct Struct_Name_##ListNode *head)\
{\
struct Struct_Name_##ListNode *new_head = NULL;\
struct Struct_Name_##ListNode *new_next = NULL;\
size_t iter = 0;\
\
if (head == NULL) {\
return NULL;\
}\
\
new_head = Functions_Prefix_##_list_new(NULL, head->key, head->value);\
new_next = new_head;\
\
for (iter = 0; head->next != NULL;\
head = head->next, new_next = new_next->next, iter++) {\
/* Debug test for infinite loops */\
assert(iter < 0xFFFFFFFFUL);\
new_next->next = Functions_Prefix_##_list_new(NULL, head->next->key,\
head->next->value);\
}\
\
return new_head;\
}\
\
unsigned long (*Functions_Prefix_##_compare_hash_callback(void))(Custom_Key_Type_)\
{\
return Custom_Hash_Func_;\
}\
\
size_t Functions_Prefix_##_hash_index(const struct Struct_Name_ *map,\
Custom_Key_Type_ key)\
{\
size_t idx = 0;\
unsigned long (*callback)(Custom_Key_Type_) = Functions_Prefix_##_compare_hash_callback();\
\
if (callback == NULL) {\
idx = Functions_Prefix_##_fnv1a_32_buf((const void *)&key,\
sizeof(Custom_Key_Type_));\
} else {\
idx = callback(key);\
}\
\
idx &= map->capacity - 1;\
\
return idx;\
}\
\
void Functions_Prefix_##_grow(struct Struct_Name_ *map)\
{\
size_t new_capacity = 0;\
struct Struct_Name_ new_map = { 0 };\
int (*iteration_callback_backup)(Custom_Key_Type_, Custom_Value_Type_, void *);\
size_t new_capacity_size = 0;\
\
if (map == NULL) {\
if (HASHMAP_NO_PANIC_ON_NULL) {\
return;\
}\
Functions_Prefix_##_panic(\
"Null passed to "#Functions_Prefix_"_grow but non-null argument expected.");\
}\
\
Functions_Prefix_##_assert(map);\
\
if (map->buckets == NULL) {\
Functions_Prefix_##_init(map);\
}\
\
iteration_callback_backup = map->iteration_callback;\
\
/* Calculate the next power of 2 */\
new_capacity = map->capacity;\
new_capacity |= new_capacity >> 1;\
new_capacity |= new_capacity >> 2;\
new_capacity |= new_capacity >> 4;\
new_capacity |= new_capacity >> 8;\
if (sizeof(size_t) >= 4) {\
new_capacity |= new_capacity >> 16;\
}\
if (sizeof(size_t) >= 8) {\
new_capacity |= new_capacity >> 32;\
}\
new_capacity++;\
\
if (new_capacity < map->capacity) {\
/* Overflow, do not grow, let separate chaining handle\
* collisions */\
return;\
}\
if (new_capacity > ((size_t)-1) / sizeof(struct Struct_Name_##ListNode *)) {\
/* Would overflow, do not grow, let separate chaining handle\
* collisions */\
return;\
}\
\
new_capacity_size = new_capacity * sizeof(struct Struct_Name_##ListNode *);\
new_map.capacity = new_capacity;\
new_map.buckets = (struct Struct_Name_##ListNode **)HASHMAP_REALLOC(\
NULL, new_capacity_size);\
if (new_map.buckets == NULL) {\
Functions_Prefix_##_panic("Out of memory. Panic.");\
}\
memset((void *)new_map.buckets, 0, new_capacity_size);\
\
map->iteration_callback = Functions_Prefix_##_grow_internal;\
Functions_Prefix_##_iterate(map, &new_map);\
\
assert(map->size >= new_map.size);\
if (map->size != new_map.size) {\
/* Failed to grow, revert operation */\
map->iteration_callback = iteration_callback_backup;\
Functions_Prefix_##_free(&new_map);\
return;\
}\
\
Functions_Prefix_##_free(map);\
*map = new_map;\
map->iteration_callback = iteration_callback_backup;\
}\
\
int Functions_Prefix_##_grow_internal(Custom_Key_Type_ key, Custom_Value_Type_ value, void *new_map)\
{\
/* Should never overwrite */\
assert(Functions_Prefix_##_insert(new_map, key, value) == 0);\
return 1;\
}\
\
int Functions_Prefix_##_insert(struct Struct_Name_ *map, Custom_Key_Type_ key, Custom_Value_Type_ value)\
{\
size_t idx = 0;\
int overwritten = 0;\
\
if (map == NULL) {\
if (HASHMAP_NO_PANIC_ON_NULL) {\
return -1;\
}\
Functions_Prefix_##_panic(\
"Null passed to "#Functions_Prefix_"_insert but non-null argument expected.");\
}\
\
Functions_Prefix_##_assert(map);\
\
if (map->buckets == NULL) {\
Functions_Prefix_##_init(map);\
}\
\
/* Check for being above load factor */\
if ((float)map->buckets_filled / (float)map->capacity >\
HASHMAP_LOAD_FACTOR) {\
Functions_Prefix_##_grow(map);\
}\
\
idx = Functions_Prefix_##_hash_index(map, key);\
\
if (map->buckets[idx] == NULL) {\
map->buckets[idx] = Functions_Prefix_##_list_new(NULL, key, value);\
map->buckets_filled++;\
map->size++;\
return 0;\
}\
\
overwritten = Functions_Prefix_##_list_insert(map->buckets[idx], key, value);\
if (!overwritten) {\
map->size++;\
}\
\
return overwritten;\
}\
\
int Functions_Prefix_##_remove(struct Struct_Name_ *RESTRICT map, Custom_Key_Type_ key,\
Custom_Value_Type_ *RESTRICT out)\
{\
size_t idx = 0;\
int found = 0;\
\
if (map == NULL) {\
if (HASHMAP_NO_PANIC_ON_NULL) {\
return -1;\
}\
Functions_Prefix_##_panic(\
"Null passed to "#Functions_Prefix_"_remove but non-null argument expected.");\
}\
\
Functions_Prefix_##_assert(map);\
\
if (map->buckets == NULL) {\
return 0;\
}\
\
idx = Functions_Prefix_##_hash_index(map, key);\
\
found = Functions_Prefix_##_list_remove(&map->buckets[idx], key, out);\
\
if (found) {\
if (map->buckets[idx] == NULL) {\
assert(map->buckets_filled > 0);\
map->buckets_filled--;\
}\
assert(map->size > 0);\
map->size--;\
}\
\
return found;\
}\
\
int Functions_Prefix_##_get(const struct Struct_Name_ *RESTRICT map, Custom_Key_Type_ key,\
Custom_Value_Type_ *RESTRICT out)\
{\
size_t idx = 0;\
\
if (map == NULL) {\
if (HASHMAP_NO_PANIC_ON_NULL) {\
return -1;\
}\
Functions_Prefix_##_panic(\
"Null passed to "#Functions_Prefix_"_get but non-null argument expected.");\
}\
\
Functions_Prefix_##_assert(map);\
\
if (map->buckets == NULL) {\
return 0;\
}\
\
idx = Functions_Prefix_##_hash_index(map, key);\
\
return Functions_Prefix_##_list_find(map->buckets[idx], key, out);\
}\
\
int Functions_Prefix_##_has(const struct Struct_Name_ *map, Custom_Key_Type_ key)\
{\
return Functions_Prefix_##_get(map, key, NULL);\
}\
\
size_t Functions_Prefix_##_size(const Struct_Name_ *map)\
{\
return map->size;\
}\
\
void Functions_Prefix_##_free(struct Struct_Name_ *map)\
{\
size_t idx = 0;\
\
if (map == NULL) {\
if (HASHMAP_NO_PANIC_ON_NULL) {\
return;\
}\
Functions_Prefix_##_panic(\
"Null passed to "#Functions_Prefix_"_free but non-null argument expected.");\
}\
\
Functions_Prefix_##_assert(map);\
\
for (idx = 0; idx < map->capacity; idx++) {\
Functions_Prefix_##_list_free(map->buckets[idx]);\
}\
\
HASHMAP_FREE((void *)map->buckets);\
\
memset((void *)map, 0, sizeof(struct Struct_Name_));\
}\
\
void Functions_Prefix_##_iterate(struct Struct_Name_ *map, void *context)\
{\
size_t idx = 0;\
int callback_response = 0;\
\
if (map == NULL) {\
if (HASHMAP_NO_PANIC_ON_NULL) {\
return;\
}\
Functions_Prefix_##_panic(\
"Null passed to "#Functions_Prefix_"_iterate but non-null argument expected.");\
}\
\
Functions_Prefix_##_assert(map);\
\
if (map->iteration_callback == NULL) {\
return;\
}\
\
for (idx = 0; idx < map->capacity; idx++) {\
callback_response = Functions_Prefix_##_list_iterate(\
map->buckets[idx], map->iteration_callback, context);\
if (callback_response == 0) {\
break;\
}\
}\
}\
\
void Functions_Prefix_##_duplicate(struct Struct_Name_ *RESTRICT dest,\
struct Struct_Name_ *RESTRICT src)\
{\
size_t idx = 0;\
\
if (dest == NULL || src == NULL) {\
if (HASHMAP_NO_PANIC_ON_NULL) {\
return;\
}\
Functions_Prefix_##_panic(\
"Null passed to "#Functions_Prefix_"_clear but non-null argument expected.");\
}\
Functions_Prefix_##_assert(src);\
\
if (src->capacity == 0) {\
memset(dest, 0, sizeof(struct Struct_Name_));\
return;\
}\
\
dest->buckets = (struct Struct_Name_##ListNode **)HASHMAP_REALLOC(\
NULL, src->capacity * sizeof(struct Struct_Name_##ListNode *));\
\
if (dest->buckets == NULL) {\
Functions_Prefix_##_panic("Out of memory. Panic.");\
}\
\
dest->capacity = src->capacity;\
dest->size = src->size;\
dest->buckets_filled = src->buckets_filled;\
dest->iteration_callback = src->iteration_callback;\
\
memset((void *)dest->buckets, 0,\
dest->capacity * sizeof(struct Struct_Name_##ListNode *));\
\
for (idx = 0; idx < dest->capacity; idx++) {\
dest->buckets[idx] = Functions_Prefix_##_list_duplicate(src->buckets[idx]);\
}\
\
Functions_Prefix_##_assert(dest);\
}\
\
void Functions_Prefix_##_clear(struct Struct_Name_ *map)\
{\
size_t idx = 0;\
\
if (map == NULL) {\
if (HASHMAP_NO_PANIC_ON_NULL) {\
return;\
}\
Functions_Prefix_##_panic(\
"Null passed to "#Functions_Prefix_"_clear but non-null argument expected.");\
}\
\
Functions_Prefix_##_assert(map);\
\
for (idx = 0; idx < map->capacity; idx++) {\
Functions_Prefix_##_list_free(map->buckets[idx]);\
map->buckets[idx] = NULL;\
}\
\
map->size = 0;\
map->buckets_filled = 0;\
}\
\
unsigned long Functions_Prefix_##_fnv1a_32_buf(const void *buf, size_t len)\
{\
const unsigned char *bptr = (const unsigned char *)buf;\
const unsigned char *bend = bptr + len;\
unsigned long hval = 0x811c9dc5U;\
\
for (; bptr < bend; bptr++) {\
hval ^= (unsigned long)bptr[0];\
hval *= 0x01000193U;\
hval &= 0xFFFFFFFFUL;\
}\
\
return hval;\
}\
\
unsigned long Functions_Prefix_##_fnv1a_32_str(const char *str)\
{\
const unsigned char *ustr = (const unsigned char *)str;\
unsigned long hval = 0x811c9dc5U;\
\
for (; ustr[0] != '\0'; ustr++) {\
hval ^= (unsigned long)ustr[0];\
hval *= 0x01000193U;\
hval &= 0xFFFFFFFFUL;\
}\
\
return hval;\
}
/****************************************************************************
* Copyright (C) 2025 by Roland Marchand <[email protected]> *
* *
* Permission to use, copy, modify, and/or distribute this software for any *
* purpose with or without fee is hereby granted. *
* *
* THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL WARRANTIES *
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF *
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR *
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES *
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN *
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF *
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. *
****************************************************************************/
#endif /* HASHMAP_H */