-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathflat_hash_map.cpp
1711 lines (1509 loc) · 61.4 KB
/
flat_hash_map.cpp
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 part of Peredvizhnikov Engine
* Copyright (C) 2023 Eduard Permyakov
*
* Peredvizhnikov Engine is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Peredvizhnikov Engine is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
export module flat_hash_map;
import meta;
import <immintrin.h>;
import <functional>;
import <utility>;
import <iterator>;
import <ranges>;
import <compare>;
import <initializer_list>;
import <memory>;
import <algorithm>;
import <tuple>;
import <functional>;
import <limits>;
import <exception>;
namespace pe{
/*
* Implementation of a dense hash map structure based on
* the design of Google's absl::flat_hash_map from the
* talk "Designing a Fast, Efficient, Cache-friendly Hash
* Table, Step by Step." It allows efficient iteration
* of all elements due to them being stored in a single
* flat array. Furthermore, it allows for fast probing
* as most probes only access the metadata bytes of the
* bin, which are packed together for efficient forward
* iteration.
*
* The API is similar to that of C++23's std::flat_hash_map
* but focusing on highly optimized table scans and forward
* iteration rather than trying to be a completely generic
* container adapter. The container invalidates all iterators
* when rehashing. Ordering of elements is not maintained.
*/
template <typename T>
concept CopyableOrMovable = std::copyable<T> or std::movable<T>;
export
template <CopyableOrMovable Key,
CopyableOrMovable T,
typename Hash = std::hash<Key>,
typename KeyEqual = std::equal_to<Key>,
typename KeyAllocator = std::allocator<Key>,
typename MappedAllocator = std::allocator<T>,
typename MetaAllocator = std::allocator<uint8_t>>
class FlatHashMap
{
template <typename KeyType, typename ValueType,
typename IteratorTag, bool Reverse = false> class Iterator;
public:
using key_type = Key;
using mapped_type = T;
using value_type = std::pair<key_type, mapped_type>;
using key_equal = KeyEqual;
using key_allocator_type = KeyAllocator;
using mapped_allocator_type = MappedAllocator;
using meta_allocator_type = MetaAllocator;
using reference = std::pair<const key_type&, mapped_type&>;
using const_reference = std::pair<const key_type&, const mapped_type&>;
using size_type = size_t;
using difference_type = ptrdiff_t;
using hasher = Hash;
using iterator = Iterator<key_type, mapped_type, std::bidirectional_iterator_tag>;
using const_iterator = Iterator<const key_type, const mapped_type,
std::bidirectional_iterator_tag>;
using reverse_iterator = Iterator<key_type, mapped_type,
std::bidirectional_iterator_tag, true>;
using const_reverse_iterator = Iterator<const key_type, const mapped_type,
std::bidirectional_iterator_tag, true>;
using ctrl_t = int8_t;
static inline constexpr size_type kGroupSize = 16;
static inline constexpr float kMaxLoadFactor = 0.75f;
FlatHashMap() : FlatHashMap(kGroupSize) {}
FlatHashMap(size_type min_bucket_count,
const Hash& hash = Hash{}, const key_equal& equal = KeyEqual{},
const KeyAllocator& key_alloc = KeyAllocator{},
const MappedAllocator& mapped_alloc = MappedAllocator{},
const MetaAllocator& meta_alloc = MetaAllocator{});
template <std::input_iterator InputIterator>
requires requires (InputIterator it) {
{std::tuple_size_v<decltype(*it)> == 2};
requires std::convertible_to<
std::tuple_element_t<0, std::iter_value_t<std::remove_pointer_t<decltype(it)>>>, Key>;
requires std::convertible_to<
std::tuple_element_t<1, std::iter_value_t<std::remove_pointer_t<decltype(it)>>>, T>;
}
FlatHashMap(InputIterator first, InputIterator last, size_type min_bucket_count = kGroupSize,
const Hash& hash = Hash{}, const key_equal& equal = KeyEqual{},
const KeyAllocator& key_alloc = KeyAllocator{},
const MappedAllocator& mapped_alloc = MappedAllocator{},
const MetaAllocator& meta_alloc = MetaAllocator{});
template <std::ranges::input_range Range>
requires requires (std::ranges::range_value_t<Range> value) {
{std::tuple_size_v<std::ranges::range_value_t<Range>> == 2};
{std::get<0>(value)} -> std::convertible_to<Key>;
{std::get<1>(value)} -> std::convertible_to<T>;
}
FlatHashMap(Range&& range, size_type min_bucket_count = kGroupSize,
const Hash& hash = Hash{}, const key_equal& equal = KeyEqual{},
const KeyAllocator& key_alloc = KeyAllocator{},
const MappedAllocator& mapped_alloc = MappedAllocator{},
const MetaAllocator& meta_alloc = MetaAllocator{});
template <typename K, typename V>
FlatHashMap(std::initializer_list<std::pair<K, V>> init,
size_type min_bucket_count = kGroupSize,
const Hash& hash = Hash{}, const key_equal& equal = KeyEqual{},
const KeyAllocator& key_alloc = KeyAllocator{},
const MappedAllocator& mapped_alloc = MappedAllocator{},
const MetaAllocator& meta_alloc = MetaAllocator{});
FlatHashMap(FlatHashMap&&);
FlatHashMap& operator=(FlatHashMap&&);
FlatHashMap(FlatHashMap const&);
FlatHashMap& operator=(FlatHashMap const&);
~FlatHashMap() = default;
/* Iterators
*/
iterator begin() noexcept
{ return iterator_at(first_full_bin(m_capacity, m_metadata.get())); }
const_iterator begin() const noexcept
{ return const_iterator_at(first_full_bin(m_capacity, m_metadata.get())); }
iterator end() noexcept
{ return iterator_at(m_capacity); }
const_iterator end() const noexcept
{ return const_iterator_at(m_capacity); }
reverse_iterator rbegin() noexcept
{ return reverse_iterator_at(last_full_bin(m_capacity, m_metadata.get())); }
const_reverse_iterator rbegin() const noexcept
{ return const_reverse_iterator_at(last_full_bin(m_capacity, m_metadata.get())); }
reverse_iterator rend() noexcept
{ return reverse_iterator_at(m_capacity); }
const_reverse_iterator rend() const noexcept
{ return const_reverse_iterator_at(m_capacity); }
const_iterator cbegin() const noexcept
{ return const_iterator_at(first_full_bin(m_capacity, m_metadata.get())); }
const_iterator cend() const noexcept
{ return const_iterator_at(m_capacity); }
const_reverse_iterator crbegin() const noexcept
{ return const_reverse_iterator_at(last_full_bin(m_capacity, m_metadata.get())); }
const_reverse_iterator crend() const noexcept
{ return const_reverse_iterator_at(m_capacity); }
/* Capacity
*/
[[nodiscard]] bool empty() const noexcept { return (m_size == 0); }
size_type size() const noexcept { return m_size; }
size_type max_size() const noexcept { return m_capacity; }
/* Element access
*/
mapped_type& operator[](const key_type& x);
mapped_type& operator[](key_type&& x);
template<class K> mapped_type& operator[](K&& x);
mapped_type& at(const key_type& x);
const mapped_type& at(const key_type& x) const;
template<class K> mapped_type& at(const K& x);
template<class K> const mapped_type& at(const K& x) const;
/* Modifiers
*/
template<class... Args> std::pair<iterator, bool> emplace(Args&&... args);
template<class... Args>
iterator emplace_hint(const_iterator position, Args&&... args);
std::pair<iterator, bool> insert(const value_type& x)
{ return emplace(x); }
std::pair<iterator, bool> insert(value_type&& x)
{ return emplace(std::move(x)); }
iterator insert(const_iterator position, const value_type& x)
{ return emplace_hint(position, x); }
iterator insert(const_iterator position, value_type&& x)
{ return emplace_hint(position, std::move(x)); }
template<class P>
std::pair<iterator, bool> insert(P&& x);
template<class P>
iterator insert(const_iterator position, P&&);
template<std::input_iterator InputIterator>
void insert(InputIterator first, InputIterator last);
template<std::ranges::input_range R> void insert_range(R&& rg);
void insert(std::initializer_list<value_type> il)
{ insert(il.begin(), il.end()); }
template <class M>
std::pair <iterator, bool> insert_or_assign(const key_type& k, M&& obj);
template <class M>
std::pair <iterator, bool> insert_or_assign(key_type&& k, M&& obj);
template <class K, class M>
std::pair<iterator, bool> insert_or_assign(K&& k, M&& obj);
template <class M>
iterator insert_or_assign(const_iterator hint, const key_type& k, M&& obj);
template <class M>
iterator insert_or_assign(const_iterator hint, key_type&& k, M&& obj);
template <class K, class M>
iterator insert_or_assign(const_iterator hint, K&& k, M&& obj);
iterator erase(const_iterator position);
iterator erase(iterator position) { return erase(const_iterator{position}); };
size_type erase(const key_type& x);
template<class K> size_type erase(K&& x);
iterator erase(const_iterator first, const_iterator last);
void swap(FlatHashMap& y) noexcept;
void clear() noexcept;
void rehash(size_type min_bucket_count);
/* Map Operations
*/
iterator find(const key_type& x);
const_iterator find(const key_type& x) const;
template <class K> iterator find(const K& x);
template <class K> const_iterator find(const K& x) const;
bool contains(const key_type& x) const;
template <class K> bool contains(const K& x) const;
bool operator==(const FlatHashMap& y) const;
friend void swap(FlatHashMap& x, FlatHashMap& y) noexcept
{ x.swap(y); }
float load_factor() const
{
return ((float)m_loaded_bins) / m_capacity;
}
private:
template <typename... Args>
requires requires (Args... args) {
sizeof...(Args) > 0;
/* The key is constructible with the first argument */
std::is_convertible_v<
decltype(std::get<0>(std::forward_as_tuple(std::forward<Args>(args)...))),
key_type>;
/* The value is constructible with the remaining arguments */
constructible_with_v<mapped_type, decltype(extract_tuple(
make_seq<sizeof...(Args) - 1, 1>{},
std::forward_as_tuple(std::forward<Args>(args)...)))>;
}
std::pair<iterator, bool> emplace_hint_impl(const_iterator position, Args&&... args);
template <typename Pair>
requires requires (Pair pair) {
is_template_instance_v<std::remove_cvref_t<Pair>, std::pair>;
std::is_constructible_v<typename std::remove_cvref_t<Pair>::first_type, key_type>;
std::is_constructible_v<typename std::remove_cvref_t<Pair>::second_type, mapped_type>;
}
std::pair<iterator, bool> emplace_hint_impl(const_iterator position, Pair&& pair);
enum Ctrl : ctrl_t
{
eEmpty = -128, // 0b10000000
eDeleted = -1, // 0b11111111
// Full // 0b0xxxxxxx
};
static inline uint8_t *u8_ptr(Ctrl *ptr) { return reinterpret_cast<uint8_t*>(ptr); }
static inline Ctrl *ctrl_ptr(uint8_t *ptr) { return reinterpret_cast<Ctrl*>(ptr); }
std::size_t H1(std::size_t hash) const noexcept { return (hash >> 7); }
ctrl_t H2(std::size_t hash) const noexcept { return (hash & 0x7f); }
static constexpr std::size_t ngroups(std::size_t min_bucket_count)
{
/* Ensure we have a minimum of 2 groups. At the expens of
* wasting a small amount of memory for small tables, this
* allows simplyfying wrap-around logic for table scans.
*/
std::size_t n = (min_bucket_count / kGroupSize) + !!(min_bucket_count % kGroupSize);
return std::max(std::size_t{2}, n);
}
static void destroy_keys(std::size_t capacity, Ctrl *metadata, key_type *keys);
static void destroy_values(std::size_t capacity, Ctrl *metadata, mapped_type *values);
static std::size_t next_free_bin(std::size_t capacity, std::size_t start, const Ctrl *metadata);
static std::size_t next_full_bin(std::size_t capacity, std::size_t start, const Ctrl *metadata);
static std::size_t first_full_bin(std::size_t capacity, const Ctrl *metadata);
static std::size_t last_full_bin(std::size_t capacity, const Ctrl *metadata);
static std::size_t prev_full_bin(std::size_t capacity, std::size_t start, const Ctrl *metadata);
inline iterator iterator_at(std::size_t bin) const noexcept
{
return {bin, m_capacity, m_metadata.get(), m_keys.get(), m_values.get()};
}
inline const_iterator const_iterator_at(std::size_t bin) const noexcept
{
return {bin, m_capacity, m_metadata.get(), m_keys.get(), m_values.get()};
}
inline reverse_iterator reverse_iterator_at(std::size_t bin) const noexcept
{
return {bin, m_capacity, m_metadata.get(), m_keys.get(), m_values.get()};
}
inline const_reverse_iterator const_reverse_iterator_at(std::size_t bin) const noexcept
{
return {bin, m_capacity, m_metadata.get(), m_keys.get(), m_values.get()};
}
iterator find(const key_type& key, std::size_t hash) const;
/* Since the keys and values are stored disjointly and we don't want to
* copy them, we package the references/pointers to the keys and values
* into a std::pair. Note, however, that this means that the semantics of
* the iterator are such that the reference and pointer types must be
* 'unpacked' before writing to them.
*/
template <typename KeyType, typename ValueType, typename IteratorTag, bool Reverse>
class Iterator
{
public:
using iterator_category = IteratorTag;
using difference_type = std::ptrdiff_t;
using value_type = const std::pair<KeyType&, ValueType&>;
using pointer = const std::pair<KeyType*, ValueType*>;
using reference = const std::pair<KeyType&, ValueType&>;
friend class FlatHashMap<Key, T, Hash, KeyEqual, KeyAllocator, MappedAllocator>;
private:
std::size_t m_bin_idx;
std::size_t m_capacity;
const Ctrl *m_metadata;
key_type *m_keys;
mapped_type *m_values;
Iterator(std::size_t bin_idx, std::size_t capacity,
Ctrl *ctrl, key_type *keys, mapped_type *values)
: m_bin_idx{bin_idx}
, m_capacity{capacity}
, m_metadata{ctrl}
, m_keys{keys}
, m_values{values}
{}
public:
Iterator() = default;
Iterator(Iterator const& other) = default;
Iterator& operator=(Iterator const&) = default;
/* construct const_iterator from iterator */
template <typename IterType = Iterator>
requires std::is_same_v<IterType, const_iterator>
Iterator(iterator const& other)
: m_bin_idx{other.m_bin_idx}
, m_capacity{other.m_capacity}
, m_metadata{other.m_metadata}
, m_keys{other.m_keys}
, m_values{other.m_values}
{}
template <typename IterType = Iterator>
requires std::is_same_v<IterType, const_reverse_iterator>
Iterator(reverse_iterator const& other)
: m_bin_idx{other.m_bin_idx}
, m_capacity{other.m_capacity}
, m_metadata{other.m_metadata}
, m_keys{other.m_keys}
, m_values{other.m_values}
{}
reference operator*() const
{
return reference{m_keys[m_bin_idx], m_values[m_bin_idx]};
}
pointer operator->()
{
return pointer{&m_keys[m_bin_idx], &m_values[m_bin_idx]};
}
Iterator& operator++()
{
if constexpr (Reverse) {
m_bin_idx = prev_full_bin(m_capacity, m_bin_idx, m_metadata);
return *this;
}else{
m_bin_idx = next_full_bin(m_capacity, m_bin_idx, m_metadata);
return *this;
}
}
Iterator operator++(int)
{
Iterator ret = *this;
++(*this);
return ret;
}
template <typename Tag = IteratorTag>
requires (std::is_same_v<Tag, std::bidirectional_iterator_tag>)
Iterator& operator--()
{
if constexpr (Reverse) {
m_bin_idx = next_full_bin(m_capacity, m_bin_idx, m_metadata);
return *this;
}else{
m_bin_idx = prev_full_bin(m_capacity, m_bin_idx, m_metadata);
return *this;
}
}
template <typename Tag = IteratorTag>
requires (std::is_same_v<Tag, std::bidirectional_iterator_tag>)
Iterator operator--(int)
{
Iterator ret = *this;
--(*this);
return ret;
}
friend bool operator==(const Iterator& a, const Iterator& b)
{
return a.m_bin_idx == b.m_bin_idx;
};
friend bool operator!=(const Iterator& a, const Iterator& b)
{
return a.m_bin_idx != b.m_bin_idx;
};
};
template <std::integral Integral>
struct BitMask
{
constexpr static inline std::size_t kNumBits = sizeof(Integral) * 8;
static_assert(kNumBits >= kGroupSize);
alignas(16) Integral m_value;
std::size_t m_curr;
BitMask(Integral value, std::size_t start = {})
: m_value{value}
, m_curr{start}
{}
operator bool() const
{
return m_value;
}
std::size_t LastSet() const
{
Integral trailing;
asm volatile(
"lzcnt %1, %0\n"
: "=r" (trailing)
: "r" (m_value)
);
if(trailing == kNumBits)
return kNumBits;
return (kNumBits - 1 - trailing);
}
std::size_t FirstSet() const
{
Integral first;
asm volatile(
"tzcnt %1, %0\n"
: "=r" (first)
: "r" (m_value)
);
if(first == kGroupSize)
return kNumBits;
return first;
}
BitMask begin()
{
return {m_value, FirstSet()};
}
BitMask end()
{
return {m_value, kNumBits};
}
BitMask& operator++()
{
std::size_t shift = m_curr + 1;
if(shift == kNumBits) {
m_curr = kNumBits;
return *this;
}
Integral shifted = m_value >> shift;
Integral first;
asm volatile(
"tzcnt %1, %0\n"
: "=r" (first)
: "r" (shifted)
);
if(first == kNumBits) {
m_curr = kNumBits;
return *this;
}
m_curr = shift + first;
return *this;
}
BitMask operator++(int)
{
BitMask ret = *this;
++(*this);
return ret;
}
std::size_t operator*() const
{
return m_curr;
}
bool operator!=(const BitMask& other) const
{
return m_curr != other.m_curr;
}
};
struct Group
{
const Ctrl *m_group_base;
Group(const Ctrl *group_base)
: m_group_base{group_base}
{}
BitMask<uint32_t> Match(ctrl_t value) const
{
auto match = _mm_set1_epi8(value);
auto ctrl = _mm_load_si128(reinterpret_cast<const __m128i*>(m_group_base));
return {static_cast<uint32_t>(_mm_movemask_epi8(_mm_cmpeq_epi8(match, ctrl)))};
}
BitMask<uint32_t> MatchEmpty() const
{
auto match = _mm_set1_epi8(Ctrl::eEmpty);
auto ctrl = _mm_load_si128(reinterpret_cast<const __m128i*>(m_group_base));
return {static_cast<uint32_t>(_mm_movemask_epi8(_mm_cmpeq_epi8(match, ctrl)))};
}
BitMask<uint32_t> MatchDeleted() const
{
auto match = _mm_set1_epi8(Ctrl::eDeleted);
auto ctrl = _mm_load_si128(reinterpret_cast<const __m128i*>(m_group_base));
return {static_cast<uint32_t>(_mm_movemask_epi8(_mm_cmpeq_epi8(match, ctrl)))};
}
BitMask<uint32_t> MatchEmptyOrDeleted() const
{
uint32_t empty = MatchEmpty().m_value;
uint32_t deleted = MatchDeleted().m_value;
return {empty | deleted};
}
BitMask<uint32_t> MatchNotEmptyOrDeleted() const
{
auto flipped = MatchEmptyOrDeleted();
uint32_t mask = std::exp2(kGroupSize)-1;
return {(~flipped.m_value) & mask};
}
BitMask<uint32_t> MatchEmptyOrDeletedFrom(std::size_t start) const
{
auto value = MatchEmptyOrDeleted();
uint32_t mask = 0;
if(start > 0) {
mask = std::exp2(start) - 1;
}
return {value.m_value & ~mask};
}
BitMask<uint32_t> MatchNotEmptyOrDeletedFrom(std::size_t start) const
{
auto flipped = MatchEmptyOrDeleted();
uint32_t mask = 0;
if(start > 0) {
mask = std::exp2(start) - 1;
}
return {(~flipped.m_value) & ~mask};
}
BitMask<uint32_t> MatchNotEmptyOrDeletedUntil(std::size_t end) const
{
auto flipped = MatchEmptyOrDeleted();
uint32_t mask = 0;
if(end < kGroupSize) {
mask = std::exp2(kGroupSize - end) - 1;
mask <<= end + 1;
mask |= 0xffffffff << kGroupSize;
}
return {(~flipped.m_value) & ~mask};
}
};
key_equal m_comparator;
key_allocator_type m_key_allocator;
mapped_allocator_type m_mapped_allocator;
meta_allocator_type m_meta_allocator;
hasher m_hasher;
size_type m_capacity;
size_type m_size;
size_type m_loaded_bins;
std::unique_ptr<Ctrl[], std::function<void(Ctrl*)>> m_metadata;
std::unique_ptr<key_type[], std::function<void(key_type*)>> m_keys;
std::unique_ptr<mapped_type[], std::function<void(mapped_type*)>> m_values;
};
/* Template deduction guides
*/
template <std::input_iterator InputIterator>
using IteratorKeyType = std::tuple_element_t<0,
typename std::iterator_traits<InputIterator>::value_type>;
template <std::input_iterator InputIterator>
using IteratorValueType = std::tuple_element_t<1,
typename std::iterator_traits<InputIterator>::value_type>;
template <
std::input_iterator InputIterator,
typename Hash = std::hash<IteratorKeyType<InputIterator>>,
typename KeyEqual = std::equal_to<IteratorKeyType<InputIterator>>,
typename KeyAllocator = std::allocator<IteratorKeyType<InputIterator>>,
typename MappedAllocator = std::allocator<IteratorValueType<InputIterator>>,
typename MetaAllocator = std::allocator<uint8_t>
>
FlatHashMap(InputIterator, InputIterator,
std::size_t = FlatHashMap<IteratorKeyType<InputIterator>,
IteratorValueType<InputIterator>,
Hash, KeyEqual, KeyAllocator,
MappedAllocator, MetaAllocator>::kGroupSize,
const Hash& = Hash{}, const KeyEqual& = KeyEqual{}, const KeyAllocator& = KeyAllocator{},
const MappedAllocator& = MappedAllocator{}, const MetaAllocator& = MetaAllocator{})
-> FlatHashMap<IteratorKeyType<InputIterator>,
IteratorValueType<InputIterator>,
Hash, KeyEqual, KeyAllocator, MappedAllocator, MetaAllocator>;
template <std::ranges::input_range Range>
using RangeKeyType = std::remove_cvref_t<
decltype(std::get<0>(std::ranges::range_value_t<Range>{}))>;
template <std::ranges::input_range Range>
using RangeValueType = std::remove_cvref_t<
decltype(std::get<1>(std::ranges::range_value_t<Range>{}))>;
template <
std::ranges::input_range Range,
typename Hash = std::hash<RangeKeyType<Range>>,
typename KeyEqual = std::equal_to<RangeKeyType<Range>>,
typename KeyAllocator = std::allocator<RangeKeyType<Range>>,
typename MappedAllocator = std::allocator<RangeValueType<Range>>,
typename MetaAllocator = std::allocator<uint8_t>
>
FlatHashMap(Range&&,
std::size_t = FlatHashMap<RangeKeyType<Range>,
RangeValueType<Range>,
Hash, KeyEqual, KeyAllocator, MappedAllocator>::kGroupSize,
const Hash& = Hash{}, const KeyEqual& = KeyEqual{},
const KeyAllocator& = KeyAllocator{}, const MappedAllocator& = MappedAllocator{},
const MetaAllocator& = MetaAllocator{})
-> FlatHashMap<RangeKeyType<Range>, RangeValueType<Range>,
Hash, KeyEqual, KeyAllocator, MappedAllocator, MetaAllocator>;
template <
typename K,
typename V,
typename Hash = std::hash<K>,
typename KeyEqual = std::equal_to<K>,
typename KeyAllocator = std::allocator<K>,
typename MappedAllocator = std::allocator<V>,
typename MetaAllocator = std::allocator<uint8_t>
>
FlatHashMap(std::initializer_list<std::pair<K, V>>,
std::size_t = FlatHashMap<K, V, Hash, KeyEqual, KeyAllocator, MappedAllocator>::kGroupSize,
const Hash& = Hash{}, const KeyEqual& = KeyEqual{},
const KeyAllocator& = KeyAllocator{}, const MappedAllocator& = MappedAllocator{},
const MetaAllocator& = MetaAllocator{})
-> FlatHashMap<K, V, Hash, KeyEqual, KeyAllocator, MappedAllocator, MetaAllocator>;
/*****************************************************************************/
/* MODULE IMPLEMENTATION */
/*****************************************************************************/
template <CopyableOrMovable Key, CopyableOrMovable T, typename H, typename KE,
typename KeAl, typename MaAl, typename MeAl>
FlatHashMap<Key, T, H, KE, KeAl, MaAl, MeAl>::FlatHashMap(
std::size_t min_bucket_count, const H& hash, const KE& equal,
const KeAl& key_alloc, const MaAl& mapped_alloc, const MeAl& meta_alloc)
: m_comparator{equal}
, m_key_allocator{key_alloc}
, m_mapped_allocator{mapped_alloc}
, m_meta_allocator{meta_alloc}
, m_hasher{hash}
, m_capacity{ngroups(min_bucket_count) * kGroupSize}
, m_size{}
, m_loaded_bins{}
, m_metadata{ctrl_ptr(m_meta_allocator.allocate(m_capacity)),
[this, cap = this->m_capacity](Ctrl *ptr){
m_meta_allocator.deallocate(u8_ptr(ptr), cap);
}}
, m_keys{m_key_allocator.allocate(m_capacity),
[this, cap = this->m_capacity, meta = this->m_metadata.get()](key_type *ptr){
destroy_keys(cap, meta, ptr);
m_key_allocator.deallocate(ptr, cap);
}}
, m_values{m_mapped_allocator.allocate(m_capacity),
[this, cap = this->m_capacity, meta = this->m_metadata.get()](mapped_type *ptr){
destroy_values(cap, meta, ptr);
m_mapped_allocator.deallocate(ptr, cap);
}}
{
std::fill(m_metadata.get(), m_metadata.get() + m_capacity, Ctrl::eEmpty);
}
template <CopyableOrMovable Key, CopyableOrMovable T, typename H, typename KE,
typename KeAl, typename MaAl, typename MeAl>
template <std::input_iterator InputIterator>
requires requires (InputIterator it) {
{std::tuple_size_v<decltype(*it)> == 2};
requires std::convertible_to<
std::tuple_element_t<0, std::iter_value_t<std::remove_pointer_t<decltype(it)>>>, Key>;
requires std::convertible_to<
std::tuple_element_t<1, std::iter_value_t<std::remove_pointer_t<decltype(it)>>>, T>;
}
FlatHashMap<Key, T, H, KE, KeAl, MaAl, MeAl>::FlatHashMap(
InputIterator first, InputIterator last, size_type min_bucket_count,
const H& hash, const KE& equal, const KeAl& key_alloc,
const MaAl& mapped_alloc, const MeAl& meta_alloc)
: FlatHashMap{min_bucket_count, hash, equal, key_alloc, mapped_alloc, meta_alloc}
{
insert(first, last);
}
template <CopyableOrMovable Key, CopyableOrMovable T, typename H, typename KE,
typename KeAl, typename MaAl, typename MeAl>
template <std::ranges::input_range Range>
requires requires (std::ranges::range_value_t<Range> value) {
{std::tuple_size_v<std::ranges::range_value_t<Range>> == 2};
{std::get<0>(value)} -> std::convertible_to<Key>;
{std::get<1>(value)} -> std::convertible_to<T>;
}
FlatHashMap<Key, T, H, KE, KeAl, MaAl, MeAl>::FlatHashMap(
Range&& range, size_type min_bucket_count, const H& hash, const KE& equal,
const KeAl& key_alloc, const MaAl& mapped_alloc, const MeAl& meta_alloc)
: FlatHashMap{min_bucket_count, hash, equal, key_alloc, mapped_alloc, meta_alloc}
{
insert_range(std::forward<Range>(range));
}
template <CopyableOrMovable Key, CopyableOrMovable T, typename H, typename KE,
typename KeAl, typename MaAl, typename MeAl>
template <typename K, typename V>
FlatHashMap<Key, T, H, KE, KeAl, MaAl, MeAl>::FlatHashMap(
std::initializer_list<std::pair<K, V>> init, size_type min_bucket_count,
const H& hash, const KE& equal, const KeAl& key_alloc,
const MaAl& mapped_alloc, const MeAl& meta_alloc)
: FlatHashMap{min_bucket_count, hash, equal, key_alloc, mapped_alloc, meta_alloc}
{
insert(std::begin(init), std::end(init));
}
template <CopyableOrMovable Key, CopyableOrMovable T, typename H, typename KE,
typename KeAl, typename MaAl, typename MeAl>
FlatHashMap<Key, T, H, KE, KeAl, MaAl, MeAl>::FlatHashMap(FlatHashMap&& other)
: m_comparator{std::move(other.m_comparator)}
, m_key_allocator{std::move(other.m_key_allocator)}
, m_mapped_allocator{std::move(other.m_mapped_allocator)}
, m_meta_allocator{std::move(other.m_meta_allocator)}
, m_hasher{std::move(other.m_hasher)}
, m_capacity{other.m_capacity}
, m_size{other.m_size}
, m_loaded_bins{other.m_loaded_bins}
, m_metadata{std::move(other.m_metadata)}
, m_keys{std::move(other.m_keys)}
, m_values{std::move(other.m_values)}
{
other.m_size = 0;
other.m_loaded_bins = 0;
other.m_capacity = 0;
}
template <CopyableOrMovable Key, CopyableOrMovable T, typename H, typename KE,
typename KeAl, typename MaAl, typename MeAl>
FlatHashMap<Key, T, H, KE, KeAl, MaAl, MeAl>&
FlatHashMap<Key, T, H, KE, KeAl, MaAl, MeAl>::operator=(FlatHashMap&& other)
{
FlatHashMap<Key, T, H, KE, KeAl, MaAl, MeAl> tmp{std::move(other)};
swap(tmp);
return *this;
}
template <CopyableOrMovable Key, CopyableOrMovable T, typename H, typename KE,
typename KeAl, typename MaAl, typename MeAl>
FlatHashMap<Key, T, H, KE, KeAl, MaAl, MeAl>::FlatHashMap(
FlatHashMap const& other)
: m_comparator{other.m_comparator}
, m_key_allocator{KeAl{}}
, m_mapped_allocator{MaAl{}}
, m_meta_allocator{MeAl{}}
, m_hasher{other.m_hasher}
, m_capacity{other.m_capacity}
, m_size{other.m_size}
, m_loaded_bins{other.m_loaded_bins}
, m_metadata{ctrl_ptr(m_meta_allocator.allocate(other.m_capacity)),
[this, cap = other.m_capacity](Ctrl *ptr){
m_meta_allocator.deallocate(u8_ptr(ptr), cap);
}}
, m_keys{m_key_allocator.allocate(other.m_capacity),
[this, cap = other.m_capacity, meta = this->m_metadata.get()](key_type *ptr){
destroy_keys(cap, meta, ptr);
m_key_allocator.deallocate(ptr, cap);
}}
, m_values{m_mapped_allocator.allocate(other.m_capacity),
[this, cap = other.m_capacity, meta = this->m_metadata.get()](mapped_type *ptr){
destroy_values(cap, meta, ptr);
m_mapped_allocator.deallocate(ptr, cap);
}}
{
std::copy(other.m_metadata.get(), other.m_metadata.get() + other.m_capacity, m_metadata.get());
std::copy(other.m_keys.get(), other.m_keys.get() + other.m_capacity, m_keys.get());
std::copy(other.m_values.get(), other.m_values.get() + other.m_capacity, m_values.get());
}
template <CopyableOrMovable Key, CopyableOrMovable T, typename H, typename KE,
typename KeAl, typename MaAl, typename MeAl>
FlatHashMap<Key, T, H, KE, KeAl, MaAl, MeAl>&
FlatHashMap<Key, T, H, KE, KeAl, MaAl, MeAl>::operator=(
FlatHashMap const& other)
{
FlatHashMap<Key, T, H, KE, KeAl, MaAl, MeAl> tmp{other};
swap(tmp);
return *this;
}
template <CopyableOrMovable Key, CopyableOrMovable T, typename H, typename KE,
typename KeAl, typename MaAl, typename MeAl>
std::size_t FlatHashMap<Key, T, H, KE, KeAl, MaAl, MeAl>::next_free_bin(
std::size_t capacity, std::size_t start, const Ctrl *metadata)
{
size_t num_groups = capacity / kGroupSize;
std::size_t group = start / kGroupSize;
while(true) {
Group g{metadata + group * kGroupSize};
if(start / kGroupSize == group) {
auto bits = g.MatchEmptyOrDeletedFrom(start % kGroupSize);
if(auto idx = bits.FirstSet(); idx != *bits.end())
return {group * kGroupSize + idx};
}else{
auto bits = g.MatchEmptyOrDeleted();
if(auto idx = bits.FirstSet(); idx != *bits.end())
return {group * kGroupSize + idx};
}
group = (group + 1) % num_groups;
}
}
template <CopyableOrMovable Key, CopyableOrMovable T, typename H, typename KE,
typename KeAl, typename MaAl, typename MeAl>
std::size_t FlatHashMap<Key, T, H, KE, KeAl, MaAl, MeAl>::next_full_bin(
std::size_t capacity, std::size_t start, const Ctrl *metadata)
{
size_t num_groups = capacity / kGroupSize;
std::size_t group = (start + 1) / kGroupSize;
while(group != num_groups) {
Group g{metadata + group * kGroupSize};
if((start + 1) / kGroupSize == group) {
auto bits = g.MatchNotEmptyOrDeletedFrom((start + 1) % kGroupSize);
if(auto idx = bits.FirstSet(); idx != *bits.end())
return {group * kGroupSize + idx};
}else{
auto bits = g.MatchNotEmptyOrDeleted();
if(auto idx = bits.FirstSet(); idx != *bits.end())
return {group * kGroupSize + idx};
}
group++;
}
return capacity;
}
template <CopyableOrMovable Key, CopyableOrMovable T, typename H, typename KE,
typename KeAl, typename MaAl, typename MeAl>
std::size_t FlatHashMap<Key, T, H, KE, KeAl, MaAl, MeAl>::first_full_bin(
std::size_t capacity, const Ctrl *metadata)
{
size_t num_groups = capacity / kGroupSize;
std::size_t group = 0;
while(group != num_groups) {
Group g{metadata + group * kGroupSize};
auto bits = g.MatchNotEmptyOrDeleted();
if(auto idx = bits.FirstSet(); idx != *bits.end())
return {group * kGroupSize + idx};
group++;
}
return capacity;
}
template <CopyableOrMovable Key, CopyableOrMovable T, typename H, typename KE,
typename KeAl, typename MaAl, typename MeAl>
std::size_t FlatHashMap<Key, T, H, KE, KeAl, MaAl, MeAl>::last_full_bin(
std::size_t capacity, const Ctrl *metadata)
{
size_t num_groups = capacity / kGroupSize;
std::size_t group = num_groups - 1;
while(group != static_cast<std::size_t>(-1)) {
Group g{metadata + group * kGroupSize};
auto bits = g.MatchNotEmptyOrDeleted();
if(auto idx = bits.LastSet(); idx != *bits.end())
return {group * kGroupSize + idx};
group--;
}
return capacity;
}
template <CopyableOrMovable Key, CopyableOrMovable T, typename H, typename KE,
typename KeAl, typename MaAl, typename MeAl>
std::size_t FlatHashMap<Key, T, H, KE, KeAl, MaAl, MeAl>::prev_full_bin(
std::size_t capacity, std::size_t start, const Ctrl *metadata)
{
if(start == 0) [[unlikely]]
return capacity;
std::size_t group = (start - 1) / kGroupSize;
while(group != static_cast<std::size_t>(-1)) {
Group g{metadata + group * kGroupSize};
if((start - 1) / kGroupSize == group) {
auto bits = g.MatchNotEmptyOrDeletedUntil((start - 1) % kGroupSize);
if(auto idx = bits.LastSet(); idx != *bits.end())
return {group * kGroupSize + idx};
}else{
auto bits = g.MatchNotEmptyOrDeleted();
if(auto idx = bits.LastSet(); idx != *bits.end())
return {group * kGroupSize + idx};
}
group--;
}
return capacity;
}
template <CopyableOrMovable Key, CopyableOrMovable T, typename H, typename KE,
typename KeAl, typename MaAl, typename MeAl>
void FlatHashMap<Key, T, H, KE, KeAl, MaAl, MeAl>::destroy_keys(
std::size_t capacity, Ctrl *metadata, key_type *keys)