-
Notifications
You must be signed in to change notification settings - Fork 0
/
id_table.c
1588 lines (1344 loc) · 36.8 KB
/
id_table.c
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
/* This file is included by symbol.c */
#include "id_table.h"
#ifndef ID_TABLE_DEBUG
#define ID_TABLE_DEBUG 0
#endif
#if ID_TABLE_DEBUG == 0
#define NDEBUG
#endif
#include "ruby_assert.h"
/*
* st
* 0: using st with debug information.
* 1: using st.
* array
* 11: simple array. ids = [ID1, ID2, ...], values = [val1, val2, ...]
* 12: simple array, and use rb_id_serial_t instead of ID.
* 13: simple array, and use rb_id_serial_t instead of ID. Swap recent access.
* 14: sorted array, and use rb_id_serial_t instead of ID.
* 15: sorted array, and use rb_id_serial_t instead of ID, linear small part.
* hash
* 21: funny falcon's Coalesced Hashing implementation [Feature #6962]
* 22: simple open addressing with quadratic probing.
* mix (array + hash)
* 31: array(12) (capa <= 32) + hash(22)
* 32: array(14) (capa <= 32) + hash(22)
* 33: array(12) (capa <= 64) + hash(22)
* 34: array(14) (capa <= 64) + hash(22)
* 34: array(15) (capa <= 64) + hash(22)
*/
#ifndef ID_TABLE_IMPL
#define ID_TABLE_IMPL 34
#endif
#if ID_TABLE_IMPL == 0
#define ID_TABLE_NAME st
#define ID_TABLE_IMPL_TYPE struct st_id_table
#define ID_TABLE_USE_ST 1
#define ID_TABLE_USE_ST_DEBUG 1
#elif ID_TABLE_IMPL == 1
#define ID_TABLE_NAME st
#define ID_TABLE_IMPL_TYPE struct st_id_table
#define ID_TABLE_USE_ST 1
#define ID_TABLE_USE_ST_DEBUG 0
#elif ID_TABLE_IMPL == 11
#define ID_TABLE_NAME list
#define ID_TABLE_IMPL_TYPE struct list_id_table
#define ID_TABLE_USE_LIST 1
#define ID_TABLE_USE_CALC_VALUES 1
#elif ID_TABLE_IMPL == 12
#define ID_TABLE_NAME list
#define ID_TABLE_IMPL_TYPE struct list_id_table
#define ID_TABLE_USE_LIST 1
#define ID_TABLE_USE_CALC_VALUES 1
#define ID_TABLE_USE_ID_SERIAL 1
#elif ID_TABLE_IMPL == 13
#define ID_TABLE_NAME list
#define ID_TABLE_IMPL_TYPE struct list_id_table
#define ID_TABLE_USE_LIST 1
#define ID_TABLE_USE_CALC_VALUES 1
#define ID_TABLE_USE_ID_SERIAL 1
#define ID_TABLE_SWAP_RECENT_ACCESS 1
#elif ID_TABLE_IMPL == 14
#define ID_TABLE_NAME list
#define ID_TABLE_IMPL_TYPE struct list_id_table
#define ID_TABLE_USE_LIST 1
#define ID_TABLE_USE_CALC_VALUES 1
#define ID_TABLE_USE_ID_SERIAL 1
#define ID_TABLE_USE_LIST_SORTED 1
#elif ID_TABLE_IMPL == 15
#define ID_TABLE_NAME list
#define ID_TABLE_IMPL_TYPE struct list_id_table
#define ID_TABLE_USE_LIST 1
#define ID_TABLE_USE_CALC_VALUES 1
#define ID_TABLE_USE_ID_SERIAL 1
#define ID_TABLE_USE_LIST_SORTED 1
#define ID_TABLE_USE_LIST_SORTED_LINEAR_SMALL_RANGE 1
#elif ID_TABLE_IMPL == 21
#define ID_TABLE_NAME hash
#define ID_TABLE_IMPL_TYPE sa_table
#define ID_TABLE_USE_COALESCED_HASHING 1
#define ID_TABLE_USE_ID_SERIAL 1
#elif ID_TABLE_IMPL == 22
#define ID_TABLE_NAME hash
#define ID_TABLE_IMPL_TYPE struct hash_id_table
#define ID_TABLE_USE_SMALL_HASH 1
#define ID_TABLE_USE_ID_SERIAL 1
#elif ID_TABLE_IMPL == 31
#define ID_TABLE_NAME mix
#define ID_TABLE_IMPL_TYPE struct mix_id_table
#define ID_TABLE_USE_MIX 1
#define ID_TABLE_USE_MIX_LIST_MAX_CAPA 32
#define ID_TABLE_USE_ID_SERIAL 1
#define ID_TABLE_USE_LIST 1
#define ID_TABLE_USE_CALC_VALUES 1
#define ID_TABLE_USE_SMALL_HASH 1
#elif ID_TABLE_IMPL == 32
#define ID_TABLE_NAME mix
#define ID_TABLE_IMPL_TYPE struct mix_id_table
#define ID_TABLE_USE_MIX 1
#define ID_TABLE_USE_MIX_LIST_MAX_CAPA 32
#define ID_TABLE_USE_ID_SERIAL 1
#define ID_TABLE_USE_LIST 1
#define ID_TABLE_USE_CALC_VALUES 1
#define ID_TABLE_USE_LIST_SORTED 1
#define ID_TABLE_USE_SMALL_HASH 1
#elif ID_TABLE_IMPL == 33
#define ID_TABLE_NAME mix
#define ID_TABLE_IMPL_TYPE struct mix_id_table
#define ID_TABLE_USE_MIX 1
#define ID_TABLE_USE_MIX_LIST_MAX_CAPA 64
#define ID_TABLE_USE_ID_SERIAL 1
#define ID_TABLE_USE_LIST 1
#define ID_TABLE_USE_CALC_VALUES 1
#define ID_TABLE_USE_SMALL_HASH 1
#elif ID_TABLE_IMPL == 34
#define ID_TABLE_NAME mix
#define ID_TABLE_IMPL_TYPE struct mix_id_table
#define ID_TABLE_USE_MIX 1
#define ID_TABLE_USE_MIX_LIST_MAX_CAPA 64
#define ID_TABLE_USE_ID_SERIAL 1
#define ID_TABLE_USE_LIST 1
#define ID_TABLE_USE_CALC_VALUES 1
#define ID_TABLE_USE_LIST_SORTED 1
#define ID_TABLE_USE_SMALL_HASH 1
#elif ID_TABLE_IMPL == 35
#define ID_TABLE_NAME mix
#define ID_TABLE_IMPL_TYPE struct mix_id_table
#define ID_TABLE_USE_MIX 1
#define ID_TABLE_USE_MIX_LIST_MAX_CAPA 64
#define ID_TABLE_USE_ID_SERIAL 1
#define ID_TABLE_USE_LIST 1
#define ID_TABLE_USE_CALC_VALUES 1
#define ID_TABLE_USE_LIST_SORTED 1
#define ID_TABLE_USE_LIST_SORTED_LINEAR_SMALL_RANGE 1
#define ID_TABLE_USE_SMALL_HASH 1
#else
#error
#endif
#if ID_TABLE_SWAP_RECENT_ACCESS && ID_TABLE_USE_LIST_SORTED
#error
#endif
/* IMPL(create) will be "hash_id_table_create" and so on */
#define IMPL1(name, op) TOKEN_PASTE(name, _id##op) /* expand `name' */
#define IMPL(op) IMPL1(ID_TABLE_NAME, _table##op) /* but prevent `op' */
#ifdef __GNUC__
# define UNUSED(func) static func __attribute__((unused))
#else
# define UNUSED(func) static func
#endif
UNUSED(ID_TABLE_IMPL_TYPE *IMPL(_create)(size_t));
UNUSED(void IMPL(_free)(ID_TABLE_IMPL_TYPE *));
UNUSED(void IMPL(_clear)(ID_TABLE_IMPL_TYPE *));
UNUSED(size_t IMPL(_size)(const ID_TABLE_IMPL_TYPE *));
UNUSED(size_t IMPL(_memsize)(const ID_TABLE_IMPL_TYPE *));
UNUSED(int IMPL(_insert)(ID_TABLE_IMPL_TYPE *, ID, VALUE));
UNUSED(int IMPL(_lookup)(ID_TABLE_IMPL_TYPE *, ID, VALUE *));
UNUSED(int IMPL(_delete)(ID_TABLE_IMPL_TYPE *, ID));
UNUSED(void IMPL(_foreach)(ID_TABLE_IMPL_TYPE *, rb_id_table_foreach_func_t *, void *));
UNUSED(void IMPL(_foreach_values)(ID_TABLE_IMPL_TYPE *, rb_id_table_foreach_values_func_t *, void *));
#if ID_TABLE_USE_ID_SERIAL
typedef rb_id_serial_t id_key_t;
static inline ID
key2id(id_key_t key)
{
return rb_id_serial_to_id(key);
}
static inline id_key_t
id2key(ID id)
{
return rb_id_to_serial(id);
}
#else /* ID_TABLE_USE_ID_SERIAL */
typedef ID id_key_t;
#define key2id(key) key
#define id2key(id) id
#endif /* ID_TABLE_USE_ID_SERIAL */
/***************************************************************
* 0: using st with debug information.
* 1: using st.
***************************************************************/
#if ID_TABLE_USE_ST
#if ID_TABLE_USE_ST_DEBUG
#define ID_TABLE_MARK 0x12345678
struct st_id_table {
struct st_table *st;
unsigned int check;
};
static struct st_table *
tbl2st(struct st_id_table *tbl)
{
if (tbl->check != ID_TABLE_MARK) rb_bug("tbl2st: check error %x", tbl->check);
return tbl->st;
}
static struct st_id_table *
st_id_table_create(size_t size)
{
struct st_id_table *tbl = ALLOC(struct st_id_table);
tbl->st = st_init_numtable_with_size(size);
tbl->check = ID_TABLE_MARK;
return tbl;
}
static void
st_id_table_free(struct st_id_table *tbl)
{
st_free_table(tbl->st);
xfree(tbl);
}
#else /* ID_TABLE_USE_ST_DEBUG */
struct st_id_table {
struct st_table st;
};
static struct st_table *
tbl2st(struct st_id_table *tbl)
{
return (struct st_table *)tbl;
}
static struct st_id_table *
st_id_table_create(size_t size)
{
return (struct st_id_table *)st_init_numtable_with_size(size);
}
static void
st_id_table_free(struct st_id_table *tbl)
{
st_free_table((struct st_table*)tbl);
}
#endif /* ID_TABLE_USE_ST_DEBUG */
static void
st_id_table_clear(struct st_id_table *tbl)
{
st_clear(tbl2st(tbl));
}
static size_t
st_id_table_size(const struct st_id_table *tbl)
{
return tbl2st(tbl)->num_entries;
}
static size_t
st_id_table_memsize(const struct st_id_table *tbl)
{
size_t header_size = ID_TABLE_USE_ST_DEBUG ? sizeof(struct st_id_table) : 0;
return header_size + st_memsize(tbl2st(tbl));
}
static int
st_id_table_lookup(struct st_id_table *tbl, ID id, VALUE *val)
{
return st_lookup(tbl2st(tbl), (st_data_t)id, (st_data_t *)val);
}
static int
st_id_table_insert(struct st_id_table *tbl, ID id, VALUE val)
{
return st_insert(tbl2st(tbl), id, val);
}
static int
st_id_table_delete(struct st_id_table *tbl, ID id)
{
return st_delete(tbl2st(tbl), (st_data_t *)&id, NULL);
}
static void
st_id_table_foreach(struct st_id_table *tbl, rb_id_table_foreach_func_t *func, void *data)
{
st_foreach(tbl2st(tbl), (int (*)(ANYARGS))func, (st_data_t)data);
}
struct values_iter_data {
rb_id_table_foreach_values_func_t *values_i;
void *data;
};
static int
each_values(st_data_t key, st_data_t val, st_data_t ptr)
{
struct values_iter_data *values_iter_data = (struct values_iter_data *)ptr;
return values_iter_data->values_i(val, values_iter_data->data);
}
static void
st_id_table_foreach_values(struct st_id_table *tbl, rb_id_table_foreach_values_func_t *func, void *data)
{
struct values_iter_data values_iter_data;
values_iter_data.values_i = func;
values_iter_data.data = data;
st_foreach(tbl2st(tbl), each_values, (st_data_t)&values_iter_data);
}
#endif /* ID_TABLE_USE_ST */
#if ID_TABLE_USE_LIST
#define LIST_MIN_CAPA 4
struct list_id_table {
int capa;
int num;
id_key_t *keys;
#if ID_TABLE_USE_CALC_VALUES == 0
VALUE *values_;
#endif
};
#if ID_TABLE_USE_CALC_VALUES
#define TABLE_VALUES(tbl) ((VALUE *)((tbl)->keys + (tbl)->capa))
#else
#define TABLE_VALUES(tbl) (tbl)->values_
#endif
static struct list_id_table *
list_id_table_init(struct list_id_table *tbl, size_t capa)
{
if (capa > 0) {
#if ID_TABLE_USE_CALC_VALUES && \
(UNALIGNED_WORD_ACCESS == 0) && (SIZEOF_VALUE == 8)
/* Workaround for 8-byte word alignment on 64-bit SPARC.
* This code assumes that sizeof(ID) == 4, sizeof(VALUE) == 8, and
* xmalloc() returns 8-byte aligned memory block.
*/
if (capa & (size_t)1) capa += 1;
#endif
tbl->capa = (int)capa;
#if ID_TABLE_USE_CALC_VALUES
tbl->keys = (id_key_t *)xmalloc(sizeof(id_key_t) * capa + sizeof(VALUE) * capa);
#else
tbl->keys = ALLOC_N(id_key_t, capa);
tbl->values_ = ALLOC_N(VALUE, capa);
#endif
}
return tbl;
}
#ifndef ID_TABLE_USE_MIX
static struct list_id_table *
list_id_table_create(size_t capa)
{
struct list_id_table *tbl = ZALLOC(struct list_id_table);
return list_id_table_init(tbl, capa);
}
#endif
static void
list_id_table_free(struct list_id_table *tbl)
{
xfree(tbl->keys);
#if ID_TABLE_USE_CALC_VALUES == 0
xfree(tbl->values_);
#endif
xfree(tbl);
}
static void
list_id_table_clear(struct list_id_table *tbl)
{
tbl->num = 0;
}
static size_t
list_id_table_size(const struct list_id_table *tbl)
{
return (size_t)tbl->num;
}
static size_t
list_id_table_memsize(const struct list_id_table *tbl)
{
return (sizeof(id_key_t) + sizeof(VALUE)) * tbl->capa + sizeof(struct list_id_table);
}
static void
list_table_extend(struct list_id_table *tbl)
{
if (tbl->capa == tbl->num) {
const int capa = tbl->capa == 0 ? LIST_MIN_CAPA : (tbl->capa * 2);
#if ID_TABLE_USE_CALC_VALUES
{
VALUE *old_values, *new_values;
VALUE *debug_values = NULL;
const int num = tbl->num;
const int size = sizeof(id_key_t) * capa + sizeof(VALUE) * capa;
int i;
if (num > 0) {
VALUE *orig_values = (VALUE *)(tbl->keys + num);
debug_values = ALLOC_N(VALUE, num);
for (i=0; i<num; i++) {
debug_values[i] = orig_values[i];
}
if (0)
for (i=0; i< 2 * num; i++) {
unsigned char *cs = (unsigned char *)&tbl->keys[i];
size_t j;
fprintf(stderr, ">> %3d | %p - ", i, cs);
for (j=0; j<sizeof(VALUE); j++) {
fprintf(stderr, "%x ", cs[j]);
}
fprintf(stderr, "\n");
}
}
tbl->keys = (id_key_t *)xrealloc(tbl->keys, size);
old_values = (VALUE *)(tbl->keys + num);
new_values = (VALUE *)(tbl->keys + capa);
/* [ keys (num) ] [ values (num) ]
* ^ old_values
* realloc =>
* [ keys (capa = num * 2) ] [ values (capa = num * 2) ]
* ^ new_values
*/
/* memmove */
if (0) {
fprintf(stderr, "memmove: %p -> %p (%d, capa: %d)\n",
old_values, new_values, num, capa);
}
assert(num < capa);
assert(num == 0 || old_values < new_values);
for (i=num-1; i>=0; i--) {
new_values[i] = old_values[i];
}
if (num > 0) {
for (i=0; i<num; i++) {
assert(debug_values[i] == new_values[i]);
}
xfree(debug_values);
}
}
tbl->capa = capa;
#else
tbl->capa = capa;
tbl->keys = (id_key_t *)xrealloc(tbl->keys, sizeof(id_key_t) * capa);
tbl->values_ = (VALUE *)xrealloc(tbl->values_, sizeof(VALUE) * capa);
#endif
}
}
#if ID_TABLE_DEBUG
static void
list_table_show(struct list_id_table *tbl)
{
const id_key_t *keys = tbl->keys;
const int num = tbl->num;
int i;
fprintf(stderr, "tbl: %p (num: %d)\n", tbl, num);
for (i=0; i<num; i++) {
fprintf(stderr, " -> [%d] %s %d\n", i, rb_id2name(key2id(keys[i])), (int)keys[i]);
}
}
#endif
static void
tbl_assert(struct list_id_table *tbl)
{
#if ID_TABLE_DEBUG
#if ID_TABLE_USE_LIST_SORTED
const id_key_t *keys = tbl->keys;
const int num = tbl->num;
int i;
for (i=0; i<num-1; i++) {
if (keys[i] >= keys[i+1]) {
list_table_show(tbl);
rb_bug(": not sorted.");
}
}
#endif
#endif
}
#if ID_TABLE_USE_LIST_SORTED
static int
list_ids_bsearch(const id_key_t *keys, id_key_t key, int num)
{
int p, min = 0, max = num;
#if ID_TABLE_USE_LIST_SORTED_LINEAR_SMALL_RANGE
if (num <= 64) {
if (num > 32) {
if (keys[num/2] <= key) {
min = num/2;
} else {
max = num/2;
}
}
for (p = min; p<num && keys[p] < key; p++) {
assert(keys[p] != 0);
}
return (p<num && keys[p] == key) ? p : -p-1;
}
#endif /* ID_TABLE_USE_LIST_SORTED_LINEAR_SMALL_RANGE */
while (1) {
p = min + (max - min) / 2;
if (min >= max) {
break;
}
else {
id_key_t kp = keys[p];
assert(p < max);
assert(p >= min);
if (kp > key) max = p;
else if (kp < key) min = p+1;
else {
assert(kp == key);
assert(p >= 0);
assert(p < num);
return p;
}
}
}
assert(min == max);
assert(min == p);
return -p-1;
}
#endif /* ID_TABLE_USE_LIST_SORTED */
static int
list_table_index(struct list_id_table *tbl, id_key_t key)
{
const int num = tbl->num;
const id_key_t *keys = tbl->keys;
#if ID_TABLE_USE_LIST_SORTED
return list_ids_bsearch(keys, key, num);
#else /* ID_TABLE_USE_LIST_SORTED */
int i;
for (i=0; i<num; i++) {
assert(keys[i] != 0);
if (keys[i] == key) {
return (int)i;
}
}
return -1;
#endif
}
static int
list_id_table_lookup(struct list_id_table *tbl, ID id, VALUE *valp)
{
id_key_t key = id2key(id);
int index = list_table_index(tbl, key);
if (index >= 0) {
*valp = TABLE_VALUES(tbl)[index];
#if ID_TABLE_SWAP_RECENT_ACCESS
if (index > 0) {
VALUE *values = TABLE_VALUES(tbl);
id_key_t tk = tbl->keys[index-1];
VALUE tv = values[index-1];
tbl->keys[index-1] = tbl->keys[index];
tbl->keys[index] = tk;
values[index-1] = values[index];
values[index] = tv;
}
#endif /* ID_TABLE_SWAP_RECENT_ACCESS */
return TRUE;
}
else {
return FALSE;
}
}
static int
list_id_table_insert(struct list_id_table *tbl, ID id, VALUE val)
{
const id_key_t key = id2key(id);
const int index = list_table_index(tbl, key);
if (index >= 0) {
TABLE_VALUES(tbl)[index] = val;
}
else {
list_table_extend(tbl);
{
const int num = tbl->num++;
#if ID_TABLE_USE_LIST_SORTED
const int insert_index = -(index + 1);
id_key_t *keys = tbl->keys;
VALUE *values = TABLE_VALUES(tbl);
int i;
if (0) fprintf(stderr, "insert: %d into %d on\n", (int)key, insert_index);
for (i=num; i>insert_index; i--) {
keys[i] = keys[i-1];
values[i] = values[i-1];
}
keys[i] = key;
values[i] = val;
tbl_assert(tbl);
#else
tbl->keys[num] = key;
TABLE_VALUES(tbl)[num] = val;
#endif
}
}
return TRUE;
}
static int
list_delete_index(struct list_id_table *tbl, id_key_t key, int index)
{
if (index >= 0) {
VALUE *values = TABLE_VALUES(tbl);
#if ID_TABLE_USE_LIST_SORTED
int i;
const int num = tbl->num;
id_key_t *keys = tbl->keys;
for (i=index+1; i<num; i++) { /* compaction */
keys[i-1] = keys[i];
values[i-1] = values[i];
}
#else
tbl->keys[index] = tbl->keys[tbl->num-1];
values[index] = values[tbl->num-1];
#endif
tbl->num--;
tbl_assert(tbl);
return TRUE;
}
else {
return FALSE;
}
}
static int
list_id_table_delete(struct list_id_table *tbl, ID id)
{
const id_key_t key = id2key(id);
int index = list_table_index(tbl, key);
return list_delete_index(tbl, key, index);
}
#define FOREACH_LAST() do { \
switch (ret) { \
case ID_TABLE_CONTINUE: \
case ID_TABLE_STOP: \
break; \
case ID_TABLE_DELETE: \
list_delete_index(tbl, key, i); \
values = TABLE_VALUES(tbl); \
num = tbl->num; \
i--; /* redo same index */ \
break; \
} \
} while (0)
static void
list_id_table_foreach(struct list_id_table *tbl, rb_id_table_foreach_func_t *func, void *data)
{
int num = tbl->num;
int i;
const id_key_t *keys = tbl->keys;
const VALUE *values = TABLE_VALUES(tbl);
for (i=0; i<num; i++) {
const id_key_t key = keys[i];
enum rb_id_table_iterator_result ret = (*func)(key2id(key), values[i], data);
assert(key != 0);
FOREACH_LAST();
if (ret == ID_TABLE_STOP) return;
}
}
static void
list_id_table_foreach_values(struct list_id_table *tbl, rb_id_table_foreach_values_func_t *func, void *data)
{
int num = tbl->num;
int i;
const id_key_t *keys = tbl->keys;
VALUE *values = TABLE_VALUES(tbl);
for (i=0; i<num; i++) {
const id_key_t key = keys[i];
enum rb_id_table_iterator_result ret = (*func)(values[i], data);
assert(key != 0);
FOREACH_LAST();
if (ret == ID_TABLE_STOP) return;
}
}
#endif /* ID_TABLE_USE_LIST */
#if ID_TABLE_USE_COALESCED_HASHING
/* implementation is based on
* https://bugs.ruby-lang.org/issues/6962 by funny_falcon
*/
typedef unsigned int sa_index_t;
#define SA_EMPTY 0
#define SA_LAST 1
#define SA_OFFSET 2
#define SA_MIN_SIZE 4
typedef struct sa_entry {
sa_index_t next;
id_key_t key;
VALUE value;
} sa_entry;
typedef struct {
sa_index_t num_bins;
sa_index_t num_entries;
sa_index_t free_pos;
sa_entry *entries;
} sa_table;
static void
sa_init_table(register sa_table *table, sa_index_t num_bins)
{
if (num_bins) {
table->num_entries = 0;
table->entries = ZALLOC_N(sa_entry, num_bins);
table->num_bins = num_bins;
table->free_pos = num_bins;
}
}
static sa_table*
hash_id_table_create(size_t size)
{
sa_table* table = ZALLOC(sa_table);
sa_init_table(table, (sa_index_t)size);
return table;
}
static void
hash_id_table_clear(sa_table *table)
{
xfree(table->entries);
memset(table, 0, sizeof(sa_table));
}
static void
hash_id_table_free(sa_table *table)
{
xfree(table->entries);
xfree(table);
}
static size_t
hash_id_table_memsize(const sa_table *table)
{
return sizeof(sa_table) + table->num_bins * sizeof (sa_entry);
}
static inline sa_index_t
calc_pos(register sa_table* table, id_key_t key)
{
return key & (table->num_bins - 1);
}
static void
fix_empty(register sa_table* table)
{
while (--table->free_pos &&
table->entries[table->free_pos-1].next != SA_EMPTY);
}
#define FLOOR_TO_4 ((~((sa_index_t)0)) << 2)
static sa_index_t
find_empty(register sa_table* table, register sa_index_t pos)
{
sa_index_t new_pos = table->free_pos-1;
sa_entry *entry;
static const unsigned offsets[][3] = {
{1, 2, 3},
{2, 3, 0},
{3, 1, 0},
{2, 1, 0}
};
const unsigned *const check = offsets[pos&3];
pos &= FLOOR_TO_4;
entry = table->entries+pos;
if (entry[check[0]].next == SA_EMPTY) { new_pos = pos + check[0]; goto check; }
if (entry[check[1]].next == SA_EMPTY) { new_pos = pos + check[1]; goto check; }
if (entry[check[2]].next == SA_EMPTY) { new_pos = pos + check[2]; goto check; }
check:
if (new_pos+1 == table->free_pos) fix_empty(table);
return new_pos;
}
static void resize(register sa_table* table);
static int insert_into_chain(register sa_table*, register id_key_t, st_data_t, sa_index_t pos);
static int insert_into_main(register sa_table*, id_key_t, st_data_t, sa_index_t pos, sa_index_t prev_pos);
static int
sa_insert(register sa_table* table, id_key_t key, VALUE value)
{
register sa_entry *entry;
sa_index_t pos, main_pos;
if (table->num_bins == 0) {
sa_init_table(table, SA_MIN_SIZE);
}
pos = calc_pos(table, key);
entry = table->entries + pos;
if (entry->next == SA_EMPTY) {
entry->next = SA_LAST;
entry->key = key;
entry->value = value;
table->num_entries++;
if (pos+1 == table->free_pos) fix_empty(table);
return 0;
}
if (entry->key == key) {
entry->value = value;
return 1;
}
if (table->num_entries + (table->num_entries >> 2) > table->num_bins) {
resize(table);
return sa_insert(table, key, value);
}
main_pos = calc_pos(table, entry->key);
if (main_pos == pos) {
return insert_into_chain(table, key, value, pos);
}
else {
if (!table->free_pos) {
resize(table);
return sa_insert(table, key, value);
}
return insert_into_main(table, key, value, pos, main_pos);
}
}
static int
hash_id_table_insert(register sa_table* table, ID id, VALUE value)
{
return sa_insert(table, id2key(id), value);
}
static int
insert_into_chain(register sa_table* table, id_key_t key, st_data_t value, sa_index_t pos)
{
sa_entry *entry = table->entries + pos, *new_entry;
sa_index_t new_pos;
while (entry->next != SA_LAST) {
pos = entry->next - SA_OFFSET;
entry = table->entries + pos;
if (entry->key == key) {
entry->value = value;
return 1;
}
}
if (!table->free_pos) {
resize(table);
return sa_insert(table, key, value);
}
new_pos = find_empty(table, pos);
new_entry = table->entries + new_pos;
entry->next = new_pos + SA_OFFSET;
new_entry->next = SA_LAST;
new_entry->key = key;
new_entry->value = value;
table->num_entries++;
return 0;
}
static int
insert_into_main(register sa_table* table, id_key_t key, st_data_t value, sa_index_t pos, sa_index_t prev_pos)
{
sa_entry *entry = table->entries + pos;
sa_index_t new_pos = find_empty(table, pos);
sa_entry *new_entry = table->entries + new_pos;
sa_index_t npos;
*new_entry = *entry;
while((npos = table->entries[prev_pos].next - SA_OFFSET) != pos) {
prev_pos = npos;
}
table->entries[prev_pos].next = new_pos + SA_OFFSET;
entry->next = SA_LAST;
entry->key = key;
entry->value = value;
table->num_entries++;
return 0;
}
static sa_index_t
new_size(sa_index_t num_entries)
{
sa_index_t size = num_entries >> 3;
size |= size >> 1;
size |= size >> 2;
size |= size >> 4;
size |= size >> 8;
size |= size >> 16;
return (size + 1) << 3;
}
static void
resize(register sa_table *table)
{
sa_table tmp_table;
sa_entry *entry;
sa_index_t i;