forked from mattiasgustavsson/libs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashtable.h
874 lines (655 loc) · 30.8 KB
/
hashtable.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
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
/*
------------------------------------------------------------------------------
Licensing information can be found at the end of the file.
------------------------------------------------------------------------------
hashtable.h - v2.0 - Cache efficient hash table implementation for C/C++.
Do this:
#define HASHTABLE_IMPLEMENTATION
before you include this file in *one* C/C++ file to create the implementation.
*/
#ifndef hashtable_h
#define hashtable_h
#ifndef HASHTABLE_U32
#define HASHTABLE_U32 unsigned int
#endif
typedef struct hashtable_t hashtable_t;
void hashtable_init( hashtable_t* table, int key_size, int item_size, int initial_capacity, void* memctx );
void hashtable_term( hashtable_t* table );
void hashtable_insert( hashtable_t* table, HASHTABLE_U32 hash, void const* key, void const* item );
void hashtable_remove( hashtable_t* table, HASHTABLE_U32 hash, void const* key );
void hashtable_clear( hashtable_t* table );
void* hashtable_find( hashtable_t const* table, HASHTABLE_U32 hash, void const* key );
int hashtable_count( hashtable_t const* table );
void* hashtable_items( hashtable_t const* table );
void const* hashtable_keys( hashtable_t const* table );
void hashtable_swap( hashtable_t* table, int index_a, int index_b );
#endif /* hashtable_h */
/*
uint32_t hash_u32( uint32_t key ) {
key = ~key + ( key << 15 );
key = key ^ ( key >> 12 );
key = key + ( key << 2 );
key = key ^ ( key >> 4 );
key = (key + ( key << 3 ) ) + ( key << 11 );
key = key ^ ( key >> 16);
return key;
}
uint32_t hash_u64( uint64_t key ) {
key = ( ~key ) + ( key << 18 );
key = key ^ ( key >> 31 );
key = ( key + ( key << 2 ) ) + ( key << 4 );
key = key ^ ( key >> 11 );
key = key + ( key << 6 );
key = key ^ ( key >> 22 );
return (uint32_t) key;
}
uint32_t hash_str( char const* key ) {
uint32_t hash = 5381u;
for( char const* s = key; *s != ’\0’; ++s ) {
hash = ( ( hash << 5u ) + hash ) ^ (*s);
}
return hash;
}
uint32_t murmur_hash( const void * key, int len, uint32_t seed ) {
uint32_t const m = 0x5bd1e995;
int const r = 24;
uint32_t h = seed ^ len;
uint8_t const* data = (uint8_t const*) key;
while( len >= 4 ) {
#ifdef PLATFORM_BIG_ENDIAN
uint32_t k = ( data[0] ) + ( data[1] << 8 ) + ( data[2] << 16 ) + ( data[3] << 24 );
#else
uint32_t k = *(uint32_t *)data;
#endif
k *= m;
k ^= k >> r;
k *= m;
h *= m;
h ^= k;
data += 4;
len -= 4;
}
switch(len) {
case 3: h ^= data[2] << 16;
case 2: h ^= data[1] << 8;
case 1: h ^= data[0];
h *= m;
};
h ^= h >> 13;
h *= m;
h ^= h >> 15;
return h;
}
*/
/**
hashtable.h
===========
Cache efficient hash table implementation for C/C++.
Example
-------
#define HASHTABLE_IMPLEMENTATION
#include "hashtable.h"
#include <stdio.h> // for printf
int main( int argc, char** argv ) {
// define some example key and value types
typedef struct key_t { int a, b, c; } key_t;
typedef struct value_t {
char id[ 64 ];
float x, y, z;
int n[ 250 ];
} value_t;
// create a couple of sample keys
// (don't bother to fill in the fields for this sample)
key_t* key_a = (key_t*)malloc( sizeof( key_t ) );
key_t* key_b = (key_t*)malloc( sizeof( key_t ) );
hashtable_t table;
hashtable_init( &table, sizeof( value_t ), 256, 0 );
{
// values are copied into the table, not stored by pointer
// (don't bother to fill in all the fields for this sample)
value_t value_a = { "Item A" };
value_t value_b = { "Item B" };
hashtable_insert( &table, (HASHTABLE_U64)(uintptr_t)key_a, &value_a );
hashtable_insert( &table, (HASHTABLE_U64)(uintptr_t)key_b, &value_b );
}
// find the values by key
value_t* value_a = (value_t*)hashtable_find( &table, (HASHTABLE_U64)(uintptr_t)key_a );
printf( "First item: %s\n", value_a->id );
value_t* value_b = (value_t*)hashtable_find( &table, (HASHTABLE_U64)(uintptr_t)key_b );
printf( "Second item: %s\n", value_b->id );
// remove one of the items
hashtable_remove( &table, (HASHTABLE_U64)(uintptr_t)key_a );
// it is possible to enumerate keys and values
int count = hashtable_count( &table );
HASHTABLE_U64 const* keys = hashtable_keys( &table );
value_t* items = (value_t*)hashtable_items( &table );
printf( "\nEnumeration:\n" );
for( int i = 0; i < count; ++i ) {
printf( " 0x%X : %s\n", (int) keys[ i ], items[ i ].id );
}
// cleanup
hashtable_term( &table );
free( key_b );
free( key_a );
return 0;
}
API Documentation
-----------------
hashtable.h is a small library for storing values in a table and access them efficiently by a 64-bit key. It is a
single-header library, and does not need any .lib files or other binaries, or any build scripts. To use it, you just
include hashtable.h to get the API declarations. To get the definitions, you must include hashtable.h from *one* single
C or C++ file, and #define the symbol `HASHTABLE_IMPLEMENTATION` before you do.
The key value must be unique per entry, and is hashed for efficient lookup using an internal hashing algorithm. This
library does not support custom key types, so typically pointers or handles are used as key values.
The library is written with efficiency in mind. Data and keys are stored in separate structures, for better cache
coherency, and hash collisions are resolved with open addressing/linear probing using the next available slot, which is
also good for the cache.
### Customization
There are a few different things in hashtable.h which are configurable by #defines. Most of the API use the `int` data
type, for integer values where the exact size is not important. However, for some functions, it specifically makes use
of 32 and 64 bit data types. These default to using `unsigned int` and `unsigned long long` by default, but can be
redefined by #defining HASHTABLE_U32 and HASHTABLE_U64 respectively, before including hashtable.h. This is useful if
you, for example, use the types from `<stdint.h>` in the rest of your program, and you want hashtable.h to use
compatible types. In this case, you would include hashtable.h using the following code:
#define HASHTABLE_U32 uint32_t
#define HASHTABLE_U64 uint64_t
#include "hashtable.h"
Note that when customizing the data types, you need to use the same definition in every place where you include
hashtable.h, as they affect the declarations as well as the definitions.
The rest of the customizations only affect the implementation, so will only need to be defined in the file where you
have the #define HASHTABLE_IMPLEMENTATION.
Note that if all customizations are utilized, hashtable.h will include no external files whatsoever, which might be
useful if you need full control over what code is being built.
#### size_t
Internally, the hashtable.h implementation makes use of the standard `size_t` data type. This requires including the
c runtime library header `<stddef.h>`. To allow full configurability, and avoid hashtable.h including stddef.h, you can
specify which type hashtable.h should use for its size_t, by #defining HASHTABLE_SIZE_T, like this:
#define HASHTABLE_IMPLEMENTATION
#define HASHTABLE_SIZE_T uint64_t
#include "hashtable.h"
If not specified, hashtable.h will by default include stddef.h and use the standard `size_t` type.
#### Custom memory allocators
To store the internal data structures, hashtable.h needs to do dynamic allocation by calling `malloc`. Programs might
want to keep track of allocations done, or use custom defined pools to allocate memory from. hashtable.h allows for
specifying custom memory allocation functions for `malloc` and `free`. This is done with the following code:
#define HASHTABLE_IMPLEMENTATION
#define HASHTABLE_MALLOC( ctx, size ) ( my_custom_malloc( ctx, size ) )
#define HASHTABLE_FREE( ctx, ptr ) ( my_custom_free( ctx, ptr ) )
#include "hashtable.h"
where `my_custom_malloc` and `my_custom_free` are your own memory allocation/deallocation functions. The `ctx` parameter
is an optional parameter of type `void*`. When `hashtable_init` is called, you can pass in a `memctx` parameter, which
can be a pointer to anything you like, and which will be passed through as the `ctx` parameter to every
`HASHTABLE_MALLOC`/`HASHTABLE_FREE` call. For example, if you are doing memory tracking, you can pass a pointer to your
tracking data as `memctx`, and in your custom allocation/deallocation function, you can cast the `ctx` param back to the
right type, and access the tracking data.
If no custom allocator is defined, hashtable.h will default to `malloc` and `free` from the C runtime library.
#### Custom assert
hashtable.h makes use of asserts to report usage errors and failed allocation errors. By default, it makes use of the C
runtime library `assert` macro, which only executes in debug builds. However, it allows for substituting with your own
assert function or macro using the following code:
#define HASHTABLE_IMPLEMENTATION
#define HASHTABLE_ASSERT( condition ) ( my_custom_assert( condition ) )
#include "hashtable.h"
Note that if you only want the asserts to trigger in debug builds, you must add a check for this in your custom assert.
#### Custom C runtime functions
The library makes use of two additional functions from the C runtime library, and for full flexibility, it allows you
to substitute them for your own. Here's an example:
#define HASHTABLE_IMPLEMENTATION
#define HASHTABLE_MEMCPY( dst, src, cnt ) ( my_memcpy_func( dst, src, cnt ) )
#define HASHTABLE_MEMSET( ptr, val, cnt ) ( my_memset_func( ptr, val, cnt ) )
#include "hashtable.h"
If no custom function is defined, hashtable.h will default to the C runtime library equivalent.
hashtable_init
--------------
void hashtable_init( hashtable_t* table, int item_size, int initial_capacity, void* memctx )
Initialize a hashtable instance. `item_size` specifies the size, in bytes, of the data type holding a single item stored
in the table. `initial_capacity` is the number of items to allocate storage for initially - capacity will automatically
grow as needed, by reallocating memory.
hashtable_term
--------------
void hashtable_term( hashtable_t* table )
Terminates a hashtable instance, releasing all memory used by it. No further calls to the hashtable API are valid until
the instance is reinitialized by another call to `hashtable_init`.
hashtable_insert
----------------
void hashtable_insert( hashtable_t* table, HASHTABLE_U64 key, void const* item )
Inserts a data item into the hashtable, associating it with the specified key. The item is copied into the hashtable,
rather than just storing the `item` pointer, so the `item` pointer can be safely released after the call to
`hashtable_insert`. The value of `key` must be unique - it is not valid to store two items with the same key value. An
assert is triggered if trying to add a key which already exists, which means that if the default assert is used, it will
only be checked in debug builds - in release builds, it is up to the calling code to ensure this doesn't happen, or the
hashtable will be left in an undefined state.
hashtable_remove
----------------
void hashtable_remove( hashtable_t* table, HASHTABLE_U64 key )
Removes the item associated with the specified key, and the instance of the key itself, from the hashtable. If the
specified key could not be found, an assert is triggered.
hashtable_clear
---------------
void hashtable_clear( hashtable_t* table )
Removes all the items stored in the hashtable, without deallocating any of the memory it has allocated.
hashtable_find
--------------
void* hashtable_find( hashtable_t const* table, HASHTABLE_U64 key )
Returns a pointer to the item associated with the specified key, or NULL it the key was not found. The lookup is
designed for efficiency, and for minimizing cache missed.
hashtable_count
---------------
int hashtable_count( hashtable_t const* table )
Returns the number of items currently held in the table.
hashtable_items
---------------
void* hashtable_items( hashtable_t const* table )
Returns a pointer to the items currently held in the table. All items are stored in a contiguous memory block, and you
can get to the next item be moving the pointer `item_size` bytes forward, where `item_size` is the same value as passed
to hash_table_init. The easiest way to acces items is to cast the return value to the correct type and just index it as
a normal array. It contains as many items as returned by `hashtable_count`.
hashtable_keys
--------------
HASHTABLE_U64 const* hashtable_keys( hashtable_t const* table )
Returns a pointer to the keys currently held in the table, in the same order as the items returned from
`hashtable_items`. Can be indexed as an array with as many elements as returned by `hashtable_count`.
hashtable_swap
--------------
void hashtable_swap( hashtable_t* table, int index_a, int index_b )
Swaps the specified item/key pairs, and updates the hash lookup for both. Can be used to re-order the contents, as
retrieved by calling `hashtable_items` and `hashtable_keys`, while keeping the hashing intact.
*/
/*
----------------------
IMPLEMENTATION
----------------------
*/
#ifndef hashtable_t_h
#define hashtable_t_h
struct hashtable_internal_slot_t
{
HASHTABLE_U32 key_hash;
int item_index;
int base_count;
};
struct hashtable_t
{
void* memctx;
int count;
int key_size;
int item_size;
struct hashtable_internal_slot_t* slots;
int slot_capacity;
int prime_index;
void* items_key;
int* items_slot;
void* items_data;
int item_capacity;
void* swap_temp;
};
#endif /* hashtable_t_h */
#ifdef HASHTABLE_IMPLEMENTATION
#undef HASHTABLE_IMPLEMENTATION
#ifndef HASHTABLE_SIZE_T
#undef _CRT_NONSTDC_NO_DEPRECATE
#define _CRT_NONSTDC_NO_DEPRECATE
#undef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#include <stddef.h>
#define HASHTABLE_SIZE_T size_t
#endif
#ifndef HASHTABLE_ASSERT
#undef _CRT_NONSTDC_NO_DEPRECATE
#define _CRT_NONSTDC_NO_DEPRECATE
#undef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#include <assert.h>
#define HASHTABLE_ASSERT( x ) assert( x )
#endif
#ifndef HASHTABLE_MEMCPY
#undef _CRT_NONSTDC_NO_DEPRECATE
#define _CRT_NONSTDC_NO_DEPRECATE
#undef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#include <string.h>
#define HASHTABLE_MEMCPY( dst, src, cnt ) ( memcpy( dst, src, cnt ) )
#endif
#ifndef HASHTABLE_KEYCOPY
#undef _CRT_NONSTDC_NO_DEPRECATE
#define _CRT_NONSTDC_NO_DEPRECATE
#undef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#include <string.h>
#define HASHTABLE_KEYCOPY( dst, src, cnt ) ( memcpy( dst, src, cnt ) )
#endif
#ifndef HASHTABLE_ITEMCOPY
#undef _CRT_NONSTDC_NO_DEPRECATE
#define _CRT_NONSTDC_NO_DEPRECATE
#undef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#include <string.h>
#define HASHTABLE_ITEMCOPY( dst, src, cnt ) ( memcpy( dst, src, cnt ) )
#endif
#ifndef HASHTABLE_KEYCMP
#undef _CRT_NONSTDC_NO_DEPRECATE
#define _CRT_NONSTDC_NO_DEPRECATE
#undef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#include <string.h>
#define HASHTABLE_KEYCMP( a, b, len ) ( memcmp( a, b, len ) == 0 )
#endif
#ifndef HASHTABLE_MALLOC
#undef _CRT_NONSTDC_NO_DEPRECATE
#define _CRT_NONSTDC_NO_DEPRECATE
#undef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#define HASHTABLE_MALLOC( ctx, size ) ( malloc( size ) )
#define HASHTABLE_FREE( ctx, ptr ) ( free( ptr ) )
#endif
static HASHTABLE_U32 hashtable_internal_pow2ceil( HASHTABLE_U32 v )
{
--v;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
++v;
v += ( v == 0 );
return v;
}
static int hashtable_internal_primes[] = { 31, 67, 127, 257, 509, 1021, 2053, 4099, 8191, 16381, 32771, 65537, 131071,
262147, 524287, 1048573, 2097143, 4194301, 8388617, 16777213, 33554467, 67108859, 134217757, 268435459, 536870909,
1073741827, 2147483647 };
void hashtable_init( hashtable_t* table, int key_size, int item_size, int initial_capacity, void* memctx )
{
initial_capacity = (int)hashtable_internal_pow2ceil( initial_capacity >=0 ? (HASHTABLE_U32) initial_capacity : 32U );
int prime_index = 0;
while( hashtable_internal_primes[ prime_index ] < initial_capacity + initial_capacity / 2 ) ++prime_index;
table->memctx = memctx;
table->count = 0;
table->key_size = key_size;
table->item_size = item_size;
table->slot_capacity = hashtable_internal_primes[ prime_index ];
table->prime_index = prime_index;
if( key_size > 0 )
{
int slots_size = (int)( table->slot_capacity * sizeof( *table->slots ) );
table->slots = (struct hashtable_internal_slot_t*) HASHTABLE_MALLOC( table->memctx, (HASHTABLE_SIZE_T) slots_size );
HASHTABLE_ASSERT( table->slots );
for( int i = 0; i < table->slot_capacity; ++i )
{
table->slots[ i ].base_count = 0;
table->slots[ i ].item_index = -1;
}
}
else
{
table->slots = 0;
}
table->item_capacity = initial_capacity;
table->items_key = HASHTABLE_MALLOC( table->memctx,
table->item_capacity * ( table->key_size + sizeof( *table->items_slot ) + table->item_size ) +
( table->key_size > table->item_size ? table->key_size : table->item_size ) );
HASHTABLE_ASSERT( table->items_key );
table->items_slot = (int*)( ( (uintptr_t) table->items_key ) + table->key_size * table->item_capacity );
table->items_data = (void*)( table->items_slot + table->item_capacity );
table->swap_temp = (void*)( ( (uintptr_t) table->items_data ) + table->item_size * table->item_capacity );
}
void hashtable_term( hashtable_t* table )
{
HASHTABLE_FREE( table->memctx, table->items_key );
HASHTABLE_FREE( table->memctx, table->slots );
}
static int hashtable_internal_find_slot( hashtable_t const* table, HASHTABLE_U32 hash, void const* key )
{
HASHTABLE_U32 slot_capacity = (HASHTABLE_U32) table->slot_capacity;
int const base_slot = (int)( hash % slot_capacity );
int base_count = table->slots[ base_slot ].base_count;
int slot = base_slot;
while( base_count > 0 )
{
if( table->slots[ slot ].item_index >= 0 )
{
HASHTABLE_U32 slot_hash = table->slots[ slot ].key_hash;
int slot_base = (int)( slot_hash % slot_capacity );
if( slot_base == base_slot )
{
HASHTABLE_ASSERT( base_count > 0 );
if( slot_hash == hash )
{
void const* slot_key = (void const*)( ( (uintptr_t) table->items_key ) + table->key_size * table->slots[ slot ].item_index );
if( HASHTABLE_KEYCMP( slot_key, key, table->key_size ) )
return slot;
}
--base_count;
}
}
slot = (int)( ( slot + 1 ) % slot_capacity );
}
return -1;
}
static void hashtable_internal_expand_slots( hashtable_t* table )
{
if( !table->slots ) return;
int const old_capacity = table->slot_capacity;
struct hashtable_internal_slot_t* old_slots = table->slots;
table->slot_capacity = hashtable_internal_primes[ ++table->prime_index ];
HASHTABLE_U32 slot_capacity = (HASHTABLE_U32) table->slot_capacity;
int const size = (int)( table->slot_capacity * sizeof( *table->slots ) );
table->slots = (struct hashtable_internal_slot_t*) HASHTABLE_MALLOC( table->memctx, (HASHTABLE_SIZE_T) size );
HASHTABLE_ASSERT( table->slots );
for( int i = 0; i < table->slot_capacity; ++i )
{
table->slots[ i ].base_count = 0;
table->slots[ i ].item_index = -1;
}
for( int i = 0; i < old_capacity; ++i )
{
if( old_slots[ i ].item_index >= 0 )
{
HASHTABLE_U32 const hash = old_slots[ i ].key_hash;
int const base_slot = (int)( hash % slot_capacity );
int slot = base_slot;
while( table->slots[ slot ].item_index >= 0 )
slot = (int)( ( slot + 1 ) % slot_capacity );
table->slots[ slot ].key_hash = hash;
int item_index = old_slots[ i ].item_index;
table->slots[ slot ].item_index = item_index;
table->items_slot[ item_index ] = slot;
++table->slots[ base_slot ].base_count;
}
}
HASHTABLE_FREE( table->memctx, old_slots );
}
static void hashtable_internal_expand_items( hashtable_t* table )
{
table->item_capacity *= 2;
void* const new_items_key = HASHTABLE_MALLOC( table->memctx,
table->item_capacity * ( table->key_size + sizeof( *table->items_slot ) + table->item_size ) +
( table->key_size > table->item_size ? table->key_size : table->item_size ) );
HASHTABLE_ASSERT( new_items_key );
int* const new_items_slot = (int*)( ( (uintptr_t) new_items_key ) + table->key_size * table->item_capacity );
void* const new_items_data = (void*)( new_items_slot + table->item_capacity );
void* const new_swap_temp = (void*)( ( (uintptr_t) new_items_data ) + table->item_size * table->item_capacity );
HASHTABLE_MEMCPY( new_items_key, table->items_key, table->count * table->key_size );
HASHTABLE_MEMCPY( new_items_slot, table->items_slot, table->count * sizeof( *table->items_slot ) );
HASHTABLE_MEMCPY( new_items_data, table->items_data, (HASHTABLE_SIZE_T) table->count * table->item_size );
HASHTABLE_FREE( table->memctx, table->items_key );
table->items_key = new_items_key;
table->items_slot = new_items_slot;
table->items_data = new_items_data;
table->swap_temp = new_swap_temp;
}
void hashtable_insert( hashtable_t* table, HASHTABLE_U32 hash, void const* key, void const* item )
{
if( !table->slots )
{
if( table->count >= table->item_capacity )
hashtable_internal_expand_items( table );
void* dest_item = (void*)( ( (uintptr_t) table->items_data ) + table->count * table->item_size );
HASHTABLE_ITEMCOPY( dest_item, item, (HASHTABLE_SIZE_T) table->item_size );
++table->count;
return;
}
HASHTABLE_ASSERT( hashtable_internal_find_slot( table, hash, key ) < 0 );
if( table->count >= ( table->slot_capacity - table->slot_capacity / 3 ) )
hashtable_internal_expand_slots( table );
HASHTABLE_U32 slot_capacity = (HASHTABLE_U32) table->slot_capacity;
int const base_slot = (int)( hash % slot_capacity );
int base_count = table->slots[ base_slot ].base_count;
int slot = base_slot;
int first_free = slot;
while( base_count )
{
if( table->slots[ slot ].item_index < 0 && table->slots[ first_free ].item_index >= 0 ) first_free = slot;
HASHTABLE_U32 const slot_hash = table->slots[ slot ].key_hash;
int slot_base = (int)( slot_hash % slot_capacity );
if( slot_base == base_slot )
--base_count;
slot = (int)( ( slot + 1 ) % slot_capacity );
}
slot = first_free;
while( table->slots[ slot ].item_index >= 0 )
slot = (int)( ( slot + 1 ) % slot_capacity );
if( table->count >= table->item_capacity )
hashtable_internal_expand_items( table );
HASHTABLE_ASSERT( table->slots[ slot ].item_index < 0 && (int)( hash % slot_capacity ) == (HASHTABLE_U32) base_slot );
table->slots[ slot ].key_hash = hash;
table->slots[ slot ].item_index = table->count;
++table->slots[ base_slot ].base_count;
void* dest_item = (void*)( ( (uintptr_t) table->items_data ) + table->count * table->item_size );
HASHTABLE_ITEMCOPY( dest_item, item, (HASHTABLE_SIZE_T) table->item_size );
void* dest_key = (void*)( ( (uintptr_t) table->items_key ) + table->count * table->key_size );
HASHTABLE_KEYCOPY( dest_key, key, (HASHTABLE_SIZE_T) table->key_size );
table->items_slot[ table->count ] = slot;
++table->count;
}
void hashtable_remove( hashtable_t* table, HASHTABLE_U32 hash, void const* key )
{
if( table->slots )
{
int const slot = hashtable_internal_find_slot( table, hash, key );
HASHTABLE_ASSERT( slot >= 0 );
HASHTABLE_U32 slot_capacity = (HASHTABLE_U32) table->slot_capacity;
int const base_slot = (int)( hash % slot_capacity );
int index = table->slots[ slot ].item_index;
int last_index = table->count - 1;
--table->slots[ base_slot ].base_count;
table->slots[ slot ].item_index = -1;
if( index != last_index )
{
void* dst_key = (void*)( ( (uintptr_t) table->items_key ) + index * table->key_size );
void* src_key = (void*)( ( (uintptr_t) table->items_key ) + last_index * table->key_size );
HASHTABLE_KEYCOPY( dst_key, src_key, (HASHTABLE_SIZE_T) table->key_size );
table->items_slot[ index ] = table->items_slot[ last_index ];
void* dst_item = (void*)( ( (uintptr_t) table->items_data ) + index * table->item_size );
void* src_item = (void*)( ( (uintptr_t) table->items_data ) + last_index * table->item_size );
HASHTABLE_ITEMCOPY( dst_item, src_item, (HASHTABLE_SIZE_T) table->item_size );
table->slots[ table->items_slot[ last_index ] ].item_index = index;
}
}
--table->count;
}
void hashtable_clear( hashtable_t* table )
{
table->count = 0;
if( table->slots )
{
for( int i = 0; i < table->slot_capacity; ++i )
{
table->slots[ i ].base_count = 0;
table->slots[ i ].item_index = -1;
}
}
}
void* hashtable_find( hashtable_t const* table, HASHTABLE_U32 hash, void const* key )
{
int const slot = table->slots ? hashtable_internal_find_slot( table, hash, key ) : -1;
if( slot < 0 ) return 0;
int const index = table->slots[ slot ].item_index;
void* const item = (void*)( ( (uintptr_t) table->items_data ) + index * table->item_size );
return item;
}
int hashtable_count( hashtable_t const* table )
{
return table->count;
}
void* hashtable_items( hashtable_t const* table )
{
return table->items_data;
}
void const* hashtable_keys( hashtable_t const* table )
{
return table->items_key;
}
void hashtable_swap( hashtable_t* table, int index_a, int index_b )
{
if( index_a < 0 || index_a >= table->count || index_b < 0 || index_b >= table->count ) return;
int slot_a = table->items_slot[ index_a ];
int slot_b = table->items_slot[ index_b ];
table->items_slot[ index_a ] = slot_b;
table->items_slot[ index_b ] = slot_a;
void* key_a = (void*)( ( (uintptr_t) table->items_key ) + index_a * table->key_size );
void* key_b = (void*)( ( (uintptr_t) table->items_key ) + index_b * table->key_size );
HASHTABLE_KEYCOPY( table->swap_temp, key_a, table->key_size );
HASHTABLE_KEYCOPY( key_a, key_b, table->key_size );
HASHTABLE_KEYCOPY( key_b, table->swap_temp, table->key_size );
void* item_a = (void*)( ( (uintptr_t) table->items_data ) + index_a * table->item_size );
void* item_b = (void*)( ( (uintptr_t) table->items_data ) + index_b * table->item_size );
HASHTABLE_ITEMCOPY( table->swap_temp, item_a, table->item_size );
HASHTABLE_ITEMCOPY( item_a, item_b, table->item_size );
HASHTABLE_ITEMCOPY( item_b, table->swap_temp, table->item_size );
if( table->slots )
{
table->slots[ slot_a ].item_index = index_b;
table->slots[ slot_b ].item_index = index_a;
}
}
#endif /* HASHTABLE_IMPLEMENTATION */
/*
contributors:
Randy Gaul (hashtable_clear, hashtable_swap )
revision history:
2.0 variable key size, custom hashing
1.1 added hashtable_clear, hashtable_swap
1.0 first released version
*/
/*
------------------------------------------------------------------------------
This software is available under 2 licenses - you may choose the one you like.
------------------------------------------------------------------------------
ALTERNATIVE A - MIT License
Copyright (c) 2015 Mattias Gustavsson
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
------------------------------------------------------------------------------
ALTERNATIVE B - Public Domain (www.unlicense.org)
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
software, either in source code form or as a compiled binary, for any purpose,
commercial or non-commercial, and by any means.
In jurisdictions that recognize copyright laws, the author or authors of this
software dedicate any and all copyright interest in the software to the public
domain. We make this dedication for the benefit of the public at large and to
the detriment of our heirs and successors. We intend this dedication to be an
overt act of relinquishment in perpetuity of all present and future rights to
this software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
------------------------------------------------------------------------------
*/