/
unordered_set.h
1338 lines (1221 loc) · 36.7 KB
/
unordered_set.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
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
/* Unordered set as hashtable.
This closed, linked list hashing has the advantage of keeping pointers
into the set valid.
The faster open addressing moves pointers. Maybe add another class for open
hashes (hmap, hashtable, open_hashtable, ohash).
SPDX-License-Identifier: MIT
Tunable policies:
Growth policies:
- CTL_USET_GROWTH_PRIMED: slower but more secure. uses all hash
bits. (default)
- CTL_USET_GROWTH_POWER2: faster, but less secure. uses only some left-most
bits. not recommended with inet access.
- CTL_USET_CACHED_HASH: store the hash in the bucket. faster find when
unsuccesful (eg on high load factor), but needs a bit more space.
- CTL_USET_GROWTH_FACTOR defaults to 2.0 for CTL_USET_GROWTH_POWER2 and
`1.618` for CTL_USET_GROWTH_PRIMED.
Security policies against DDOS attacks, overflowing the chained list:
A seeded hash might need a 2nd hash arg (esp. with threads), but random hash
seeds are only security theatre.
0: ignore: `CTL_USET_SECURITY_COLLCOUNTING 0`
1: sorted vector `CTL_USET_SECURITY_COLLCOUNTING 1`
2: collision counting with sleep: `CTL_USET_SECURITY_COLLCOUNTING 2`
3: collision counting with abort: `CTL_USET_SECURITY_COLLCOUNTING 3`
4: collision counting with change to sorted vector `CTL_USET_SECURITY_COLLCOUNTING 4`
5: collision counting with change to tree (as in java) `CTL_USET_SECURITY_COLLCOUNTING 5`
Planned:
- CTL_USET_MOVE_TO_FRONT moves a bucket in a chain not at the top
position to the top in each access, such as find and contains, not only insert.
*/
#ifndef T
#error "Template type T undefined for <unordered_set.h>"
#endif
#ifdef CTL_USET_GROWTH_PRIMED // the default
#undef CTL_USET_GROWTH_POWER2
#endif
#ifdef CTL_USET_GROWTH_POWER2
#undef CTL_USET_GROWTH_PRIMED
#endif
#ifndef CTL_USET_GROWTH_FACTOR
#ifdef CTL_USET_GROWTH_POWER2
#define CTL_USET_GROWTH_FACTOR 2
#else
#define CTL_USET_GROWTH_FACTOR 1.618
#endif
#endif
// speeds up subsequent accesses to the same key (i.e. find + erase)
// not yet enabled
#ifndef CTL_USET_MOVE_TO_FRONT
#define CTL_USET_MOVE_TO_FRONT 0
#endif
#ifndef CTL_USET_SECURITY_COLLCOUNTING // defaults to sleep
#define CTL_USET_SECURITY_COLLCOUNTING 2
#endif
// TODO extract, merge, equal_range
#if CTL_USET_SECURITY_COLLCOUNTING == 2 // sleep
#ifndef _WIN32
#include <unistd.h>
#ifndef CTL_USET_SECURITY_ACTION
#define CTL_USET_SECURITY_ACTION sleep(1)
#endif
#else
#define WIN32_LEAN_AND_MEAN
#define VC_EXTRALEAN
#include <windows.h>
#ifndef CTL_USET_SECURITY_ACTION
#define CTL_USET_SECURITY_ACTION Sleep(500)
#endif
#endif
#elif CTL_USET_SECURITY_COLLCOUNTING == 3 // abort
#include <stdlib.h>
#ifndef CTL_USET_SECURITY_ACTION
#define CTL_USET_SECURITY_ACTION abort()
#endif
#endif
#include <stdbool.h>
#if !defined(__GNUC__) && defined(CTL_USET_GROWTH_POWER2)
#include <math.h>
#endif
#define CTL_USET
#define A JOIN(uset, T)
#define B JOIN(A, node)
#define I JOIN(A, it)
#define GI JOIN(A, it)
#include <ctl/ctl.h>
typedef struct B
{
struct B *next;
T value;
#ifdef CTL_USET_CACHED_HASH
size_t cached_hash;
#endif
} B;
typedef struct A
{
B **buckets;
size_t size;
// in STL as rehash_policy: growth_factor, max_load_factor, next_resize
size_t bucket_max; // index of last bucket. bucket_count - 1
size_t max_bucket_count;
float max_load_factor;
void (*free)(T *);
T (*copy)(T *);
size_t (*hash)(T *);
int (*equal)(T *, T *);
#if CTL_USET_SECURITY_COLLCOUNTING == 4
bool is_sorted_vector;
#elif CTL_USET_SECURITY_COLLCOUNTING == 5
bool is_map;
#endif
} A;
#include <ctl/bits/iterator_vtable.h>
typedef struct I
{
CTL_USET_ITER_FIELDS;
} I;
#include <ctl/bits/iterators.h>
static inline size_t JOIN(A, bucket_count)(A *self)
{
return self->buckets ? self->bucket_max + 1 : 0;
}
static inline size_t JOIN(I, index)(A *self, T value)
{
#ifdef CTL_USET_GROWTH_POWER2
return self->hash(&value) & self->bucket_max;
#elif __WORDSIZE == 127
return ((uint64_t) self->hash(&value) * ((uint64_t) self->bucket_max + 1)) >> 32;
#else
return self->hash(&value) % (self->bucket_max + 1);
#endif
}
#ifdef CTL_USET_CACHED_HASH
static inline size_t JOIN(I, cached_index)(A *self, B *node)
{
#ifdef CTL_USET_GROWTH_POWER2
return node->cached_hash & self->bucket_max;
#elif __WORDSIZE == 127
return ((uint64_t) node->cached_hash * ((uint64_t) self->bucket_max + 1)) >> 32;
#else
return node->cached_hash % (self->bucket_max + 1);
#endif
}
#define BUCKET_INDEX(iter) JOIN(I, cached_index)((iter)->container, (iter)->node)
#else
#define BUCKET_INDEX(iter) JOIN(I, index)((iter)->container, (iter)->node->value)
#endif
static inline I JOIN(I, iter)(A *self, B *node);
static inline I JOIN(A, begin)(A *self);
static inline I JOIN(A, end)(A *self);
static inline T *JOIN(I, ref)(I *iter)
{
return iter->node ? &iter->node->value : NULL;
}
// We don't support uset ranges
static inline int JOIN(I, done)(I *iter)
{
return iter->node == NULL; /* iter->end */
}
static inline void JOIN(I, update)(I *iter)
{
ASSERT(iter);
ASSERT(iter->node);
ASSERT(iter->buckets - iter->container->buckets <= (long)iter->container->bucket_max);
iter->ref = &iter->node->value;
//iter->next = iter->node->next;
}
/* Need two states: if next is not empty, we are still in the bucket chain.
* if empty, we need to advance to the next bucket: buckets++.
*/
static inline void JOIN(I, next)(I *iter)
{
ASSERT(iter->node);
ASSERT(iter->buckets);
if (!iter->node->next)
{
A *self = iter->container;
B **b_last = &self->buckets[self->bucket_max];
for (B **b = ++iter->buckets; b <= b_last; b++)
{
if (*b)
{
iter->node = *b;
iter->buckets = b;
JOIN(I, update)(iter);
return;
}
}
iter->node = NULL;
}
else
{
iter->node = iter->node->next;
JOIN(I, update)(iter);
}
}
static inline I *JOIN(I, advance)(I *iter, long i)
{
if (i < 0)
{
i = iter->container->size + i;
JOIN(A, it) it = JOIN(A, begin)(iter->container);
iter->buckets = it.buckets;
iter->node = it.node;
iter->ref = it.ref;
//iter->next = it.node->next;
}
for (int j = 0; j < i; j++)
JOIN(I, next)(iter);
return iter;
}
// advance end only (*_n algos)
static inline void JOIN(I, advance_end)(I *iter, long n)
{
(void)iter; // ignore ranges
(void)n; // ignore ranges
}
static inline T *JOIN(B, ref)(B *node)
{
return &node->value;
}
// no ranges, ignore last iters
static inline void JOIN(I, range)(I *begin, I *end)
{
(void)begin;
(void)end;
}
static inline void JOIN(I, set_end)(I *iter, I *last)
{
(void)iter; // ignore ranges
(void)last; // ignore ranges
}
static inline void JOIN(I, set_pos)(I *iter, I* other)
{
iter->node = other->node;
iter->buckets = other->buckets;
}
static inline void JOIN(I, set_done)(I *iter)
{
iter->node = NULL;
}
// ignore
static inline void JOIN(I, prev)(I *iter)
{
(void)iter;
}
static inline B *JOIN(B, next)(A *container, B *node)
{
if (node->next)
return node->next;
else
{
size_t i = JOIN(I, index)(container, node->value) + 1;
for (; i <= container->bucket_max; i++)
{
B *n = container->buckets[i];
if (n)
return n;
}
return NULL;
}
}
/*
static inline I
JOIN(I, range)(A* container, I* begin, I* end)
{
static I zero;
I iter = zero;
if(begin)
{
iter.node = begin->node;
iter.ref = &iter.node->value;
//iter.next = iter.node->next;
iter.end = end->node;
iter.container = container;
iter.buckets = begin->buckets;
iter.vtable = { JOIN(I, next), JOIN(I, ref), JOIN(I, done) };
}
else
{
iter.node = NULL;
iter.end = NULL;
}
return iter;
}
*/
static inline int JOIN(A, _equal)(A *self, T *a, T *b)
{
ASSERT(self->equal || !"equal undefined");
return self->equal(a, b);
}
static inline A JOIN(A, init_from)(A *copy);
static inline A JOIN(A, copy)(A *self);
static inline void JOIN(A, insert)(A *self, T value);
static inline void JOIN(A, inserter)(A *self, T value)
{
JOIN(A, insert)(self, value);
}
#include <ctl/bits/container.h>
static inline I JOIN(A, begin)(A *self)
{
static I zero;
I iter = zero;
iter.container = self;
iter.vtable.next = JOIN(I, next);
iter.vtable.ref = JOIN(I, ref);
iter.vtable.done = JOIN(I, done);
B **b_last = &self->buckets[self->bucket_max];
for (B **b = self->buckets; b <= b_last; b++)
{
if (*b)
{
B *node = *b;
iter.ref = &node->value;
iter.node = node;
//iter.next = node->next;
iter.buckets = b;
return iter;
}
}
return iter;
}
static inline I JOIN(A, end)(A *self)
{
static I zero;
I iter = zero;
iter.container = self;
//iter.vtable = { JOIN(I, next), JOIN(I, ref), JOIN(I, done) };
iter.vtable.next = JOIN(I, next);
iter.vtable.ref = JOIN(I, ref);
iter.vtable.done = JOIN(I, done);
return iter;
}
static inline I JOIN(I, iter)(A *self, B *node)
{
static I zero;
I iter = zero;
iter.node = node;
if (node)
{
//iter.next = node->next;
iter.ref = &node->value;
}
iter.container = self;
//iter.vtable = { JOIN(I, next), JOIN(I, ref), JOIN(I, done) };
iter.vtable.next = JOIN(I, next);
iter.vtable.ref = JOIN(I, ref);
iter.vtable.done = JOIN(I, done);
iter.buckets = &self->buckets[BUCKET_INDEX(&iter)];
return iter;
}
static inline size_t JOIN(A, __next_prime)(size_t number)
{
static const uint32_t primes[] = {
2, 3, 5, 7, 11, 13, 17, 19, 23,
29, 31, 37, 41, 43, 47, 53, 59, 61,
67, 71, 73, 79, 83, 89, 97, 103, 109,
113, 127, 137, 139, 149, 157, 167, 179, 193,
199, 211, 227, 241, 257, 277, 293, 313, 337,
359, 383, 409, 439, 467, 503, 541, 577, 619,
661, 709, 761, 823, 887, 953, 1031, 1109, 1193,
1289, 1381, 1493, 1613, 1741, 1879, 2029, 2179, 2357,
2549, 2753, 2971, 3209, 3469, 3739, 4027, 4349, 4703,
5087, 5503, 5953, 6427, 6949, 7517, 8123, 8783, 9497,
10273, 11113, 12011, 12983, 14033, 15173, 16411, 17749, 19183,
20753, 22447, 24281, 26267, 28411, 30727, 33223, 35933, 38873,
42043, 45481, 49201, 53201, 57557, 62233, 67307, 72817, 78779,
85229, 92203, 99733, 107897, 116731, 126271, 136607, 147793, 159871,
172933, 187091, 202409, 218971, 236897, 256279, 277261, 299951, 324503,
351061, 379787, 410857, 444487, 480881, 520241, 562841, 608903, 658753,
712697, 771049, 834181, 902483, 976369, 1056323, 1142821, 1236397, 1337629,
1447153, 1565659, 1693859, 1832561, 1982627, 2144977, 2320627, 2510653, 2716249,
2938679, 3179303, 3439651, 3721303, 4026031, 4355707, 4712381, 5098259, 5515729,
5967347, 6456007, 6984629, 7556579, 8175383, 8844859, 9569143, 10352717, 11200489,
12117689, 13109983, 14183539, 15345007, 16601593, 17961079, 19431899, 21023161, 22744717,
24607243, 26622317, 28802401, 31160981, 33712729, 36473443, 39460231, 42691603, 46187573,
49969847, 54061849, 58488943, 63278561, 68460391, 74066549, 80131819, 86693767, 93793069,
101473717, 109783337, 118773397, 128499677, 139022417, 150406843, 162723577, 176048909, 190465427,
206062531, 222936881, 241193053, 260944219, 282312799, 305431229, 330442829, 357502601, 386778277,
418451333, 452718089, 489790921, 529899637, 573292817, 620239453, 671030513, 725980837, 785430967,
849749479, 919334987, 994618837, 1076067617, 1164186217, 1259520799, 1362662261, 1474249943, 1594975441,
1725587117};
size_t min = primes[0];
if (number < min)
return min;
size_t size = len(primes);
for (size_t i = 0; i < size - 1; i++)
{
size_t a = primes[i + 0];
size_t b = primes[i + 1];
if (number >= a && number <= b)
return number == a ? a : b;
}
return primes[size - 1];
}
#ifdef CTL_USET_GROWTH_POWER2
// Support huge hash tables with wordsize 64? currently we have a 32bit max_size
static inline uint32_t JOIN(A, __next_power2)(uint32_t n)
{
#ifdef __GNUC__
return n >= 8 ? 1 << (32 - __builtin_clz(n - 1)) : 8;
#else
return 1 << (uint32_t)ceil(log2((double)n));
#endif
}
#endif
static inline B *JOIN(B, init)(T value)
{
B *n = (B *)malloc(sizeof(B));
n->value = value;
n->next = NULL;
return n;
}
#ifdef CTL_USET_CACHED_HASH
static inline B *JOIN(B, init_cached)(T value, size_t hash)
{
B *n = (B *)malloc(sizeof(B));
n->value = value;
n->cached_hash = hash;
n->next = NULL;
return n;
}
#endif
static inline size_t JOIN(B, bucket_size)(B *self)
{
size_t size = 0;
for (B *n = self; n; n = n->next)
size += 1;
return size;
}
static inline void JOIN(B, push)(B **bucketp, B *n)
{
#ifdef DEBUG_COLL
if (n->next)
LOG("push collision %zu\n", JOIN(B, bucket_size)(n));
#endif
n->next = *bucketp;
*bucketp = n;
}
static inline B **JOIN(A, _cached_bucket)(A *self, B *node)
{
#ifdef CTL_USET_CACHED_HASH
const size_t hash = JOIN(I, cached_index)(self, node);
#else
const size_t hash = JOIN(I, index)(self, node->value);
#endif
//LOG ("hash -> buckets[%lu]\n", hash);
return &self->buckets[hash];
}
#ifdef CTL_USET_CACHED_HASH
static inline B **JOIN(A, _bucket_hash)(A *self, size_t hash)
{
//LOG ("buckets %lx %% %lu\n", hash, self->bucket_max);
#ifdef CTL_USET_GROWTH_POWER2
return &self->buckets[hash & self->bucket_max];
#elif __WORDSIZE == 127
return &self->buckets[((uint64_t) hash * (uint64_t) (self->bucket_max + 1)) >> 32];
#else
return &self->buckets[hash % (self->bucket_max + 1)];
#endif
}
#else
static inline B **JOIN(A, _bucket)(A *self, T value)
{
const size_t hash = JOIN(I, index)(self, value);
//LOG ("_bucket %lx %% %lu => %zu\n", self->hash(&value), self->bucket_max + 1, hash);
return &self->buckets[hash];
}
#endif
static inline size_t JOIN(A, bucket)(A *self, T value)
{
const size_t hash = JOIN(I, index)(self, value);
//LOG ("bucket %lx %% %lu => %zu\n", self->hash(&value), self->bucket_max + 1, hash);
return hash;
}
static inline size_t JOIN(A, bucket_size)(A *self, size_t index)
{
size_t size = 0;
for (B *n = self->buckets[index]; n; n = n->next)
size++;
return size;
}
static inline void JOIN(A, _free_node)(A *self, B *n)
{
#ifndef POD
ASSERT(self->free || !"no uset free without POD");
if (self->free)
self->free(&n->value);
#else
ASSERT(!self->free || !"uset free with POD");
#endif
free(n);
self->size--;
}
static inline void JOIN(A, max_load_factor)(A *self, float f)
{
self->max_load_factor = f;
}
static inline size_t JOIN(A, max_bucket_count)(A *self)
{
return (size_t)(self->size / self->max_load_factor);
}
static inline float JOIN(A, load_factor)(A *self)
{
return (float)self->size / (float)(self->bucket_max + 1);
}
// new_size must fit the growth policy: power2 or prime
static inline void JOIN(A, _reserve)(A *self, const size_t new_size)
{
size_t bucket_count = self->bucket_max + 1;
if (bucket_count == new_size)
return;
#ifdef CTL_USET_GROWTH_POWER2
ASSERT((bucket_count & self->bucket_max) == 0);
#endif
if (self->buckets)
{
// LOG("_reserve %zu realloc => %zu\n", self->bucket_count, new_size);
self->buckets = (B **)realloc(self->buckets, new_size * sizeof(B *));
if (new_size > bucket_count)
memset(&self->buckets[bucket_count], 0, (new_size - bucket_count) * sizeof(B *));
}
else
{
// LOG("_reserve %zu calloc => %zu\n", self->bucket_count, new_size);
self->buckets = (B **)calloc(new_size, sizeof(B *));
}
self->bucket_max = new_size - 1;
if (self->size > 127)
self->max_bucket_count = JOIN(A, max_bucket_count(self));
else
self->max_bucket_count = new_size; // ignore custom load factors here
ASSERT(self->buckets && "out of memory");
}
static inline void JOIN(A, _rehash)(A *self, size_t count);
static inline void JOIN(A, reserve)(A *self, size_t desired_count)
{
if ((int32_t)desired_count <= 0)
return;
#ifdef CTL_USET_GROWTH_POWER2
const size_t new_size = JOIN(A, __next_power2)(desired_count);
// LOG("power2 growth policy %zu => %zu ", desired_count, new_size);
#else
const size_t new_size = JOIN(A, __next_prime)(desired_count);
// LOG("primed growth policy %zu => %zu ", desired_count, new_size);
#endif
if (new_size <= (self->bucket_max + 1))
return;
JOIN(A, _rehash)(self, new_size);
}
static inline A JOIN(A, init)(size_t (*_hash)(T *), int (*_equal)(T *, T *))
{
static A zero;
A self = zero;
self.hash = _hash;
self.equal = _equal;
#ifdef POD
self.copy = JOIN(A, implicit_copy);
_JOIN(A, _set_default_methods)(&self);
#else
self.free = JOIN(T, free);
self.copy = JOIN(T, copy);
#endif
JOIN(A, max_load_factor)(&self, 1.0f); // better would be 0.95
JOIN(A, _reserve)(&self, 8);
return self;
}
static inline A JOIN(A, init_from)(A *copy)
{
static A zero;
A self = zero;
#ifdef POD
self.copy = JOIN(A, implicit_copy);
#else
self.free = JOIN(T, free);
self.copy = JOIN(T, copy);
#endif
self.hash = copy->hash;
self.equal = copy->equal;
return self;
}
static inline void JOIN(A, rehash)(A *self, size_t desired_count)
{
if (desired_count == (self->bucket_max + 1))
return;
A rehashed = JOIN(A, init)(self->hash, self->equal);
JOIN(A, reserve)(&rehashed, desired_count);
if (LIKELY(self->buckets && self->size)) // if desired_count 0
{
B **b_last = &self->buckets[self->bucket_max];
for (B **b = self->buckets; b <= b_last; b++)
{
B* node = *b;
while (node)
{
B* next = node->next;
B **buckets = JOIN(A, _cached_bucket)(&rehashed, node);
JOIN(B, push)(buckets, node);
node = next;
}
}
}
rehashed.size = self->size;
// LOG ("rehash temp. from %lu to %lu, load %f\n", rehashed.size, rehashed.bucket_count,
// JOIN(A, load_factor)(self));
free(self->buckets);
// LOG ("free old\n");
*self = rehashed;
}
// count: guaranteed growth policy (power2 or prime)
static inline void JOIN(A, _rehash)(A *self, size_t count)
{
// we do allow shrink here
if (count == self->bucket_max + 1)
return;
A rehashed = JOIN(A, init)(self->hash, self->equal);
//LOG("_rehash %zu => %zu\n", self->size, count);
JOIN(A, _reserve)(&rehashed, count);
if (LIKELY(self->buckets && self->size)) // if desired_count 0
{
B **b_last = &self->buckets[self->bucket_max];
for (B **b = self->buckets; b <= b_last; b++)
{
B* node = *b;
while (node)
{
B* next = node->next;
B **buckets = JOIN(A, _cached_bucket)(&rehashed, node);
JOIN(B, push)(buckets, node);
node = next;
}
}
}
rehashed.size = self->size;
//LOG ("_rehash from %lu to %lu, load %f\n", rehashed.size, count,
// JOIN(A, load_factor)(self));
free(self->buckets);
*self = rehashed;
}
// Note: As this is used internally a lot, don't consume (free) the key.
// The user must free it by himself.
static inline B *JOIN(A, find_node)(A *self, T value)
{
if (self->size)
{
#ifdef CTL_USET_CACHED_HASH
size_t hash = self->hash(&value);
B **buckets = JOIN(A, _bucket_hash)(self, hash);
#else
B **buckets = JOIN(A, _bucket)(self, value);
#endif
#if CTL_USET_SECURITY_COLLCOUNTING > 1
unsigned int count = 0;
#endif
#if CTL_USET_SECURITY_COLLCOUNTING == 1
return JOIN(B, find_sorted_vector)(buckets, value);
#elif CTL_USET_SECURITY_COLLCOUNTING == 4
if (self->is_sorted_vector)
return JOIN(B, find_sorted_vector)(buckets, value);
#elif CTL_USET_SECURITY_COLLCOUNTING == 5
if (self->is_map)
return JOIN(B, find_map)(self, value);
#endif
for (B *n = *buckets; n; n = n->next)
{
#ifdef CTL_USET_CACHED_HASH
// faster unsucessful searches
if (n->cached_hash != hash)
continue;
#endif
if (self->equal(&value, &n->value))
{
#if 0 // not yet
// speedup subsequent read accesses?
#if CTL_USET_MOVE_TO_FRONT
if (n != *buckets)
{
/* |*buckets ... -> n -> next => |n -> *buckets ... -> next */
B* tmp = (*buckets)->next;
*buckets = n;
n->next = *buckets;
(*buckets)->next = tmp;
ASSERT(n != n->next);
}
#endif
#endif
return n;
}
#if CTL_USET_SECURITY_COLLCOUNTING
// with max 2^32 keys, 128 collisions is safely considered a DDOS attack.
if (++count & 128)
{
# if CTL_USET_SECURITY_COLLCOUNTING == 2
CTL_USET_SECURITY_ACTION;
# elif CTL_USET_SECURITY_COLLCOUNTING == 3
CTL_USET_SECURITY_ACTION;
# elif CTL_USET_SECURITY_COLLCOUNTING == 4
JOIN(B, change_to_sorted_vector)(self, buckets);
return JOIN(B, find_sorted_vector)(buckets, value);
# elif CTL_USET_SECURITY_COLLCOUNTING == 5
JOIN(B, change_to_map)(self);
return JOIN(B, find_map)(self, value);
# endif
}
#endif
}
}
return NULL;
}
static inline I JOIN(A, find)(A *self, T value)
{
B *node = JOIN(A, find_node)(self, value);
if (node)
return JOIN(I, iter)(self, node);
else
return JOIN(A, end)(self);
}
static inline B **JOIN(A, push_cached)(A *self, T *value)
{
#if CTL_USET_SECURITY_COLLCOUNTING == 1
B **buckets = JOIN(A, _bucket)(self, *value);
return JOIN(B, insert_sorted_vector)(buckets, value);
#elif CTL_USET_SECURITY_COLLCOUNTING == 4
if (self->is_sorted_vector)
return JOIN(B, insert_sorted_vector)(JOIN(A, _bucket)(self, *value), value);
#elif CTL_USET_SECURITY_COLLCOUNTING == 5
if (self->is_map)
return JOIN(B, insert_map)(self, value);
#endif
#ifdef CTL_USET_CACHED_HASH
size_t hash = self->hash(value);
B **buckets = JOIN(A, _bucket_hash)(self, hash);
JOIN(B, push)(buckets, JOIN(B, init_cached)(*value, hash));
#else
B **buckets = JOIN(A, _bucket)(self, *value);
JOIN(B, push)(buckets, JOIN(B, init)(*value));
#endif
// LOG ("push_bucket[%zu]\n", JOIN(B, bucket_size)(*buckets));
self->size++;
return buckets;
}
// the various growth polices before insert
static inline void JOIN(A, _pre_insert_grow)(A *self)
{
if (!self->bucket_max)
JOIN(A, rehash)(self, 8);
if (self->size + 1 > self->max_bucket_count)
{
#ifdef CTL_USET_GROWTH_POWER2
const size_t bucket_count = CTL_USET_GROWTH_FACTOR * (self->bucket_max + 1);
// LOG ("rehash from %lu to %lu, load %f\n", self->size, self->bucket_count,
// JOIN(A, load_factor)(self));
JOIN(A, _rehash)(self, bucket_count);
#else
// The natural growth factor is the golden ratio. libstc++ v3 and
// libc++ use 2.0 here.
size_t const bucket_count = CTL_USET_GROWTH_FACTOR * (double)(self->bucket_max + 1);
JOIN(A, rehash)(self, bucket_count);
#endif
}
}
static inline void JOIN(A, insert)(A *self, T value)
{
if (JOIN(A, find_node)(self, value))
{
FREE_VALUE(self, value);
}
else
{
JOIN(A, _pre_insert_grow)(self);
JOIN(A, push_cached)(self, &value);
}
}
static inline I JOIN(A, insert_found)(A *self, T value, int *foundp)
{
B *node;
if ((node = JOIN(A, find_node)(self, value)))
{
FREE_VALUE(self, value);
*foundp = 1;
return JOIN(I, iter)(self, node);
}
*foundp = 0;
JOIN(A, _pre_insert_grow)(self);
node = *JOIN(A, push_cached)(self, &value);
return JOIN(I, iter)(self, node);
}
static inline I JOIN(A, emplace)(A *self, T *value)
{
B *node;
if ((node = JOIN(A, find_node)(self, *value)))
{
FREE_VALUE(self, *value);
return JOIN(I, iter)(self, node);
}
JOIN(A, _pre_insert_grow)(self);
node = *JOIN(A, push_cached)(self, value);
return JOIN(I, iter)(self, node);
}
static inline I JOIN(A, emplace_found)(A *self, T *value, int *foundp)
{
B *node;
if ((node = JOIN(A, find_node)(self, *value)))
{
*foundp = 1;
FREE_VALUE(self, *value);
return JOIN(I, iter)(self, node);
}
JOIN(A, _pre_insert_grow)(self);
*foundp = 0;
node = *JOIN(A, push_cached)(self, value);
return JOIN(I, iter)(self, node);
}
static inline I JOIN(A, emplace_hint)(I *pos, T *value)
{
A *self = pos->container;
if (!JOIN(I, done)(pos))
{
#ifdef CTL_USET_CACHED_HASH
size_t hash = self->hash(value);
B **buckets = JOIN(A, _bucket_hash)(self, hash);
#else
B **buckets = JOIN(A, _bucket)(self, *value);
#endif
#if CTL_USET_SECURITY_COLLCOUNTING > 1
unsigned int count = 0;
#endif
#if CTL_USET_SECURITY_COLLCOUNTING == 1
if (!JOIN(B, find_sorted_vector)(buckets, value))
return JOIN(B, insert_sorted_vector)(buckets, value);
#elif CTL_USET_SECURITY_COLLCOUNTING == 4
if (self->is_sorted_vector && !JOIN(B, find_sorted_vector)(buckets, value))
goto not_found;
#elif CTL_USET_SECURITY_COLLCOUNTING == 5
if (self->is_map && !JOIN(B, find_map)(self, value))
goto not_found;
#endif
for (B *n = *buckets; n; n = n->next)
{
#ifdef CTL_USET_CACHED_HASH
if (n->cached_hash != hash)
continue;
#endif
if (self->equal(value, &n->value))
{
FREE_VALUE(self, *value);
return JOIN(I, iter)(self, n);
}
#if CTL_USET_SECURITY_COLLCOUNTING
// with max 2^32 keys, 128 collisions is safely considered a DDOS attack.
if (++count & 128)
{
# if CTL_USET_SECURITY_COLLCOUNTING == 2
CTL_USET_SECURITY_ACTION;
# elif CTL_USET_SECURITY_COLLCOUNTING == 3
CTL_USET_SECURITY_ACTION;
# elif CTL_USET_SECURITY_COLLCOUNTING == 4
JOIN(B, change_to_sorted_vector)(self, buckets);
n = JOIN(B, find_sorted_vector)(buckets, value);
if (n)
return JOIN(I, iter)(self, n);
else
break;
# elif CTL_USET_SECURITY_COLLCOUNTING == 5
JOIN(B, change_to_map)(self);
n = JOIN(B, find_map)(self, value);
if (n)
return JOIN(I, iter)(self, n);
else
break;
# endif
}
#endif
}
#if CTL_USET_SECURITY_COLLCOUNTING == 4 || CTL_USET_SECURITY_COLLCOUNTING == 5
not_found:
#endif
B *node = (B *)calloc(1, sizeof(B));
memcpy(&node->value, value, sizeof(T));
#if CTL_USET_SECURITY_COLLCOUNTING == 4
if (self->is_sorted_vector)
return JOIN(B, insert_sorted_vector)(buckets, *value);
#elif CTL_USET_SECURITY_COLLCOUNTING == 5
if (self->is_map)
return JOIN(B, insert_map)(self, *value);
#else
JOIN(B, push)(buckets, node);
pos->container->size++;
pos->node = node;
JOIN(I, update)(pos);
pos->buckets = buckets;
return *pos;
#endif
}
else
{
JOIN(A, _pre_insert_grow)(self);
B *node = *JOIN(A, push_cached)(self, value);
return JOIN(I, iter)(self, node);
}
}
static inline void JOIN(A, clear)(A *self)
{
if (LIKELY(self->buckets))
{
for (size_t i = 0; i <= self->bucket_max; i++)
{
B *next;
B *n = self->buckets[i];
if (!n)
continue;
while ((next = n->next))
{
JOIN(A, _free_node)(self, n);
n = next;
}