-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMultiKeyMap.mpp
More file actions
1009 lines (864 loc) · 33.9 KB
/
Copy pathMultiKeyMap.mpp
File metadata and controls
1009 lines (864 loc) · 33.9 KB
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
export module CppUtils.Container.MultiKeyMap;
import std;
import CppUtils.String.Hash;
import CppUtils.Type.Utility;
export namespace CppUtils::Container
{
template<class T>
struct DefaultHasher final
{
using is_transparent = void;
[[nodiscard]] inline auto operator()(const T& value) const noexcept -> std::size_t
{
return std::hash<T>{}(value);
}
};
template<>
struct DefaultHasher<std::string> final
{
using is_transparent = void;
[[nodiscard]] inline auto operator()(std::string_view value) const noexcept -> std::size_t
{
return std::hash<std::string_view>{}(value);
}
[[nodiscard]] inline auto operator()(const std::string& value) const noexcept -> std::size_t
{
return std::hash<std::string_view>{}(value);
}
[[nodiscard]] inline auto operator()(const char* value) const noexcept -> std::size_t
{
return std::hash<std::string_view>{}(std::string_view{value});
}
};
template<class First, class Second>
struct DefaultHasher<std::pair<First, Second>> final
{
[[nodiscard]] inline auto operator()(const std::pair<First, Second>& pair) const noexcept -> std::size_t
{
const auto hash1 = DefaultHasher<First>{}(pair.first);
const auto hash2 = DefaultHasher<Second>{}(pair.second);
return hash1 ^ (hash2 << 1);
}
};
template<class T, class Hasher = DefaultHasher<T>, class Equal = std::equal_to<T>>
struct Key final
{
using KeyType = T;
using HashType = Hasher;
using EqualType = Equal;
static constexpr auto isUnique = true;
};
template<class T, class Hasher = DefaultHasher<T>, class Equal = std::equal_to<T>>
struct NonUnique final
{
using KeyType = T;
using HashType = Hasher;
using EqualType = Equal;
static constexpr auto isUnique = false;
};
template<String::Hasher Tag, class T, class Hasher = DefaultHasher<T>, class Equal = std::equal_to<T>>
struct TaggedKey final
{
static constexpr auto tag = Tag;
using KeyType = T;
using HashType = Hasher;
using EqualType = Equal;
static constexpr auto isUnique = true;
};
template<String::Hasher Tag, class T, class Hasher = DefaultHasher<T>, class Equal = std::equal_to<T>>
struct TaggedNonUnique final
{
static constexpr auto tag = Tag;
using KeyType = T;
using HashType = Hasher;
using EqualType = Equal;
static constexpr auto isUnique = false;
};
namespace Detail
{
template<class T>
using DefaultEqualFor = std::conditional_t<std::same_as<T, std::string>, std::equal_to<>, std::equal_to<T>>;
template<class T>
struct KeyDescriptorTrait final
{
using KeyType = T;
using HashType = DefaultHasher<T>;
using EqualType = DefaultEqualFor<T>;
static constexpr auto isUnique = true;
static constexpr auto tag = Type::Hash{};
static constexpr auto hasTag = false;
};
template<class T, class Hasher, class Equal>
struct KeyDescriptorTrait<Key<T, Hasher, Equal>> final
{
using KeyType = T;
using HashType = Hasher;
using EqualType = Equal;
static constexpr auto isUnique = true;
static constexpr auto tag = Type::Hash{};
static constexpr auto hasTag = false;
};
template<class T, class Hasher, class Equal>
struct KeyDescriptorTrait<NonUnique<T, Hasher, Equal>> final
{
using KeyType = T;
using HashType = Hasher;
using EqualType = Equal;
static constexpr auto isUnique = false;
static constexpr auto tag = Type::Hash{};
static constexpr auto hasTag = false;
};
template<String::Hasher Tag, class T, class Hasher, class Equal>
struct KeyDescriptorTrait<TaggedKey<Tag, T, Hasher, Equal>> final
{
using KeyType = T;
using HashType = Hasher;
using EqualType = Equal;
static constexpr auto isUnique = true;
static constexpr auto tag = static_cast<Type::Hash>(Tag);
static constexpr auto hasTag = true;
};
template<String::Hasher Tag, class T, class Hasher, class Equal>
struct KeyDescriptorTrait<TaggedNonUnique<Tag, T, Hasher, Equal>> final
{
using KeyType = T;
using HashType = Hasher;
using EqualType = Equal;
static constexpr auto isUnique = false;
static constexpr auto tag = static_cast<Type::Hash>(Tag);
static constexpr auto hasTag = true;
};
template<class KeysTuple, class ValueType>
struct EntryStorage;
template<class... KeyTypes, class ValueType>
struct EntryStorage<std::tuple<KeyTypes...>, ValueType> final
{
using TupleType = std::tuple<KeyTypes..., ValueType>;
std::tuple<KeyTypes...> keys;
ValueType value;
template<std::size_t Index>
[[nodiscard]] inline constexpr decltype(auto) key(this auto&& self) noexcept
{
return std::get<Index>(self.keys);
}
template<class TargetKey>
[[nodiscard]] inline constexpr decltype(auto) key(this auto&& self) noexcept
{
return std::get<TargetKey>(self.keys);
}
template<std::size_t Index>
[[nodiscard]] inline constexpr decltype(auto) get(this auto&& self) noexcept
{
if constexpr (Index < sizeof...(KeyTypes))
return self.template key<Index>();
else
return (self.value);
}
[[nodiscard]] inline constexpr auto operator==(const EntryStorage& other) const noexcept -> bool = default;
};
template<class... KeyTypes>
struct EntryStorage<std::tuple<KeyTypes...>, void> final
{
using TupleType = std::tuple<KeyTypes...>;
std::tuple<KeyTypes...> keys;
constexpr EntryStorage() = default;
constexpr explicit EntryStorage(std::tuple<KeyTypes...> tuple): keys{std::move(tuple)} {}
template<std::size_t Index>
[[nodiscard]] inline constexpr decltype(auto) key(this auto&& self) noexcept
{
return std::get<Index>(self.keys);
}
template<class TargetKey>
[[nodiscard]] inline constexpr decltype(auto) key(this auto&& self) noexcept
{
return std::get<TargetKey>(self.keys);
}
template<std::size_t Index>
[[nodiscard]] inline constexpr decltype(auto) get(this auto&& self) noexcept
{
return std::get<Index>(self.keys);
}
[[nodiscard]] inline constexpr auto operator==(const EntryStorage& other) const noexcept -> bool = default;
};
template<class FirstKey, class SecondKey>
struct EntryStorage<std::tuple<FirstKey, SecondKey>, void> final
{
using TupleType = std::tuple<FirstKey, SecondKey>;
FirstKey first{};
SecondKey second{};
constexpr EntryStorage() = default;
constexpr EntryStorage(FirstKey first, SecondKey second): first{std::move(first)}, second{std::move(second)} {}
constexpr explicit EntryStorage(std::tuple<FirstKey, SecondKey> tuple): first{std::get<0>(std::move(tuple))}, second{std::get<1>(std::move(tuple))} {}
template<std::size_t Index>
[[nodiscard]] inline constexpr decltype(auto) key(this auto&& self) noexcept
{
if constexpr (Index == 0)
return (self.first);
else
return (self.second);
}
template<class TargetKey>
[[nodiscard]] inline constexpr decltype(auto) key(this auto&& self) noexcept
{
if constexpr (std::same_as<std::remove_cvref_t<TargetKey>, FirstKey>)
return (self.first);
else
return (self.second);
}
template<std::size_t Index>
[[nodiscard]] inline constexpr decltype(auto) get(this auto&& self) noexcept
{
return self.template key<Index>();
}
[[nodiscard]] inline constexpr auto operator==(const EntryStorage& other) const noexcept -> bool = default;
};
template<class IndexDescriptor>
struct KeyIndexTable;
template<class IndexDescriptor>
requires (IndexDescriptor::isUnique)
struct KeyIndexTable<IndexDescriptor> final
{
private:
using KeyType = typename IndexDescriptor::KeyType;
using Hasher = typename IndexDescriptor::HashType;
using Equal = typename IndexDescriptor::EqualType;
using Map = std::unordered_map<KeyType, std::size_t, Hasher, Equal>;
public:
Map map;
};
template<class IndexDescriptor>
requires (not IndexDescriptor::isUnique)
struct KeyIndexTable<IndexDescriptor> final
{
private:
using KeyType = typename IndexDescriptor::KeyType;
using Hasher = typename IndexDescriptor::HashType;
using Equal = typename IndexDescriptor::EqualType;
using Map = std::unordered_map<KeyType, std::vector<std::size_t>, Hasher, Equal>;
public:
Map map;
};
}
template<class EntryType>
class NonUniqueRange final
{
public:
using value_type = std::remove_cv_t<EntryType>;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
using reference = EntryType&;
using const_reference = const EntryType&;
using pointer = EntryType*;
using const_pointer = const EntryType*;
struct Iterator final
{
using iterator_concept = std::random_access_iterator_tag;
using iterator_category = std::random_access_iterator_tag;
using value_type = std::remove_cv_t<EntryType>;
using difference_type = std::ptrdiff_t;
using pointer = EntryType*;
using const_pointer = const EntryType*;
using reference = EntryType&;
using const_reference = const EntryType&;
EntryType* entries = nullptr;
std::vector<std::size_t>::const_iterator indexIterator{};
[[nodiscard]] inline constexpr auto operator*() const noexcept -> reference { return entries[*indexIterator]; }
[[nodiscard]] inline constexpr auto operator->() const noexcept -> pointer { return std::addressof(entries[*indexIterator]); }
inline constexpr auto operator++() noexcept -> Iterator&
{
++indexIterator;
return *this;
}
inline constexpr auto operator++(int) noexcept -> Iterator
{
auto temporary = *this;
++indexIterator;
return temporary;
}
inline constexpr auto operator--() noexcept -> Iterator&
{
--indexIterator;
return *this;
}
inline constexpr auto operator--(int) noexcept -> Iterator
{
auto temporary = *this;
--indexIterator;
return temporary;
}
inline constexpr auto operator+=(difference_type offset) noexcept -> Iterator&
{
indexIterator += offset;
return *this;
}
inline constexpr auto operator-=(difference_type offset) noexcept -> Iterator&
{
indexIterator -= offset;
return *this;
}
[[nodiscard]] inline constexpr auto operator+(difference_type offset) const noexcept -> Iterator { return Iterator{entries, indexIterator + offset}; }
[[nodiscard]] friend inline constexpr auto operator+(difference_type offset, const Iterator& iterator) noexcept -> Iterator { return iterator + offset; }
[[nodiscard]] inline constexpr auto operator-(difference_type offset) const noexcept -> Iterator { return Iterator{entries, indexIterator - offset}; }
[[nodiscard]] inline constexpr auto operator-(const Iterator& other) const noexcept -> difference_type { return indexIterator - other.indexIterator; }
[[nodiscard]] inline constexpr auto operator[](difference_type offset) const noexcept -> reference { return entries[*(indexIterator + offset)]; }
[[nodiscard]] inline constexpr auto operator<=>(const Iterator& other) const noexcept = default;
};
using iterator = Iterator;
using const_iterator = Iterator;
using reverse_iterator = std::reverse_iterator<Iterator>;
using const_reverse_iterator = std::reverse_iterator<Iterator>;
EntryType* entries = nullptr;
const std::vector<std::size_t>* indices = nullptr;
// Complexité : O(1) au runtime
[[nodiscard]] inline constexpr auto begin() const noexcept -> Iterator
{
return Iterator{entries, indices ? std::ranges::cbegin(*indices) : std::vector<std::size_t>::const_iterator{}};
}
[[nodiscard]] inline constexpr auto end() const noexcept -> Iterator
{
return Iterator{entries, indices ? std::ranges::cend(*indices) : std::vector<std::size_t>::const_iterator{}};
}
[[nodiscard]] inline constexpr auto cbegin() const noexcept -> Iterator { return begin(); }
[[nodiscard]] inline constexpr auto cend() const noexcept -> Iterator { return end(); }
[[nodiscard]] inline constexpr auto rbegin() const noexcept -> reverse_iterator { return reverse_iterator{end()}; }
[[nodiscard]] inline constexpr auto rend() const noexcept -> reverse_iterator { return reverse_iterator{begin()}; }
[[nodiscard]] inline constexpr auto crbegin() const noexcept -> const_reverse_iterator { return const_reverse_iterator{cend()}; }
[[nodiscard]] inline constexpr auto crend() const noexcept -> const_reverse_iterator { return const_reverse_iterator{cbegin()}; }
[[nodiscard]] inline constexpr auto empty() const noexcept -> bool
{
return not indices or std::ranges::empty(*indices);
}
[[nodiscard]] inline constexpr auto size() const noexcept -> std::size_t
{
return indices ? std::ranges::size(*indices) : 0uz;
}
[[nodiscard]] inline constexpr auto front() const -> reference
{
return (*this)[0uz];
}
[[nodiscard]] inline constexpr auto back() const -> reference
{
return (*this)[size() - 1uz];
}
[[nodiscard]] inline constexpr auto operator[](std::size_t index) const -> reference
{
return entries[(*indices)[index]];
}
};
template<class... KeysAndValue>
class MultiKeyMap final
{
static_assert(sizeof...(KeysAndValue) >= 2, "MultiKeyMap requires at least one key and one value type (or void).");
using AllTypesTuple = std::tuple<KeysAndValue...>;
public:
static constexpr auto keyCount = sizeof...(KeysAndValue) - 1uz;
using ValueType = std::tuple_element_t<keyCount, AllTypesTuple>;
private:
template<std::size_t IndexPosition>
using KeyAt = std::tuple_element_t<IndexPosition, AllTypesTuple>;
template<std::size_t IndexPosition>
using DescriptorAt = Detail::KeyDescriptorTrait<KeyAt<IndexPosition>>;
template<std::size_t IndexPosition>
using KeyTypeAt = typename DescriptorAt<IndexPosition>::KeyType;
template<class IndexSequence>
struct PureKeysTupleBuilder;
template<std::size_t... I>
struct PureKeysTupleBuilder<std::index_sequence<I...>>
{
using type = std::tuple<KeyTypeAt<I>...>;
};
using PureKeysTuple = typename PureKeysTupleBuilder<std::make_index_sequence<keyCount>>::type;
public:
using Entry = Detail::EntryStorage<PureKeysTuple, ValueType>;
using value_type = Entry;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
using reference = Entry&;
using const_reference = const Entry&;
using pointer = Entry*;
using const_pointer = const Entry*;
using StorageVector = std::vector<Entry>;
using iterator = typename StorageVector::iterator;
using const_iterator = typename StorageVector::const_iterator;
using reverse_iterator = typename StorageVector::reverse_iterator;
using const_reverse_iterator = typename StorageVector::const_reverse_iterator;
private:
template<class IndexSequence>
struct IndicesTupleBuilder;
template<std::size_t... I>
struct IndicesTupleBuilder<std::index_sequence<I...>> final
{
using type = std::tuple<Detail::KeyIndexTable<DescriptorAt<I>>...>;
};
using IndicesTuple = typename IndicesTupleBuilder<std::make_index_sequence<keyCount>>::type;
public:
// Complexité : O(n) à la compilation seulement
template<class TargetKey>
[[nodiscard]] static consteval auto resolveKeyIndex() noexcept -> std::size_t
{
struct MatchResult final
{
std::size_t count = 0uz;
std::size_t index = 0uz;
};
using PureTarget = std::remove_cvref_t<TargetKey>;
constexpr auto countMatches = [](auto matches) {
auto count = 0uz;
auto index = 0uz;
for (auto i = 0uz; i < std::ranges::size(matches); ++i)
{
if (matches[i])
{
++count;
index = i;
}
}
return MatchResult{.count = count, .index = index};
};
constexpr auto exact = [&]<std::size_t... I>(std::index_sequence<I...>) {
return countMatches(std::array<bool, sizeof...(I)>{std::same_as<PureTarget, KeyTypeAt<I>>...});
}(std::make_index_sequence<keyCount>{});
if constexpr (exact.count == 1uz)
return exact.index;
else if constexpr (exact.count > 1uz)
static_assert(Type::FalseType<TargetKey>, "MultiKeyMap: Ambiguous key type. Multiple keys share this type; use findAt<Index>(key) or find<Tag>(key).");
else
{
constexpr auto convertible = [&]<std::size_t... I>(std::index_sequence<I...>) {
return countMatches(std::array<bool, sizeof...(I)>{
(std::convertible_to<TargetKey, KeyTypeAt<I>> or std::constructible_from<KeyTypeAt<I>, TargetKey>)...});
}(std::make_index_sequence<keyCount>{});
if constexpr (convertible.count == 1uz)
return convertible.index;
else if constexpr (convertible.count > 1uz)
static_assert(Type::FalseType<TargetKey>, "MultiKeyMap: Ambiguous key type. Multiple keys can accept this argument; use findAt<Index>(key) or find<Tag>(key).");
else
static_assert(Type::FalseType<TargetKey>, "MultiKeyMap: No key matches the given argument type.");
}
}
// Complexité : O(n) à la compilation seulement
template<String::Hasher Tag>
[[nodiscard]] static consteval auto resolveTagPosition() noexcept -> std::size_t
{
constexpr auto tagHash = static_cast<Type::Hash>(Tag);
constexpr auto searchResult = []<std::size_t... I>(std::index_sequence<I...>) {
constexpr auto tagMatches = std::array<bool, sizeof...(I)>{(DescriptorAt<I>::hasTag and DescriptorAt<I>::tag == tagHash)...};
for (auto i = 0uz; i < sizeof...(I); ++i)
if (tagMatches[i])
return std::optional<std::size_t>{i};
return std::optional<std::size_t>{std::nullopt};
}(std::make_index_sequence<keyCount>{});
if constexpr (searchResult.has_value())
return searchResult.value();
else
static_assert(Type::FalseValue<tagHash>, "MultiKeyMap: Tag not found in key descriptors.");
}
inline MultiKeyMap() = default;
// Complexité : O(n) au runtime
inline explicit MultiKeyMap(auto&&... entries)
requires (sizeof...(entries) > 0 and (std::same_as<std::remove_cvref_t<decltype(entries)>, Entry> and ...))
{
reserve(sizeof...(entries));
(insert(std::forward<decltype(entries)>(entries)), ...);
}
// Complexité : O(n) au runtime
template<std::input_iterator InputIterator>
inline MultiKeyMap(InputIterator first, InputIterator last)
{
insert(first, last);
}
[[nodiscard]] static inline constexpr auto makeEntry(auto&&... args) -> Entry
{
if constexpr (std::is_void_v<ValueType>)
{
static_assert(sizeof...(args) == keyCount, "MultiKeyMap: Argument count must match key count when Value is void.");
return Entry{PureKeysTuple{std::forward<decltype(args)>(args)...}};
}
else
{
static_assert(sizeof...(args) == keyCount + 1, "MultiKeyMap: Argument count must match key count + 1 value.");
return []<std::size_t... I>(std::index_sequence<I...>, auto&&... pack) {
auto tupleOfArgs = std::forward_as_tuple(std::forward<decltype(pack)>(pack)...);
return Entry{
PureKeysTuple{std::get<I>(std::move(tupleOfArgs))...},
std::get<sizeof...(I)>(std::move(tupleOfArgs))};
}(std::make_index_sequence<keyCount>{}, std::forward<decltype(args)>(args)...);
}
}
// Complexité : O(1) au runtime
inline auto insert(Entry entry) -> std::pair<iterator, bool>
{
const auto hasCollision = [&]<std::size_t... I>(std::index_sequence<I...>) {
return ((DescriptorAt<I>::isUnique and std::get<I>(m_indices).map.contains(entry.template key<I>())) or ...);
}(std::make_index_sequence<keyCount>{});
if (hasCollision)
return {std::ranges::end(m_entries), false};
const auto newIndex = std::ranges::size(m_entries);
m_entries.push_back(std::move(entry));
[&]<std::size_t... I>(std::index_sequence<I...>) {
([&]() {
using Descriptor = DescriptorAt<I>;
const auto& key = m_entries[newIndex].template key<I>();
if constexpr (Descriptor::isUnique)
std::get<I>(m_indices).map.emplace(key, newIndex);
else
std::get<I>(m_indices).map[key].push_back(newIndex);
}(), ...);
}(std::make_index_sequence<keyCount>{});
return {std::ranges::begin(m_entries) + static_cast<difference_type>(newIndex), true};
}
// Complexité : O(1) au runtime
inline auto insert(auto&&... args) -> std::pair<iterator, bool>
requires (sizeof...(args) > 1 or (sizeof...(args) == 1 and (not std::same_as<std::remove_cvref_t<decltype(args)>, Entry> and ...)))
{
return insert(makeEntry(std::forward<decltype(args)>(args)...));
}
// Complexité : O(1) au runtime
inline auto emplace(auto&&... args) -> std::pair<iterator, bool>
{
if constexpr (sizeof...(args) == 1 and (std::same_as<std::remove_cvref_t<decltype(args)>, Entry> and ...))
return insert(std::forward<decltype(args)>(args)...);
else
return insert(makeEntry(std::forward<decltype(args)>(args)...));
}
// Complexité : O(1) au runtime
template<class... Args>
inline auto emplace_back(Args&&... args) -> reference
{
return *emplace(std::forward<Args>(args)...).first;
}
// Complexité : O(n) au runtime
template<std::input_iterator InputIterator>
inline auto insert(InputIterator first, InputIterator last) -> void
{
for (; first != last; ++first)
insert(*first);
}
// Complexité : O(n) au runtime
inline auto insert_range(std::ranges::input_range auto&& range) -> void
{
for (auto&& element : range)
insert(std::forward<decltype(element)>(element));
}
// Complexité : O(1) au runtime
template<std::size_t Index>
[[nodiscard]] inline auto findAt(this auto&& self, const auto& key) -> decltype(auto)
{
static_assert(Index < keyCount, "MultiKeyMap: Index position out of range.");
using Descriptor = DescriptorAt<Index>;
using MapEntry = std::conditional_t<std::is_const_v<std::remove_reference_t<decltype(self)>>, const Entry, Entry>;
auto& indexMap = std::get<Index>(self.m_indices).map;
auto iterator = indexMap.find(key);
if constexpr (Descriptor::isUnique)
{
if (iterator == std::ranges::end(indexMap))
return std::ranges::end(self.m_entries);
return std::ranges::begin(self.m_entries) + static_cast<difference_type>(iterator->second);
}
else
{
if (iterator == std::ranges::end(indexMap))
return NonUniqueRange<MapEntry>{std::ranges::data(self.m_entries), nullptr};
return NonUniqueRange<MapEntry>{std::ranges::data(self.m_entries), std::addressof(iterator->second)};
}
}
// Complexité : O(1) au runtime
[[nodiscard]] inline auto find(this auto&& self, const auto& key) -> decltype(auto)
{
return self.template findAt<resolveKeyIndex<decltype(key)>()>(key);
}
// Complexité : O(1) au runtime
template<String::Hasher Tag>
[[nodiscard]] inline auto find(this auto&& self, const auto& key) -> decltype(auto)
{
return self.template findAt<resolveTagPosition<Tag>()>(key);
}
// Complexité : O(1) au runtime
template<std::size_t Index>
[[nodiscard]] inline auto findIteratorAt(this auto&& self, const auto& key) -> decltype(auto)
{
static_assert(Index < keyCount, "MultiKeyMap: Index position out of range.");
using Descriptor = DescriptorAt<Index>;
if constexpr (Descriptor::isUnique)
return self.template findAt<Index>(key);
else
{
auto range = self.template findAt<Index>(key);
if (std::ranges::empty(range))
return std::ranges::end(self.m_entries);
return std::ranges::begin(self.m_entries) + static_cast<difference_type>(range.indices->front());
}
}
// Complexité : O(1) au runtime
[[nodiscard]] inline auto findIterator(this auto&& self, const auto& key) -> decltype(auto)
{
return self.template findIteratorAt<resolveKeyIndex<decltype(key)>()>(key);
}
// Complexité : O(1) au runtime
template<String::Hasher Tag>
[[nodiscard]] inline auto findIterator(this auto&& self, const auto& key) -> decltype(auto)
{
return self.template findIteratorAt<resolveTagPosition<Tag>()>(key);
}
// Complexité : O(1) au runtime
template<std::size_t Index>
[[nodiscard]] inline auto containsAt(const auto& key) const -> bool
{
static_assert(Index < keyCount, "MultiKeyMap: Index position out of range.");
return std::get<Index>(m_indices).map.contains(key);
}
// Complexité : O(1) au runtime
[[nodiscard]] inline auto contains(const auto& key) const -> bool
{
return containsAt<resolveKeyIndex<decltype(key)>()>(key);
}
// Complexité : O(1) au runtime
template<String::Hasher Tag>
[[nodiscard]] inline auto contains(const auto& key) const -> bool
{
return containsAt<resolveTagPosition<Tag>()>(key);
}
// Complexité : O(1) au runtime
template<std::size_t Index>
[[nodiscard]] inline auto countAt(const auto& key) const -> std::size_t
{
static_assert(Index < keyCount, "MultiKeyMap: Index position out of range.");
using Descriptor = DescriptorAt<Index>;
auto& indexMap = std::get<Index>(m_indices).map;
if constexpr (Descriptor::isUnique)
return indexMap.contains(key) ? 1uz : 0uz;
else
{
auto iterator = indexMap.find(key);
return iterator != std::ranges::end(indexMap) ? std::ranges::size(iterator->second) : 0uz;
}
}
// Complexité : O(1) au runtime
[[nodiscard]] inline auto count(const auto& key) const -> std::size_t
{
return countAt<resolveKeyIndex<decltype(key)>()>(key);
}
// Complexité : O(1) au runtime
template<String::Hasher Tag>
[[nodiscard]] inline auto count(const auto& key) const -> std::size_t
{
return countAt<resolveTagPosition<Tag>()>(key);
}
private:
template<std::size_t Index>
[[nodiscard]] static inline constexpr decltype(auto) mappedValue(auto&& entry) noexcept
{
if constexpr (not std::is_void_v<ValueType>)
return (entry.value);
else
{
static_assert(keyCount == 2, "MultiKeyMap: Access on void value requires exactly 2 keys.");
constexpr auto otherIndex = (Index == 0 ? 1uz : 0uz);
return (entry.template key<otherIndex>());
}
}
public:
// Complexité : O(1) au runtime
template<std::size_t Index>
[[nodiscard]] inline auto atIndex(this auto&& self, const auto& key) -> decltype(auto)
{
static_assert(Index < keyCount, "MultiKeyMap: Index position out of range.");
using Descriptor = DescriptorAt<Index>;
static_assert(Descriptor::isUnique, "MultiKeyMap: at() is only supported on unique keys.");
auto iterator = self.template findAt<Index>(key);
if (iterator == std::ranges::end(self.m_entries))
throw std::out_of_range{"MultiKeyMap: key not found"};
return mappedValue<Index>(*iterator);
}
// Complexité : O(1) au runtime
[[nodiscard]] inline auto at(this auto&& self, const auto& key) -> decltype(auto)
{
return self.template atIndex<resolveKeyIndex<decltype(key)>()>(key);
}
// Complexité : O(1) au runtime
template<String::Hasher Tag>
[[nodiscard]] inline auto at(this auto&& self, const auto& key) -> decltype(auto)
{
return self.template atIndex<resolveTagPosition<Tag>()>(key);
}
// Complexité : O(1) au runtime
[[nodiscard]] inline auto operator[](this auto&& self, const auto& key) -> decltype(auto)
{
constexpr auto index = resolveKeyIndex<decltype(key)>();
using Descriptor = DescriptorAt<index>;
static_assert(Descriptor::isUnique, "MultiKeyMap: operator[] is only supported on unique keys.");
auto iterator = self.template findAt<index>(key);
if (iterator != std::ranges::end(self.m_entries))
return mappedValue<index>(*iterator);
auto keysTuple = PureKeysTuple{};
std::get<index>(keysTuple) = key;
if constexpr (not std::is_void_v<ValueType>)
{
auto [insertedIterator, inserted] = self.insert(Entry{std::move(keysTuple), ValueType{}});
return mappedValue<index>(*insertedIterator);
}
else
{
auto [insertedIterator, inserted] = self.insert(Entry{std::move(keysTuple)});
return mappedValue<index>(*insertedIterator);
}
}
private:
// Complexité : O(1) au runtime
inline auto eraseAtIndex(std::size_t erasedIndex) -> iterator
{
if (erasedIndex >= std::ranges::size(m_entries))
return std::ranges::end(m_entries);
const auto lastIndex = std::ranges::size(m_entries) - 1uz;
[&]<std::size_t... I>(std::index_sequence<I...>) {
([&]() {
using Descriptor = DescriptorAt<I>;
const auto& key = m_entries[erasedIndex].template key<I>();
if constexpr (Descriptor::isUnique)
std::get<I>(m_indices).map.erase(key);
else
{
auto& vector = std::get<I>(m_indices).map[key];
std::erase(vector, erasedIndex);
if (std::ranges::empty(vector))
std::get<I>(m_indices).map.erase(key);
}
}(), ...);
}(std::make_index_sequence<keyCount>{});
if (erasedIndex != lastIndex)
{
[&]<std::size_t... I>(std::index_sequence<I...>) {
([&]() {
using Descriptor = DescriptorAt<I>;
const auto& key = m_entries[lastIndex].template key<I>();
if constexpr (Descriptor::isUnique)
std::get<I>(m_indices).map[key] = erasedIndex;
else
{
auto& vector = std::get<I>(m_indices).map[key];
auto iterator = std::ranges::find(vector, lastIndex);
if (iterator != std::ranges::end(vector))
*iterator = erasedIndex;
}
}(), ...);
}(std::make_index_sequence<keyCount>{});
m_entries[erasedIndex] = std::move(m_entries[lastIndex]);
}
m_entries.pop_back();
return std::ranges::begin(m_entries) + static_cast<difference_type>(erasedIndex);
}
public:
// Complexité : O(1) au runtime (clé unique) ou O(n) (clé non-unique)
template<std::size_t Index>
inline auto eraseAt(const auto& key)
{
static_assert(Index < keyCount, "MultiKeyMap: Index position out of range.");
using Descriptor = DescriptorAt<Index>;
auto& indexMap = std::get<Index>(m_indices).map;
auto iterator = indexMap.find(key);
if constexpr (Descriptor::isUnique)
{
if (iterator == std::ranges::end(indexMap))
return false;
const auto entryIndex = iterator->second;
eraseAtIndex(entryIndex);
return true;
}
else
{
if (iterator == std::ranges::end(indexMap))
return 0uz;
auto erasedCount = 0uz;
for (auto it = std::ranges::begin(m_entries); it != std::ranges::end(m_entries);)
if (it->template key<Index>() == key)
{
it = erase(it);
++erasedCount;
}
else
++it;
return erasedCount;
}
}
// Complexité : O(1) au runtime (clé unique) ou O(n) (clé non-unique)
inline auto erase(const auto& key)
requires (not std::same_as<std::remove_cvref_t<decltype(key)>, const_iterator> and not std::same_as<std::remove_cvref_t<decltype(key)>, iterator>)
{
return eraseAt<resolveKeyIndex<decltype(key)>()>(key);
}
// Complexité : O(1) au runtime (clé unique) ou O(n) (clé non-unique)
template<String::Hasher Tag>
inline auto erase(const auto& key)
{
return eraseAt<resolveTagPosition<Tag>()>(key);
}
// Complexité : O(1) au runtime
inline auto erase(const_iterator position) -> iterator
{
const auto index = static_cast<std::size_t>(position - std::ranges::cbegin(m_entries));
return eraseAtIndex(index);
}
// Complexité : O(n) au runtime
inline auto erase(const_iterator first, const_iterator last) -> iterator
{
while (first != last)
{
first = erase(first);
--last;
}
const auto index = static_cast<std::size_t>(first - std::ranges::cbegin(m_entries));
return std::ranges::begin(m_entries) + static_cast<difference_type>(index);
}
// Complexité : O(n) au runtime
inline auto clear() noexcept -> void
{
[&]<std::size_t... I>(std::index_sequence<I...>) {
(std::get<I>(m_indices).map.clear(), ...);
}(std::make_index_sequence<keyCount>{});
m_entries.clear();
}
// Complexité : O(n) au runtime
inline auto reserve(std::size_t newCapacity) -> void
{
m_entries.reserve(newCapacity);
[&]<std::size_t... I>(std::index_sequence<I...>) {
(std::get<I>(m_indices).map.reserve(newCapacity), ...);
}(std::make_index_sequence<keyCount>{});
}
// Complexité : O(1) au runtime
[[nodiscard]] inline auto begin() noexcept -> iterator { return std::ranges::begin(m_entries); }
[[nodiscard]] inline auto begin() const noexcept -> const_iterator { return std::ranges::cbegin(m_entries); }
[[nodiscard]] inline auto cbegin() const noexcept -> const_iterator { return std::ranges::cbegin(m_entries); }
[[nodiscard]] inline auto end() noexcept -> iterator { return std::ranges::end(m_entries); }
[[nodiscard]] inline auto end() const noexcept -> const_iterator { return std::ranges::cend(m_entries); }
[[nodiscard]] inline auto cend() const noexcept -> const_iterator { return std::ranges::cend(m_entries); }
// Complexité : O(1) au runtime
[[nodiscard]] inline auto rbegin() noexcept -> reverse_iterator { return std::ranges::rbegin(m_entries); }
[[nodiscard]] inline auto rbegin() const noexcept -> const_reverse_iterator { return std::ranges::crbegin(m_entries); }
[[nodiscard]] inline auto crbegin() const noexcept -> const_reverse_iterator { return std::ranges::crbegin(m_entries); }
[[nodiscard]] inline auto rend() noexcept -> reverse_iterator { return std::ranges::rend(m_entries); }
[[nodiscard]] inline auto rend() const noexcept -> const_reverse_iterator { return std::ranges::crend(m_entries); }
[[nodiscard]] inline auto crend() const noexcept -> const_reverse_iterator { return std::ranges::crend(m_entries); }
// Complexité : O(1) au runtime
[[nodiscard]] inline auto empty() const noexcept -> bool { return std::ranges::empty(m_entries); }
[[nodiscard]] inline auto size() const noexcept -> std::size_t { return std::ranges::size(m_entries); }
[[nodiscard]] inline auto max_size() const noexcept -> std::size_t { return m_entries.max_size(); }
[[nodiscard]] inline auto capacity() const noexcept -> std::size_t { return m_entries.capacity(); }
// Complexité : O(1) au runtime
[[nodiscard]] inline auto front() -> reference { return *std::ranges::begin(m_entries); }
[[nodiscard]] inline auto front() const -> const_reference { return *std::ranges::cbegin(m_entries); }
[[nodiscard]] inline auto back() -> reference { return *std::ranges::rbegin(m_entries); }
[[nodiscard]] inline auto back() const -> const_reference { return *std::ranges::crbegin(m_entries); }
private:
StorageVector m_entries;
IndicesTuple m_indices;
};
}