-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathfixed_map.hpp
840 lines (763 loc) · 32.1 KB
/
fixed_map.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
#pragma once
#include "fixed_containers/assert_or_abort.hpp"
#include "fixed_containers/bidirectional_iterator.hpp"
#include "fixed_containers/concepts.hpp"
#include "fixed_containers/emplace.hpp"
#include "fixed_containers/erase_if.hpp"
#include "fixed_containers/fixed_red_black_tree.hpp"
#include "fixed_containers/map_checking.hpp"
#include "fixed_containers/max_size.hpp"
#include "fixed_containers/preconditions.hpp"
#include "fixed_containers/source_location.hpp"
#include <algorithm>
#include <array>
#include <cstddef>
#include <functional>
namespace fixed_containers
{
/**
* Fixed-capacity red-black tree map with maximum size that is declared at compile-time via
* template parameter. Properties:
* - constexpr
* - retains the copy/move/destruction properties of K, V
* - no pointers stored (data layout is purely self-referential and can be serialized directly)
* - no dynamic allocations
* - no recursion
*/
template <class K,
class V,
std::size_t MAXIMUM_SIZE,
class Compare = std::less<K>,
fixed_red_black_tree_detail::RedBlackTreeNodeColorCompactness COMPACTNESS =
fixed_red_black_tree_detail::RedBlackTreeNodeColorCompactness::EMBEDDED_COLOR,
template <class /*Would be IsFixedIndexBasedStorage but gcc doesn't like the constraints
here. clang accepts it */
,
std::size_t>
typename StorageTemplate = FixedIndexBasedPoolStorage,
customize::MapChecking<K> CheckingType = customize::MapAbortChecking<K, V, MAXIMUM_SIZE>>
class FixedMap
{
public:
using key_type = K;
using mapped_type = V;
using value_type = std::pair<const K, V>;
using reference = std::pair<const K&, V&>;
using const_reference = std::pair<const K&, const V&>;
using pointer = std::add_pointer_t<reference>;
using const_pointer = std::add_pointer_t<const_reference>;
private:
using NodeIndex = fixed_red_black_tree_detail::NodeIndex;
using NodeIndexAndParentIndex = fixed_red_black_tree_detail::NodeIndexAndParentIndex;
static constexpr NodeIndex NULL_INDEX = fixed_red_black_tree_detail::NULL_INDEX;
using Tree = fixed_red_black_tree_detail::
FixedRedBlackTree<K, V, MAXIMUM_SIZE, Compare, COMPACTNESS, StorageTemplate>;
template <bool IS_CONST>
class PairProvider
{
friend class PairProvider<!IS_CONST>;
using ConstOrMutableTree = std::conditional_t<IS_CONST, const Tree, Tree>;
private:
ConstOrMutableTree* tree_;
NodeIndex current_index_;
public:
constexpr PairProvider() noexcept
: PairProvider{nullptr, MAXIMUM_SIZE}
{
}
constexpr PairProvider(ConstOrMutableTree* const tree,
const NodeIndex& current_index) noexcept
: tree_{tree}
, current_index_{current_index}
{
}
constexpr PairProvider(const PairProvider&) = default;
constexpr PairProvider(PairProvider&&) noexcept = default;
constexpr PairProvider& operator=(const PairProvider&) = default;
constexpr PairProvider& operator=(PairProvider&&) noexcept = default;
// https://github.com/llvm/llvm-project/issues/62555
template <bool IS_CONST_2>
constexpr PairProvider(const PairProvider<IS_CONST_2>& m) noexcept
requires(IS_CONST and !IS_CONST_2)
: PairProvider{m.tree_, m.current_index_}
{
}
constexpr void advance() noexcept
{
if (current_index_ == NULL_INDEX)
{
current_index_ = tree_->index_of_min_at();
}
else
{
current_index_ = tree_->index_of_successor_at(current_index_);
current_index_ = replace_null_index_with_max_size_for_end_iterator(current_index_);
}
}
constexpr void recede() noexcept
{
if (current_index_ == MAXIMUM_SIZE)
{
current_index_ = tree_->index_of_max_at();
}
else
{
current_index_ = tree_->index_of_predecessor_at(current_index_);
}
}
constexpr std::conditional_t<IS_CONST, const_reference, reference> get() const noexcept
{
fixed_red_black_tree_detail::RedBlackTreeNodeView node = tree_->node_at(current_index_);
return {node.key(), node.value()};
}
template <bool IS_CONST2>
constexpr bool operator==(const PairProvider<IS_CONST2>& other) const noexcept
{
return tree_ == other.tree_ && current_index_ == other.current_index_;
}
[[nodiscard]] constexpr NodeIndex current_index() const { return current_index_; }
};
template <IteratorConstness CONSTNESS, IteratorDirection DIRECTION>
using Iterator =
BidirectionalIterator<PairProvider<true>, PairProvider<false>, CONSTNESS, DIRECTION>;
// The tree returns NULL_INDEX when an index is not available.
// For the purposes of iterators, use NULL_INDEX for rend() and
// MAXIMUM_SIZE for end()
static constexpr NodeIndex replace_null_index_with_max_size_for_end_iterator(
const NodeIndex& i) noexcept
{
return i == NULL_INDEX ? MAXIMUM_SIZE : i;
}
public:
using const_iterator =
Iterator<IteratorConstness::CONSTANT_ITERATOR, IteratorDirection::FORWARD>;
using iterator = Iterator<IteratorConstness::MUTABLE_ITERATOR, IteratorDirection::FORWARD>;
using const_reverse_iterator =
Iterator<IteratorConstness::CONSTANT_ITERATOR, IteratorDirection::REVERSE>;
using reverse_iterator =
Iterator<IteratorConstness::MUTABLE_ITERATOR, IteratorDirection::REVERSE>;
using size_type = typename Tree::size_type;
using difference_type = typename Tree::difference_type;
public:
[[nodiscard]] static constexpr std::size_t static_max_size() noexcept { return MAXIMUM_SIZE; }
public: // Public so this type is a structural type and can thus be used in template parameters
Tree IMPLEMENTATION_DETAIL_DO_NOT_USE_tree_;
public:
constexpr FixedMap() noexcept
: FixedMap{Compare{}}
{
}
explicit constexpr FixedMap(const Compare& comparator) noexcept
: IMPLEMENTATION_DETAIL_DO_NOT_USE_tree_{comparator}
{
}
template <InputIterator InputIt>
constexpr FixedMap(
InputIt first,
InputIt last,
const Compare& comparator = {},
const std_transition::source_location& loc = std_transition::source_location::current())
: FixedMap{comparator}
{
insert(first, last, loc);
}
constexpr FixedMap(std::initializer_list<value_type> list,
const Compare& comparator = {},
const std_transition::source_location& loc =
std_transition::source_location::current()) noexcept
: FixedMap{comparator}
{
this->insert(list, loc);
}
public:
[[nodiscard]] constexpr V& at(const K& key,
const std_transition::source_location& loc =
std_transition::source_location::current()) noexcept
{
const NodeIndex i = tree().index_of_node_or_null(key);
if (preconditions::test(tree().contains_at(i)))
{
CheckingType::out_of_range(key, size(), loc);
}
return tree().node_at(i).value();
}
[[nodiscard]] constexpr const V& at(
const K& key,
const std_transition::source_location& loc =
std_transition::source_location::current()) const noexcept
{
const NodeIndex i = tree().index_of_node_or_null(key);
if (preconditions::test(tree().contains_at(i)))
{
CheckingType::out_of_range(key, size(), loc);
}
return tree().node_at(i).value();
}
constexpr V& operator[](const K& key) noexcept
{
NodeIndexAndParentIndex np = tree().index_of_node_with_parent(key);
if (tree().contains_at(np.i))
{
return tree().node_at(np.i).value();
}
// Cannot capture real source_location for operator[]
check_not_full(std_transition::source_location::current());
tree().insert_new_at(np, key);
return tree().node_at(np.i).value();
}
constexpr V& operator[](K&& key) noexcept
{
NodeIndexAndParentIndex np = tree().index_of_node_with_parent(key);
if (tree().contains_at(np.i))
{
return tree().node_at(np.i).value();
}
// Cannot capture real source_location for operator[]
check_not_full(std_transition::source_location::current());
tree().insert_new_at(np, std::move(key));
return tree().node_at(np.i).value();
}
constexpr const_iterator cbegin() const noexcept
{
return create_const_iterator(tree().index_of_min_at());
}
constexpr const_iterator cend() const noexcept { return create_const_iterator(MAXIMUM_SIZE); }
constexpr const_iterator begin() const noexcept { return cbegin(); }
constexpr iterator begin() noexcept { return create_iterator(tree().index_of_min_at()); }
constexpr const_iterator end() const noexcept { return cend(); }
constexpr iterator end() noexcept { return create_iterator(MAXIMUM_SIZE); }
constexpr reverse_iterator rbegin() noexcept { return create_reverse_iterator(MAXIMUM_SIZE); }
constexpr const_reverse_iterator rbegin() const noexcept { return crbegin(); }
constexpr const_reverse_iterator crbegin() const noexcept
{
return create_const_reverse_iterator(MAXIMUM_SIZE);
}
constexpr reverse_iterator rend() noexcept
{
return create_reverse_iterator(tree().index_of_min_at());
}
constexpr const_reverse_iterator rend() const noexcept { return crend(); }
constexpr const_reverse_iterator crend() const noexcept
{
return create_const_reverse_iterator(tree().index_of_min_at());
}
[[nodiscard]] constexpr std::size_t max_size() const noexcept { return static_max_size(); }
[[nodiscard]] constexpr std::size_t size() const noexcept { return tree().size(); }
[[nodiscard]] constexpr bool empty() const noexcept { return tree().empty(); }
constexpr void clear() noexcept { tree().clear(); }
constexpr std::pair<iterator, bool> insert(
const value_type& value,
const std_transition::source_location& loc =
std_transition::source_location::current()) noexcept
{
NodeIndexAndParentIndex np = tree().index_of_node_with_parent(value.first);
if (tree().contains_at(np.i))
{
return {create_iterator(np.i), false};
}
check_not_full(loc);
tree().insert_new_at(np, value.first, value.second);
return {create_iterator(np.i), true};
}
constexpr std::pair<iterator, bool> insert(
value_type&& value,
const std_transition::source_location& loc =
std_transition::source_location::current()) noexcept
{
NodeIndexAndParentIndex np = tree().index_of_node_with_parent(value.first);
if (tree().contains_at(np.i))
{
return {create_iterator(np.i), false};
}
check_not_full(loc);
tree().insert_new_at(np, value.first, std::move(value.second));
return {create_iterator(np.i), true};
}
template <InputIterator InputIt>
constexpr void insert(InputIt first,
InputIt last,
const std_transition::source_location& loc =
std_transition::source_location::current()) noexcept
{
for (; first != last; std::advance(first, 1))
{
this->insert(*first, loc);
}
}
constexpr void insert(std::initializer_list<value_type> list,
const std_transition::source_location& loc =
std_transition::source_location::current()) noexcept
{
this->insert(list.begin(), list.end(), loc);
}
template <class M>
constexpr std::pair<iterator, bool> insert_or_assign(
const K& key,
M&& obj,
const std_transition::source_location& loc =
std_transition::source_location::current()) noexcept
requires std::is_assignable_v<mapped_type&, M&&>
{
NodeIndexAndParentIndex np = tree().index_of_node_with_parent(key);
if (tree().contains_at(np.i))
{
tree().node_at(np.i).value() = std::forward<M>(obj);
return {create_iterator(np.i), false};
}
check_not_full(loc);
tree().insert_new_at(np, key, std::forward<M>(obj));
return {create_iterator(np.i), true};
}
template <class M>
constexpr std::pair<iterator, bool> insert_or_assign(
K&& key,
M&& obj,
const std_transition::source_location& loc =
std_transition::source_location::current()) noexcept
requires std::is_assignable_v<mapped_type&, M&&>
{
NodeIndexAndParentIndex np = tree().index_of_node_with_parent(key);
if (tree().contains_at(np.i))
{
tree().node_at(np.i).value() = std::forward<M>(obj);
return {create_iterator(np.i), false};
}
check_not_full(loc);
tree().insert_new_at(np, std::move(key), std::forward<M>(obj));
return {create_iterator(np.i), true};
}
template <class M>
constexpr iterator insert_or_assign(const_iterator /*hint*/,
const K& key,
M&& obj,
const std_transition::source_location& loc =
std_transition::source_location::current()) noexcept
requires std::is_assignable_v<mapped_type&, M&&>
{
return insert_or_assign(key, std::forward<M>(obj), loc).first;
}
template <class M>
constexpr iterator insert_or_assign(const_iterator /*hint*/,
K&& key,
M&& obj,
const std_transition::source_location& loc =
std_transition::source_location::current()) noexcept
requires std::is_assignable_v<mapped_type&, M&&>
{
return insert_or_assign(std::move(key), std::forward<M>(obj), loc).first;
}
template <class... Args>
constexpr std::pair<iterator, bool> try_emplace(const K& key, Args&&... args) noexcept
{
NodeIndexAndParentIndex np = tree().index_of_node_with_parent(key);
if (tree().contains_at(np.i))
{
return {create_iterator(np.i), false};
}
check_not_full(std_transition::source_location::current());
tree().insert_new_at(np, key, std::forward<Args>(args)...);
return {create_iterator(np.i), true};
}
template <class... Args>
constexpr std::pair<iterator, bool> try_emplace(K&& key, Args&&... args) noexcept
{
NodeIndexAndParentIndex np = tree().index_of_node_with_parent(key);
if (tree().contains_at(np.i))
{
return {create_iterator(np.i), false};
}
check_not_full(std_transition::source_location::current());
tree().insert_new_at(np, std::move(key), std::forward<Args>(args)...);
return {create_iterator(np.i), true};
}
template <class... Args>
constexpr std::pair<iterator, bool> try_emplace(const_iterator /*hint*/,
const K& key,
Args&&... args) noexcept
{
return try_emplace(key, std::forward<Args>(args)...);
}
template <class... Args>
constexpr std::pair<iterator, bool> try_emplace(const_iterator /*hint*/,
K&& key,
Args&&... args) noexcept
{
return try_emplace(std::move(key), std::forward<Args>(args)...);
}
template <class... Args>
requires(sizeof...(Args) >= 1 and sizeof...(Args) <= 3)
constexpr std::pair<iterator, bool> emplace(Args&&... args) noexcept
{
return emplace_detail::emplace_in_terms_of_try_emplace_impl(*this,
std::forward<Args>(args)...);
}
template <class... Args>
constexpr std::pair<iterator, bool> emplace_hint(const_iterator /*hint*/,
Args&&... args) noexcept
{
return emplace(std::forward<Args>(args)...);
}
constexpr iterator erase(const_iterator pos) noexcept
{
assert_or_abort(pos != cend());
const NodeIndex i = get_node_index_from_iterator(pos);
assert_or_abort(tree().contains_at(i));
const NodeIndex successor_index = tree().delete_at_and_return_successor(i);
return create_iterator(successor_index);
}
constexpr iterator erase(iterator pos) noexcept { return erase(const_iterator{pos}); }
constexpr iterator erase(const_iterator first, const_iterator last) noexcept
{
// iterators might be invalidated after every deletion, so we can't just loop through
const NodeIndex from = first == cend() ? NULL_INDEX : get_node_index_from_iterator(first);
const NodeIndex to = last == cend() ? NULL_INDEX : get_node_index_from_iterator(last);
const NodeIndex successor_index = tree().delete_range_and_return_successor(from, to);
return create_iterator(successor_index);
}
constexpr size_type erase(const K& key) noexcept { return tree().delete_node(key); }
[[nodiscard]] constexpr iterator find(const K& key) noexcept
{
const NodeIndex i = tree().index_of_node_or_null(key);
if (!tree().contains_at(i))
{
return this->end();
}
return create_iterator(i);
}
[[nodiscard]] constexpr const_iterator find(const K& key) const noexcept
{
const NodeIndex i = tree().index_of_node_or_null(key);
if (!tree().contains_at(i))
{
return this->cend();
}
return create_const_iterator(i);
}
template <class K0>
[[nodiscard]] constexpr iterator find(const K0& key) noexcept
requires IsTransparent<Compare>
{
const NodeIndex i = tree().index_of_node_or_null(key);
if (!tree().contains_at(i))
{
return this->end();
}
return create_iterator(i);
}
template <class K0>
[[nodiscard]] constexpr const_iterator find(const K0& key) const noexcept
requires IsTransparent<Compare>
{
const NodeIndex i = tree().index_of_node_or_null(key);
if (!tree().contains_at(i))
{
return this->cend();
}
return create_const_iterator(i);
}
[[nodiscard]] constexpr bool contains(const K& key) const noexcept
{
return tree().contains_node(key);
}
template <class K0>
[[nodiscard]] constexpr bool contains(const K0& key) const noexcept
requires IsTransparent<Compare>
{
return tree().contains_node(key);
}
[[nodiscard]] constexpr std::size_t count(const K& key) const noexcept
{
return static_cast<std::size_t>(contains(key));
}
template <class K0>
[[nodiscard]] constexpr std::size_t count(const K0& key) const noexcept
requires IsTransparent<Compare>
{
return static_cast<std::size_t>(contains(key));
}
[[nodiscard]] constexpr iterator lower_bound(const K& key) noexcept
{
return create_iterator(tree().index_of_node_ceiling(key));
}
[[nodiscard]] constexpr const_iterator lower_bound(const K& key) const noexcept
{
return create_const_iterator(tree().index_of_node_ceiling(key));
}
template <class K0>
[[nodiscard]] constexpr iterator lower_bound(const K0& key) noexcept
requires IsTransparent<Compare>
{
const NodeIndexAndParentIndex np = tree().index_of_node_with_parent(key);
return create_iterator(tree().index_of_node_ceiling(np));
}
template <class K0>
[[nodiscard]] constexpr const_iterator lower_bound(const K0& key) const noexcept
requires IsTransparent<Compare>
{
const NodeIndexAndParentIndex np = tree().index_of_node_with_parent(key);
return create_const_iterator(tree().index_of_node_ceiling(np));
}
[[nodiscard]] constexpr iterator upper_bound(const K& key) noexcept
{
return create_iterator(tree().index_of_node_higher(key));
}
[[nodiscard]] constexpr const_iterator upper_bound(const K& key) const noexcept
{
return create_const_iterator(tree().index_of_node_higher(key));
}
template <class K0>
[[nodiscard]] constexpr iterator upper_bound(const K0& key) noexcept
requires IsTransparent<Compare>
{
const NodeIndexAndParentIndex np = tree().index_of_node_with_parent(key);
return create_iterator(tree().index_of_node_higher(np));
}
template <class K0>
[[nodiscard]] constexpr const_iterator upper_bound(const K0& key) const noexcept
requires IsTransparent<Compare>
{
const NodeIndexAndParentIndex np = tree().index_of_node_with_parent(key);
return create_const_iterator(tree().index_of_node_higher(np));
}
[[nodiscard]] constexpr std::pair<iterator, iterator> equal_range(const K& key) noexcept
{
const NodeIndexAndParentIndex np = tree().index_of_node_with_parent(key);
return equal_range_impl(np);
}
[[nodiscard]] constexpr std::pair<const_iterator, const_iterator> equal_range(
const K& key) const noexcept
{
const NodeIndexAndParentIndex np = tree().index_of_node_with_parent(key);
return equal_range_impl(np);
}
template <class K0>
[[nodiscard]] constexpr std::pair<iterator, iterator> equal_range(const K0& key) noexcept
requires IsTransparent<Compare>
{
const NodeIndexAndParentIndex np = tree().index_of_node_with_parent(key);
return equal_range_impl(np);
}
template <class K0>
[[nodiscard]] constexpr std::pair<const_iterator, const_iterator> equal_range(
const K0& key) const noexcept
requires IsTransparent<Compare>
{
const NodeIndexAndParentIndex np = tree().index_of_node_with_parent(key);
return equal_range_impl(np);
}
template <std::size_t MAXIMUM_SIZE_2,
class Compare2,
fixed_red_black_tree_detail::RedBlackTreeNodeColorCompactness COMPACTNESS_2,
template <class /*Would be IsFixedIndexBasedStorage but gcc doesn't like the
constraints here. clang accepts it */
,
std::size_t>
typename StorageTemplate2,
customize::MapChecking<K> CheckingType2>
[[nodiscard]] constexpr bool operator==(const FixedMap<K,
V,
MAXIMUM_SIZE_2,
Compare2,
COMPACTNESS_2,
StorageTemplate2,
CheckingType2>& other) const
{
if constexpr (MAXIMUM_SIZE == MAXIMUM_SIZE_2)
{
if (this == &other)
{
return true;
}
}
return std::ranges::equal(*this, other);
}
private:
constexpr Tree& tree() { return IMPLEMENTATION_DETAIL_DO_NOT_USE_tree_; }
constexpr const Tree& tree() const { return IMPLEMENTATION_DETAIL_DO_NOT_USE_tree_; }
constexpr iterator create_iterator(const NodeIndex& start_index) noexcept
{
const NodeIndex i = replace_null_index_with_max_size_for_end_iterator(start_index);
return iterator{PairProvider<false>{std::addressof(tree()), i}};
}
constexpr const_iterator create_const_iterator(const NodeIndex& start_index) const noexcept
{
const NodeIndex i = replace_null_index_with_max_size_for_end_iterator(start_index);
return const_iterator{PairProvider<true>{std::addressof(tree()), i}};
}
constexpr reverse_iterator create_reverse_iterator(const NodeIndex& start_index) noexcept
{
return reverse_iterator{PairProvider<false>{std::addressof(tree()), start_index}};
}
constexpr const_reverse_iterator create_const_reverse_iterator(
const NodeIndex& start_index) const noexcept
{
return const_reverse_iterator{PairProvider<true>{std::addressof(tree()), start_index}};
}
constexpr void check_not_full(const std_transition::source_location& loc) const
{
if (preconditions::test(!tree().full()))
{
CheckingType::length_error(MAXIMUM_SIZE + 1, loc);
}
}
[[nodiscard]] constexpr std::pair<iterator, iterator> equal_range_impl(
const NodeIndexAndParentIndex& np) noexcept
{
const NodeIndex l = tree().index_of_node_ceiling(np);
const NodeIndex r = tree().contains_at(np.i) ? tree().index_of_successor_at(l) : l;
return {create_iterator(l), create_iterator(r)};
}
[[nodiscard]] constexpr std::pair<const_iterator, const_iterator> equal_range_impl(
const NodeIndexAndParentIndex& np) const noexcept
{
const NodeIndex l = tree().index_of_node_ceiling(np);
const NodeIndex r = tree().contains_at(np.i) ? tree().index_of_successor_at(l) : l;
return {create_const_iterator(l), create_const_iterator(r)};
}
[[nodiscard]] constexpr NodeIndex get_node_index_from_iterator(const_iterator it)
{
return it.template private_reference_provider<PairProvider<true>>().current_index();
}
};
template <class K,
class V,
std::size_t MAXIMUM_SIZE,
class Compare,
fixed_red_black_tree_detail::RedBlackTreeNodeColorCompactness COMPACTNESS,
template <class /*Would be IsFixedIndexBasedStorage but gcc doesn't like the constraints
here. clang accepts it */
,
std::size_t>
typename StorageTemplate,
customize::MapChecking<K> CheckingType>
[[nodiscard]] constexpr bool is_full(
const FixedMap<K, V, MAXIMUM_SIZE, Compare, COMPACTNESS, StorageTemplate, CheckingType>& c)
{
return c.size() >= c.max_size();
}
template <class K,
class V,
std::size_t MAXIMUM_SIZE,
class Compare,
fixed_red_black_tree_detail::RedBlackTreeNodeColorCompactness COMPACTNESS,
template <class /*Would be IsFixedIndexBasedStorage but gcc doesn't like the constraints
here. clang accepts it */
,
std::size_t>
typename StorageTemplate,
customize::MapChecking<K> CheckingType,
class Predicate>
constexpr
typename FixedMap<K, V, MAXIMUM_SIZE, Compare, COMPACTNESS, StorageTemplate, CheckingType>::
size_type
erase_if(
FixedMap<K, V, MAXIMUM_SIZE, Compare, COMPACTNESS, StorageTemplate, CheckingType>& c,
Predicate predicate)
{
return erase_if_detail::erase_if_impl(c, predicate);
}
/**
* Construct a FixedMap with its capacity being deduced from the number of key-value pairs being
* passed.
*/
template <typename K,
typename V,
typename Compare = std::less<K>,
fixed_red_black_tree_detail::RedBlackTreeNodeColorCompactness COMPACTNESS =
fixed_red_black_tree_detail::RedBlackTreeNodeColorCompactness::EMBEDDED_COLOR,
template <typename, std::size_t> typename StorageTemplate = FixedIndexBasedPoolStorage,
customize::MapChecking<K> CheckingType,
std::size_t MAXIMUM_SIZE,
// Exposing this as a template parameter is useful for customization (for example with
// child classes that set the CheckingType)
typename FixedMapType =
FixedMap<K, V, MAXIMUM_SIZE, Compare, COMPACTNESS, StorageTemplate, CheckingType>>
[[nodiscard]] constexpr FixedMapType make_fixed_map(
const std::pair<K, V> (&list)[MAXIMUM_SIZE],
const Compare& comparator = Compare{},
const std_transition::source_location& loc =
std_transition::source_location::current()) noexcept
{
return {std::begin(list), std::end(list), comparator, loc};
}
template <
typename K,
typename V,
typename Compare = std::less<K>,
fixed_red_black_tree_detail::RedBlackTreeNodeColorCompactness COMPACTNESS =
fixed_red_black_tree_detail::RedBlackTreeNodeColorCompactness::EMBEDDED_COLOR,
template <typename, std::size_t> typename StorageTemplate = FixedIndexBasedPoolStorage,
customize::MapChecking<K> CheckingType,
typename FixedMapType = FixedMap<K, V, 0, Compare, COMPACTNESS, StorageTemplate, CheckingType>>
[[nodiscard]] constexpr FixedMapType make_fixed_map(
const std::array<std::pair<K, V>, 0>& /*list*/,
const Compare& comparator = Compare{},
const std_transition::source_location& /*loc*/ =
std_transition::source_location::current()) noexcept
{
return FixedMapType{comparator};
}
template <typename K,
typename V,
typename Compare = std::less<K>,
fixed_red_black_tree_detail::RedBlackTreeNodeColorCompactness COMPACTNESS =
fixed_red_black_tree_detail::RedBlackTreeNodeColorCompactness::EMBEDDED_COLOR,
template <typename, std::size_t> typename StorageTemplate = FixedIndexBasedPoolStorage,
std::size_t MAXIMUM_SIZE>
[[nodiscard]] constexpr auto make_fixed_map(const std::pair<K, V> (&list)[MAXIMUM_SIZE],
const Compare& comparator = Compare{},
const std_transition::source_location& loc =
std_transition::source_location::current()) noexcept
{
using CheckingType = customize::MapAbortChecking<K, V, MAXIMUM_SIZE>;
using FixedMapType =
FixedMap<K, V, MAXIMUM_SIZE, Compare, COMPACTNESS, StorageTemplate, CheckingType>;
return make_fixed_map<K,
V,
Compare,
COMPACTNESS,
StorageTemplate,
CheckingType,
MAXIMUM_SIZE,
FixedMapType>(list, comparator, loc);
}
template <typename K,
typename V,
typename Compare = std::less<K>,
fixed_red_black_tree_detail::RedBlackTreeNodeColorCompactness COMPACTNESS =
fixed_red_black_tree_detail::RedBlackTreeNodeColorCompactness::EMBEDDED_COLOR,
template <typename, std::size_t> typename StorageTemplate = FixedIndexBasedPoolStorage>
[[nodiscard]] constexpr auto make_fixed_map(const std::array<std::pair<K, V>, 0>& list,
const Compare& comparator = Compare{},
const std_transition::source_location& loc =
std_transition::source_location::current()) noexcept
{
using CheckingType = customize::MapAbortChecking<K, V, 0>;
using FixedMapType = FixedMap<K, V, 0, Compare, COMPACTNESS, StorageTemplate, CheckingType>;
return make_fixed_map<K, V, Compare, COMPACTNESS, StorageTemplate, CheckingType, FixedMapType>(
list, comparator, loc);
}
} // namespace fixed_containers
// Specializations
namespace std
{
template <
typename K,
typename V,
std::size_t MAXIMUM_SIZE,
typename Compare,
fixed_containers::fixed_red_black_tree_detail::RedBlackTreeNodeColorCompactness COMPACTNESS,
template <class /*Would be IsFixedIndexBasedStorage but gcc doesn't like the constraints
here. clang accepts it */
,
std::size_t>
typename StorageTemplate,
fixed_containers::customize::MapChecking<K> CheckingType>
struct tuple_size<
fixed_containers::
FixedMap<K, V, MAXIMUM_SIZE, Compare, COMPACTNESS, StorageTemplate, CheckingType>>
: std::integral_constant<std::size_t, 0>
{
// Implicit Structured Binding due to the fields being public is disabled
};
} // namespace std