forked from KhronosGroup/Vulkan-ValidationLayers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
range_vector.h
1788 lines (1566 loc) · 77.1 KB
/
range_vector.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2019-2020 The Khronos Group Inc.
* Copyright (c) 2019-2020 Valve Corporation
* Copyright (c) 2019-2020 LunarG, Inc.
* Copyright (C) 2019-2020 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* John Zulauf <[email protected]>
*
*/
#pragma once
#ifndef RANGE_VECTOR_H_
#define RANGE_VECTOR_H_
#include <algorithm>
#include <cassert>
#include <limits>
#include <map>
#include <utility>
#include <cstdint>
#define RANGE_ASSERT(b) assert(b)
namespace sparse_container {
// range_map
//
// Implements an ordered map of non-overlapping, non-empty ranges
//
template <typename Index>
struct range {
using index_type = Index;
index_type begin; // Inclusive lower bound of range
index_type end; // Exlcusive upper bound of range
inline bool empty() const { return begin == end; }
inline bool valid() const { return begin <= end; }
inline bool invalid() const { return !valid(); }
inline bool non_empty() const { return begin < end; } // valid and !empty
inline bool is_prior_to(const range &other) const { return end == other.begin; }
inline bool is_subsequent_to(const range &other) const { return begin == other.end; }
inline bool includes(const index_type &index) const { return (begin <= index) && (index < end); }
inline bool includes(const range &other) const { return (begin <= other.begin) && (other.end <= end); }
inline bool excludes(const index_type &index) const { return (index < begin) || (end <= index); }
inline bool excludes(const range &other) const { return (other.end <= begin) || (end <= other.begin); }
inline bool intersects(const range &other) const { return includes(other.begin) || other.includes(begin); }
inline index_type distance() const { return end - begin; }
inline bool operator==(const range &rhs) const { return (begin == rhs.begin) && (end == rhs.end); }
inline bool operator!=(const range &rhs) const { return (begin != rhs.begin) || (end != rhs.end); }
inline range &operator-=(const index_type &offset) {
begin = begin - offset;
end = end - offset;
return *this;
}
inline range &operator+=(const index_type &offset) {
begin = begin + offset;
end = end + offset;
return *this;
}
inline range operator+(const index_type &offset) const { return range(begin + offset, end + offset); }
// for a reversible/transitive < operator compare first on begin and then end
// only less or begin is less or if end is less when begin is equal
bool operator<(const range &rhs) const {
bool result = false;
if (invalid()) {
// all invalid < valid, allows map/set validity check by looking at begin()->first
// all invalid are equal, thus only equal if this is invalid and rhs is valid
result = rhs.valid();
} else if (begin < rhs.begin) {
result = true;
} else if ((begin == rhs.begin) && (end < rhs.end)) {
result = true; // Simple common case -- boundary case require equality check for correctness.
}
return result;
}
// use as "strictly less/greater than" to check for non-overlapping ranges
bool strictly_less(const range &rhs) const { return end <= rhs.begin; }
bool strictly_less(const index_type &index) const { return end <= index; }
bool strictly_greater(const range &rhs) const { return rhs.end <= begin; }
bool strictly_greater(const index_type &index) const { return index < begin; }
range &operator=(const range &rhs) {
begin = rhs.begin;
end = rhs.end;
return *this;
}
range operator&(const range &rhs) const {
if (includes(rhs.begin)) {
return range(rhs.begin, std::min(end, rhs.end));
} else if (rhs.includes(begin)) {
return range(begin, std::min(end, rhs.end));
}
return range(); // Empty default range on non-intersection
}
range() : begin(), end() {}
range(const index_type &begin_, const index_type &end_) : begin(begin_), end(end_) {}
range(const range &other) : begin(other.begin), end(other.end) {}
};
template <typename Range>
class range_view {
public:
using index_type = typename Range::index_type;
class iterator {
public:
iterator &operator++() {
++current;
return *this;
}
const index_type &operator*() const { return current; }
bool operator!=(const iterator &rhs) const { return current != rhs.current; }
iterator(index_type value) : current(value) {}
private:
index_type current;
};
range_view(const Range &range) : range_(range) {}
const iterator begin() const { return iterator(range_.begin); }
const iterator end() const { return iterator(range_.end); }
private:
const Range &range_;
};
// Type parameters for the range_map(s)
struct insert_range_no_split_bounds {
const static bool split_boundaries = false;
};
struct insert_range_split_bounds {
const static bool split_boundaries = true;
};
struct split_op_keep_both {
static constexpr bool keep_lower() { return true; }
static constexpr bool keep_upper() { return true; }
};
struct split_op_keep_lower {
static constexpr bool keep_lower() { return true; }
static constexpr bool keep_upper() { return false; }
};
struct split_op_keep_upper {
static constexpr bool keep_lower() { return false; }
static constexpr bool keep_upper() { return true; }
};
enum class value_precedence { prefer_source, prefer_dest };
// The range based sparse map implemented on the ImplMap
template <typename Key, typename T, typename RangeKey = range<Key>, typename ImplMap = std::map<RangeKey, T>>
class range_map {
public:
protected:
using MapKey = RangeKey;
ImplMap impl_map_;
using ImplIterator = typename ImplMap::iterator;
using ImplConstIterator = typename ImplMap::const_iterator;
public:
using mapped_type = typename ImplMap::mapped_type;
using value_type = typename ImplMap::value_type;
using key_type = typename ImplMap::key_type;
using index_type = typename key_type::index_type;
protected:
template <typename ThisType>
using ConstCorrectImplIterator = decltype(std::declval<ThisType>().impl_begin());
template <typename ThisType, typename WrappedIterator = ConstCorrectImplIterator<ThisType>>
static WrappedIterator lower_bound_impl(ThisType &that, const key_type &key) {
if (key.valid()) {
// ImplMap doesn't give us what want with a direct query, it will give us the first entry contained (if any) in key,
// not the first entry intersecting key, so, first look for the the first entry that starts at or after key.begin
// with the operator > in range, we can safely use an empty range for comparison
auto lower = that.impl_map_.lower_bound(key_type(key.begin, key.begin));
// If there is a preceding entry it's possible that begin is included, as all we know is that lower.begin >= key.begin
// or lower is at end
if (!that.at_impl_begin(lower)) {
auto prev = lower;
--prev;
// If the previous entry includes begin (and we know key.begin > prev.begin) then prev is actually lower
if (key.begin < prev->first.end) {
lower = prev;
}
}
return lower;
}
// Key is ill-formed
return that.impl_end(); // Point safely to nothing.
}
ImplIterator lower_bound_impl(const key_type &key) { return lower_bound_impl(*this, key); }
ImplConstIterator lower_bound_impl(const key_type &key) const { return lower_bound_impl(*this, key); }
template <typename ThisType, typename WrappedIterator = ConstCorrectImplIterator<ThisType>>
static WrappedIterator upper_bound_impl(ThisType &that, const key_type &key) {
if (key.valid()) {
// the upper bound is the first range that is full greater (upper.begin >= key.end
// we can get close by looking for the first to exclude key.end, then adjust to account for the fact that key.end is
// exclusive and we thus ImplMap::upper_bound may be off by one here, i.e. the previous may be the upper bound
auto upper = that.impl_map_.upper_bound(key_type(key.end, key.end));
if (!that.at_impl_end(upper) && (upper != that.impl_begin())) {
auto prev = upper;
--prev;
// We know key.end is >= prev.begin, the only question is whether it's ==
if (prev->first.begin == key.end) {
upper = prev;
}
}
return upper;
}
return that.impl_end(); // Point safely to nothing.
}
ImplIterator upper_bound_impl(const key_type &key) { return upper_bound_impl(*this, key); }
ImplConstIterator upper_bound_impl(const key_type &key) const { return upper_bound_impl(*this, key); }
ImplIterator impl_find(const key_type &key) { return impl_map_.find(key); }
ImplConstIterator impl_find(const key_type &key) const { return impl_map_.find(key); }
bool impl_not_found(const key_type &key) const { return impl_end() == impl_find(key); }
ImplIterator impl_end() { return impl_map_.end(); }
ImplConstIterator impl_end() const { return impl_map_.end(); }
ImplIterator impl_begin() { return impl_map_.begin(); }
ImplConstIterator impl_begin() const { return impl_map_.begin(); }
inline bool at_impl_end(const ImplIterator &pos) { return pos == impl_end(); }
inline bool at_impl_end(const ImplConstIterator &pos) const { return pos == impl_end(); }
inline bool at_impl_begin(const ImplIterator &pos) { return pos == impl_begin(); }
inline bool at_impl_begin(const ImplConstIterator &pos) const { return pos == impl_begin(); }
ImplIterator impl_erase(const ImplIterator &pos) { return impl_map_.erase(pos); }
template <typename Value>
ImplIterator impl_insert(const ImplIterator &hint, Value &&value) {
RANGE_ASSERT(impl_not_found(value.first));
RANGE_ASSERT(value.first.non_empty());
return impl_map_.emplace_hint(hint, std::forward<Value>(value));
}
ImplIterator impl_insert(const ImplIterator &hint, const key_type &key, const mapped_type &value) {
return impl_insert(hint, std::make_pair(key, value));
}
ImplIterator impl_insert(const ImplIterator &hint, const index_type &begin, const index_type &end, const mapped_type &value) {
return impl_insert(hint, key_type(begin, end), value);
}
template <typename SplitOp>
ImplIterator split_impl(const ImplIterator &split_it, const index_type &index, const SplitOp &) {
// Make sure contains the split point
if (!split_it->first.includes(index)) return split_it; // If we don't have a valid split point, just return the iterator
const auto range = split_it->first;
key_type lower_range(range.begin, index);
if (lower_range.empty() && SplitOp::keep_upper()) {
return split_it; // this is a noop we're keeping the upper half which is the same as split_it;
}
// Save the contents of it and erase it
auto value = std::move(split_it->second);
auto next_it = impl_map_.erase(split_it); // Keep this, just in case the split point results in an empty "keep" set
if (lower_range.empty() && !SplitOp::keep_upper()) {
// This effectively an erase...
return next_it;
}
// Upper range cannot be empty
key_type upper_range(index, range.end);
key_type move_range;
key_type copy_range;
// Were either going to keep one or both of the split pieces. If we keep both, we'll copy value to the upper,
// and move to the lower, and return the lower, else move to, and return the kept one.
if (SplitOp::keep_lower() && !lower_range.empty()) {
move_range = lower_range;
if (SplitOp::keep_upper()) {
copy_range = upper_range; // only need a valid copy range if we keep both.
}
} else if (SplitOp::keep_upper()) { // We're not keeping the lower split because it's either empty or not wanted
move_range = upper_range; // this will be non_empty as index is included ( < end) in the original range)
}
// we insert from upper to lower because that's what emplace_hint can do in constant time. (not log time in C++11)
if (!copy_range.empty()) {
// We have a second range to create, so do it by copy
RANGE_ASSERT(impl_map_.find(copy_range) == impl_map_.end());
next_it = impl_map_.emplace_hint(next_it, std::make_pair(copy_range, value));
}
if (!move_range.empty()) {
// Whether we keep one or both, the one we return gets value moved to it, as the other one already has a copy
RANGE_ASSERT(impl_map_.find(move_range) == impl_map_.end());
next_it = impl_map_.emplace_hint(next_it, std::make_pair(move_range, std::move(value)));
}
// point to the beginning of the inserted elements (or the next from the erase
return next_it;
}
// do an ranged insert that splits existing ranges at the boundaries, and writes value to any non-initialized sub-ranges
range<ImplIterator> infill_and_split(const key_type &bounds, const mapped_type &value, ImplIterator lower, bool split_bounds) {
auto pos = lower;
if (at_impl_end(pos)) return range<ImplIterator>(pos, pos); // defensive...
// Logic assumes we are starting at lower bound
RANGE_ASSERT(lower == lower_bound_impl(bounds));
// Trim/infil the beginning if needed
const auto first_begin = pos->first.begin;
if (bounds.begin > first_begin && split_bounds) {
pos = split_impl(pos, bounds.begin, split_op_keep_both());
lower = pos;
++lower;
RANGE_ASSERT(lower == lower_bound_impl(bounds));
} else if (bounds.begin < first_begin) {
pos = impl_insert(pos, bounds.begin, first_begin, value);
lower = pos;
RANGE_ASSERT(lower == lower_bound_impl(bounds));
}
// in the trim case pos starts one before lower_bound, but that allows trimming a single entry range in loop.
// NOTE that the loop is trimming and infilling at pos + 1
while (!at_impl_end(pos) && pos->first.begin < bounds.end) {
auto last_end = pos->first.end;
// check for in-fill
++pos;
if (at_impl_end(pos)) {
if (last_end < bounds.end) {
// Gap after last entry in impl_map and before end,
pos = impl_insert(pos, last_end, bounds.end, value);
++pos; // advances to impl_end, as we're at upper boundary
RANGE_ASSERT(at_impl_end(pos));
}
} else if (pos->first.begin != last_end) {
// we have a gap between last entry and current... fill, but not beyond bounds
if (bounds.includes(pos->first.begin)) {
pos = impl_insert(pos, last_end, pos->first.begin, value);
// don't further advance pos, because we may need to split the next entry and thus can't skip it.
} else if (last_end < bounds.end) {
// Non-zero length final gap in-bounds
pos = impl_insert(pos, last_end, bounds.end, value);
++pos; // advances back to the out of bounds entry which we inserted just before
RANGE_ASSERT(!bounds.includes(pos->first.begin));
}
} else if (pos->first.includes(bounds.end)) {
if (split_bounds) {
// extends past the end of the bounds range, snip to only include the bounded section
// NOTE: this splits pos, but the upper half of the split should now be considered upper_bound
// for the range
pos = split_impl(pos, bounds.end, split_op_keep_both());
}
// advance to the upper haf of the split which will be upper_bound or to next which will both be out of bounds
++pos;
RANGE_ASSERT(!bounds.includes(pos->first.begin));
}
}
// Return the current position which should be the upper_bound for bounds
RANGE_ASSERT(pos == upper_bound_impl(bounds));
return range<ImplIterator>(lower, pos);
}
ImplIterator impl_erase_range(const key_type &bounds, ImplIterator lower) {
// Logic assumes we are starting at a valid lower bound
RANGE_ASSERT(!at_impl_end(lower));
RANGE_ASSERT(lower == lower_bound_impl(bounds));
// Trim/infil the beginning if needed
auto current = lower;
const auto first_begin = current->first.begin;
if (bounds.begin > first_begin) {
// Preserve the portion of lower bound excluded from bounds
if (current->first.end <= bounds.end) {
// If current ends within the erased bound we can discard the the upper portion of current
current = split_impl(current, bounds.begin, split_op_keep_lower());
} else {
// Keep the upper portion of current for the later split below
current = split_impl(current, bounds.begin, split_op_keep_both());
}
// Exclude the preserved portion
++current;
RANGE_ASSERT(current == lower_bound_impl(bounds));
}
// Loop over completely contained entries and erase them
while (!at_impl_end(current) && (current->first.end <= bounds.end)) {
current = impl_erase(current);
}
if (!at_impl_end(current) && current->first.includes(bounds.end)) {
// last entry extends past the end of the bounds range, snip to only erase the bounded section
current = split_impl(current, bounds.end, split_op_keep_upper());
}
RANGE_ASSERT(current == upper_bound_impl(bounds));
return current;
}
template <typename ValueType, typename WrappedIterator_>
struct iterator_impl {
public:
friend class range_map;
using WrappedIterator = WrappedIterator_;
private:
WrappedIterator pos_;
// Create an iterator at a specific internal state -- only from the parent container
iterator_impl(const WrappedIterator &pos) : pos_(pos) {}
public:
iterator_impl() : iterator_impl(WrappedIterator()){};
iterator_impl(const iterator_impl &other) : pos_(other.pos_){};
iterator_impl &operator=(const iterator_impl &rhs) {
pos_ = rhs.pos_;
return *this;
}
inline bool operator==(const iterator_impl &rhs) const { return pos_ == rhs.pos_; }
inline bool operator!=(const iterator_impl &rhs) const { return pos_ != rhs.pos_; }
ValueType &operator*() const { return *pos_; }
ValueType *operator->() const { return &*pos_; }
iterator_impl &operator++() {
++pos_;
return *this;
}
iterator_impl &operator--() {
--pos_;
return *this;
}
// To allow for iterator -> const_iterator construction
// NOTE: while it breaks strict encapsulation, it does so less than friend
const WrappedIterator &get_pos() const { return pos_; };
};
public:
using iterator = iterator_impl<value_type, ImplIterator>;
// The const iterator must be derived to allow the conversion from iterator, which iterator doesn't support
class const_iterator : public iterator_impl<const value_type, ImplConstIterator> {
using Base = iterator_impl<const value_type, ImplConstIterator>;
friend range_map;
public:
const_iterator &operator=(const const_iterator &other) {
Base::operator=(other);
return *this;
}
const_iterator(const const_iterator &other) : Base(other){};
const_iterator(const iterator &it) : Base(ImplConstIterator(it.get_pos())) {}
const_iterator() : Base() {}
private:
const_iterator(const ImplConstIterator &pos) : Base(pos) {}
};
protected:
inline bool at_end(const iterator &it) { return at_impl_end(it.pos_); }
inline bool at_end(const const_iterator &it) const { return at_impl_end(it.pos_); }
inline bool at_begin(const iterator &it) { return at_impl_begin(it.pos_); }
template <typename That, typename Iterator>
static bool is_contiguous_impl(That *const that, const key_type &range, const Iterator &lower) {
// Search range or intersection is empty
if (lower == that->impl_end() || lower->first.excludes(range)) return false;
if (lower->first.includes(range)) {
return true; // there is one entry that contains the whole key range
}
bool contiguous = true;
for (auto pos = lower; contiguous && pos != that->impl_end() && range.includes(pos->first.begin); ++pos) {
// if current doesn't cover the rest of the key range, check to see that the next is extant and abuts
if (pos->first.end < range.end) {
auto next = pos;
++next;
contiguous = (next != that->impl_end()) && pos->first.is_prior_to(next->first);
}
}
return contiguous;
}
public:
iterator end() { return iterator(impl_map_.end()); } // policy and bounds don't matter for end
const_iterator end() const { return const_iterator(impl_map_.end()); } // policy and bounds don't matter for end
iterator begin() { return iterator(impl_map_.begin()); } // with default policy, and thus no bounds
const_iterator begin() const { return const_iterator(impl_map_.begin()); } // with default policy, and thus no bounds
const_iterator cbegin() const { return const_iterator(impl_map_.cbegin()); } // with default policy, and thus no bounds
const_iterator cend() const { return const_iterator(impl_map_.cend()); } // with default policy, and thus no bounds
iterator erase(const iterator &pos) {
RANGE_ASSERT(!at_end(pos));
return iterator(impl_erase(pos.pos_));
}
iterator erase(range<iterator> bounds) {
auto current = bounds.begin.pos_;
while (current != bounds.end.pos_) {
RANGE_ASSERT(!at_impl_end(current));
current = impl_map_.erase(current);
}
RANGE_ASSERT(current == bounds.end.pos_);
return current;
}
iterator erase(iterator first, iterator last) { return erase(range<iterator>(first, last)); }
iterator erase_range(const key_type &bounds) {
auto lower = lower_bound_impl(bounds);
if (at_impl_end(lower) || !bounds.intersects(lower->first)) {
// There is nothing in this range lower bound is above bound
return iterator(lower);
}
auto next = impl_erase_range(bounds, lower);
return iterator(next);
}
void clear() { impl_map_.clear(); }
iterator find(const key_type &key) { return iterator(impl_map_.find(key)); }
const_iterator find(const key_type &key) const { return const_iterator(impl_map_.find(key)); }
iterator find(const index_type &index) {
auto lower = lower_bound(range<index_type>(index, index + 1));
if (!at_end(lower) && lower->first.includes(index)) {
return lower;
}
return end();
}
const_iterator find(const index_type &index) const {
auto lower = lower_bound(key_type(index, index + 1));
if (!at_end(lower) && lower->first.includes(index)) {
return lower;
}
return end();
}
iterator lower_bound(const key_type &key) { return iterator(lower_bound_impl(key)); }
const_iterator lower_bound(const key_type &key) const { return const_iterator(lower_bound_impl(key)); }
iterator upper_bound(const key_type &key) { return iterator(upper_bound_impl(key)); }
const_iterator upper_bound(const key_type &key) const { return const_iterator(upper_bound_impl(key)); }
range<iterator> bounds(const key_type &key) { return {lower_bound(key), upper_bound(key)}; }
range<const_iterator> cbounds(const key_type &key) const { return {lower_bound(key), upper_bound(key)}; }
range<const_iterator> bounds(const key_type &key) const { return cbounds(key); }
using insert_pair = std::pair<iterator, bool>;
// This is traditional no replacement insert.
insert_pair insert(const value_type &value) {
const auto &key = value.first;
if (!key.non_empty()) {
// It's an invalid key, early bail pointing to end
return std::make_pair(end(), false);
}
// Look for range conflicts (and an insertion point, which makes the lower_bound *not* wasted work)
// we don't have to check upper if just check that lower doesn't intersect (which it would if lower != upper)
auto lower = lower_bound_impl(key);
if (at_impl_end(lower) || !lower->first.intersects(key)) {
// range is not even paritally overlapped, and lower is strictly > than key
auto impl_insert = impl_map_.emplace_hint(lower, value);
// auto impl_insert = impl_map_.emplace(value);
iterator wrap_it(impl_insert);
return std::make_pair(wrap_it, true);
}
// We don't replace
return std::make_pair(iterator(lower), false);
};
iterator insert(const_iterator hint, const value_type &value) {
bool hint_open;
ImplConstIterator impl_next = hint.pos_;
if (impl_map_.empty()) {
hint_open = true;
} else if (impl_next == impl_map_.cbegin()) {
hint_open = value.first.strictly_less(impl_next->first);
} else if (impl_next == impl_map_.cend()) {
auto impl_prev = impl_next;
--impl_prev;
hint_open = value.first.strictly_greater(impl_prev->first);
} else {
auto impl_prev = impl_next;
--impl_prev;
hint_open = value.first.strictly_greater(impl_prev->first) && value.first.strictly_less(impl_next->first);
}
if (!hint_open) {
// Hint was unhelpful, fall back to the non-hinted version
auto plain_insert = insert(value);
return plain_insert.first;
}
auto impl_insert = impl_map_.insert(impl_next, value);
return iterator(impl_insert);
}
template <typename SplitOp>
iterator split(const iterator whole_it, const index_type &index, const SplitOp &split_op) {
auto split_it = split_impl(whole_it.pos_, index, split_op);
return iterator(split_it);
}
// The overwrite hint here is lower.... and if it's not right... this fails
template <typename Value>
iterator overwrite_range(const iterator &lower, Value &&value) {
// We're not robust to a bad hint, so detect it with extreme prejudice
// TODO: Add bad hint test to make this robust...
auto lower_impl = lower.pos_;
auto insert_hint = lower_impl;
if (!at_impl_end(lower_impl)) {
// If we're at end (and the hint is good, there's nothing to erase
RANGE_ASSERT(lower == lower_bound(value.first));
insert_hint = impl_erase_range(value.first, lower_impl);
}
auto inserted = impl_insert(insert_hint, std::forward<Value>(value));
return iterator(inserted);
}
template <typename Value>
iterator overwrite_range(Value &&value) {
auto lower = lower_bound(value.first);
return overwrite_range(lower, value);
}
bool empty() const { return impl_map_.empty(); }
size_t size() const { return impl_map_.size(); }
// For configuration/debug use // Use with caution...
ImplMap &get_implementation_map() { return impl_map_; }
const ImplMap &get_implementation_map() const { return impl_map_; }
};
template <typename Container>
using const_correct_iterator = decltype(std::declval<Container>().begin());
// The an array based small ordered map for range keys for use as the range map "ImplMap" as an alternate to std::map
//
// Assumes RangeKey::index_type is unsigned (TBD is it useful to generalize to unsigned?)
// Assumes RangeKey implements begin, end, < and (TBD) from template range above
template <typename Key, typename T, typename RangeKey = range<Key>, size_t N = 64, typename SmallIndex = uint8_t>
class small_range_map {
using SmallRange = range<SmallIndex>;
public:
using mapped_type = T;
using key_type = RangeKey;
using value_type = std::pair<const key_type, mapped_type>;
using index_type = typename key_type::index_type;
using size_type = SmallIndex;
template <typename Map_, typename Value_>
struct IteratorImpl {
public:
using Map = Map_;
using Value = Value_;
friend Map;
Value *operator->() const { return map_->get_value(pos_); }
Value &operator*() const { return *(map_->get_value(pos_)); }
IteratorImpl &operator++() {
pos_ = map_->next_range(pos_);
return *this;
}
IteratorImpl &operator--() {
pos_ = map_->prev_range(pos_);
return *this;
}
IteratorImpl &operator=(const IteratorImpl &other) {
map_ = other.map_;
pos_ = other.pos_;
return *this;
}
bool operator==(const IteratorImpl &other) const {
if (at_end() && other.at_end()) {
return true; // all ends are equal
}
return (map_ == other.map_) && (pos_ == other.pos_);
}
bool operator!=(const IteratorImpl &other) const { return !(*this == other); }
// At end()
IteratorImpl() : map_(nullptr), pos_(N) {}
IteratorImpl(const IteratorImpl &other) : map_(other.map_), pos_(other.pos_) {}
// Raw getters to allow for const_iterator conversion below
Map *get_map() const { return map_; }
SmallIndex get_pos() const { return pos_; }
bool at_end() const { return (map_ == nullptr) || (pos_ >= map_->get_limit()); }
protected:
IteratorImpl(Map *map, SmallIndex pos) : map_(map), pos_(pos) {}
private:
Map *map_;
SmallIndex pos_; // the begin of the current small_range
};
using iterator = IteratorImpl<small_range_map, value_type>;
// The const iterator must be derived to allow the conversion from iterator, which iterator doesn't support
class const_iterator : public IteratorImpl<const small_range_map, const value_type> {
using Base = IteratorImpl<const small_range_map, const value_type>;
friend small_range_map;
public:
const_iterator(const iterator &it) : Base(it.get_map(), it.get_pos()) {}
const_iterator() : Base() {}
private:
const_iterator(const small_range_map *map, SmallIndex pos) : Base(map, pos) {}
};
iterator begin() {
// Either ranges of 0 is valid and begin is 0 and begin *or* it's invalid an points to the first valid range (or end)
return iterator(this, ranges_[0].begin);
}
const_iterator cbegin() const { return const_iterator(this, ranges_[0].begin); }
const_iterator begin() const { return cbegin(); }
iterator end() { return iterator(); }
const_iterator cend() const { return const_iterator(); }
const_iterator end() const { return cend(); }
void clear() {
const SmallRange clear_range(limit_, 0);
for (SmallIndex i = 0; i < limit_; ++i) {
auto &range = ranges_[i];
if (range.begin == i) {
// Clean up the backing store
destruct_value(i);
}
range = clear_range;
}
size_ = 0;
}
// Find entry with an exact key match (uncommon use case)
iterator find(const key_type &key) {
RANGE_ASSERT(in_bounds(key));
if (key.begin < limit_) {
const SmallIndex small_begin = static_cast<SmallIndex>(key.begin);
const auto &range = ranges_[small_begin];
if (range.begin == small_begin) {
const auto small_end = static_cast<SmallIndex>(key.end);
if (range.end == small_end) return iterator(this, small_begin);
}
}
return end();
}
const_iterator find(const key_type &key) const {
RANGE_ASSERT(in_bounds(key));
if (key.begin < limit_) {
const SmallIndex small_begin = static_cast<SmallIndex>(key.begin);
const auto &range = ranges_[small_begin];
if (range.begin == small_begin) {
const auto small_end = static_cast<SmallIndex>(key.end);
if (range.end == small_end) return const_iterator(this, small_begin);
}
}
return end();
}
iterator find(const index_type &index) {
if (index < get_limit()) {
const SmallIndex small_index = static_cast<SmallIndex>(index);
const auto &range = ranges_[small_index];
if (range.valid()) {
return iterator(this, range.begin);
}
}
return end();
}
const_iterator find(const index_type &index) const {
if (index < get_limit()) {
const SmallIndex small_index = static_cast<SmallIndex>(index);
const auto &range = ranges_[small_index];
if (range.valid()) {
return const_iterator(this, range.begin);
}
}
return end();
}
size_type size() const { return size_; }
bool empty() const { return 0 == size_; }
iterator erase(const_iterator pos) {
RANGE_ASSERT(pos.map_ == this);
return erase_impl(pos.get_pos());
}
iterator erase(iterator pos) {
RANGE_ASSERT(pos.map_ == this);
return erase_impl(pos.get_pos());
}
// Must be called with rvalue or lvalue of value_type
template <typename Value>
iterator emplace(Value &&value) {
const auto &key = value.first;
RANGE_ASSERT(in_bounds(key));
if (key.begin >= limit_) return end(); // Invalid key (end is checked in "is_open")
const SmallRange range(static_cast<SmallIndex>(key.begin), static_cast<SmallIndex>(key.end));
if (is_open(key)) {
// This needs to be the fast path, but I don't see how we can avoid the sanity checks above
for (auto i = range.begin; i < range.end; ++i) {
ranges_[i] = range;
}
// Update the next information for the previous unused slots (as stored in begin invalidly)
auto prev = range.begin;
while (prev > 0) {
--prev;
if (ranges_[prev].valid()) break;
ranges_[prev].begin = range.begin;
}
// Placement new into the storage interpreted as Value
construct_value(range.begin, value_type(std::forward<Value>(value)));
auto next = range.end;
// update the previous range information for the next unsed slots (as stored in end invalidly)
while (next < limit_) {
// End is exclusive... increment *after* update
if (ranges_[next].valid()) break;
ranges_[next].end = range.end;
++next;
}
return iterator(this, range.begin);
} else {
// Can't insert into occupied ranges.
// if ranges_[key.begin] is valid then this is the collision (starting at .begin
// if it's invalid .begin points to the overlapping entry from is_open (or end if key was out of range)
return iterator(this, ranges_[range.begin].begin);
}
}
// As hint is going to be ignored, make it as lightweight as possible, by reference and no conversion construction
template <typename Value>
iterator emplace_hint(const const_iterator &hint, Value &&value) {
// We have direct access so we can drop the hint
return emplace(std::forward<Value>(value));
}
template <typename Value>
iterator emplace_hint(const iterator &hint, Value &&value) {
// We have direct access so we can drop the hint
return emplace(std::forward<Value>(value));
}
// Again, hint is going to be ignored, make it as lightweight as possible, by reference and no conversion construction
iterator insert(const const_iterator &hint, const value_type &value) { return emplace(value); }
iterator insert(const iterator &hint, const value_type &value) { return emplace(value); }
std::pair<iterator, bool> insert(const value_type &value) {
const auto &key = value.first;
RANGE_ASSERT(in_bounds(key));
if (key.begin >= limit_) return std::make_pair(end(), false); // Invalid key, not inserted.
if (is_open(key)) {
return std::make_pair(emplace(value), true);
}
// If invalid we point to the subsequent range that collided, if valid begin is the start of the valid range
const auto &collision_begin = ranges_[key.begin].begin;
RANGE_ASSERT(ranges_[collision_begin].valid());
return std::make_pair(iterator(this, collision_begin), false);
}
template <typename SplitOp>
iterator split(const iterator whole_it, const index_type &index, const SplitOp &split_op) {
if (!whole_it->first.includes(index)) return whole_it; // If we don't have a valid split point, just return the iterator
const auto &key = whole_it->first;
const auto small_key = make_small_range(key);
key_type lower_key(key.begin, index);
if (lower_key.empty() && SplitOp::keep_upper()) {
return whole_it; // this is a noop we're keeping the upper half which is the same as whole_it;
}
if ((lower_key.empty() && !SplitOp::keep_upper()) || !(SplitOp::keep_lower() || SplitOp::keep_upper())) {
// This effectively an erase... so erase.
return erase(whole_it);
}
// Upper range cannot be empty (because the split point would be included...
const auto small_lower_key = make_small_range(lower_key);
const SmallRange small_upper_key{small_lower_key.end, small_key.end};
if (SplitOp::keep_upper()) {
// Note: create the upper section before the lower, as processing the lower may erase it
RANGE_ASSERT(!small_upper_key.empty());
const key_type upper_key{lower_key.end, key.end};
if (SplitOp::keep_lower()) {
construct_value(small_upper_key.begin, std::make_pair(upper_key, get_value(small_key.begin)->second));
} else {
// If we aren't keeping the lower, move instead of copy
construct_value(small_upper_key.begin, std::make_pair(upper_key, std::move(get_value(small_key.begin)->second)));
}
for (auto i = small_upper_key.begin; i < small_upper_key.end; ++i) {
ranges_[i] = small_upper_key;
}
} else {
// rewrite "end" to the next valid range (or end)
RANGE_ASSERT(SplitOp::keep_lower());
auto next = next_range(small_key.begin);
rerange(small_upper_key, SmallRange(next, small_lower_key.end));
// for any already invalid, we just rewrite the end.
rerange_end(small_upper_key.end, next, small_lower_key.end);
}
SmallIndex split_index;
if (SplitOp::keep_lower()) {
resize_value(small_key.begin, lower_key.end);
rerange_end(small_lower_key.begin, small_lower_key.end, small_lower_key.end);
split_index = small_lower_key.begin;
} else {
// Remove lower and rewrite empty space
RANGE_ASSERT(SplitOp::keep_upper());
destruct_value(small_key.begin);
// Rewrite prior empty space (if any)
auto prev = prev_range(small_key.begin);
SmallIndex limit = small_lower_key.end;
SmallIndex start = 0;
if (small_key.begin != 0) {
const auto &prev_start = ranges_[prev];
if (prev_start.valid()) {
// If there is a previous used range, the empty space starts after it.
start = prev_start.end;
} else {
RANGE_ASSERT(prev == 0); // prev_range only returns invalid ranges "off the front"
start = prev;
}
// for the section *prior* to key begin only need to rewrite the "invalid" begin (i.e. next "in use" begin)
rerange_begin(start, small_lower_key.begin, limit);
}
// for the section being erased rewrite the invalid range reflecting the empty space
rerange(small_lower_key, SmallRange(limit, start));
split_index = small_lower_key.end;
}
return iterator(this, split_index);
}
// For the value.first range rewrite the range...
template <typename Value>
iterator overwrite_range(Value &&value) {
const auto &key = value.first;
// Small map only has a restricted range supported
RANGE_ASSERT(in_bounds(key));
if (key.end > get_limit()) {
return end();
}
const auto small_key = make_small_range(key);
clear_out_range(small_key, /* valid clear range */ true);
construct_value(small_key.begin, std::forward<Value>(value));
return iterator(this, small_key.begin);
}
// We don't need a hint...
template <typename Value>
iterator overwrite_range(const iterator &hint, Value &&value) {
return overwrite_range(std::forward<Value>(value));
}
// For the range erase all contents within range, trimming any overlapping ranges
iterator erase_range(const key_type &range) {
// Small map only has a restricted range supported