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
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
|
/* ------------------------------------------------------------ */
/*
Coucal, Cuckoo hashing-based hashtable with stash area.
Copyright (C) 2013-2014 Xavier Roche and other contributors
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdarg.h>
#include "coucal.h"
/* We use md5 as hashing function for its quality regarding diffusion and
collisions. MD5 is slower than other hashing functions, but is known to be
an excellent hashing function. FNV-1 is generally good enough for this
purpose, too, but the performance gain is not sufficient to use it by
default.
On several benchmarks, both MD5 and FNV were quite good (0.45 cuckoo moved
on average for each new item inserted in the hashtable), but FNV-1 was more
prone to mutual collisions (creating cycles requiring stash handling), and
was causing the stash area to be more filled than the MD5 variant.
Simpler hashing functions, such as rolling hashes (LCG) were also tested,
but with collision rate and diffusion were terrible.
[ On a 10M key tests, both variants acheived 0.45 cuckoo/add ration,
but the FNV-1 variant collided 11 times with a maximum stash area
filled with 4 entries ; whereas the MD5 variant did only collide
once ]
*/
#if (!defined(HTS_INTHASH_USES_MD5) && !defined(HTS_INTHASH_USES_MURMUR))
#define HTS_INTHASH_USES_MURMUR 1
#endif
#if (defined(HTS_INTHASH_USES_MURMUR))
#include "murmurhash3.h"
#elif HTS_INTHASH_USES_MD5 == 1
#include "md5.h"
#else
/* use the Openssl implementation */
#include <openssl/md5.h>
#define MD5Init MD5_Init
#define MD5Update MD5_Update
#define MD5Final MD5_Final
#endif
/** Size of auxiliary stash. **/
#define STASH_SIZE 16
/** Minimum value for lg_size. **/
#define MIN_LG_SIZE 4
/** Minimum value for pool.capacity. **/
#define MIN_POOL_CAPACITY 256
/* 64-bit constant */
#if defined(WIN32)
#define UINT_64_CONST(X) ((uint64_t) (X))
#define UINT_64_FORMAT "I64d"
#elif (defined(_LP64) || defined(__x86_64__) \
|| defined(__powerpc64__) || defined(__64BIT__))
#define UINT_64_CONST(X) ((uint64_t) X##UL)
#define UINT_64_FORMAT "ld"
#else
#define UINT_64_CONST(X) ((uint64_t) X##ULL)
#define UINT_64_FORMAT "lld"
#endif
/** Hashtable. **/
struct struct_inthash {
/** Hashtable items. **/
inthash_item *items;
/** Log-2 of the hashtable size. **/
size_t lg_size;
/** Number of used items (<= POW2(lg_size)). **/
size_t used;
/** Stash area for collisions. **/
struct {
/** Stash items. **/
inthash_item items[STASH_SIZE];
/** Stash size (<= STASH_SIZE). **/
size_t size;
} stash;
/** String pool. **/
struct {
/** String buffer. **/
char *buffer;
/** Buffer used size (high watermark). **/
size_t size;
/** Buffer capacity. **/
size_t capacity;
/** Used chars (== size if compacted). **/
size_t used;
} pool;
/** Statistics **/
struct {
/** Highest stash.size seen. **/
size_t max_stash_size;
/** Number of writes. **/
size_t write_count;
/** Number of writes causing an add. **/
size_t add_count;
/** Number of cuckoo moved during adds. **/
size_t cuckoo_moved;
/** Number of items added to stash. **/
size_t stash_added;
/** Number of hashtable rehash/expand operations. **/
size_t rehash_count;
/** Number of pool compact operations. **/
size_t pool_compact_count;
/** Number of pool realloc operations. **/
size_t pool_realloc_count;
} stats;
/** Settings. **/
struct {
/** How to handle values (might be NULL). **/
struct {
/** free() **/
t_inthash_value_freehandler free;
/** opaque argument **/
inthash_opaque arg;
} value;
/** How to handle names (might be NULL). **/
struct {
/** strdup() **/
t_inthash_duphandler dup;
/** free() **/
t_inthash_key_freehandler free;
/** hash **/
t_inthash_hasheshandler hash;
/** comparison **/
t_inthash_cmphandler equals;
/** opaque argument **/
inthash_opaque arg;
} key;
/** How to handle fatal assertions (might be NULL). **/
struct {
/** logging **/
t_inthash_loghandler log;
/** abort() **/
t_inthash_asserthandler fatal;
/** opaque argument **/
inthash_opaque arg;
/** hashtable name for logging **/
inthash_key_const name;
} error;
/** How to handle pretty-print (debug) (might be NULL). **/
struct {
/** key print() **/
t_inthash_printkeyhandler key;
/** value print() **/
t_inthash_printvaluehandler value;
/** opaque argument **/
inthash_opaque arg;
} print;
} custom;
};
/* Assertion check. */
#define inthash_assert(HASHTABLE, EXP) \
(void)( (EXP) || (inthash_assert_failed(HASHTABLE, #EXP, __FILE__, __LINE__), 0) )
/* Compiler-specific. */
#ifdef __GNUC__
#define INTHASH_PRINTF_FUN(fmt, arg) __attribute__ ((format (printf, fmt, arg)))
#define INTHASH_INLINE __inline__
#elif (defined(_MSC_VER))
#define INTHASH_PRINTF_FUN(FMT, ARGS)
#define INTHASH_INLINE __inline
#else
#define INTHASH_PRINTF_FUN(FMT, ARGS)
#define INTHASH_INLINE
#endif
/* Logging level. */
static void inthash_log(const inthash hashtable, inthash_loglevel level,
const char *format, va_list args);
#define DECLARE_LOG_FUNCTION(NAME, LEVEL) \
static void NAME(const inthash hashtable, const char *format, ...) \
INTHASH_PRINTF_FUN(2, 3); \
static void NAME(const inthash hashtable, const char *format, ...) { \
va_list args; \
va_start(args, format); \
inthash_log(hashtable, LEVEL, format, args); \
va_end(args); \
}
#if 0
/* Verbose. */
DECLARE_LOG_FUNCTION(inthash_crit, inthash_log_critical)
DECLARE_LOG_FUNCTION(inthash_warning, inthash_log_warning)
DECLARE_LOG_FUNCTION(inthash_info, inthash_log_info)
DECLARE_LOG_FUNCTION(inthash_debug, inthash_log_debug)
DECLARE_LOG_FUNCTION(inthash_trace, inthash_log_trace)
#elif 0
/* Info. */
DECLARE_LOG_FUNCTION(inthash_crit, inthash_log_critical)
DECLARE_LOG_FUNCTION(inthash_warning, inthash_log_warning)
DECLARE_LOG_FUNCTION(inthash_info, inthash_log_info)
#define inthash_debug inthash_log
#define inthash_trace inthash_nolog
#else
/* No logging except stats and critical. */
DECLARE_LOG_FUNCTION(inthash_crit, inthash_log_critical)
DECLARE_LOG_FUNCTION(inthash_warning, inthash_log_warning)
DECLARE_LOG_FUNCTION(inthash_info, inthash_log_info)
#define inthash_debug inthash_nolog
#define inthash_trace inthash_nolog
#endif
/* 2**X */
#define POW2(X) ( (size_t) 1 << (X) )
/* the empty string for the string pool */
static char the_empty_string[1] = { 0 };
/* global assertion handler */
static t_inthash_asserthandler global_assert_handler = NULL;
/* global assertion handler */
static t_inthash_loghandler global_log_handler = NULL;
/* default assertion handler, if neither hashtable one nor global one
were defined */
static void inthash_fail(const char* exp, const char* file, int line) {
fprintf(stderr, "assertion '%s' failed at %s:%d\n", exp, file, line);
abort();
}
/* assert failed handler. */
static void inthash_assert_failed(const inthash hashtable, const char* exp, const char* file, int line) {
const char *const name = inthash_get_name(hashtable);
inthash_crit(hashtable, "hashtable %s: %s failed at %s:%d",
name != NULL ? name : "<unknown>", exp, file, line);
if (hashtable != NULL && hashtable->custom.error.fatal != NULL) {
hashtable->custom.error.fatal(hashtable->custom.error.arg, exp, file, line);
} else if (global_assert_handler != NULL) {
global_assert_handler(hashtable, exp, file, line);
} else {
inthash_fail(exp, file, line);
}
abort();
}
/* Logging */
static void inthash_log(const inthash hashtable, inthash_loglevel level,
const char *format, va_list args) {
inthash_assert(hashtable, format != NULL);
if (hashtable != NULL && hashtable->custom.error.log != NULL) {
hashtable->custom.error.log(hashtable->custom.error.arg, level, format, args);
} else if (global_log_handler != NULL) {
global_log_handler(hashtable, level, format, args);
} else {
fprintf(stderr, "[%p] ", (void*) hashtable);
(void) vfprintf(stderr, format, args);
putc('\n', stderr);
}
}
/* No logging (should be dropped by the compiler) */
static void inthash_nolog(const inthash hashtable, const char *format, ...)
INTHASH_PRINTF_FUN(2, 3);
static void inthash_nolog(const inthash hashtable, const char *format, ...) {
(void) hashtable;
(void) format;
}
const char* inthash_get_name(inthash hashtable) {
return hashtable->custom.error.name;
}
static void inthash_log_stats(inthash hashtable) {
const char *const name = inthash_get_name(hashtable);
inthash_info(hashtable, "hashtable %s%s%ssummary: "
"size=%"UINT_64_FORMAT" (lg2=%"UINT_64_FORMAT") "
"used=%"UINT_64_FORMAT" "
"stash-size=%"UINT_64_FORMAT" "
"pool-size=%"UINT_64_FORMAT" "
"pool-capacity=%"UINT_64_FORMAT" "
"pool-used=%"UINT_64_FORMAT" "
"writes=%"UINT_64_FORMAT" "
"(new=%"UINT_64_FORMAT") "
"moved=%"UINT_64_FORMAT " "
"stashed=%"UINT_64_FORMAT" "
"max-stash-size=%"UINT_64_FORMAT" "
"avg-moved=%g "
"rehash=%"UINT_64_FORMAT" "
"pool-compact=%"UINT_64_FORMAT" "
"pool-realloc=%"UINT_64_FORMAT" "
"memory=%"UINT_64_FORMAT,
name != NULL ? "\"" : "",
name != NULL ? name : "",
name != NULL ? "\" " : "",
(uint64_t) POW2(hashtable->lg_size),
(uint64_t) hashtable->lg_size,
(uint64_t) hashtable->used,
(uint64_t) hashtable->stash.size,
(uint64_t) hashtable->pool.size,
(uint64_t) hashtable->pool.capacity,
(uint64_t) hashtable->pool.used,
(uint64_t) hashtable->stats.write_count,
(uint64_t) hashtable->stats.add_count,
(uint64_t) hashtable->stats.cuckoo_moved,
(uint64_t) hashtable->stats.stash_added,
(uint64_t) hashtable->stats.max_stash_size,
(double) hashtable->stats.cuckoo_moved / (double) hashtable->stats.add_count,
(uint64_t) hashtable->stats.rehash_count,
(uint64_t) hashtable->stats.pool_compact_count,
(uint64_t) hashtable->stats.pool_realloc_count,
(uint64_t) inthash_memory_size(hashtable)
);
}
/* default hash function when key is a regular C-string */
inthash_hashkeys inthash_hash_string(const char *name) {
#if HTS_INTHASH_USES_MD5 == 1
/* compute a regular MD5 and extract two 32-bit integers */
MD5_CTX ctx;
union {
unsigned char md5digest[16];
inthash_hashkeys mhashes[2];
inthash_hashkeys hashes;
} u;
/* compute MD5 */
MD5Init(&ctx, 0);
MD5Update(&ctx, (const unsigned char *) name,
(unsigned int) strlen(name));
MD5Final(u.md5digest, &ctx);
/* mix mix mix */
u.mhashes[0].hash1 ^= u.mhashes[1].hash1;
u.mhashes[0].hash2 ^= u.mhashes[1].hash2;
/* do not keep identical hashes */
if (u.hashes.hash1 == u.hashes.hash2) {
u.hashes.hash2 = ~u.hashes.hash2;
}
return u.hashes;
#elif (defined(HTS_INTHASH_USES_MURMUR))
union {
uint32_t result[4];
inthash_hashkeys hashes;
} u;
MurmurHash3_x86_128(name, (const int) strlen(name),
42, &u.result) ;
/* mix mix mix */
u.result[0] ^= u.result[2];
u.result[1] ^= u.result[3];
/* do not keep identical hashes */
if (u.hashes.hash1 == u.hashes.hash2) {
u.hashes.hash2 = ~u.hashes.hash2;
}
return u.hashes;
#else
/* compute two Fowler-Noll-Vo hashes (64-bit FNV-1 variant) ;
each 64-bit hash being XOR-folded into a single 32-bit hash. */
size_t i;
inthash_hashkeys hashes;
uint64_t h1, h2;
/* FNV-1, 64-bit. */
#define FNV1_PRIME UINT_64_CONST(1099511628211)
#define FNV1_OFFSET_BASIS UINT_64_CONST(14695981039346656037)
/* compute the hashes ; second variant is using xored data */
h1 = FNV1_OFFSET_BASIS;
h2 = ~FNV1_OFFSET_BASIS;
for(i = 0 ; name[i] != '\0' ; i++) {
const unsigned char c1 = name[i];
const unsigned char c2 = ~c1;
h1 = ( h1 * FNV1_PRIME ) ^ c1;
h2 = ( h2 * FNV1_PRIME ) ^ c2;
}
/* XOR-folding to improve diffusion (Wikipedia) */
hashes.hash1 = ( (uint32_t) h1 ^ (uint32_t) ( h1 >> 32 ) );
hashes.hash2 = ( (uint32_t) h2 ^ (uint32_t) ( h2 >> 32 ) );
#undef FNV1_PRIME
#undef FNV1_OFFSET_BASIS
/* do not keep identical hashes */
if (hashes.hash1 == hashes.hash2) {
hashes.hash2 = ~hashes.hash2;
}
return hashes;
#endif
}
static INTHASH_INLINE inthash_hashkeys inthash_calc_hashes(inthash hashtable,
inthash_key_const value) {
return hashtable->custom.key.hash == NULL
? inthash_hash_string(value)
: hashtable->custom.key.hash(hashtable->custom.key.arg, value);
}
/* position 'pos' is free ? */
static INTHASH_INLINE int inthash_is_free(const inthash hashtable, size_t pos) {
return hashtable->items[pos].name == NULL;
}
/* compare two keys ; by default using strcmp() */
static INTHASH_INLINE int inthash_equals(inthash hashtable,
inthash_key_const a,
inthash_key_const b) {
return hashtable->custom.key.equals == NULL
? strcmp((const char*) a, (const char*) b) == 0
: hashtable->custom.key.equals(hashtable->custom.key.arg, a, b);
}
static INTHASH_INLINE int inthash_matches_(inthash hashtable,
const inthash_item *const item,
inthash_key_const name,
const inthash_hashkeys *hashes) {
return item->name != NULL
&& item->hashes.hash1 == hashes->hash1
&& item->hashes.hash2 == hashes->hash2
&& inthash_equals(hashtable, item->name, name);
}
static INTHASH_INLINE int inthash_matches(inthash hashtable, size_t pos,
inthash_key_const name,
const inthash_hashkeys *hashes) {
const inthash_item *const item = &hashtable->items[pos];
return inthash_matches_(hashtable, item, name, hashes);
}
/* compact string pool ; does not change the capacity */
static void inthash_compact_pool(inthash hashtable, size_t capacity) {
const size_t hash_size = POW2(hashtable->lg_size);
size_t i;
char*const old_pool = hashtable->pool.buffer;
const size_t old_size = hashtable->pool.size;
size_t count = 0;
/* we manage the string pool */
inthash_assert(hashtable, hashtable->custom.key.dup == NULL);
/* statistics */
hashtable->stats.pool_compact_count++;
/* change capacity now */
if (hashtable->pool.capacity != capacity) {
hashtable->pool.capacity = capacity;
}
/* realloc */
hashtable->pool.buffer = malloc(hashtable->pool.capacity);
hashtable->pool.size = 0;
hashtable->pool.used = 0;
if (hashtable->pool.buffer == NULL) {
inthash_debug(hashtable,
"** hashtable string pool compaction error: could not allocate "
"%"UINT_64_FORMAT" bytes",
(uint64_t) hashtable->pool.capacity);
inthash_assert(hashtable, ! "hashtable string pool compaction error");
}
/* relocate a string on a different pool */
#define RELOCATE_STRING(S) do { \
if (S != NULL && S != the_empty_string) { \
const char *const src = (S); \
char *const dest = \
&hashtable->pool.buffer[hashtable->pool.size]; \
const size_t capacity = hashtable->pool.capacity; \
char *const max_dest = \
&hashtable->pool.buffer[capacity]; \
/* copy string */ \
inthash_assert(hashtable, dest < max_dest); \
dest[0] = src[0]; \
{ \
size_t i; \
for(i = 1 ; src[i - 1] != '\0' ; i++) { \
inthash_assert(hashtable, &dest[i] < max_dest); \
dest[i] = src[i]; \
} \
/* update pool size */ \
hashtable->pool.size += i; \
inthash_assert(hashtable, \
hashtable->pool.size <= capacity); \
} \
/* update source */ \
S = dest; \
count++; \
} \
} while(0)
/* relocate */
for(i = 0 ; i < hash_size ; i++) {
RELOCATE_STRING(hashtable->items[i].name);
}
for(i = 0 ; i < hashtable->stash.size ; i++) {
RELOCATE_STRING(hashtable->stash.items[i].name);
}
#undef RELOCATE_STRING
/* compacted (used chars == current size) */
hashtable->pool.used = hashtable->pool.size;
/* wipe previous pool */
free(old_pool);
inthash_debug(hashtable,
"compacted string pool for %"UINT_64_FORMAT" strings: "
"%"UINT_64_FORMAT" bytes => %"UINT_64_FORMAT" bytes",
(uint64_t) count, (uint64_t) old_size,
(uint64_t) hashtable->pool.size);
}
/* realloc (expand) string pool ; does not change the compacity */
static void inthash_realloc_pool(inthash hashtable, size_t capacity) {
const size_t hash_size = POW2(hashtable->lg_size);
char *const oldbase = hashtable->pool.buffer;
size_t i;
size_t count = 0;
/* we manage the string pool */
inthash_assert(hashtable, hashtable->custom.key.dup == NULL);
/* compact instead ? */
if (hashtable->pool.used < ( hashtable->pool.size*3 ) / 4) {
inthash_compact_pool(hashtable, capacity);
return ;
}
/* statistics */
hashtable->stats.pool_realloc_count++;
/* change capacity now */
hashtable->pool.capacity = capacity;
/* realloc */
hashtable->pool.buffer = realloc(hashtable->pool.buffer,
hashtable->pool.capacity);
if (hashtable->pool.buffer == NULL) {
inthash_crit(hashtable,
"** hashtable string pool allocation error: could not allocate "
"%"UINT_64_FORMAT" bytes",
(uint64_t) hashtable->pool.capacity);
inthash_assert(hashtable, ! "hashtable string pool allocation error");
}
/* recompute string address */
#define RECOMPUTE_STRING(S) do { \
if (S != NULL && S != the_empty_string) { \
const size_t offset = (const char*) (S) - oldbase; \
inthash_assert(hashtable, offset < hashtable->pool.capacity); \
S = &hashtable->pool.buffer[offset]; \
count++; \
} \
} while(0)
/* recompute */
for(i = 0 ; i < hash_size ; i++) {
RECOMPUTE_STRING(hashtable->items[i].name);
}
for(i = 0 ; i < hashtable->stash.size ; i++) {
RECOMPUTE_STRING(hashtable->stash.items[i].name);
}
#undef RECOMPUTE_STRING
inthash_debug(hashtable, "reallocated string pool for "
"%"UINT_64_FORMAT" strings: %"UINT_64_FORMAT" bytes",
(uint64_t) count, (uint64_t) hashtable->pool.capacity);
}
static inthash_key inthash_dup_name_internal(inthash hashtable,
inthash_key_const name_) {
const char *const name = (const char*) name_;
const size_t len = strlen(name) + 1;
char *s;
/* the pool does not allow empty strings for safety purpose ; handhe that
(keys are being emptied when free'd to detect duplicate free) */
if (len == 1) {
inthash_assert(hashtable, the_empty_string[0] == '\0');
return the_empty_string;
}
/* expand pool capacity */
inthash_assert(hashtable, hashtable->pool.size <= hashtable->pool.capacity);
if (hashtable->pool.capacity - hashtable->pool.size < len) {
size_t capacity;
for(capacity = MIN_POOL_CAPACITY ; capacity < hashtable->pool.size + len
; capacity <<= 1) ;
inthash_assert(hashtable, hashtable->pool.size < capacity);
inthash_realloc_pool(hashtable, capacity);
}
/* copy */
inthash_assert(hashtable, len + hashtable->pool.size <= hashtable->pool.capacity);
s = &hashtable->pool.buffer[hashtable->pool.size];
memcpy(s, name, len);
hashtable->pool.size += len;
hashtable->pool.used += len;
return s;
}
/* duplicate a key. default is to use the internal pool. */
static INTHASH_INLINE inthash_key inthash_dup_name(inthash hashtable,
inthash_key_const name) {
return hashtable->custom.key.dup == NULL
? inthash_dup_name_internal(hashtable, name)
: hashtable->custom.key.dup(hashtable->custom.key.arg, name);
}
/* internal pool free handler.
note: pointer must have been kicked from the pool first */
static void inthash_free_key_internal(inthash hashtable, inthash_key name_) {
char *const name = (char*) name_;
const size_t len = strlen(name) + 1;
/* see inthash_dup_name_internal() handling */
if (len == 1 && name == the_empty_string) {
inthash_assert(hashtable, the_empty_string[0] == '\0');
return ;
}
inthash_assert(hashtable, *name != '\0' || !"duplicate or bad string pool release");
hashtable->pool.used -= len;
*name = '\0'; /* the string is now invalidated */
/* compact the pool is too many holes */
if (hashtable->pool.used < hashtable->pool.size / 2) {
size_t capacity = hashtable->pool.capacity;
/* compact and shrink */
if (hashtable->pool.used < capacity / 4) {
capacity /= 2;
}
inthash_assert(hashtable, hashtable->pool.used < capacity);
inthash_compact_pool(hashtable, capacity);
}
}
/* free a key. default is to use the internal pool.
note: pointer must have been kicked from the pool first */
static void inthash_free_key(inthash hashtable, inthash_key name) {
if (hashtable->custom.key.free == NULL) {
inthash_free_key_internal(hashtable, name);
} else {
hashtable->custom.key.free(hashtable->custom.key.arg, name);
}
}
static INTHASH_INLINE size_t inthash_hash_to_pos_(size_t lg_size,
inthash_hashkey hash) {
const inthash_hashkey mask = POW2(lg_size) - 1;
return hash & mask;
}
static INTHASH_INLINE size_t inthash_hash_to_pos(const inthash hashtable,
inthash_hashkey hash) {
return inthash_hash_to_pos_(hashtable->lg_size, hash);
}
int inthash_read_pvoid(inthash hashtable, inthash_key_const name, void **pvalue) {
inthash_value value = INTHASH_VALUE_NULL;
const int ret =
inthash_read_value(hashtable, name, (pvalue != NULL) ? &value : NULL);
if (pvalue != NULL)
*pvalue = value.ptr;
return ret;
}
int inthash_write_pvoid(inthash hashtable, inthash_key_const name, void *pvalue) {
inthash_value value = INTHASH_VALUE_NULL;
value.ptr = pvalue;
return inthash_write_value(hashtable, name, value);
}
void inthash_add_pvoid(inthash hashtable, inthash_key_const name, void *pvalue) {
inthash_value value = INTHASH_VALUE_NULL;
value.ptr = pvalue;
inthash_write_value(hashtable, name, value);
}
int inthash_write(inthash hashtable, inthash_key_const name, intptr_t intvalue) {
inthash_value value = INTHASH_VALUE_NULL;
value.intg = intvalue;
return inthash_write_value(hashtable, name, value);
}
static void inthash_default_free_handler(inthash_opaque arg,
inthash_value value) {
(void) arg;
if (value.ptr != NULL)
free(value.ptr);
}
static void inthash_del_value_(inthash hashtable, inthash_value *pvalue) {
if (hashtable->custom.value.free != NULL)
hashtable->custom.value.free(hashtable->custom.value.arg, *pvalue);
pvalue->ptr = NULL;
}
static void inthash_del_value(inthash hashtable, size_t pos) {
inthash_del_value_(hashtable, &hashtable->items[pos].value);
}
static void inthash_del_name(inthash hashtable, inthash_item *item) {
const inthash_hashkeys nullHash = INTHASH_KEYS_NULL;
char *const name = (char*) item->name;
item->name = NULL; /* there must be no reference remaining */
item->hashes = nullHash;
/* free after detach (we may compact the pool) */
inthash_free_key(hashtable, name);
}
static void inthash_del_item(inthash hashtable, inthash_item *pitem) {
inthash_del_value_(hashtable, &pitem->value);
inthash_del_name(hashtable, pitem);
}
static int inthash_add_item_(inthash hashtable, inthash_item item);
/* Write (add or replace) a value in the hashtable. */
static int inthash_write_value_(inthash hashtable, inthash_key_const name,
inthash_value value) {
inthash_item item;
size_t pos;
const inthash_hashkeys hashes = inthash_calc_hashes(hashtable, name);
/* Statistics */
hashtable->stats.write_count++;
/* replace at position 1 ? */
pos = inthash_hash_to_pos(hashtable, hashes.hash1);
if (inthash_matches(hashtable, pos, name, &hashes)) {
inthash_del_value(hashtable, pos);
hashtable->items[pos].value = value;
return 0; /* replaced */
}
/* replace at position 2 ? */
pos = inthash_hash_to_pos(hashtable, hashes.hash2);
if (inthash_matches(hashtable, pos, name, &hashes)) {
inthash_del_value(hashtable, pos);
hashtable->items[pos].value = value;
return 0; /* replaced */
}
/* replace in the stash ? */
if (hashtable->stash.size != 0) {
size_t i;
for(i = 0 ; i < hashtable->stash.size ; i++) {
if (inthash_matches_(hashtable, &hashtable->stash.items[i], name,
&hashes)) {
inthash_del_value_(hashtable, &hashtable->stash.items[i].value);
hashtable->stash.items[i].value = value;
return 0; /* replaced */
}
}
}
/* Statistics */
hashtable->stats.add_count++;
/* otherwise we need to create a new item */
item.name = inthash_dup_name(hashtable, name);
item.value = value;
item.hashes = hashes;
return inthash_add_item_(hashtable, item);
}
/* Return the string representation of a key */
static const char* inthash_print_key(inthash hashtable,
inthash_key_const name) {
return hashtable->custom.print.key != NULL
? hashtable->custom.print.key(hashtable->custom.print.arg, name)
: (const char*) name;
}
/* Add a new item in the hashtable. The item SHALL NOT be alreasy present. */
static int inthash_add_item_(inthash hashtable, inthash_item item) {
inthash_hashkey cuckoo_hash, initial_cuckoo_hash;
size_t loops;
size_t pos;
/* place at free position 1 ? */
pos = inthash_hash_to_pos(hashtable, item.hashes.hash1);
if (inthash_is_free(hashtable, pos)) {
hashtable->items[pos] = item;
return 1; /* added */
} else {
/* place at free position 2 ? */
pos = inthash_hash_to_pos(hashtable, item.hashes.hash2);
if (inthash_is_free(hashtable, pos)) {
hashtable->items[pos] = item;
return 1; /* added */
}
/* prepare cuckoo ; let's take position 1 */
else {
cuckoo_hash = initial_cuckoo_hash = item.hashes.hash1;
inthash_trace(hashtable,
"debug:collision with '%s' at %"UINT_64_FORMAT" (%x)",
inthash_print_key(hashtable, item.name),
(uint64_t) pos, cuckoo_hash);
}
}
/* put 'item' in place with hash 'cuckoo_hash' */
for(loops = POW2(hashtable->lg_size) ; loops != 0 ; --loops) {
const size_t pos = inthash_hash_to_pos(hashtable, cuckoo_hash);
inthash_trace(hashtable,
"\tdebug:placing cuckoo '%s' at %"UINT_64_FORMAT" (%x)",
inthash_print_key(hashtable, item.name),
(uint64_t) pos, cuckoo_hash);
/* place at alternate free position ? */
if (inthash_is_free(hashtable, pos)) {
inthash_trace(hashtable, "debug:free position");
hashtable->items[pos] = item;
return 1; /* added */
}
/* then cuckoo's place it is */
else {
/* replace */
const inthash_item backup_item = hashtable->items[pos];
hashtable->items[pos] = item;
/* statistics */
hashtable->stats.cuckoo_moved++;
/* take care of new lost item */
item = backup_item;
/* we just kicked this item from its position 1 */
if (pos == inthash_hash_to_pos(hashtable, item.hashes.hash1)) {
/* then place it on position 2 on next run */
inthash_trace(hashtable, "\tdebug:position 1");
cuckoo_hash = item.hashes.hash2;
}
/* we just kicked this item from its position 2 */
else if (pos == inthash_hash_to_pos(hashtable, item.hashes.hash2)) {
/* then place it on position 1 on next run */
inthash_trace(hashtable, "\tdebug:position 2");
cuckoo_hash = item.hashes.hash1;
}
else {
inthash_assert(hashtable, ! "hashtable internal error: unexpected position");
}
/* we are looping (back to same hash) */
/* TODO FIXME: we should actually check the positions */
if (cuckoo_hash == initial_cuckoo_hash) {
/* emergency stash */
break;
}
}
}
/* emergency stashing for the rare cases of collisions */
if (hashtable->stash.size < STASH_SIZE) {
hashtable->stash.items[hashtable->stash.size] = item;
hashtable->stash.size++;
/* for statistics */
hashtable->stats.stash_added++;
if (hashtable->stash.size > hashtable->stats.max_stash_size) {
hashtable->stats.max_stash_size = hashtable->stash.size;
}
inthash_debug(hashtable, "used stash because of collision (%d entries)",
(int) hashtable->stash.size);
return 1; /* added */
} else {
/* debugging */
if (hashtable->custom.print.key != NULL
&& hashtable->custom.print.value != NULL) {
size_t i;
for(i = 0 ; i < hashtable->stash.size ; i++) {
inthash_item *const item = &hashtable->stash.items[i];
const size_t pos1 = inthash_hash_to_pos(hashtable, item->hashes.hash1);
const size_t pos2 = inthash_hash_to_pos(hashtable, item->hashes.hash2);
inthash_crit(hashtable,
"stash[%u]: key='%s' value='%s' pos1=%d pos2=%d hash1=%04x hash2=%04x",
(int) i,
hashtable->custom.print.key(hashtable->custom.print.arg, item->name),
hashtable->custom.print.value(hashtable->custom.print.arg, item->value),
(int) pos1, (int) pos2,
item->hashes.hash1, item->hashes.hash2);
if (!inthash_is_free(hashtable, pos1)) {
inthash_item *const item = &hashtable->items[pos1];
const size_t pos1 = inthash_hash_to_pos(hashtable, item->hashes.hash1);
const size_t pos2 = inthash_hash_to_pos(hashtable, item->hashes.hash2);
inthash_crit(hashtable,
"\t.. collisionning with key='%s' value='%s' pos1=%d pos2=%d hash1=%04x hash2=%04x",
hashtable->custom.print.key(hashtable->custom.print.arg, item->name),
hashtable->custom.print.value(hashtable->custom.print.arg, item->value),
(int) pos1, (int) pos2,
item->hashes.hash1, item->hashes.hash2);
} else {
inthash_crit(hashtable, "\t.. collisionning with a free slot (%d)!", (int) pos1);
}
if (!inthash_is_free(hashtable, pos2)) {
inthash_item *const item = &hashtable->items[pos2];
const size_t pos1 = inthash_hash_to_pos(hashtable, item->hashes.hash1);
const size_t pos2 = inthash_hash_to_pos(hashtable, item->hashes.hash2);
inthash_crit(hashtable,
"\t.. collisionning with key='%s' value='%s' pos1=%d pos2=%d hash1=%04x hash2=%04x",
hashtable->custom.print.key(hashtable->custom.print.arg, item->name),
hashtable->custom.print.value(hashtable->custom.print.arg, item->value),
(int) pos1, (int) pos2,
item->hashes.hash1, item->hashes.hash2);
} else {
inthash_crit(hashtable, "\t.. collisionning with a free slot (%d)!", (int) pos2);
}
}
//struct_inthash_enum e = inthash_enum_new(hashtable);
//while((item = inthash_enum_next(&e)) != NULL) {
// inthash_crit(hashtable, "element key='%s' value='%s' hash1=%04x hash2=%04x",
// hashtable->custom.print.key(hashtable->custom.print.arg, item->name),
// hashtable->custom.print.value(hashtable->custom.print.arg, item->value.ptr),
// item->hashes.hash1, item->hashes.hash2);
//}
}
/* we are doomed. hopefully the probability is lower than being killed
by a wandering radioactive monkey */
inthash_log_stats(hashtable);
inthash_assert(hashtable, ! "hashtable internal error: cuckoo/stash collision");
/* not reachable code */
return -1;
}
}
int inthash_write_value(inthash hashtable, inthash_key_const name,
inthash_value_const value) {
/* replace of add item */
const int ret = inthash_write_value_(hashtable, name, value);
/* added ? */
if (ret) {
/* size of half of the table */
const size_t half_size = POW2(hashtable->lg_size - 1);
/* size of half of the stash */
const size_t half_stash_size = STASH_SIZE / 2;
/* item was added: increase count */
hashtable->used++;
/* table is more than half-full, or stash is more than half-full */
if (hashtable->used >= half_size
|| hashtable->stash.size >= half_stash_size) {
size_t i;
/* size before */
const size_t prev_power = hashtable->lg_size;
const size_t prev_size = half_size * 2;
const size_t prev_alloc_size = prev_size*sizeof(inthash_item);
/* size after doubling it */
const size_t alloc_size = prev_alloc_size * 2;
/* log stash issues */
if (hashtable->stash.size >= half_stash_size
&& half_size > POW2(16)
&& hashtable->used < half_size / 4) {
inthash_warning(hashtable,
"stash size still full despite %"UINT_64_FORMAT
" elements used out of %"UINT_64_FORMAT,
(uint64_t) hashtable->used, (uint64_t) half_size*2);
}
/* statistics */
hashtable->stats.rehash_count++;
/* realloc */
hashtable->lg_size++;
hashtable->items =
(inthash_item *) realloc(hashtable->items, alloc_size);
if (hashtable->items == NULL) {
inthash_crit(hashtable,
"** hashtable allocation error: "
"could not allocate %"UINT_64_FORMAT" bytes",
(uint64_t) alloc_size);
inthash_assert(hashtable, ! "hashtable allocation error");
}
/* clear upper half */
memset(&hashtable->items[prev_size], 0, prev_alloc_size);
/* relocate lower half items when needed */
for(i = 0 ; i < prev_size ; i++) {
if (!inthash_is_free(hashtable, i)) {
const inthash_hashkeys *const hashes = &hashtable->items[i].hashes;
/* currently at old position 1 */
if (inthash_hash_to_pos_(prev_power, hashes->hash1) == i) {
const size_t pos = inthash_hash_to_pos(hashtable, hashes->hash1);
/* no more the expected position */
if (pos != i) {
inthash_assert(hashtable, pos >= prev_size);
hashtable->items[pos] = hashtable->items[i];
memset(&hashtable->items[i], 0, sizeof(hashtable->items[i]));
}
}
else if (inthash_hash_to_pos_(prev_power, hashes->hash2) == i) {
const size_t pos = inthash_hash_to_pos(hashtable, hashes->hash2);
/* no more the expected position */
if (pos != i) {
inthash_assert(hashtable, pos >= prev_size);
hashtable->items[pos] = hashtable->items[i];
memset(&hashtable->items[i], 0, sizeof(hashtable->items[i]));
}
}
else {
inthash_assert(hashtable, ! "hashtable unexpected internal error (bad position)");
}
}
}
inthash_debug(hashtable,
"expanded hashtable to %"UINT_64_FORMAT" elements",
(uint64_t) POW2(hashtable->lg_size));
/* attempt to merge the stash if present */
if (hashtable->stash.size != 0) {
const size_t old_size = hashtable->stash.size;
size_t i;
/* backup stash and reset it */
inthash_item stash[STASH_SIZE];
memcpy(&stash, hashtable->stash.items, sizeof(hashtable->stash.items));
hashtable->stash.size = 0;
/* insert all items */
for(i = 0 ; i < old_size ; i++) {
const int ret = inthash_add_item_(hashtable, stash[i]);
if (ret == 0) {
inthash_assert(hashtable, ! "hashtable duplicate key when merging the stash");
}
}
/* logging */
inthash_assert(hashtable, hashtable->stash.size <= old_size);
if (hashtable->stash.size < old_size) {
inthash_debug(hashtable, "reduced stash size from %"UINT_64_FORMAT" "
"to %"UINT_64_FORMAT,
(uint64_t) old_size, (uint64_t) hashtable->stash.size);
} else {
inthash_trace(hashtable, "stash has still %"UINT_64_FORMAT" elements",
(uint64_t) hashtable->stash.size);
}
}
}
}
return ret;
}
void inthash_add(inthash hashtable, inthash_key_const name, intptr_t intvalue) {
inthash_value value = INTHASH_VALUE_NULL;
memset(&value, 0, sizeof(value));
value.intg = intvalue;
inthash_write_value(hashtable, name, value);
}
int inthash_read(inthash hashtable, inthash_key_const name, intptr_t * intvalue) {
inthash_value value = INTHASH_VALUE_NULL;
int ret =
inthash_read_value(hashtable, name, (intvalue != NULL) ? &value : NULL);
if (intvalue != NULL)
*intvalue = value.intg;
return ret;
}
static inthash_value* inthash_read_value_(inthash hashtable,
inthash_key_const name) {
const inthash_hashkeys hashes = inthash_calc_hashes(hashtable, name);
size_t pos;
/* found at position 1 ? */
pos = inthash_hash_to_pos(hashtable, hashes.hash1);
if (inthash_matches(hashtable, pos, name, &hashes)) {
return &hashtable->items[pos].value;
}
/* found at position 2 ? */
pos = inthash_hash_to_pos(hashtable, hashes.hash2);
if (inthash_matches(hashtable, pos, name, &hashes)) {
return &hashtable->items[pos].value;
}
/* find in stash ? */
if (hashtable->stash.size != 0) {
size_t i;
for(i = 0 ; i < hashtable->stash.size ; i++) {
if (inthash_matches_(hashtable, &hashtable->stash.items[i], name,
&hashes)) {
return &hashtable->stash.items[i].value;
}
}
}
/* not found */
return NULL;
}
int inthash_read_value(inthash hashtable, inthash_key_const name,
inthash_value * pvalue) {
inthash_value* const value = inthash_read_value_(hashtable, name);
if (value != NULL) {
if (pvalue != NULL) {
*pvalue = *value;
}
return 1;
}
return 0;
}
static size_t inthash_inc_(inthash hashtable, inthash_key_const name,
size_t inc) {
inthash_value* const value = inthash_read_value_(hashtable, name);
if (value != NULL) {
value->uintg += inc;
return value->uintg;
} else {
/* create a new value */
const int ret = inthash_write(hashtable, name, inc);
inthash_assert(hashtable, ret);
return inc;
}
}
int inthash_inc(inthash hashtable, inthash_key_const name) {
return (int) inthash_inc_(hashtable, name, 1);
}
int inthash_dec(inthash hashtable, inthash_key_const name) {
return (int) inthash_inc_(hashtable, name, (size_t) -1);
}
int inthash_exists(inthash hashtable, inthash_key_const name) {
return inthash_read_value(hashtable, name, NULL);
}
static int inthash_remove_(inthash hashtable, inthash_key_const name,
const inthash_hashkeys *hashes, size_t *removed) {
size_t pos;
/* found at position 1 ? */
pos = inthash_hash_to_pos(hashtable, hashes->hash1);
if (inthash_matches(hashtable, pos, name, hashes)) {
inthash_del_item(hashtable, &hashtable->items[pos]);
*removed = pos;
return 1;
}
/* found at position 2 ? */
pos = inthash_hash_to_pos(hashtable, hashes->hash2);
if (inthash_matches(hashtable, pos, name, hashes)) {
inthash_del_item(hashtable, &hashtable->items[pos]);
*removed = pos;
return 1;
}
/* find in stash ? */
if (hashtable->stash.size != 0) {
size_t i;
for(i = 0 ; i < hashtable->stash.size ; i++) {
if (inthash_matches_(hashtable, &hashtable->stash.items[i], name,
hashes)) {
inthash_del_item(hashtable, &hashtable->stash.items[i]);
for( ; i + 1 < hashtable->stash.size ; i++) {
hashtable->stash.items[i] = hashtable->stash.items[i + 1];
}
hashtable->stash.size--;
*removed = (size_t) -1;
inthash_debug(hashtable, "debug:deleted item in stash (%d entries)",
(int) hashtable->stash.size);
return 1;
}
}
}
/* not found */
return 0;
}
int inthash_remove(inthash hashtable, inthash_key_const name) {
const inthash_hashkeys hashes = inthash_calc_hashes(hashtable, name);
size_t removed;
const int ret = inthash_remove_(hashtable, name, &hashes, &removed);
if (ret) {
/* item was removed: decrease count */
inthash_assert(hashtable, hashtable->used != 0);
hashtable->used--;
/* can we place stash entry back to the table ? */
if (hashtable->stash.size != 0 && removed != (size_t) -1) {
size_t i;
for(i = 0 ; i < hashtable->stash.size ; i++) {
const size_t pos1 =
inthash_hash_to_pos(hashtable, hashtable->stash.items[i].hashes.hash1);
const size_t pos2 =
inthash_hash_to_pos(hashtable, hashtable->stash.items[i].hashes.hash2);
if (pos1 == removed || pos2 == removed) {
if (pos1 == removed) {
hashtable->items[pos1] = hashtable->stash.items[i];
} else if (pos2 == removed) {
hashtable->items[pos2] = hashtable->stash.items[i];
}
for( ; i + 1 < hashtable->stash.size ; i++) {
hashtable->stash.items[i] = hashtable->stash.items[i + 1];
}
hashtable->stash.size--;
inthash_debug(hashtable, "debug:moved item from stash (%d entries)",
(int) hashtable->stash.size);
break;
}
}
}
}
return ret;
}
int inthash_readptr(inthash hashtable, inthash_key_const name, intptr_t * value) {
int ret;
*value = 0;
ret = inthash_read(hashtable, name, value);
if (*value == 0)
ret = 0;
return ret;
}
inthash inthash_new(size_t initial_size) {
inthash hashtable = (inthash) calloc(1, sizeof(struct_inthash));
if (hashtable) {
size_t size;
for(size = MIN_LG_SIZE ; POW2(size) < initial_size ; size++) ;
if ((hashtable->items =
(inthash_item *) calloc(POW2(size), sizeof(inthash_item)))) {
hashtable->lg_size = size;
}
hashtable->used = 0;
hashtable->stash.size = 0;
hashtable->pool.buffer = NULL;
hashtable->pool.size = 0;
hashtable->pool.capacity = 0;
hashtable->pool.used = 0;
hashtable->stats.max_stash_size = 0;
hashtable->stats.write_count = 0;
hashtable->stats.add_count = 0;
hashtable->stats.cuckoo_moved = 0;
hashtable->stats.stash_added= 0;
hashtable->stats.pool_compact_count = 0;
hashtable->stats.pool_realloc_count = 0;
hashtable->stats.rehash_count = 0;
hashtable->custom.value.free = NULL;
hashtable->custom.value.arg = NULL;
hashtable->custom.key.dup = NULL;
hashtable->custom.key.free = NULL;
hashtable->custom.key.hash = NULL;
hashtable->custom.key.equals = NULL;
hashtable->custom.key.arg = NULL;
hashtable->custom.error.log = NULL;
hashtable->custom.error.fatal = NULL;
hashtable->custom.error.name = NULL;
hashtable->custom.error.arg = NULL;
hashtable->custom.print.key = NULL;
hashtable->custom.print.value = NULL;
hashtable->custom.print.arg = NULL;
}
return hashtable;
}
int inthash_created(inthash hashtable) {
return hashtable != NULL && hashtable->items != NULL;
}
void inthash_value_is_malloc(inthash hashtable, int flag) {
if (flag) {
if (hashtable->custom.value.free == NULL) {
hashtable->custom.value.free = inthash_default_free_handler;
hashtable->custom.value.arg = NULL;
}
} else {
hashtable->custom.value.free = NULL;
hashtable->custom.value.arg = NULL;
}
}
void inthash_set_name(inthash hashtable, inthash_key_const name) {
hashtable->custom.error.name = name;
}
void inthash_value_set_value_handler(inthash hashtable,
t_inthash_value_freehandler free,
inthash_opaque arg) {
hashtable->custom.value.free = free;
hashtable->custom.value.arg = arg;
}
void inthash_value_set_key_handler(inthash hashtable,
t_inthash_duphandler dup,
t_inthash_key_freehandler free,
t_inthash_hasheshandler hash,
t_inthash_cmphandler equals,
inthash_opaque arg) {
/* dup and free must be consistent */
inthash_assert(hashtable, ( dup == NULL ) == ( free == NULL ) );
hashtable->custom.key.dup = dup;
hashtable->custom.key.free = free;
hashtable->custom.key.hash = hash;
hashtable->custom.key.equals = equals;
hashtable->custom.key.arg = arg;
}
void inthash_set_assert_handler(inthash hashtable,
t_inthash_loghandler log,
t_inthash_asserthandler fatal,
inthash_opaque arg) {
hashtable->custom.error.log = log;
hashtable->custom.error.fatal = fatal;
hashtable->custom.error.arg = arg;
}
void inthash_set_print_handler(inthash hashtable,
t_inthash_printkeyhandler key,
t_inthash_printvaluehandler value,
inthash_opaque arg) {
hashtable->custom.print.key = key;
hashtable->custom.print.value = value;
hashtable->custom.print.arg = arg;
}
size_t inthash_nitems(inthash hashtable) {
if (hashtable != NULL)
return hashtable->used;
return 0;
}
size_t inthash_memory_size(inthash hashtable) {
const size_t size_struct = sizeof(struct_inthash);
const size_t hash_size = POW2(hashtable->lg_size)*sizeof(inthash_item);
const size_t pool_size = hashtable->pool.capacity*sizeof(char);
return size_struct + hash_size + pool_size;
}
void inthash_delete(inthash *phashtable) {
if (phashtable != NULL) {
inthash hashtable = *phashtable;
if (hashtable != NULL) {
inthash_log_stats(hashtable);
if (hashtable->items != NULL) {
/* we need to delete values */
const size_t hash_size = POW2(hashtable->lg_size);
size_t i;
/* wipe hashtable values (not names) */
for(i = 0 ; i < hash_size ; i++) {
if (!inthash_is_free(hashtable, i)) {
inthash_del_value(hashtable, i);
}
}
/* wipe auxiliary stash values (not names) if any */
for(i = 0 ; i < hashtable->stash.size ; i++) {
inthash_del_value_(hashtable, &hashtable->stash.items[i].value);
}
}
/* wipe top-level */
hashtable->lg_size = 0;
hashtable->used = 0;
free(hashtable->pool.buffer);
hashtable->pool.buffer = NULL;
free(hashtable->items);
hashtable->items = NULL;
free(hashtable);
*phashtable = NULL;
}
}
}
/* Enumerator */
struct_inthash_enum inthash_enum_new(inthash hashtable) {
struct_inthash_enum e;
e.index = 0;
e.table = hashtable;
return e;
}
inthash_item *inthash_enum_next(struct_inthash_enum * e) {
const size_t hash_size = POW2(e->table->lg_size);
for( ; e->index < hash_size
&& inthash_is_free(e->table, e->index) ; e->index++) ;
/* enumerate all table */
if (e->index < hash_size) {
inthash_item *const next = &e->table->items[e->index];
e->index++;
return next;
}
/* enumerate stash if present */
else if (e->index < hash_size + e->table->stash.size) {
const size_t index = e->index - hash_size;
inthash_item *const next = &e->table->stash.items[index];
e->index++;
return next;
}
/* eof */
else {
return NULL;
}
}
void inthash_set_global_assert_handler(t_inthash_loghandler log,
t_inthash_asserthandler fatal) {
global_log_handler = log;
global_assert_handler = fatal;
}
|