-
Notifications
You must be signed in to change notification settings - Fork 1
/
bytell_hash_map.hpp
1260 lines (1180 loc) · 41 KB
/
bytell_hash_map.hpp
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
// Copyright Malte Skarupke 2017.
// Distributed under the Boost Software License, Version 1.0.
// (See http://www.boost.org/LICENSE_1_0.txt)
#pragma once
#include <cstdint>
#include <cstddef>
#include <cmath>
#include <algorithm>
#include <iterator>
#include <utility>
#include <type_traits>
#include "flat_hash_map.hpp"
#include <vector>
#include <array>
namespace ska
{
namespace detailv8
{
using ska::detailv3::functor_storage;
using ska::detailv3::KeyOrValueHasher;
using ska::detailv3::KeyOrValueEquality;
using ska::detailv3::AssignIfTrue;
using ska::detailv3::HashPolicySelector;
template<typename = void>
struct sherwood_v8_constants
{
static constexpr int8_t magic_for_empty = int8_t(0b11111111);
static constexpr int8_t magic_for_reserved = int8_t(0b11111110);
static constexpr int8_t bits_for_direct_hit = int8_t(0b10000000);
static constexpr int8_t magic_for_direct_hit = int8_t(0b00000000);
static constexpr int8_t magic_for_list_entry = int8_t(0b10000000);
static constexpr int8_t bits_for_distance = int8_t(0b01111111);
inline static int distance_from_metadata(int8_t metadata)
{
return metadata & bits_for_distance;
}
static constexpr int num_jump_distances = 126;
// jump distances chosen like this:
// 1. pick the first 16 integers to promote staying in the same block
// 2. add the next 66 triangular numbers to get even jumps when
// the hash table is a power of two
// 3. add 44 more triangular numbers at a much steeper growth rate
// to get a sequence that allows large jumps so that a table
// with 10000 sequential numbers doesn't endlessly re-allocate
static constexpr size_t jump_distances[num_jump_distances]
{
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136, 153, 171, 190, 210, 231,
253, 276, 300, 325, 351, 378, 406, 435, 465, 496, 528, 561, 595, 630,
666, 703, 741, 780, 820, 861, 903, 946, 990, 1035, 1081, 1128, 1176,
1225, 1275, 1326, 1378, 1431, 1485, 1540, 1596, 1653, 1711, 1770, 1830,
1891, 1953, 2016, 2080, 2145, 2211, 2278, 2346, 2415, 2485, 2556,
3741, 8385, 18915, 42486, 95703, 215496, 485605, 1091503, 2456436,
5529475, 12437578, 27986421, 62972253, 141700195, 318819126, 717314626,
1614000520, 3631437253, 8170829695, 18384318876, 41364501751,
93070021080, 209407709220, 471167588430, 1060127437995, 2385287281530,
5366895564381, 12075513791265, 27169907873235, 61132301007778,
137547673121001, 309482258302503, 696335090510256, 1566753939653640,
3525196427195653, 7931691866727775, 17846306747368716,
40154190394120111, 90346928493040500, 203280588949935750,
457381324898247375, 1029107980662394500, 2315492957028380766,
5209859150892887590,
};
};
template<typename T>
constexpr int8_t sherwood_v8_constants<T>::magic_for_empty;
template<typename T>
constexpr int8_t sherwood_v8_constants<T>::magic_for_reserved;
template<typename T>
constexpr int8_t sherwood_v8_constants<T>::bits_for_direct_hit;
template<typename T>
constexpr int8_t sherwood_v8_constants<T>::magic_for_direct_hit;
template<typename T>
constexpr int8_t sherwood_v8_constants<T>::magic_for_list_entry;
template<typename T>
constexpr int8_t sherwood_v8_constants<T>::bits_for_distance;
template<typename T>
constexpr int sherwood_v8_constants<T>::num_jump_distances;
template<typename T>
constexpr size_t sherwood_v8_constants<T>::jump_distances[num_jump_distances];
template<typename T, uint8_t BlockSize>
struct sherwood_v8_block
{
sherwood_v8_block()
{
}
~sherwood_v8_block()
{
}
int8_t control_bytes[BlockSize];
union
{
T data[BlockSize];
};
static sherwood_v8_block * empty_block()
{
static std::array<int8_t, BlockSize> empty_bytes = []
{
std::array<int8_t, BlockSize> result;
result.fill(sherwood_v8_constants<>::magic_for_empty);
return result;
}();
return reinterpret_cast<sherwood_v8_block *>(&empty_bytes);
}
int first_empty_index() const
{
for (int i = 0; i < BlockSize; ++i)
{
if (control_bytes[i] == sherwood_v8_constants<>::magic_for_empty)
return i;
}
return -1;
}
void fill_control_bytes(int8_t value)
{
std::fill(std::begin(control_bytes), std::end(control_bytes), value);
}
};
template<typename T, typename FindKey, typename ArgumentHash, typename Hasher, typename ArgumentEqual, typename Equal, typename ArgumentAlloc, typename ByteAlloc, uint8_t BlockSize>
class sherwood_v8_table : private ByteAlloc, private Hasher, private Equal
{
using AllocatorTraits = std::allocator_traits<ByteAlloc>;
using BlockType = sherwood_v8_block<T, BlockSize>;
using BlockPointer = BlockType *;
using BytePointer = typename AllocatorTraits::pointer;
struct convertible_to_iterator;
using Constants = sherwood_v8_constants<>;
public:
using value_type = T;
using size_type = size_t;
using difference_type = std::ptrdiff_t;
using hasher = ArgumentHash;
using key_equal = ArgumentEqual;
using allocator_type = ByteAlloc;
using reference = value_type &;
using const_reference = const value_type &;
using pointer = value_type *;
using const_pointer = const value_type *;
sherwood_v8_table()
{
}
explicit sherwood_v8_table(size_type bucket_count, const ArgumentHash & hash = ArgumentHash(), const ArgumentEqual & equal = ArgumentEqual(), const ArgumentAlloc & alloc = ArgumentAlloc())
: ByteAlloc(alloc), Hasher(hash), Equal(equal)
{
if (bucket_count)
rehash(bucket_count);
}
sherwood_v8_table(size_type bucket_count, const ArgumentAlloc & alloc)
: sherwood_v8_table(bucket_count, ArgumentHash(), ArgumentEqual(), alloc)
{
}
sherwood_v8_table(size_type bucket_count, const ArgumentHash & hash, const ArgumentAlloc & alloc)
: sherwood_v8_table(bucket_count, hash, ArgumentEqual(), alloc)
{
}
explicit sherwood_v8_table(const ArgumentAlloc & alloc)
: ByteAlloc(alloc)
{
}
template<typename It>
sherwood_v8_table(It first, It last, size_type bucket_count = 0, const ArgumentHash & hash = ArgumentHash(), const ArgumentEqual & equal = ArgumentEqual(), const ArgumentAlloc & alloc = ArgumentAlloc())
: sherwood_v8_table(bucket_count, hash, equal, alloc)
{
insert(first, last);
}
template<typename It>
sherwood_v8_table(It first, It last, size_type bucket_count, const ArgumentAlloc & alloc)
: sherwood_v8_table(first, last, bucket_count, ArgumentHash(), ArgumentEqual(), alloc)
{
}
template<typename It>
sherwood_v8_table(It first, It last, size_type bucket_count, const ArgumentHash & hash, const ArgumentAlloc & alloc)
: sherwood_v8_table(first, last, bucket_count, hash, ArgumentEqual(), alloc)
{
}
sherwood_v8_table(std::initializer_list<T> il, size_type bucket_count = 0, const ArgumentHash & hash = ArgumentHash(), const ArgumentEqual & equal = ArgumentEqual(), const ArgumentAlloc & alloc = ArgumentAlloc())
: sherwood_v8_table(bucket_count, hash, equal, alloc)
{
if (bucket_count == 0)
rehash(il.size());
insert(il.begin(), il.end());
}
sherwood_v8_table(std::initializer_list<T> il, size_type bucket_count, const ArgumentAlloc & alloc)
: sherwood_v8_table(il, bucket_count, ArgumentHash(), ArgumentEqual(), alloc)
{
}
sherwood_v8_table(std::initializer_list<T> il, size_type bucket_count, const ArgumentHash & hash, const ArgumentAlloc & alloc)
: sherwood_v8_table(il, bucket_count, hash, ArgumentEqual(), alloc)
{
}
sherwood_v8_table(const sherwood_v8_table & other)
: sherwood_v8_table(other, AllocatorTraits::select_on_container_copy_construction(other.get_allocator()))
{
}
sherwood_v8_table(const sherwood_v8_table & other, const ArgumentAlloc & alloc)
: ByteAlloc(alloc), Hasher(other), Equal(other), _max_load_factor(other._max_load_factor)
{
rehash_for_other_container(other);
try
{
insert(other.begin(), other.end());
}
catch(...)
{
clear();
deallocate_data(entries, num_slots_minus_one);
throw;
}
}
sherwood_v8_table(sherwood_v8_table && other) noexcept
: ByteAlloc(std::move(other)), Hasher(std::move(other)), Equal(std::move(other))
, _max_load_factor(other._max_load_factor)
{
swap_pointers(other);
}
sherwood_v8_table(sherwood_v8_table && other, const ArgumentAlloc & alloc) noexcept
: ByteAlloc(alloc), Hasher(std::move(other)), Equal(std::move(other))
, _max_load_factor(other._max_load_factor)
{
swap_pointers(other);
}
sherwood_v8_table & operator=(const sherwood_v8_table & other)
{
if (this == std::addressof(other))
return *this;
clear();
if (AllocatorTraits::propagate_on_container_copy_assignment::value)
{
if (static_cast<ByteAlloc &>(*this) != static_cast<const ByteAlloc &>(other))
{
reset_to_empty_state();
}
AssignIfTrue<ByteAlloc, AllocatorTraits::propagate_on_container_copy_assignment::value>()(*this, other);
}
_max_load_factor = other._max_load_factor;
static_cast<Hasher &>(*this) = other;
static_cast<Equal &>(*this) = other;
rehash_for_other_container(other);
insert(other.begin(), other.end());
return *this;
}
sherwood_v8_table & operator=(sherwood_v8_table && other) noexcept
{
if (this == std::addressof(other))
return *this;
else if (AllocatorTraits::propagate_on_container_move_assignment::value)
{
clear();
reset_to_empty_state();
AssignIfTrue<ByteAlloc, AllocatorTraits::propagate_on_container_move_assignment::value>()(*this, std::move(other));
swap_pointers(other);
}
else if (static_cast<ByteAlloc &>(*this) == static_cast<ByteAlloc &>(other))
{
swap_pointers(other);
}
else
{
clear();
_max_load_factor = other._max_load_factor;
rehash_for_other_container(other);
for (T & elem : other)
emplace(std::move(elem));
other.clear();
}
static_cast<Hasher &>(*this) = std::move(other);
static_cast<Equal &>(*this) = std::move(other);
return *this;
}
~sherwood_v8_table()
{
clear();
deallocate_data(entries, num_slots_minus_one);
}
const allocator_type & get_allocator() const
{
return static_cast<const allocator_type &>(*this);
}
const ArgumentEqual & key_eq() const
{
return static_cast<const ArgumentEqual &>(*this);
}
const ArgumentHash & hash_function() const
{
return static_cast<const ArgumentHash &>(*this);
}
template<typename ValueType>
struct templated_iterator
{
private:
friend class sherwood_v8_table;
BlockPointer current = BlockPointer();
size_t index = 0;
public:
templated_iterator()
{
}
templated_iterator(BlockPointer entries, size_t index)
: current(entries)
, index(index)
{
}
using iterator_category = std::forward_iterator_tag;
using value_type = ValueType;
using difference_type = ptrdiff_t;
using pointer = ValueType *;
using reference = ValueType &;
friend bool operator==(const templated_iterator & lhs, const templated_iterator & rhs)
{
return lhs.index == rhs.index;
}
friend bool operator!=(const templated_iterator & lhs, const templated_iterator & rhs)
{
return !(lhs == rhs);
}
templated_iterator & operator++()
{
do
{
if (index % BlockSize == 0)
--current;
if (index-- == 0)
break;
}
while(current->control_bytes[index % BlockSize] == Constants::magic_for_empty);
return *this;
}
templated_iterator operator++(int)
{
templated_iterator copy(*this);
++*this;
return copy;
}
ValueType & operator*() const
{
return current->data[index % BlockSize];
}
ValueType * operator->() const
{
return current->data + index % BlockSize;
}
operator templated_iterator<const value_type>() const
{
return { current, index };
}
};
using iterator = templated_iterator<value_type>;
using const_iterator = templated_iterator<const value_type>;
iterator begin()
{
size_t num_slots = num_slots_minus_one ? num_slots_minus_one + 1 : 0;
return ++iterator{ entries + num_slots / BlockSize, num_slots };
}
const_iterator begin() const
{
size_t num_slots = num_slots_minus_one ? num_slots_minus_one + 1 : 0;
return ++iterator{ entries + num_slots / BlockSize, num_slots };
}
const_iterator cbegin() const
{
return begin();
}
iterator end()
{
return { entries - 1, std::numeric_limits<size_t>::max() };
}
const_iterator end() const
{
return { entries - 1, std::numeric_limits<size_t>::max() };
}
const_iterator cend() const
{
return end();
}
inline iterator find(const FindKey & key)
{
size_t index = hash_object(key);
size_t num_slots_minus_one = this->num_slots_minus_one;
BlockPointer entries = this->entries;
index = hash_policy.index_for_hash(index, num_slots_minus_one);
bool first = true;
for (;;)
{
size_t block_index = index / BlockSize;
int index_in_block = index % BlockSize;
BlockPointer block = entries + block_index;
int8_t metadata = block->control_bytes[index_in_block];
if (first)
{
if ((metadata & Constants::bits_for_direct_hit) != Constants::magic_for_direct_hit)
return end();
first = false;
}
if (compares_equal(key, block->data[index_in_block]))
return { block, index };
int8_t to_next_index = metadata & Constants::bits_for_distance;
if (to_next_index == 0)
return end();
index += Constants::jump_distances[to_next_index];
index = hash_policy.keep_in_range(index, num_slots_minus_one);
}
}
inline const_iterator find(const FindKey & key) const
{
return const_cast<sherwood_v8_table *>(this)->find(key);
}
size_t count(const FindKey & key) const
{
return find(key) == end() ? 0 : 1;
}
std::pair<iterator, iterator> equal_range(const FindKey & key)
{
iterator found = find(key);
if (found == end())
return { found, found };
else
return { found, std::next(found) };
}
std::pair<const_iterator, const_iterator> equal_range(const FindKey & key) const
{
const_iterator found = find(key);
if (found == end())
return { found, found };
else
return { found, std::next(found) };
}
template<typename Key, typename... Args>
inline std::pair<iterator, bool> emplace(Key && key, Args &&... args)
{
size_t index = hash_object(key);
size_t num_slots_minus_one = this->num_slots_minus_one;
BlockPointer entries = this->entries;
index = hash_policy.index_for_hash(index, num_slots_minus_one);
bool first = true;
for (;;)
{
size_t block_index = index / BlockSize;
int index_in_block = index % BlockSize;
BlockPointer block = entries + block_index;
int8_t metadata = block->control_bytes[index_in_block];
if (first)
{
if ((metadata & Constants::bits_for_direct_hit) != Constants::magic_for_direct_hit)
return emplace_direct_hit({ index, block }, std::forward<Key>(key), std::forward<Args>(args)...);
first = false;
}
if (compares_equal(key, block->data[index_in_block]))
return { { block, index }, false };
int8_t to_next_index = metadata & Constants::bits_for_distance;
if (to_next_index == 0)
return emplace_new_key({ index, block }, std::forward<Key>(key), std::forward<Args>(args)...);
index += Constants::jump_distances[to_next_index];
index = hash_policy.keep_in_range(index, num_slots_minus_one);
}
}
std::pair<iterator, bool> insert(const value_type & value)
{
return emplace(value);
}
std::pair<iterator, bool> insert(value_type && value)
{
return emplace(std::move(value));
}
template<typename... Args>
iterator emplace_hint(const_iterator, Args &&... args)
{
return emplace(std::forward<Args>(args)...).first;
}
iterator insert(const_iterator, const value_type & value)
{
return emplace(value).first;
}
iterator insert(const_iterator, value_type && value)
{
return emplace(std::move(value)).first;
}
template<typename It>
void insert(It begin, It end)
{
for (; begin != end; ++begin)
{
emplace(*begin);
}
}
void insert(std::initializer_list<value_type> il)
{
insert(il.begin(), il.end());
}
void rehash(size_t num_items)
{
num_items = std::max(num_items, static_cast<size_t>(std::ceil(num_elements / static_cast<double>(_max_load_factor))));
if (num_items == 0)
{
reset_to_empty_state();
return;
}
auto new_prime_index = hash_policy.next_size_over(num_items);
if (num_items == num_slots_minus_one + 1)
return;
size_t num_blocks = num_items / BlockSize;
if (num_items % BlockSize)
++num_blocks;
size_t memory_requirement = calculate_memory_requirement(num_blocks);
unsigned char * new_memory = &*AllocatorTraits::allocate(*this, memory_requirement);
BlockPointer new_buckets = reinterpret_cast<BlockPointer>(new_memory);
BlockPointer special_end_item = new_buckets + num_blocks;
for (BlockPointer it = new_buckets; it <= special_end_item; ++it)
it->fill_control_bytes(Constants::magic_for_empty);
using std::swap;
swap(entries, new_buckets);
swap(num_slots_minus_one, num_items);
--num_slots_minus_one;
hash_policy.commit(new_prime_index);
num_elements = 0;
if (num_items)
++num_items;
size_t old_num_blocks = num_items / BlockSize;
if (num_items % BlockSize)
++old_num_blocks;
for (BlockPointer it = new_buckets, end = new_buckets + old_num_blocks; it != end; ++it)
{
for (int i = 0; i < BlockSize; ++i)
{
int8_t metadata = it->control_bytes[i];
if (metadata != Constants::magic_for_empty && metadata != Constants::magic_for_reserved)
{
emplace(std::move(it->data[i]));
AllocatorTraits::destroy(*this, it->data + i);
}
}
}
deallocate_data(new_buckets, num_items - 1);
}
void reserve(size_t num_elements)
{
size_t required_buckets = num_buckets_for_reserve(num_elements);
if (required_buckets > bucket_count())
rehash(required_buckets);
}
// the return value is a type that can be converted to an iterator
// the reason for doing this is that it's not free to find the
// iterator pointing at the next element. if you care about the
// next iterator, turn the return value into an iterator
convertible_to_iterator erase(const_iterator to_erase)
{
LinkedListIt current = { to_erase.index, to_erase.current };
if (current.has_next())
{
LinkedListIt previous = current;
LinkedListIt next = current.next(*this);
while (next.has_next())
{
previous = next;
next = next.next(*this);
}
AllocatorTraits::destroy(*this, std::addressof(*current));
AllocatorTraits::construct(*this, std::addressof(*current), std::move(*next));
AllocatorTraits::destroy(*this, std::addressof(*next));
next.set_metadata(Constants::magic_for_empty);
previous.clear_next();
}
else
{
if (!current.is_direct_hit())
find_parent_block(current).clear_next();
AllocatorTraits::destroy(*this, std::addressof(*current));
current.set_metadata(Constants::magic_for_empty);
}
--num_elements;
return { to_erase.current, to_erase.index };
}
iterator erase(const_iterator begin_it, const_iterator end_it)
{
if (begin_it == end_it)
return { begin_it.current, begin_it.index };
if (std::next(begin_it) == end_it)
return erase(begin_it);
if (begin_it == begin() && end_it == end())
{
clear();
return { end_it.current, end_it.index };
}
std::vector<std::pair<int, LinkedListIt>> depth_in_chain;
for (const_iterator it = begin_it; it != end_it; ++it)
{
LinkedListIt list_it(it.index, it.current);
if (list_it.is_direct_hit())
depth_in_chain.emplace_back(0, list_it);
else
{
LinkedListIt root = find_direct_hit(list_it);
int distance = 1;
for (;;)
{
LinkedListIt next = root.next(*this);
if (next == list_it)
break;
++distance;
root = next;
}
depth_in_chain.emplace_back(distance, list_it);
}
}
std::sort(depth_in_chain.begin(), depth_in_chain.end(), [](const auto & a, const auto & b) { return a.first < b.first; });
for (auto it = depth_in_chain.rbegin(), end = depth_in_chain.rend(); it != end; ++it)
{
erase(it->second.it());
}
if (begin_it.current->control_bytes[begin_it.index % BlockSize] == Constants::magic_for_empty)
return ++iterator{ begin_it.current, begin_it.index };
else
return { begin_it.current, begin_it.index };
}
size_t erase(const FindKey & key)
{
auto found = find(key);
if (found == end())
return 0;
else
{
erase(found);
return 1;
}
}
void clear()
{
if (!num_slots_minus_one)
return;
size_t num_slots = num_slots_minus_one + 1;
size_t num_blocks = num_slots / BlockSize;
if (num_slots % BlockSize)
++num_blocks;
for (BlockPointer it = entries, end = it + num_blocks; it != end; ++it)
{
for (int i = 0; i < BlockSize; ++i)
{
if (it->control_bytes[i] != Constants::magic_for_empty)
{
AllocatorTraits::destroy(*this, std::addressof(it->data[i]));
it->control_bytes[i] = Constants::magic_for_empty;
}
}
}
num_elements = 0;
}
void shrink_to_fit()
{
rehash_for_other_container(*this);
}
void swap(sherwood_v8_table & other)
{
using std::swap;
swap_pointers(other);
swap(static_cast<ArgumentHash &>(*this), static_cast<ArgumentHash &>(other));
swap(static_cast<ArgumentEqual &>(*this), static_cast<ArgumentEqual &>(other));
if (AllocatorTraits::propagate_on_container_swap::value)
swap(static_cast<ByteAlloc &>(*this), static_cast<ByteAlloc &>(other));
}
size_t size() const
{
return num_elements;
}
size_t max_size() const
{
return (AllocatorTraits::max_size(*this)) / sizeof(T);
}
size_t bucket_count() const
{
return num_slots_minus_one ? num_slots_minus_one + 1 : 0;
}
size_type max_bucket_count() const
{
return (AllocatorTraits::max_size(*this)) / sizeof(T);
}
size_t bucket(const FindKey & key) const
{
return hash_policy.index_for_hash(hash_object(key), num_slots_minus_one);
}
float load_factor() const
{
return static_cast<double>(num_elements) / (num_slots_minus_one + 1);
}
void max_load_factor(float value)
{
_max_load_factor = value;
}
float max_load_factor() const
{
return _max_load_factor;
}
bool empty() const
{
return num_elements == 0;
}
private:
BlockPointer entries = BlockType::empty_block();
size_t num_slots_minus_one = 0;
typename HashPolicySelector<ArgumentHash>::type hash_policy;
float _max_load_factor = 0.9375f;
size_t num_elements = 0;
size_t num_buckets_for_reserve(size_t num_elements) const
{
return static_cast<size_t>(std::ceil(num_elements / static_cast<double>(_max_load_factor)));
}
void rehash_for_other_container(const sherwood_v8_table & other)
{
rehash(std::min(num_buckets_for_reserve(other.size()), other.bucket_count()));
}
bool is_full() const
{
if (!num_slots_minus_one)
return true;
else
return num_elements + 1 > (num_slots_minus_one + 1) * static_cast<double>(_max_load_factor);
}
void swap_pointers(sherwood_v8_table & other)
{
using std::swap;
swap(hash_policy, other.hash_policy);
swap(entries, other.entries);
swap(num_slots_minus_one, other.num_slots_minus_one);
swap(num_elements, other.num_elements);
swap(_max_load_factor, other._max_load_factor);
}
struct LinkedListIt
{
size_t index = 0;
BlockPointer block = nullptr;
LinkedListIt()
{
}
LinkedListIt(size_t index, BlockPointer block)
: index(index), block(block)
{
}
iterator it() const
{
return { block, index };
}
int index_in_block() const
{
return index % BlockSize;
}
bool is_direct_hit() const
{
return (metadata() & Constants::bits_for_direct_hit) == Constants::magic_for_direct_hit;
}
bool is_empty() const
{
return metadata() == Constants::magic_for_empty;
}
bool has_next() const
{
return jump_index() != 0;
}
int8_t jump_index() const
{
return Constants::distance_from_metadata(metadata());
}
int8_t metadata() const
{
return block->control_bytes[index_in_block()];
}
void set_metadata(int8_t metadata)
{
block->control_bytes[index_in_block()] = metadata;
}
LinkedListIt next(sherwood_v8_table & table) const
{
int8_t distance = jump_index();
size_t next_index = table.hash_policy.keep_in_range(index + Constants::jump_distances[distance], table.num_slots_minus_one);
return { next_index, table.entries + next_index / BlockSize };
}
void set_next(int8_t jump_index)
{
int8_t & metadata = block->control_bytes[index_in_block()];
metadata = (metadata & ~Constants::bits_for_distance) | jump_index;
}
void clear_next()
{
set_next(0);
}
value_type & operator*() const
{
return block->data[index_in_block()];
}
bool operator!() const
{
return !block;
}
explicit operator bool() const
{
return block != nullptr;
}
bool operator==(const LinkedListIt & other) const
{
return index == other.index;
}
bool operator!=(const LinkedListIt & other) const
{
return !(*this == other);
}
};
template<typename... Args>
SKA_NOINLINE(std::pair<iterator, bool>) emplace_direct_hit(LinkedListIt block, Args &&... args)
{
using std::swap;
if (is_full())
{
grow();
return emplace(std::forward<Args>(args)...);
}
if (block.metadata() == Constants::magic_for_empty)
{
AllocatorTraits::construct(*this, std::addressof(*block), std::forward<Args>(args)...);
block.set_metadata(Constants::magic_for_direct_hit);
++num_elements;
return { block.it(), true };
}
else
{
LinkedListIt parent_block = find_parent_block(block);
std::pair<int8_t, LinkedListIt> free_block = find_free_index(parent_block);
if (!free_block.first)
{
grow();
return emplace(std::forward<Args>(args)...);
}
value_type new_value(std::forward<Args>(args)...);
for (LinkedListIt it = block;;)
{
AllocatorTraits::construct(*this, std::addressof(*free_block.second), std::move(*it));
AllocatorTraits::destroy(*this, std::addressof(*it));
parent_block.set_next(free_block.first);
free_block.second.set_metadata(Constants::magic_for_list_entry);
if (!it.has_next())
{
it.set_metadata(Constants::magic_for_empty);
break;
}
LinkedListIt next = it.next(*this);
it.set_metadata(Constants::magic_for_empty);
block.set_metadata(Constants::magic_for_reserved);
it = next;
parent_block = free_block.second;
free_block = find_free_index(free_block.second);
if (!free_block.first)
{
grow();
return emplace(std::move(new_value));
}
}
AllocatorTraits::construct(*this, std::addressof(*block), std::move(new_value));
block.set_metadata(Constants::magic_for_direct_hit);
++num_elements;
return { block.it(), true };
}
}
template<typename... Args>
SKA_NOINLINE(std::pair<iterator, bool>) emplace_new_key(LinkedListIt parent, Args &&... args)
{
if (is_full())
{
grow();
return emplace(std::forward<Args>(args)...);
}
std::pair<int8_t, LinkedListIt> free_block = find_free_index(parent);
if (!free_block.first)
{
grow();
return emplace(std::forward<Args>(args)...);
}
AllocatorTraits::construct(*this, std::addressof(*free_block.second), std::forward<Args>(args)...);
free_block.second.set_metadata(Constants::magic_for_list_entry);
parent.set_next(free_block.first);
++num_elements;
return { free_block.second.it(), true };
}
LinkedListIt find_direct_hit(LinkedListIt child) const
{
size_t to_move_hash = hash_object(*child);
size_t to_move_index = hash_policy.index_for_hash(to_move_hash, num_slots_minus_one);
return { to_move_index, entries + to_move_index / BlockSize };
}
LinkedListIt find_parent_block(LinkedListIt child)
{
LinkedListIt parent_block = find_direct_hit(child);
for (;;)
{
LinkedListIt next = parent_block.next(*this);
if (next == child)
return parent_block;
parent_block = next;
}
}
std::pair<int8_t, LinkedListIt> find_free_index(LinkedListIt parent) const
{
for (int8_t jump_index = 1; jump_index < Constants::num_jump_distances; ++jump_index)
{
size_t index = hash_policy.keep_in_range(parent.index + Constants::jump_distances[jump_index], num_slots_minus_one);
BlockPointer block = entries + index / BlockSize;
if (block->control_bytes[index % BlockSize] == Constants::magic_for_empty)
return { jump_index, { index, block } };
}
return { 0, {} };
}
void grow()
{
rehash(std::max(size_t(10), 2 * bucket_count()));
}
size_t calculate_memory_requirement(size_t num_blocks)
{
size_t memory_required = sizeof(BlockType) * num_blocks;
memory_required += BlockSize; // for metadata of past-the-end pointer
return memory_required;
}
void deallocate_data(BlockPointer begin, size_t num_slots_minus_one)
{
if (begin == BlockType::empty_block())
return;
++num_slots_minus_one;
size_t num_blocks = num_slots_minus_one / BlockSize;
if (num_slots_minus_one % BlockSize)
++num_blocks;
size_t memory = calculate_memory_requirement(num_blocks);
unsigned char * as_byte_pointer = reinterpret_cast<unsigned char *>(begin);
AllocatorTraits::deallocate(*this, typename AllocatorTraits::pointer(as_byte_pointer), memory);
}
void reset_to_empty_state()
{
deallocate_data(entries, num_slots_minus_one);
entries = BlockType::empty_block();
num_slots_minus_one = 0;
hash_policy.reset();
}