-
Notifications
You must be signed in to change notification settings - Fork 3
/
qc-hash.hpp
2232 lines (1931 loc) · 76.3 KB
/
qc-hash.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
#pragma once
///
/// QC Hash 3.0.6
///
/// https://github.com/daskie/qc-hash
///
/// Extremely fast unordered map and set library for C++20
///
/// See the README for more info!
///
/// Some nomenclature:
/// - Key: A piece of data that is unique within the map/set
/// - Value: The data mapped by a key in a map. Does not exist in a set
/// - Element: A key-value pair, or just a key in the case of a set. One "thing" in the map/set
/// - Slot: One slot in the backing array. May contain an element or the "vacant" or "grave" magic constants
/// - Vacant: Indicates the slot has never had an element
/// - Grave: Means the slot used to have an element, but it was erased
/// - Size: The number of elements in the map/set
/// - Capacity: The number of elements that the map/set can currently hold without growing. Exactly half the number of
/// slots and always a power of two
/// - Special Slots: Two slots tacked on to the end of the backing array in addition to the reported capacity. Used to
/// hold the special elements if they are present
/// - Special Elements: The elements whose keys match the "vacant" or "grave" constants. Stored in the special slots
///
#if defined _CPPUNWIND || defined __cpp_exceptions
#define QC_HASH_EXCEPTIONS_ENABLED
#endif
#include <cstdint>
#include <cstring>
#include <bit>
#include <initializer_list>
#include <iterator>
#include <limits>
#include <memory>
#ifdef QC_HASH_EXCEPTIONS_ENABLED
#include <stdexcept>
#endif
#include <string>
#include <string_view>
#include <type_traits>
#include <utility>
namespace qc::hash
{
///
/// Convenience aliases
///
using u64 = uint64_t;
using s64 = int64_t;
using f64 = double;
using u32 = uint32_t;
using s32 = int32_t;
using f32 = float;
using u16 = uint16_t;
using s16 = int16_t;
using u8 = uint8_t;
using s8 = int8_t;
/// Only support 64 bit platforms
static_assert(std::is_same_v<u64, u64> && std::is_same_v<uintptr_t, u64>, "Unsupported architecture");
inline namespace config
{
///
/// The capacity new maps/sets will be initialized with, once memory is allocated. The capacity will never be
/// rehashed below this value. Does not include the two special elements, as they do not count against the load
/// factor
///
/// Must be a power of two
///
inline constexpr u64 minMapCapacity{16u};
}
///
/// Helper concepts
///
template <typename T> concept SignedInteger = std::is_same_v<T, s64> || std::is_same_v<T, s32> || std::is_same_v<T, s16> || std::is_same_v<T, s8>;
template <typename T> concept UnsignedInteger = std::is_same_v<T, u64> || std::is_same_v<T, u32> || std::is_same_v<T, u16> || std::is_same_v<T, u8>;
template <typename T> concept Enum = std::is_enum_v<T>;
template <typename T> concept Pointer = std::is_pointer_v<T>;
namespace _private
{
template <u64 size> struct UnsignedHelper;
template <> struct UnsignedHelper<1u> { using type = u8; };
template <> struct UnsignedHelper<2u> { using type = u16; };
template <> struct UnsignedHelper<4u> { using type = u32; };
template <> struct UnsignedHelper<8u> { using type = u64; };
}
///
/// Aliases the unsigned integer type of a certain size
///
template <u64 size> using Unsigned = typename _private::UnsignedHelper<size>::type;
///
/// Represents an "unsigned" value by compositing multiple native unsigned types. Useful to alias types that are
/// larger than the largest native unsigned type or that have an alignment smaller than their size
///
/// Essentially just a wrapper around an array of `elementN` native unsigned types of size `elementSize`
///
/// @tparam elementSize the size of each element
/// @tparam elementN the number of elements
///
template <u64 elementSize, u64 elementN>
struct UnsignedMulti
{
using Element = Unsigned<elementSize>;
Element elements[elementN];
constexpr bool operator==(const UnsignedMulti &) const = default;
constexpr UnsignedMulti operator~() const;
};
///
/// Specialize to explicitly specify whether a given type has a unique representation. Essentially a manual override
/// for `std::has_unique_object_representation`
///
template <typename T> struct IsUniquelyRepresentable : std::bool_constant<std::has_unique_object_representations_v<T>> {};
template <typename T> struct IsUniquelyRepresentable<std::unique_ptr<T>> : std::true_type {};
template <typename T> struct IsUniquelyRepresentable<std::shared_ptr<T>> : std::true_type {};
template <typename T1, typename T2> struct IsUniquelyRepresentable<std::pair<T1, T2>> : std::bool_constant<IsUniquelyRepresentable<T1>::value && IsUniquelyRepresentable<T2>::value> {};
template <typename CharT, typename Traits> struct IsUniquelyRepresentable<std::basic_string_view<CharT, Traits>> : std::false_type{};
///
/// A key type must meet this requirement to work with this map/set implementation. Essentially there must be a
/// one-to-one mapping between the raw binary and the logical value of a key
///
template <typename T> concept Rawable = IsUniquelyRepresentable<T>::value;
namespace _private
{
template <typename T> struct RawTypeHelper { using type = Unsigned<sizeof(T)>; };
template <typename T> requires (alignof(T) != sizeof(T) || sizeof(T) > sizeof(uintmax_t))
struct RawTypeHelper<T>
{
static constexpr u64 align{alignof(T) > alignof(uintmax_t) ? alignof(uintmax_t) : alignof(T)};
using type = UnsignedMulti<align, sizeof(T) / align>;
};
}
///
/// The "raw" type that matches the key type's size and alignment and is used to alias the key
///
template <typename T> using RawType = typename _private::RawTypeHelper<T>::type;
///
/// This default hash simply "grabs" the least significant 64 bits of data from the key's underlying binary
///
/// May specialize for custom types. Must provide a `auto operator()(const T &)` method that returns something
/// implicitly convertible to `u64`. The lowest bits are used to map to a slot, so prioritize low-order entropy
///
/// Must provide `u64 operator()(const U &)` method to support a heterogeneous type `U`
///
template <Rawable T> struct IdentityHash;
///
/// Specialization of `IdentityHash` for pointers. Simply right-shifts the pointer by the log2 of `T`'s alignment,
/// thereby discarding redundant bits and maximizing low-order entropy
///
template <typename T> struct IdentityHash<T *>;
///
/// Specialization of `IdentityHash` for `std::unique_ptr`. Works the same as the pointer specilization
///
template <typename T> struct IdentityHash<std::unique_ptr<T>>;
///
/// Specialization of `IdentityHash` for `std::shared_ptr`. Works the same as the pointer specilization
///
template <typename T> struct IdentityHash<std::shared_ptr<T>>;
///
/// A very fast/minimal non-crytographic hash purely to improve collision rates for keys with poor low-order entropy
///
/// Yields different hashes depending on word size and endianness
///
/// May specialize for custom types. Must provide a `auto operator()(const K &)` method that returns something
/// implicitly convertible to `u64`. The lowest bits are used to map to a slot, so prioritize low-order entropy
///
/// Must provide `u64 operator(const U &)` method to support a heterogeneous type `U`
///
template <typename T> struct FastHash;
///
/// Specialization of `FastHash` for pointers. Facilitates heterogeneity between const and mutable pointers
///
template <typename T> struct FastHash<T *>;
///
/// Specialization of `FastHash` for `std::unique_ptr`
///
template <typename T> struct FastHash<std::unique_ptr<T>>;
///
/// Specialization of `FastHash` for `std::shared_ptr`
///
template <typename T> struct FastHash<std::shared_ptr<T>>;
///
/// Specialization of `FastHash` for `std::string`
///
template <> struct FastHash<std::string>;
///
/// Specialization of `FastHash` for `std::string_view`
///
template <> struct FastHash<std::string_view>;
namespace fastHash
{
///
/// Quickly hash a u64 or u32
///
/// @param v the value to mix
/// @return the mixed value
///
template <UnsignedInteger H> [[nodiscard]] constexpr H mix(H v);
///
/// Direct FastHash function that hashes the given value
///
/// @param v the value to hash
/// @return the hash of the value
///
template <UnsignedInteger H, typename T> [[nodiscard]] constexpr H hash(const T & v);
///
/// Direct FastHash function that hashes the given data
///
/// @param data the data to hash
/// @param length the length of the data in bytes
/// @return the hash of the data
///
template <UnsignedInteger H> [[nodiscard]] H hash(const void * data, u64 length);
}
///
/// Indicates whether `KOther` is heterogeneous with `K`. May specialize to enable heterogeneous lookup for custom
/// types
///
template <typename K, typename KOther> struct IsCompatible : std::bool_constant<std::is_same_v<std::decay_t<K>, std::decay_t<KOther>>> {};
template <SignedInteger K, SignedInteger KOther> struct IsCompatible<K, KOther> : std::bool_constant<sizeof(KOther) <= sizeof(K)> {};
template <UnsignedInteger K, UnsignedInteger KOther> struct IsCompatible<K, KOther> : std::bool_constant<sizeof(KOther) <= sizeof(K)> {};
template <SignedInteger K, UnsignedInteger KOther> struct IsCompatible<K, KOther> : std::bool_constant<sizeof(KOther) < sizeof(K)> {};
template <UnsignedInteger K, SignedInteger KOther> struct IsCompatible<K, KOther> : std::false_type {};
template <typename T, typename TOther> requires (std::is_same_v<std::decay_t<T>, std::decay_t<TOther>> || std::is_base_of_v<T, TOther>) struct IsCompatible<T *, TOther *> : std::true_type {};
template <typename T, typename TOther> requires (std::is_same_v<std::decay_t<T>, std::decay_t<TOther>> || std::is_base_of_v<T, TOther>) struct IsCompatible<std::unique_ptr<T>, TOther *> : std::true_type {};
template <typename T, typename TOther> requires (std::is_same_v<std::decay_t<T>, std::decay_t<TOther>> || std::is_base_of_v<T, TOther>) struct IsCompatible<std::shared_ptr<T>, TOther *> : std::true_type {};
///
/// Specifies whether a key of type `KOther` may be used for lookup operations on a map/set with key type `K`
///
template <typename KOther, typename K> concept Compatible = Rawable<K> && Rawable<KOther> && IsCompatible<K, KOther>::value;
// Used for testing
struct RawFriend;
///
/// An associative container that stores unique-key key-pair values. Uses a flat memory model, linear probing, and a
/// whole lot of optimizations that make this an extremely fast map for small elements
///
/// A custom hasher must provide a `auto operator()(const K &)` method that returns something implicitly convertible
/// to `u64`. Additionally, the hash function should provide good low-order entropy, as the low bits determine the
/// slot index
///
/// @tparam K the key type
/// @tparam V the mapped value type
/// @tparam H the functor type for hashing keys
/// @tparam KE the functor type for checking key equality
/// @tparam A the allocator type
///
template <Rawable K, typename V, typename H = IdentityHash<K>, typename A = std::allocator<std::pair<K, V>>> class RawMap;
///
/// An associative container that stores unique-key key-pair values. Uses a flat memory model, linear probing, and a
/// whole lot of optimizations that make this an extremely fast set for small elements
///
/// This implementation has minimal differences between maps and sets, and those that exist are zero-cost
/// compile-time abstractions. Thus, a set is simply a map whose value type is `void`
///
/// A custom hasher must provide a `auto operator()(const K &)` method that returns something implicitly convertible
/// to `u64`. Additionally, the hash function should provide good low-order entropy, as the low bits determine the
/// slot index
///
/// @tparam K the key type
/// @tparam H the functor type for hashing keys
/// @tparam A the allocator type
///
template <Rawable K, typename H = IdentityHash<K>, typename A = std::allocator<K>> using RawSet = RawMap<K, void, H, A>;
template <Rawable K, typename V, typename H, typename A> class RawMap
{
inline static constexpr bool _isSet{std::is_same_v<V, void>};
inline static constexpr bool _isMap{!_isSet};
///
/// Element type
///
using E = std::conditional_t<_isSet, K, std::pair<K, V>>;
// Internal iterator class forward declaration. Prefer `iterator` and `const_iterator`
template <bool constant> class _Iterator;
friend ::qc::hash::RawFriend;
public:
static_assert(std::is_move_constructible_v<E>);
static_assert(std::is_move_assignable_v<E>);
static_assert(std::is_swappable_v<E>);
static_assert(requires(const H h, const K k) { u64{h(k)}; });
static_assert(std::is_move_constructible_v<H>);
static_assert(std::is_move_assignable_v<H>);
static_assert(std::is_swappable_v<H>);
static_assert(std::is_move_constructible_v<A>);
static_assert(std::is_move_assignable_v<A> || !std::allocator_traits<A>::propagate_on_container_move_assignment::value);
static_assert(std::is_swappable_v<A> || !std::allocator_traits<A>::propagate_on_container_swap::value);
using key_type = K;
using mapped_type = V;
using value_type = E;
using hasher = H;
using allocator_type = A;
using reference = E &;
using const_reference = const E &;
using pointer = E *;
using const_pointer = const E *;
using size_type = u64;
using difference_type = s64;
using iterator = _Iterator<false>;
using const_iterator = _Iterator<true>;
///
/// Constructs a new map/set
///
/// The number of backing slots will be the smallest power of two greater than or equal to twice `capacity`
///
/// Memory is not allocated until the first element is inserted
///
/// @param capacity the minimum cpacity
/// @param hash the hasher
/// @param alloc the allocator
///
explicit RawMap(u64 capacity = minMapCapacity, const H & hash = {}, const A & alloc = {});
RawMap(u64 capacity, const A & alloc);
explicit RawMap(const A & alloc);
///
/// Constructs a new map/set from copies of the elements within the iterator range
///
/// The number of backing slots will be the smallest power of two greater than or equal to twice the larger of
/// `capacity` or the number of elements within the iterator range
///
/// @param first iterator to the first element to copy, inclusive
/// @param last iterator to the last element to copy, exclusive
/// @param capacity the minumum capacity
/// @param hash the hasher
/// @param alloc the allocator
///
template <typename It> RawMap(It first, It last, u64 capacity = {}, const H & hash = {}, const A & alloc = {});
template <typename It> RawMap(It first, It last, u64 capacity, const A & alloc);
///
/// Constructs a new map/set from copies of the elements in the initializer list
///
/// The number of backing slots will be the smallest power of two greater than or equal to twice the larger of
/// `capacity` or the number of elements in the initializer list
///
/// @param elements the elements to copy
/// @param capacity the minumum capacity
/// @param hash the hasher
/// @param alloc the allocator
///
RawMap(std::initializer_list<E> elements, u64 capacity = {}, const H & hash = {}, const A & alloc = {});
RawMap(std::initializer_list<E> elements, u64 capacity, const A & alloc);
///
/// Copy constructor - new memory is allocated and each element is copied
///
/// @param other the map/set to copy
///
RawMap(const RawMap & other);
///
/// Move constructor - no memory is allocated and no elements are copied
///
/// The moved-from map/set is left in an empty state, the validitiy of which depends on the move constructors of
/// the hasher and allocator
///
/// @param other the map/set to move from
///
RawMap(RawMap && other);
///
/// Destructs existing elements and inserts the elements in the initializer list
///
/// @param elements the new elements to copy
/// @returns this
///
RawMap & operator=(std::initializer_list<E> elements);
///
/// Copy assignment operator - existing elements are destructed, more memory is allocated if necessary, and each
/// new element is inserted
///
/// @param other the map/set to copy from
/// @returns this
///
RawMap & operator=(const RawMap & other);
///
/// Move assignment operator - existing elements are destructed and memory is freed
/// @param other the map/set to move from
/// @returns this
///
RawMap & operator=(RawMap && other);
///
/// Destructor - all elements are destructed and all memory is freed
///
~RawMap();
///
/// Copies the element into the map/set if its key is not already present
///
/// Invalidates iterators if there is a rehash
///
/// @param element the element to insert
/// @returns an iterator to the element if inserted, or end iterator if not, and whether it was inserted
///
std::pair<iterator, bool> insert(const E & element);
///
/// Moves the element into the map/set if its key is not already present
///
/// Invalidates iterators if there is a rehash
///
/// @param element the element to insert
/// @returns an iterator to the element if inserted, or end iterator if not, and whether it was inserted
///
std::pair<iterator, bool> insert(E && element);
///
/// Copies each element in [`first`, `last`) into the map/set if its key is not already present
///
/// Invalidates iterators if there is a rehash
///
/// @param first the first element in the range to insert, inclusive
/// @param last the last element in the range to insert, exclusive
///
template <typename It> void insert(It first, It last);
///
/// Copies each element in the initializer list into the map/set if its key is not already present
///
/// Invalidates iterators if there is a rehash
///
/// @param elements the elements to insert
///
void insert(std::initializer_list<E> elements);
///
/// Copies the element into the map/set if its key is not already present
///
/// Invalidates iterators if there is a rehash
///
/// @param element the element to insert
/// @returns an iterator to the element if inserted, or end iterator if not, and whether it was inserted
///
std::pair<iterator, bool> emplace(const E & element);
///
/// Moves the element into the map/set if its key is not already present
///
/// Invalidates iterators if there is a rehash
///
/// @param element the element to insert
/// @returns an iterator to the element if inserted, or end iterator if not, and whether it was inserted
///
std::pair<iterator, bool> emplace(E && element);
///
/// Forwards the key and value into the map/set if the key is not already present
///
/// Invalidates iterators if there is a rehash
///
/// Defined only for maps, not for sets
///
/// @param key the key to forward
/// @param value the value to forward
/// @returns an iterator to the element if inserted, or end iterator if not, and whether it was inserted
///
template <typename K_, typename V_> std::pair<iterator, bool> emplace(K_ && key, V_ && value) requires (!std::is_same_v<V, void>);
///
/// Constructs a new key from the forwarded arguments and inserts it if it is not already present
///
/// Invalidates iterators if there is a rehash
///
/// Defined only for sets, not for maps
///
/// @param keyArgs the arguments to forward to the key's constructor
/// @returns an iterator to the element if inserted, or end iterator if not, and whether or was inserted
///
template <typename... KArgs> std::pair<iterator, bool> emplace(KArgs &&... keyArgs) requires (std::is_same_v<V, void>);
///
/// Constructs a key from the forwarded key arguments, and if the key is not present, a new value is constructed
/// from the forwarded value arguments and the two are inserted as a new element
///
/// Invalidates iterators if there is a rehash
///
/// Defined only for maps, not for sets
///
/// @param keyArgs the arguments to forward to the key's constructor
/// @param valueArgs the arguments to forward to the value's constructor
/// @returns an iterator to the element if inserted, or end iterator if not, and whether it was inserted
///
template <typename... KArgs, typename... VArgs> std::pair<iterator, bool> emplace(std::piecewise_construct_t, std::tuple<KArgs...> && keyArgs, std::tuple<VArgs...> && valueArgs) requires (!std::is_same_v<V, void>);
///
/// If the key is not already present, a new element is constructed in-place from the forwarded arguments
///
/// Invalidates iterators if there is a rehash
///
/// `valueArgs` must be present for maps and absent for sets
///
/// @param keyArgs the arguments to forward to the key's constructor
/// @param valueArgs the arguments to forward to the value's constructor
/// @returns an iterator to the element if inserted, or end iterator if not, and whether it was inserted
///
template <typename K_, typename... VArgs> std::pair<iterator, bool> try_emplace(K_ && key, VArgs &&... valueArgs);
///
/// Erase the element for the heterogeneous key if present
///
/// Does *not* invalidate iterators
///
/// @param key the key of the element to erase
/// @returns whether the element was erased
///
template <Compatible<K> K_> bool erase(const K_ & key);
///
/// Erase the element at the given position
///
/// Undefined behavior if position is the end iterator or otherwise invalid
///
/// Does *not* invalidate iterators
///
/// @param position position of the element to erase
///
void erase(iterator position);
///
/// Clears the map/set, destructing all elements
///
/// Does not alter capacity or free memory
///
/// Invalidates iterators
///
void clear();
///
/// @param key the key to check for
/// @returns whether the heterogeneous key is present
///
template <Compatible<K> K_> [[nodiscard]] bool contains(const K_ & key) const;
///
/// @param key the key to count
/// @returns `1` if the heterogeneous key is present or `0` if it is absent
///
template <Compatible<K> K_> [[nodiscard]] u64 count(const K_ & key) const;
#ifdef QC_HASH_EXCEPTIONS_ENABLED
///
/// Gets the present element for the heterogeneous key
///
/// Defined only for maps, not for sets
///
/// @param key the key to retrieve
/// @returns the element for the key
/// @throws `std::out_of_range` if the key is absent
///
template <Compatible<K> K_> [[nodiscard]] std::add_lvalue_reference_t<V> at(const K_ & key) requires (!std::is_same_v<V, void>);
template <Compatible<K> K_> [[nodiscard]] std::add_lvalue_reference_t<const V> at(const K_ & key) const requires (!std::is_same_v<V, void>);
#endif
///
/// Gets the element for the heterogeneous key, creating a new element if it is not already present
///
/// Defined only for maps, not for sets
///
/// @param key the key to retrieve
/// @returns the element for the key
///
template <Compatible<K> K_> [[nodiscard]] std::add_lvalue_reference_t<V> operator[](const K_ & key) requires (!std::is_same_v<V, void>);
template <Compatible<K> K_> [[nodiscard]] std::add_lvalue_reference_t<V> operator[](K_ && key) requires (!std::is_same_v<V, void>);
///
/// @returns an iterator to the first element in the map/set
///
[[nodiscard]] iterator begin();
[[nodiscard]] const_iterator begin() const;
[[nodiscard]] const_iterator cbegin() const;
///
/// @returns an iterator that is conceptually one-past the end of the map/set or an invalid position
///
[[nodiscard]] iterator end();
[[nodiscard]] const_iterator end() const;
[[nodiscard]] const_iterator cend() const;
///
/// @param key the key to find
/// @returns an iterator to the element for the key if present, or the end iterator if absent
///
template <Compatible<K> K_> [[nodiscard]] iterator find(const K_ & key);
template <Compatible<K> K_> [[nodiscard]] const_iterator find(const K_ & key) const;
///
/// @returns the index of the slot into which the heterogeneous key would fall
///
template <Compatible<K> K_> [[nodiscard]] u64 slot(const K_ & key) const;
///
/// Ensures there are enough slots to comfortably hold `capacity` number of elements
///
/// Equivalent to `rehash(2 * capacity)`
///
/// Invalidates iterators if there is a rehash
///
/// @param capacity the minimum capacity
///
void reserve(u64 capacity);
///
/// Ensures the number of slots is equal to the smallest power of two greater than or equal to both `slotN`
/// and the current size, down to a minimum of `config::minMapSlotN`
///
/// Equivalent to `reserve(slotN / 2)`
///
/// Invalidates iterators if there is a rehash
///
/// @param slotN the minimum slot count
///
void rehash(u64 slotN);
///
/// Swaps the contents of this map/set with the other's
///
/// Does not allocate or copy memory
///
/// Invalidates iterators
///
/// @param other the map/set to swap with
///
void swap(RawMap & other);
///
/// @returns the number of elements in the map/set
///
[[nodiscard]] u64 size() const;
///
/// @returns whether the map/set is empty
///
[[nodiscard]] bool empty() const;
///
/// @returns how many elements the map/set can hold before needing to rehash; equivalent to `slot_n() / 2`
///
[[nodiscard]] u64 capacity() const;
///
/// @returns the number of slots in the map/set; equivalent to `capacity() * 2`
///
[[nodiscard]] u64 slot_n() const;
///
/// @returns the maximum possible element count; equivalent to `max_slot_n() * 2`
///
[[nodiscard]] u64 max_size() const;
///
/// @returns the maximum possible slot count; equivalent to `max_size() / 2`
///
[[nodiscard]] u64 max_slot_n() const;
///
/// @returns the ratio of elements to slots, maximum being 0.5
///
[[nodiscard]] f32 load_factor() const;
///
/// @returns 0.5, the maximum possible load factor
///
[[nodiscard]] f32 max_load_factor() const;
///
/// @returns the hasher
///
[[nodiscard]] const H & hash_function() const;
///
/// @returns the allocator
///
[[nodiscard]] const A & get_allocator() const;
private:
using _RawKey = RawType<K>;
inline static constexpr _RawKey _vacantKey{_RawKey(~_RawKey{})};
inline static constexpr _RawKey _graveKey{_RawKey(~_RawKey{1u})};
inline static constexpr _RawKey _specialKeys[2]{_graveKey, _vacantKey};
inline static constexpr _RawKey _vacantGraveKey{_vacantKey};
inline static constexpr _RawKey _vacantVacantKey{_graveKey};
inline static constexpr _RawKey _vacantSpecialKeys[2]{_vacantGraveKey, _vacantVacantKey};
inline static constexpr _RawKey _terminalKey{0u};
static K & _key(E & element);
static const K & _key(const E & element);
static bool _isPresent(const _RawKey & key);
static bool _isSpecial(const _RawKey & key);
u64 _size;
u64 _slotN; // Does not include special elements
E * _elements;
bool _haveSpecial[2];
H _hash;
A _alloc;
template <typename KTuple, typename VTuple, u64... kIndices, u64... vIndices> std::pair<iterator, bool> _emplace(KTuple && kTuple, VTuple && vTuple, std::index_sequence<kIndices...>, std::index_sequence<vIndices...>);
template <bool preserveInvariants> void _clear();
template <Compatible<K> K_> u64 _slot(const K_ & key) const;
void _rehash(u64 slotN);
template <bool zeroControls> void _allocate();
void _deallocate();
void _clearKeys();
template <bool move> void _forwardData(std::conditional_t<move, RawMap, const RawMap> & other);
struct _FindKeyResult1 { E * element; bool isPresent; };
struct _FindKeyResult2 { E * element; bool isPresent; bool isSpecial; unsigned char specialI; };
template <bool insertionForm> using _FindKeyResult = std::conditional_t<insertionForm, _FindKeyResult2, _FindKeyResult1>;
// If the key is not present, returns the slot after the the key's bucket
template <bool insertionForm, Compatible<K> K_> _FindKeyResult<insertionForm> _findKey(const K_ & key) const;
};
template <Rawable K, typename V, typename H, typename A> bool operator==(const RawMap<K, V, H, A> & m1, const RawMap<K, V, H, A> & m2);
template <Rawable K, typename V, typename H, typename A>
template <bool constant>
class RawMap<K, V, H, A>::_Iterator
{
friend ::qc::hash::RawMap<K, V, H, A>;
friend ::qc::hash::RawFriend;
using E = std::conditional_t<constant, const RawMap::E, RawMap::E>;
public:
using iterator_category = std::forward_iterator_tag;
using value_type = E;
using difference_type = ptrdiff_t;
using pointer = E *;
using reference = E &;
///
/// Default constructor - equivalent to the end iterator
///
constexpr _Iterator() = default;
///
/// Copy constructor - a mutable iterator may be implicitly converted to a const iterator
/// @param other the iterator to copy
///
constexpr _Iterator(const _Iterator & other) = default;
template <bool constant_> requires (constant && !constant_) constexpr _Iterator(const _Iterator<constant_> & other);
///
/// Copy assignment - a mutable iterator may be implicitly converted to a const iterator
/// @param other the iterator to copy
///
_Iterator & operator=(const _Iterator & other) = default;
template <bool constant_> requires (constant && !constant_) _Iterator & operator=(const _Iterator<constant_> & other);
///
/// @returns the element pointed to by the iterator; undefined for invalid iterators
///
[[nodiscard]] E & operator*() const;
///
/// @returns a pointer to the element pointed to by the iterator; undefined for invalid iterators
///
[[nodiscard]] E * operator->() const;
///
/// Increments the iterator to point to the next element in the map/set, or the end iterator if there are no more
/// elements
///
/// Incrementing the end iterator is undefined
///
/// @returns this
///
_Iterator & operator++();
///
/// Increments the iterator to point to the next element in the map/set, or the end iterator if there are no more
/// elements
///
/// Incrementing the end iterator is undefined
///
/// @returns a copy of the iterator before it was incremented
///
_Iterator operator++(int);
///
/// @param other the other iterator to compare with
/// @returns whether this iterator is equivalent to the other iterator
///
template <bool constant_> [[nodiscard]] bool operator==(const _Iterator<constant_> & other) const;
private:
E * _element;
constexpr _Iterator(E * element);
};
}
namespace std
{
///
/// Swaps the two maps/sets. No memory is copied or allocated
///
/// @param a the map/set to swap with `b`
/// @param b the map/set to swap with `a`
///
template <typename K, typename V, typename H, typename A> void swap(qc::hash::RawMap<K, V, H, A> & a, qc::hash::RawMap<K, V, H, A> & b);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
namespace qc::hash
{
namespace _private
{
inline constexpr u64 minMapSlotN{minMapCapacity * 2u};
// Returns the lowest 64 bits from the given object
template <UnsignedInteger U, typename T>
inline constexpr U getLowBytes(const T & v)
{
// Key is aligned as `U` and can be simply reinterpreted as such
if constexpr (alignof(T) >= sizeof(U))
{
return reinterpret_cast<const U &>(v);
}
// Key's alignment matches its size and can be simply reinterpreted as an unsigned integer
else if constexpr (alignof(T) == sizeof(T))
{
return reinterpret_cast<const Unsigned<sizeof(T)> &>(v);
}
// Key is not nicely aligned, manually copy up to a `U`'s worth of memory
// Could use memcpy, but this gives better debug performance, and both compile to the same in release
else
{
U result{0u};
using Block = Unsigned<alignof(T) < sizeof(U) ? alignof(T) : sizeof(U)>;
constexpr u64 n{(sizeof(T) < sizeof(U) ? sizeof(T) : sizeof(U)) / sizeof(Block)};
const Block * src{reinterpret_cast<const Block *>(&v)};
Block * dst{reinterpret_cast<Block *>(&result)};
// We want the lower-order bytes, so need to adjust on big endian systems
if constexpr (std::endian::native == std::endian::big)
{
constexpr u64 srcBlocks{sizeof(T) / sizeof(Block)};
constexpr u64 dstBlocks{sizeof(U) / sizeof(Block)};
if constexpr (srcBlocks > n)
{
src += srcBlocks - n;
}
if constexpr (dstBlocks > n)
{
dst += dstBlocks - n;
}
}
// Copy blocks
if constexpr (n >= 1u) dst[0] = src[0];
if constexpr (n >= 2u) dst[1] = src[1];
if constexpr (n >= 3u) dst[2] = src[2];
if constexpr (n >= 4u) dst[3] = src[3];
if constexpr (n >= 5u) dst[4] = src[4];
if constexpr (n >= 6u) dst[5] = src[5];
if constexpr (n >= 7u) dst[6] = src[6];
if constexpr (n >= 8u) dst[7] = src[7];
return result;
}
}
}
template <u64 elementSize, u64 elementN>
inline constexpr auto UnsignedMulti<elementSize, elementN>::operator~() const -> UnsignedMulti
{
UnsignedMulti res;
for (u64 i{0u}; i < elementN; ++i)
{
res.elements[i] = Element(~elements[i]);
}
return res;
}
template <Rawable T>
struct IdentityHash
{
[[nodiscard]] constexpr u64 operator()(const T & v) const
{
return _private::getLowBytes<u64>(v);
}
};
template <typename T>
struct IdentityHash<T *>
{
[[nodiscard]] constexpr u64 operator()(const T * const v) const
{
// Bit shift away the low zero bits to maximize low-order entropy
constexpr s32 shift{s32(std::bit_width(alignof(T)) - 1u)};
return std::bit_cast<u64>(v) >> shift;
}
};
template <typename T>
struct IdentityHash<std::unique_ptr<T>> : IdentityHash<T *>
{
// Allows heterogeneity with raw pointers
using IdentityHash<T *>::operator();
[[nodiscard]] constexpr u64 operator()(const std::unique_ptr<T> & v) const
{
return (*this)(v.get());
}
};
template <typename T>
struct IdentityHash<std::shared_ptr<T>> : IdentityHash<T *>
{
// Allows heterogeneity with raw pointers
using IdentityHash<T *>::operator();
[[nodiscard]] constexpr u64 operator()(const std::shared_ptr<T> & v) const
{
return (*this)(v.get());
}
};
template <typename T>
struct FastHash
{
[[nodiscard]] constexpr u64 operator()(const T & v) const
{
return fastHash::hash<u64>(v);
}
};
template <typename T>
struct FastHash<T *>
{
[[nodiscard]] constexpr u64 operator()(const T * const v) const
{
return fastHash::hash<u64>(v);
}
};
template <typename T>