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
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
|
/* This is a public domain general purpose hash table package written by Peter Moore @ UCB. */
/* static char sccsid[] = "@(#) st.c 5.1 89/12/14 Crucible"; */
#ifdef NOT_RUBY
#include "regint.h"
#include "st.h"
#else
#include "ruby/ruby.h"
#endif
#include <stdio.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#include <string.h>
typedef struct st_table_entry st_table_entry;
struct st_table_entry {
st_index_t hash;
st_data_t key;
st_data_t record;
st_table_entry *next;
st_table_entry *fore, *back;
};
typedef struct st_packed_entry {
st_index_t hash;
st_data_t key, val;
} st_packed_entry;
#define STATIC_ASSERT(name, expr) typedef int static_assert_##name##_check[(expr) ? 1 : -1];
#define ST_DEFAULT_MAX_DENSITY 5
#define ST_DEFAULT_INIT_TABLE_SIZE 16
#define ST_DEFAULT_PACKED_TABLE_SIZE 18
#define PACKED_UNIT (int)(sizeof(st_packed_entry) / sizeof(st_table_entry*))
#define MAX_PACKED_HASH (int)(ST_DEFAULT_PACKED_TABLE_SIZE * sizeof(st_table_entry*) / sizeof(st_packed_entry))
STATIC_ASSERT(st_packed_entry, sizeof(st_packed_entry) == sizeof(st_table_entry*[PACKED_UNIT]))
STATIC_ASSERT(st_packed_bins, sizeof(st_packed_entry[MAX_PACKED_HASH]) <= sizeof(st_table_entry*[ST_DEFAULT_PACKED_TABLE_SIZE]))
/*
* DEFAULT_MAX_DENSITY is the default for the largest we allow the
* average number of items per bin before increasing the number of
* bins
*
* DEFAULT_INIT_TABLE_SIZE is the default for the number of bins
* allocated initially
*
*/
#define type_numhash st_hashtype_num
const struct st_hash_type st_hashtype_num = {
st_numcmp,
st_numhash,
};
/* extern int strcmp(const char *, const char *); */
static st_index_t strhash(st_data_t);
static const struct st_hash_type type_strhash = {
strcmp,
strhash,
};
static st_index_t strcasehash(st_data_t);
static const struct st_hash_type type_strcasehash = {
st_locale_insensitive_strcasecmp,
strcasehash,
};
static void rehash(st_table *);
#ifdef RUBY
#define malloc xmalloc
#define calloc xcalloc
#define realloc xrealloc
#define free(x) xfree(x)
#endif
#define numberof(array) (int)(sizeof(array) / sizeof((array)[0]))
#define EQUAL(table,x,y) ((x)==(y) || (*(table)->type->compare)((x),(y)) == 0)
#define do_hash(key,table) (st_index_t)(*(table)->type->hash)((key))
#define hash_pos(h,n) ((h) & (n - 1))
#define do_hash_bin(key,table) hash_pos(do_hash((key), (table)), (table)->num_bins)
/* preparation for possible allocation improvements */
#define st_alloc_entry() (st_table_entry *)malloc(sizeof(st_table_entry))
#define st_free_entry(entry) free(entry)
#define st_alloc_table() (st_table *)malloc(sizeof(st_table))
#define st_dealloc_table(table) free(table)
#define st_alloc_bins(size) (st_table_entry **)calloc(size, sizeof(st_table_entry *))
#define st_free_bins(bins, size) free(bins)
static inline st_table_entry**
st_realloc_bins(st_table_entry **bins, st_index_t newsize, st_index_t oldsize)
{
bins = (st_table_entry **)realloc(bins, newsize * sizeof(st_table_entry *));
MEMZERO(bins, st_table_entry*, newsize);
return bins;
}
/* Shortcut */
#define bins as.big.bins
#define head as.big.head
#define tail as.big.tail
#define real_entries as.packed.real_entries
/* preparation for possible packing improvements */
#define PACKED_BINS(table) ((table)->as.packed.entries)
#define PACKED_ENT(table, i) PACKED_BINS(table)[i]
#define PKEY(table, i) PACKED_ENT((table), (i)).key
#define PVAL(table, i) PACKED_ENT((table), (i)).val
#define PHASH(table, i) PACKED_ENT((table), (i)).hash
#define PKEY_SET(table, i, v) (PKEY((table), (i)) = (v))
#define PVAL_SET(table, i, v) (PVAL((table), (i)) = (v))
#define PHASH_SET(table, i, v) (PHASH((table), (i)) = (v))
/* this function depends much on packed layout, so that it placed here */
static inline void
remove_packed_entry(st_table *table, st_index_t i)
{
table->real_entries--;
table->num_entries--;
if (i < table->real_entries) {
MEMMOVE(&PACKED_ENT(table, i), &PACKED_ENT(table, i+1),
st_packed_entry, table->real_entries - i);
}
}
static inline void
remove_safe_packed_entry(st_table *table, st_index_t i, st_data_t never)
{
table->num_entries--;
PKEY_SET(table, i, never);
PVAL_SET(table, i, never);
PHASH_SET(table, i, 0);
}
static st_index_t
new_size(st_index_t size)
{
st_index_t i;
for (i=3; i<31; i++) {
if ((st_index_t)(1<<i) > size) return 1<<i;
}
#ifndef NOT_RUBY
rb_raise(rb_eRuntimeError, "st_table too big");
#endif
return -1; /* should raise exception */
}
#ifdef HASH_LOG
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
static struct {
int all, total, num, str, strcase;
} collision;
static int init_st = 0;
static void
stat_col(void)
{
char fname[10+sizeof(long)*3];
FILE *f = fopen((snprintf(fname, sizeof(fname), "/tmp/col%ld", (long)getpid()), fname), "w");
fprintf(f, "collision: %d / %d (%6.2f)\n", collision.all, collision.total,
((double)collision.all / (collision.total)) * 100);
fprintf(f, "num: %d, str: %d, strcase: %d\n", collision.num, collision.str, collision.strcase);
fclose(f);
}
#endif
st_table*
st_init_table_with_size(const struct st_hash_type *type, st_index_t size)
{
st_table *tbl;
#ifdef HASH_LOG
# if HASH_LOG+0 < 0
{
const char *e = getenv("ST_HASH_LOG");
if (!e || !*e) init_st = 1;
}
# endif
if (init_st == 0) {
init_st = 1;
atexit(stat_col);
}
#endif
tbl = st_alloc_table();
tbl->type = type;
tbl->num_entries = 0;
tbl->entries_packed = size <= MAX_PACKED_HASH;
if (tbl->entries_packed) {
size = ST_DEFAULT_PACKED_TABLE_SIZE;
}
else {
size = new_size(size); /* round up to power-of-two */
}
tbl->num_bins = size;
tbl->bins = st_alloc_bins(size);
tbl->head = 0;
tbl->tail = 0;
return tbl;
}
st_table*
st_init_table(const struct st_hash_type *type)
{
return st_init_table_with_size(type, 0);
}
st_table*
st_init_numtable(void)
{
return st_init_table(&type_numhash);
}
st_table*
st_init_numtable_with_size(st_index_t size)
{
return st_init_table_with_size(&type_numhash, size);
}
st_table*
st_init_strtable(void)
{
return st_init_table(&type_strhash);
}
st_table*
st_init_strtable_with_size(st_index_t size)
{
return st_init_table_with_size(&type_strhash, size);
}
st_table*
st_init_strcasetable(void)
{
return st_init_table(&type_strcasehash);
}
st_table*
st_init_strcasetable_with_size(st_index_t size)
{
return st_init_table_with_size(&type_strcasehash, size);
}
void
st_clear(st_table *table)
{
register st_table_entry *ptr, *next;
st_index_t i;
if (table->entries_packed) {
table->num_entries = 0;
table->real_entries = 0;
return;
}
for (i = 0; i < table->num_bins; i++) {
ptr = table->bins[i];
table->bins[i] = 0;
while (ptr != 0) {
next = ptr->next;
st_free_entry(ptr);
ptr = next;
}
}
table->num_entries = 0;
table->head = 0;
table->tail = 0;
}
void
st_free_table(st_table *table)
{
st_clear(table);
st_free_bins(table->bins, table->num_bins);
st_dealloc_table(table);
}
size_t
st_memsize(const st_table *table)
{
if (table->entries_packed) {
return table->num_bins * sizeof (void *) + sizeof(st_table);
}
else {
return table->num_entries * sizeof(struct st_table_entry) + table->num_bins * sizeof (void *) + sizeof(st_table);
}
}
#define PTR_NOT_EQUAL(table, ptr, hash_val, key) \
((ptr) != 0 && ((ptr)->hash != (hash_val) || !EQUAL((table), (key), (ptr)->key)))
#ifdef HASH_LOG
static void
count_collision(const struct st_hash_type *type)
{
collision.all++;
if (type == &type_numhash) {
collision.num++;
}
else if (type == &type_strhash) {
collision.strcase++;
}
else if (type == &type_strcasehash) {
collision.str++;
}
}
#define COLLISION (collision_check ? count_collision(table->type) : (void)0)
#define FOUND_ENTRY (collision_check ? collision.total++ : (void)0)
#else
#define COLLISION
#define FOUND_ENTRY
#endif
#define FIND_ENTRY(table, ptr, hash_val, bin_pos) \
((ptr) = find_entry((table), key, (hash_val), ((bin_pos) = hash_pos(hash_val, (table)->num_bins))))
static st_table_entry *
find_entry(st_table *table, st_data_t key, st_index_t hash_val, st_index_t bin_pos)
{
register st_table_entry *ptr = table->bins[bin_pos];
FOUND_ENTRY;
if (PTR_NOT_EQUAL(table, ptr, hash_val, key)) {
COLLISION;
while (PTR_NOT_EQUAL(table, ptr->next, hash_val, key)) {
ptr = ptr->next;
}
ptr = ptr->next;
}
return ptr;
}
static inline st_index_t
find_packed_index_from(st_table *table, st_index_t hash_val, st_data_t key, st_index_t i)
{
while (i < table->real_entries &&
(PHASH(table, i) != hash_val || !EQUAL(table, key, PKEY(table, i)))) {
i++;
}
return i;
}
static inline st_index_t
find_packed_index(st_table *table, st_index_t hash_val, st_data_t key)
{
return find_packed_index_from(table, hash_val, key, 0);
}
#define collision_check 0
int
st_lookup(st_table *table, register st_data_t key, st_data_t *value)
{
st_index_t hash_val;
register st_table_entry *ptr;
hash_val = do_hash(key, table);
if (table->entries_packed) {
st_index_t i = find_packed_index(table, hash_val, key);
if (i < table->real_entries) {
if (value != 0) *value = PVAL(table, i);
return 1;
}
return 0;
}
ptr = find_entry(table, key, hash_val, hash_pos(hash_val, table->num_bins));
if (ptr == 0) {
return 0;
}
else {
if (value != 0) *value = ptr->record;
return 1;
}
}
int
st_get_key(st_table *table, register st_data_t key, st_data_t *result)
{
st_index_t hash_val;
register st_table_entry *ptr;
hash_val = do_hash(key, table);
if (table->entries_packed) {
st_index_t i = find_packed_index(table, hash_val, key);
if (i < table->real_entries) {
if (result != 0) *result = PKEY(table, i);
return 1;
}
return 0;
}
ptr = find_entry(table, key, hash_val, hash_pos(hash_val, table->num_bins));
if (ptr == 0) {
return 0;
}
else {
if (result != 0) *result = ptr->key;
return 1;
}
}
#undef collision_check
#define collision_check 1
static inline st_table_entry *
new_entry(st_table * table, st_data_t key, st_data_t value,
st_index_t hash_val, register st_index_t bin_pos)
{
register st_table_entry *entry = st_alloc_entry();
entry->next = table->bins[bin_pos];
table->bins[bin_pos] = entry;
entry->hash = hash_val;
entry->key = key;
entry->record = value;
return entry;
}
static inline void
add_direct(st_table *table, st_data_t key, st_data_t value,
st_index_t hash_val, register st_index_t bin_pos)
{
register st_table_entry *entry;
if (table->num_entries > ST_DEFAULT_MAX_DENSITY * table->num_bins) {
rehash(table);
bin_pos = hash_pos(hash_val, table->num_bins);
}
entry = new_entry(table, key, value, hash_val, bin_pos);
if (table->head != 0) {
entry->fore = 0;
(entry->back = table->tail)->fore = entry;
table->tail = entry;
}
else {
table->head = table->tail = entry;
entry->fore = entry->back = 0;
}
table->num_entries++;
}
static void
unpack_entries(register st_table *table)
{
st_index_t i;
st_packed_entry packed_bins[MAX_PACKED_HASH];
register st_table_entry *entry, *preventry = 0, **chain;
st_table tmp_table = *table;
MEMCPY(packed_bins, PACKED_BINS(table), st_packed_entry, MAX_PACKED_HASH);
table->as.packed.entries = packed_bins;
tmp_table.entries_packed = 0;
#if ST_DEFAULT_INIT_TABLE_SIZE == ST_DEFAULT_PACKED_TABLE_SIZE
MEMZERO(tmp_table.bins, st_table_entry*, tmp_table.num_bins);
#else
tmp_table.bins = st_realloc_bins(tmp_table.bins, ST_DEFAULT_INIT_TABLE_SIZE, tmp_table.num_bins);
tmp_table.num_bins = ST_DEFAULT_INIT_TABLE_SIZE;
#endif
i = 0;
chain = &tmp_table.head;
do {
st_data_t key = packed_bins[i].key;
st_data_t val = packed_bins[i].val;
st_index_t hash = packed_bins[i].hash;
entry = new_entry(&tmp_table, key, val, hash,
hash_pos(hash, ST_DEFAULT_INIT_TABLE_SIZE));
*chain = entry;
entry->back = preventry;
preventry = entry;
chain = &entry->fore;
} while (++i < MAX_PACKED_HASH);
*chain = NULL;
tmp_table.tail = entry;
*table = tmp_table;
}
static void
add_packed_direct(st_table *table, st_data_t key, st_data_t value, st_index_t hash_val)
{
if (table->real_entries < MAX_PACKED_HASH) {
st_index_t i = table->real_entries++;
PKEY_SET(table, i, key);
PVAL_SET(table, i, value);
PHASH_SET(table, i, hash_val);
table->num_entries++;
}
else {
unpack_entries(table);
add_direct(table, key, value, hash_val, hash_pos(hash_val, table->num_bins));
}
}
int
st_insert(register st_table *table, register st_data_t key, st_data_t value)
{
st_index_t hash_val;
register st_index_t bin_pos;
register st_table_entry *ptr;
hash_val = do_hash(key, table);
if (table->entries_packed) {
st_index_t i = find_packed_index(table, hash_val, key);
if (i < table->real_entries) {
PVAL_SET(table, i, value);
return 1;
}
add_packed_direct(table, key, value, hash_val);
return 0;
}
FIND_ENTRY(table, ptr, hash_val, bin_pos);
if (ptr == 0) {
add_direct(table, key, value, hash_val, bin_pos);
return 0;
}
else {
ptr->record = value;
return 1;
}
}
int
st_insert2(register st_table *table, register st_data_t key, st_data_t value,
st_data_t (*func)(st_data_t))
{
st_index_t hash_val;
register st_index_t bin_pos;
register st_table_entry *ptr;
hash_val = do_hash(key, table);
if (table->entries_packed) {
st_index_t i = find_packed_index(table, hash_val, key);
if (i < table->real_entries) {
PVAL_SET(table, i, value);
return 1;
}
key = (*func)(key);
add_packed_direct(table, key, value, hash_val);
return 0;
}
FIND_ENTRY(table, ptr, hash_val, bin_pos);
if (ptr == 0) {
key = (*func)(key);
add_direct(table, key, value, hash_val, bin_pos);
return 0;
}
else {
ptr->record = value;
return 1;
}
}
void
st_add_direct(st_table *table, st_data_t key, st_data_t value)
{
st_index_t hash_val;
hash_val = do_hash(key, table);
if (table->entries_packed) {
add_packed_direct(table, key, value, hash_val);
return;
}
add_direct(table, key, value, hash_val, hash_pos(hash_val, table->num_bins));
}
static void
rehash(register st_table *table)
{
register st_table_entry *ptr, **new_bins;
st_index_t new_num_bins, hash_val;
new_num_bins = new_size(table->num_bins+1);
new_bins = st_realloc_bins(table->bins, new_num_bins, table->num_bins);
table->num_bins = new_num_bins;
table->bins = new_bins;
if ((ptr = table->head) != 0) {
do {
hash_val = hash_pos(ptr->hash, new_num_bins);
ptr->next = new_bins[hash_val];
new_bins[hash_val] = ptr;
} while ((ptr = ptr->fore) != 0);
}
}
st_table*
st_copy(st_table *old_table)
{
st_table *new_table;
st_table_entry *ptr, *entry, *prev, **tailp;
st_index_t num_bins = old_table->num_bins;
st_index_t hash_val;
new_table = st_alloc_table();
if (new_table == 0) {
return 0;
}
*new_table = *old_table;
new_table->bins = st_alloc_bins(num_bins);
if (new_table->bins == 0) {
st_dealloc_table(new_table);
return 0;
}
if (old_table->entries_packed) {
MEMCPY(new_table->bins, old_table->bins, st_table_entry*, old_table->num_bins);
return new_table;
}
if ((ptr = old_table->head) != 0) {
prev = 0;
tailp = &new_table->head;
do {
entry = st_alloc_entry();
if (entry == 0) {
st_free_table(new_table);
return 0;
}
*entry = *ptr;
hash_val = hash_pos(entry->hash, num_bins);
entry->next = new_table->bins[hash_val];
new_table->bins[hash_val] = entry;
entry->back = prev;
*tailp = prev = entry;
tailp = &entry->fore;
} while ((ptr = ptr->fore) != 0);
new_table->tail = prev;
}
return new_table;
}
static inline void
remove_entry(st_table *table, st_table_entry *ptr)
{
if (ptr->fore == 0 && ptr->back == 0) {
table->head = 0;
table->tail = 0;
}
else {
st_table_entry *fore = ptr->fore, *back = ptr->back;
if (fore) fore->back = back;
if (back) back->fore = fore;
if (ptr == table->head) table->head = fore;
if (ptr == table->tail) table->tail = back;
}
table->num_entries--;
}
int
st_delete(register st_table *table, register st_data_t *key, st_data_t *value)
{
st_index_t hash_val;
st_table_entry **prev;
register st_table_entry *ptr;
hash_val = do_hash(*key, table);
if (table->entries_packed) {
st_index_t i = find_packed_index(table, hash_val, *key);
if (i < table->real_entries) {
if (value != 0) *value = PVAL(table, i);
*key = PKEY(table, i);
remove_packed_entry(table, i);
return 1;
}
if (value != 0) *value = 0;
return 0;
}
prev = &table->bins[hash_pos(hash_val, table->num_bins)];
for (;(ptr = *prev) != 0; prev = &ptr->next) {
if (EQUAL(table, *key, ptr->key)) {
*prev = ptr->next;
remove_entry(table, ptr);
if (value != 0) *value = ptr->record;
*key = ptr->key;
st_free_entry(ptr);
return 1;
}
}
if (value != 0) *value = 0;
return 0;
}
int
st_delete_safe(register st_table *table, register st_data_t *key, st_data_t *value, st_data_t never)
{
st_index_t hash_val;
register st_table_entry *ptr;
hash_val = do_hash(*key, table);
if (table->entries_packed) {
st_index_t i = find_packed_index(table, hash_val, *key);
if (i < table->real_entries) {
if (value != 0) *value = PVAL(table, i);
*key = PKEY(table, i);
remove_safe_packed_entry(table, i, never);
return 1;
}
if (value != 0) *value = 0;
return 0;
}
ptr = table->bins[hash_pos(hash_val, table->num_bins)];
for (; ptr != 0; ptr = ptr->next) {
if ((ptr->key != never) && EQUAL(table, ptr->key, *key)) {
remove_entry(table, ptr);
*key = ptr->key;
if (value != 0) *value = ptr->record;
ptr->key = ptr->record = never;
return 1;
}
}
if (value != 0) *value = 0;
return 0;
}
int
st_shift(register st_table *table, register st_data_t *key, st_data_t *value)
{
st_table_entry **prev;
register st_table_entry *ptr;
if (table->num_entries == 0) {
if (value != 0) *value = 0;
return 0;
}
if (table->entries_packed) {
if (value != 0) *value = PVAL(table, 0);
*key = PKEY(table, 0);
remove_packed_entry(table, 0);
return 1;
}
prev = &table->bins[hash_pos(table->head->hash, table->num_bins)];
while ((ptr = *prev) != table->head) prev = &ptr->next;
*prev = ptr->next;
if (value != 0) *value = ptr->record;
*key = ptr->key;
remove_entry(table, ptr);
st_free_entry(ptr);
return 1;
}
void
st_cleanup_safe(st_table *table, st_data_t never)
{
st_table_entry *ptr, **last, *tmp;
st_index_t i;
if (table->entries_packed) {
st_index_t i = 0, j = 0;
while (PKEY(table, i) != never) {
if (i++ == table->real_entries) return;
}
for (j = i; ++i < table->real_entries;) {
if (PKEY(table, i) == never) continue;
PACKED_ENT(table, j) = PACKED_ENT(table, i);
j++;
}
table->real_entries = j;
/* table->num_entries really should be equal j at this moment, but let set it anyway */
table->num_entries = j;
return;
}
for (i = 0; i < table->num_bins; i++) {
ptr = *(last = &table->bins[i]);
while (ptr != 0) {
if (ptr->key == never) {
tmp = ptr;
*last = ptr = ptr->next;
st_free_entry(tmp);
}
else {
ptr = *(last = &ptr->next);
}
}
}
}
int
st_update(st_table *table, st_data_t key, st_update_callback_func *func, st_data_t arg)
{
st_index_t hash_val, bin_pos;
register st_table_entry *ptr, **last, *tmp;
st_data_t value = 0;
int retval, existing = 0;
hash_val = do_hash(key, table);
if (table->entries_packed) {
st_index_t i = find_packed_index(table, hash_val, key);
if (i < table->real_entries) {
key = PKEY(table, i);
value = PVAL(table, i);
existing = 1;
}
{
retval = (*func)(&key, &value, arg, existing);
if (!table->entries_packed) {
FIND_ENTRY(table, ptr, hash_val, bin_pos);
goto unpacked;
}
switch (retval) {
case ST_CONTINUE:
if (!existing) {
add_packed_direct(table, key, value, hash_val);
break;
}
PVAL_SET(table, i, value);
break;
case ST_DELETE:
if (!existing) break;
remove_packed_entry(table, i);
}
}
return existing;
}
FIND_ENTRY(table, ptr, hash_val, bin_pos);
if (ptr != 0) {
key = ptr->key;
value = ptr->record;
existing = 1;
}
{
retval = (*func)(&key, &value, arg, existing);
unpacked:
switch (retval) {
case ST_CONTINUE:
if (!existing) {
add_direct(table, key, value, hash_val, hash_pos(hash_val, table->num_bins));
break;
}
ptr->record = value;
break;
case ST_DELETE:
if (!existing) break;
last = &table->bins[bin_pos];
for (; (tmp = *last) != 0; last = &tmp->next) {
if (ptr == tmp) {
*last = ptr->next;
remove_entry(table, ptr);
st_free_entry(ptr);
break;
}
}
break;
}
return existing;
}
}
int
st_foreach_check(st_table *table, int (*func)(ANYARGS), st_data_t arg, st_data_t never)
{
st_table_entry *ptr, **last, *tmp;
enum st_retval retval;
st_index_t i;
if (table->entries_packed) {
for (i = 0; i < table->real_entries; i++) {
st_data_t key, val;
st_index_t hash;
key = PKEY(table, i);
val = PVAL(table, i);
hash = PHASH(table, i);
if (key == never) continue;
retval = (*func)(key, val, arg, 0);
if (!table->entries_packed) {
FIND_ENTRY(table, ptr, hash, i);
if (retval == ST_CHECK) {
if (!ptr) goto deleted;
goto unpacked_continue;
}
goto unpacked;
}
switch (retval) {
case ST_CHECK: /* check if hash is modified during iteration */
if (PHASH(table, i) == 0 && PKEY(table, i) == never) {
break;
}
i = find_packed_index_from(table, hash, key, i);
if (i >= table->real_entries) {
i = find_packed_index(table, hash, key);
if (i >= table->real_entries) goto deleted;
}
/* fall through */
case ST_CONTINUE:
break;
case ST_STOP:
return 0;
case ST_DELETE:
remove_safe_packed_entry(table, i, never);
break;
}
}
return 0;
}
else {
ptr = table->head;
}
if (ptr != 0) {
do {
if (ptr->key == never)
goto unpacked_continue;
i = hash_pos(ptr->hash, table->num_bins);
retval = (*func)(ptr->key, ptr->record, arg, 0);
unpacked:
switch (retval) {
case ST_CHECK: /* check if hash is modified during iteration */
for (tmp = table->bins[i]; tmp != ptr; tmp = tmp->next) {
if (!tmp) {
deleted:
/* call func with error notice */
retval = (*func)(0, 0, arg, 1);
return 1;
}
}
/* fall through */
case ST_CONTINUE:
unpacked_continue:
ptr = ptr->fore;
break;
case ST_STOP:
return 0;
case ST_DELETE:
last = &table->bins[hash_pos(ptr->hash, table->num_bins)];
for (; (tmp = *last) != 0; last = &tmp->next) {
if (ptr == tmp) {
tmp = ptr->fore;
remove_entry(table, ptr);
ptr->key = ptr->record = never;
ptr->hash = 0;
ptr = tmp;
break;
}
}
}
} while (ptr && table->head);
}
return 0;
}
int
st_foreach(st_table *table, int (*func)(ANYARGS), st_data_t arg)
{
st_table_entry *ptr, **last, *tmp;
enum st_retval retval;
st_index_t i;
if (table->entries_packed) {
for (i = 0; i < table->real_entries; i++) {
st_data_t key, val;
st_index_t hash;
key = PKEY(table, i);
val = PVAL(table, i);
hash = PHASH(table, i);
retval = (*func)(key, val, arg, 0);
if (!table->entries_packed) {
FIND_ENTRY(table, ptr, hash, i);
if (!ptr) return 0;
goto unpacked;
}
switch (retval) {
case ST_CONTINUE:
break;
case ST_CHECK:
case ST_STOP:
return 0;
case ST_DELETE:
remove_packed_entry(table, i);
i--;
break;
}
}
return 0;
}
else {
ptr = table->head;
}
if (ptr != 0) {
do {
i = hash_pos(ptr->hash, table->num_bins);
retval = (*func)(ptr->key, ptr->record, arg, 0);
unpacked:
switch (retval) {
case ST_CONTINUE:
ptr = ptr->fore;
break;
case ST_CHECK:
case ST_STOP:
return 0;
case ST_DELETE:
last = &table->bins[hash_pos(ptr->hash, table->num_bins)];
for (; (tmp = *last) != 0; last = &tmp->next) {
if (ptr == tmp) {
tmp = ptr->fore;
*last = ptr->next;
remove_entry(table, ptr);
st_free_entry(ptr);
ptr = tmp;
break;
}
}
}
} while (ptr && table->head);
}
return 0;
}
static st_index_t
get_keys(st_table *table, st_data_t *keys, st_index_t size, int check, st_data_t never)
{
st_data_t key;
st_data_t *keys_start = keys;
if (table->entries_packed) {
st_index_t i;
if (size > table->real_entries) size = table->real_entries;
for (i = 0; i < size; i++) {
key = PKEY(table, i);
if (check && key == never) continue;
*keys++ = key;
}
}
else {
st_table_entry *ptr = table->head;
st_data_t *keys_end = keys + size;
for (; ptr && keys < keys_end; ptr = ptr->fore) {
key = ptr->key;
if (check && key == never) continue;
*keys++ = key;
}
}
return keys - keys_start;
}
st_index_t
st_keys(st_table *table, st_data_t *keys, st_index_t size)
{
return get_keys(table, keys, size, 0, 0);
}
st_index_t
st_keys_check(st_table *table, st_data_t *keys, st_index_t size, st_data_t never)
{
return get_keys(table, keys, size, 1, never);
}
static st_index_t
get_values(st_table *table, st_data_t *values, st_index_t size, int check, st_data_t never)
{
st_data_t key;
st_data_t *values_start = values;
if (table->entries_packed) {
st_index_t i;
if (size > table->real_entries) size = table->real_entries;
for (i = 0; i < size; i++) {
key = PKEY(table, i);
if (check && key == never) continue;
*values++ = PVAL(table, i);
}
}
else {
st_table_entry *ptr = table->head;
st_data_t *values_end = values + size;
for (; ptr && values < values_end; ptr = ptr->fore) {
key = ptr->key;
if (check && key == never) continue;
*values++ = ptr->record;
}
}
return values - values_start;
}
st_index_t
st_values(st_table *table, st_data_t *values, st_index_t size)
{
return get_values(table, values, size, 0, 0);
}
st_index_t
st_values_check(st_table *table, st_data_t *values, st_index_t size, st_data_t never)
{
return get_values(table, values, size, 1, never);
}
#if 0 /* unused right now */
int
st_reverse_foreach(st_table *table, int (*func)(ANYARGS), st_data_t arg)
{
st_table_entry *ptr, **last, *tmp;
enum st_retval retval;
int i;
if (table->entries_packed) {
for (i = table->num_entries-1; 0 <= i; i--) {
int j;
st_data_t key, val;
key = PKEY(table, i);
val = PVAL(table, i);
retval = (*func)(key, val, arg, 0);
switch (retval) {
case ST_CHECK: /* check if hash is modified during iteration */
for (j = 0; j < table->num_entries; j++) {
if (PKEY(table, j) == key)
break;
}
if (j == table->num_entries) {
/* call func with error notice */
retval = (*func)(0, 0, arg, 1);
return 1;
}
/* fall through */
case ST_CONTINUE:
break;
case ST_STOP:
return 0;
case ST_DELETE:
remove_packed_entry(table, i);
break;
}
}
return 0;
}
if ((ptr = table->head) != 0) {
ptr = ptr->back;
do {
retval = (*func)(ptr->key, ptr->record, arg, 0);
switch (retval) {
case ST_CHECK: /* check if hash is modified during iteration */
i = hash_pos(ptr->hash, table->num_bins);
for (tmp = table->bins[i]; tmp != ptr; tmp = tmp->next) {
if (!tmp) {
/* call func with error notice */
retval = (*func)(0, 0, arg, 1);
return 1;
}
}
/* fall through */
case ST_CONTINUE:
ptr = ptr->back;
break;
case ST_STOP:
return 0;
case ST_DELETE:
last = &table->bins[hash_pos(ptr->hash, table->num_bins)];
for (; (tmp = *last) != 0; last = &tmp->next) {
if (ptr == tmp) {
tmp = ptr->back;
*last = ptr->next;
remove_entry(table, ptr);
st_free_entry(ptr);
ptr = tmp;
break;
}
}
ptr = ptr->next;
free(tmp);
table->num_entries--;
}
} while (ptr && table->head);
}
return 0;
}
#endif
/*
* hash_32 - 32 bit Fowler/Noll/Vo FNV-1a hash code
*
* @(#) $Hash32: Revision: 1.1 $
* @(#) $Hash32: Id: hash_32a.c,v 1.1 2003/10/03 20:38:53 chongo Exp $
* @(#) $Hash32: Source: /usr/local/src/cmd/fnv/RCS/hash_32a.c,v $
*
***
*
* Fowler/Noll/Vo hash
*
* The basis of this hash algorithm was taken from an idea sent
* as reviewer comments to the IEEE POSIX P1003.2 committee by:
*
* Phong Vo (http://www.research.att.com/info/kpv/)
* Glenn Fowler (http://www.research.att.com/~gsf/)
*
* In a subsequent ballot round:
*
* Landon Curt Noll (http://www.isthe.com/chongo/)
*
* improved on their algorithm. Some people tried this hash
* and found that it worked rather well. In an EMail message
* to Landon, they named it the ``Fowler/Noll/Vo'' or FNV hash.
*
* FNV hashes are designed to be fast while maintaining a low
* collision rate. The FNV speed allows one to quickly hash lots
* of data while maintaining a reasonable collision rate. See:
*
* http://www.isthe.com/chongo/tech/comp/fnv/index.html
*
* for more details as well as other forms of the FNV hash.
***
*
* To use the recommended 32 bit FNV-1a hash, pass FNV1_32A_INIT as the
* Fnv32_t hashval argument to fnv_32a_buf() or fnv_32a_str().
*
***
*
* Please do not copyright this code. This code is in the public domain.
*
* LANDON CURT NOLL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO
* EVENT SHALL LANDON CURT NOLL BE LIABLE FOR ANY SPECIAL, 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.
*
* By:
* chongo <Landon Curt Noll> /\oo/\
* http://www.isthe.com/chongo/
*
* Share and Enjoy! :-)
*/
/*
* 32 bit FNV-1 and FNV-1a non-zero initial basis
*
* The FNV-1 initial basis is the FNV-0 hash of the following 32 octets:
*
* chongo <Landon Curt Noll> /\../\
*
* NOTE: The \'s above are not back-slashing escape characters.
* They are literal ASCII backslash 0x5c characters.
*
* NOTE: The FNV-1a initial basis is the same value as FNV-1 by definition.
*/
#define FNV1_32A_INIT 0x811c9dc5
/*
* 32 bit magic FNV-1a prime
*/
#define FNV_32_PRIME 0x01000193
#ifdef ST_USE_FNV1
static st_index_t
strhash(st_data_t arg)
{
register const char *string = (const char *)arg;
register st_index_t hval = FNV1_32A_INIT;
/*
* FNV-1a hash each octet in the buffer
*/
while (*string) {
/* xor the bottom with the current octet */
hval ^= (unsigned int)*string++;
/* multiply by the 32 bit FNV magic prime mod 2^32 */
hval *= FNV_32_PRIME;
}
return hval;
}
#else
#ifndef UNALIGNED_WORD_ACCESS
# if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \
defined(__x86_64) || defined(__x86_64__) || defined(_M_AMD64) || \
defined(__mc68020__)
# define UNALIGNED_WORD_ACCESS 1
# endif
#endif
#ifndef UNALIGNED_WORD_ACCESS
# define UNALIGNED_WORD_ACCESS 0
#endif
/* MurmurHash described in http://murmurhash.googlepages.com/ */
#ifndef MURMUR
#define MURMUR 2
#endif
#define MurmurMagic_1 (st_index_t)0xc6a4a793
#define MurmurMagic_2 (st_index_t)0x5bd1e995
#if MURMUR == 1
#define MurmurMagic MurmurMagic_1
#elif MURMUR == 2
#if SIZEOF_ST_INDEX_T > 4
#define MurmurMagic ((MurmurMagic_1 << 32) | MurmurMagic_2)
#else
#define MurmurMagic MurmurMagic_2
#endif
#endif
static inline st_index_t
murmur(st_index_t h, st_index_t k, int r)
{
const st_index_t m = MurmurMagic;
#if MURMUR == 1
h += k;
h *= m;
h ^= h >> r;
#elif MURMUR == 2
k *= m;
k ^= k >> r;
k *= m;
h *= m;
h ^= k;
#endif
return h;
}
static inline st_index_t
murmur_finish(st_index_t h)
{
#if MURMUR == 1
h = murmur(h, 0, 10);
h = murmur(h, 0, 17);
#elif MURMUR == 2
h ^= h >> 13;
h *= MurmurMagic;
h ^= h >> 15;
#endif
return h;
}
#define murmur_step(h, k) murmur((h), (k), 16)
#if MURMUR == 1
#define murmur1(h) murmur_step((h), 16)
#else
#define murmur1(h) murmur_step((h), 24)
#endif
st_index_t
st_hash(const void *ptr, size_t len, st_index_t h)
{
const char *data = ptr;
st_index_t t = 0;
h += 0xdeadbeef;
#define data_at(n) (st_index_t)((unsigned char)data[(n)])
#define UNALIGNED_ADD_4 UNALIGNED_ADD(2); UNALIGNED_ADD(1); UNALIGNED_ADD(0)
#if SIZEOF_ST_INDEX_T > 4
#define UNALIGNED_ADD_8 UNALIGNED_ADD(6); UNALIGNED_ADD(5); UNALIGNED_ADD(4); UNALIGNED_ADD(3); UNALIGNED_ADD_4
#if SIZEOF_ST_INDEX_T > 8
#define UNALIGNED_ADD_16 UNALIGNED_ADD(14); UNALIGNED_ADD(13); UNALIGNED_ADD(12); UNALIGNED_ADD(11); \
UNALIGNED_ADD(10); UNALIGNED_ADD(9); UNALIGNED_ADD(8); UNALIGNED_ADD(7); UNALIGNED_ADD_8
#define UNALIGNED_ADD_ALL UNALIGNED_ADD_16
#endif
#define UNALIGNED_ADD_ALL UNALIGNED_ADD_8
#else
#define UNALIGNED_ADD_ALL UNALIGNED_ADD_4
#endif
if (len >= sizeof(st_index_t)) {
#if !UNALIGNED_WORD_ACCESS
int align = (int)((st_data_t)data % sizeof(st_index_t));
if (align) {
st_index_t d = 0;
int sl, sr, pack;
switch (align) {
#ifdef WORDS_BIGENDIAN
# define UNALIGNED_ADD(n) case SIZEOF_ST_INDEX_T - (n) - 1: \
t |= data_at(n) << CHAR_BIT*(SIZEOF_ST_INDEX_T - (n) - 2)
#else
# define UNALIGNED_ADD(n) case SIZEOF_ST_INDEX_T - (n) - 1: \
t |= data_at(n) << CHAR_BIT*(n)
#endif
UNALIGNED_ADD_ALL;
#undef UNALIGNED_ADD
}
#ifdef WORDS_BIGENDIAN
t >>= (CHAR_BIT * align) - CHAR_BIT;
#else
t <<= (CHAR_BIT * align);
#endif
data += sizeof(st_index_t)-align;
len -= sizeof(st_index_t)-align;
sl = CHAR_BIT * (SIZEOF_ST_INDEX_T-align);
sr = CHAR_BIT * align;
while (len >= sizeof(st_index_t)) {
d = *(st_index_t *)data;
#ifdef WORDS_BIGENDIAN
t = (t << sr) | (d >> sl);
#else
t = (t >> sr) | (d << sl);
#endif
h = murmur_step(h, t);
t = d;
data += sizeof(st_index_t);
len -= sizeof(st_index_t);
}
pack = len < (size_t)align ? (int)len : align;
d = 0;
switch (pack) {
#ifdef WORDS_BIGENDIAN
# define UNALIGNED_ADD(n) case (n) + 1: \
d |= data_at(n) << CHAR_BIT*(SIZEOF_ST_INDEX_T - (n) - 1)
#else
# define UNALIGNED_ADD(n) case (n) + 1: \
d |= data_at(n) << CHAR_BIT*(n)
#endif
UNALIGNED_ADD_ALL;
#undef UNALIGNED_ADD
}
#ifdef WORDS_BIGENDIAN
t = (t << sr) | (d >> sl);
#else
t = (t >> sr) | (d << sl);
#endif
#if MURMUR == 2
if (len < (size_t)align) goto skip_tail;
#endif
h = murmur_step(h, t);
data += pack;
len -= pack;
}
else
#endif
{
do {
h = murmur_step(h, *(st_index_t *)data);
data += sizeof(st_index_t);
len -= sizeof(st_index_t);
} while (len >= sizeof(st_index_t));
}
}
t = 0;
switch (len) {
#ifdef WORDS_BIGENDIAN
# define UNALIGNED_ADD(n) case (n) + 1: \
t |= data_at(n) << CHAR_BIT*(SIZEOF_ST_INDEX_T - (n) - 1)
#else
# define UNALIGNED_ADD(n) case (n) + 1: \
t |= data_at(n) << CHAR_BIT*(n)
#endif
UNALIGNED_ADD_ALL;
#undef UNALIGNED_ADD
#if MURMUR == 1
h = murmur_step(h, t);
#elif MURMUR == 2
# if !UNALIGNED_WORD_ACCESS
skip_tail:
# endif
h ^= t;
h *= MurmurMagic;
#endif
}
return murmur_finish(h);
}
st_index_t
st_hash_uint32(st_index_t h, uint32_t i)
{
return murmur_step(h + i, 16);
}
st_index_t
st_hash_uint(st_index_t h, st_index_t i)
{
st_index_t v = 0;
h += i;
#ifdef WORDS_BIGENDIAN
#if SIZEOF_ST_INDEX_T*CHAR_BIT > 12*8
v = murmur1(v + (h >> 12*8));
#endif
#if SIZEOF_ST_INDEX_T*CHAR_BIT > 8*8
v = murmur1(v + (h >> 8*8));
#endif
#if SIZEOF_ST_INDEX_T*CHAR_BIT > 4*8
v = murmur1(v + (h >> 4*8));
#endif
#endif
v = murmur1(v + h);
#ifndef WORDS_BIGENDIAN
#if SIZEOF_ST_INDEX_T*CHAR_BIT > 4*8
v = murmur1(v + (h >> 4*8));
#endif
#if SIZEOF_ST_INDEX_T*CHAR_BIT > 8*8
v = murmur1(v + (h >> 8*8));
#endif
#if SIZEOF_ST_INDEX_T*CHAR_BIT > 12*8
v = murmur1(v + (h >> 12*8));
#endif
#endif
return v;
}
st_index_t
st_hash_end(st_index_t h)
{
h = murmur_step(h, 10);
h = murmur_step(h, 17);
return h;
}
#undef st_hash_start
st_index_t
st_hash_start(st_index_t h)
{
return h;
}
static st_index_t
strhash(st_data_t arg)
{
register const char *string = (const char *)arg;
return st_hash(string, strlen(string), FNV1_32A_INIT);
}
#endif
int
st_locale_insensitive_strcasecmp(const char *s1, const char *s2)
{
unsigned int c1, c2;
while (1) {
c1 = (unsigned char)*s1++;
c2 = (unsigned char)*s2++;
if (c1 == '\0' || c2 == '\0') {
if (c1 != '\0') return 1;
if (c2 != '\0') return -1;
return 0;
}
if ((unsigned int)(c1 - 'A') <= ('Z' - 'A')) c1 += 'a' - 'A';
if ((unsigned int)(c2 - 'A') <= ('Z' - 'A')) c2 += 'a' - 'A';
if (c1 != c2) {
if (c1 > c2)
return 1;
else
return -1;
}
}
}
int
st_locale_insensitive_strncasecmp(const char *s1, const char *s2, size_t n)
{
unsigned int c1, c2;
while (n--) {
c1 = (unsigned char)*s1++;
c2 = (unsigned char)*s2++;
if (c1 == '\0' || c2 == '\0') {
if (c1 != '\0') return 1;
if (c2 != '\0') return -1;
return 0;
}
if ((unsigned int)(c1 - 'A') <= ('Z' - 'A')) c1 += 'a' - 'A';
if ((unsigned int)(c2 - 'A') <= ('Z' - 'A')) c2 += 'a' - 'A';
if (c1 != c2) {
if (c1 > c2)
return 1;
else
return -1;
}
}
return 0;
}
static st_index_t
strcasehash(st_data_t arg)
{
register const char *string = (const char *)arg;
register st_index_t hval = FNV1_32A_INIT;
/*
* FNV-1a hash each octet in the buffer
*/
while (*string) {
unsigned int c = (unsigned char)*string++;
if ((unsigned int)(c - 'A') <= ('Z' - 'A')) c += 'a' - 'A';
hval ^= c;
/* multiply by the 32 bit FNV magic prime mod 2^32 */
hval *= FNV_32_PRIME;
}
return hval;
}
int
st_numcmp(st_data_t x, st_data_t y)
{
return x != y;
}
st_index_t
st_numhash(st_data_t n)
{
/*
* This hash function is lightly-tuned for Ruby. Further tuning
* should be possible. Notes:
*
* - (n >> 3) alone is great for heap objects and OK for fixnum,
* however symbols perform poorly.
* - (n >> (RUBY_SPECIAL_SHIFT+3)) was added to make symbols hash well,
* n.b.: +3 to remove ID scope, +1 worked well initially, too
* - (n << 3) was finally added to avoid losing bits for fixnums
* - avoid expensive modulo instructions, it is currently only
* shifts and bitmask operations.
*/
return (st_index_t)((n>>(RUBY_SPECIAL_SHIFT+3)|(n<<3)) ^ (n>>3));
}
|