blob: b532d9e0eeb83c69c2b004d2eec2de01939ba756 [file] [log] [blame]
John Zulauf11211402019-11-15 14:02:36 -07001/* Copyright (c) 2019 The Khronos Group Inc.
2 * Copyright (c) 2019 Valve Corporation
3 * Copyright (c) 2019 LunarG, Inc.
4 * Copyright (C) 2019 Google Inc.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * John Zulauf <jzulauf@lunarg.com>
19 *
20 */
21#pragma once
22
23#ifndef RANGE_VECTOR_H_
24#define RANGE_VECTOR_H_
25
26#include <algorithm>
27#include <cassert>
John Zulauf81408f12019-11-27 16:40:27 -070028#include <limits>
John Zulauf11211402019-11-15 14:02:36 -070029#include <map>
30#include <utility>
31
32#define RANGE_ASSERT(b) assert(b)
33
34namespace sparse_container {
35// range_map
36//
37// Implements an ordered map of non-overlapping, non-empty ranges
38//
39template <typename Index>
40struct range {
41 using index_type = Index;
42 index_type begin; // Inclusive lower bound of range
43 index_type end; // Exlcusive upper bound of range
44
45 inline bool empty() const { return begin == end; }
46 inline bool valid() const { return begin <= end; }
47 inline bool invalid() const { return !valid(); }
48 inline bool non_empty() const { return begin < end; } // valid and !empty
49
50 inline bool is_prior_to(const range &other) const { return end == other.begin; }
51 inline bool is_subsequent_to(const range &other) const { return begin == other.end; }
52 inline bool includes(const index_type &index) const { return (begin <= index) && (index < end); }
53 inline bool includes(const range &other) const { return (begin <= other.begin) && (other.end <= end); }
54 inline bool excludes(const index_type &index) const { return (index < begin) || (end <= index); }
55 inline bool excludes(const range &other) const { return (other.end <= begin) || (end <= other.begin); }
56 inline bool intersects(const range &other) const { return includes(other.begin) || other.includes(begin); }
57 inline index_type distance() const { return end - begin; }
58
59 inline bool operator==(const range &rhs) const { return (begin == rhs.begin) && (end == rhs.end); }
60 inline bool operator!=(const range &rhs) const { return (begin != rhs.begin) || (end != rhs.end); }
61
62 inline range &operator-=(const index_type &offset) {
63 begin = begin - offset;
64 end = end - offset;
65 return *this;
66 }
67
68 inline range &operator+=(const index_type &offset) {
69 begin = begin + offset;
70 end = end + offset;
71 return *this;
72 }
73
74 // for a reversible/transitive < operator compare first on begin and then end
75 // only less or begin is less or if end is less when begin is equal
76 bool operator<(const range &rhs) const {
77 bool result = false;
78 if (invalid()) {
79 // all invalid < valid, allows map/set validity check by looking at begin()->first
80 // all invalid are equal, thus only equal if this is invalid and rhs is valid
81 result = rhs.valid();
82 } else if (begin < rhs.begin) {
83 result = true;
84 } else if ((begin == rhs.begin) && (end < rhs.end)) {
85 result = true; // Simple common case -- boundary case require equality check for correctness.
86 }
87 return result;
88 }
89
90 // use as "strictly less/greater than" to check for non-overlapping ranges
91 bool strictly_less(const range &rhs) const { return end <= rhs.begin; }
92 bool strictly_less(const index_type &index) const { return end <= index; }
93 bool strictly_greater(const range &rhs) const { return rhs.end <= begin; }
94 bool strictly_greater(const index_type &index) const { return index < begin; }
95
96 range &operator=(const range &rhs) {
97 begin = rhs.begin;
98 end = rhs.end;
99 return *this;
100 }
101
102 range operator&(const range &rhs) const {
103 if (includes(rhs.begin)) {
104 return range(rhs.begin, std::min(end, rhs.end));
105 } else if (rhs.includes(begin)) {
106 return range(begin, std::min(end, rhs.end));
107 }
108 return range(); // Empty default range on non-intersection
109 }
110
111 range() : begin(), end() {}
112 range(const index_type &begin_, const index_type &end_) : begin(begin_), end(end_) {}
113};
114
115template <typename Container>
116using const_correct_iterator = decltype(std::declval<Container>().begin());
117
John Zulauf81408f12019-11-27 16:40:27 -0700118// Type parameters for the range_map(s)
119struct insert_range_no_split_bounds {
120 const static bool split_boundaries = false;
121};
122
123struct insert_range_split_bounds {
124 const static bool split_boundaries = true;
125};
126
127struct split_op_keep_both {
128 static constexpr bool keep_lower() { return true; }
129 static constexpr bool keep_upper() { return true; }
130};
131
132struct split_op_keep_lower {
133 static constexpr bool keep_lower() { return true; }
134 static constexpr bool keep_upper() { return false; }
135};
136
137struct split_op_keep_upper {
138 static constexpr bool keep_lower() { return false; }
139 static constexpr bool keep_upper() { return true; }
140};
141
142enum class value_precedence { prefer_source, prefer_dest };
143
144// The range based sparse map implemented on the ImplMap
145template <typename Key, typename T, typename RangeKey = range<Key>, typename ImplMap = std::map<RangeKey, T>>
John Zulauf11211402019-11-15 14:02:36 -0700146class range_map {
147 public:
148 protected:
John Zulauf11211402019-11-15 14:02:36 -0700149 using MapKey = RangeKey;
John Zulauf11211402019-11-15 14:02:36 -0700150 ImplMap impl_map_;
151 using ImplIterator = typename ImplMap::iterator;
152 using ImplConstIterator = typename ImplMap::const_iterator;
153
154 public:
155 using mapped_type = typename ImplMap::mapped_type;
156 using value_type = typename ImplMap::value_type;
157 using key_type = typename ImplMap::key_type;
158 using index_type = typename key_type::index_type;
159
John Zulauf11211402019-11-15 14:02:36 -0700160 protected:
161 template <typename ThisType>
162 using ConstCorrectImplIterator = decltype(std::declval<ThisType>().impl_begin());
163
164 template <typename ThisType, typename WrappedIterator = ConstCorrectImplIterator<ThisType>>
165 static WrappedIterator lower_bound_impl(ThisType &that, const key_type &key) {
166 if (key.valid()) {
167 // ImplMap doesn't give us what want with a direct query, it will give us the first entry contained (if any) in key,
168 // not the first entry intersecting key, so, first look for the the first entry that starts at or after key.begin
169 // with the operator > in range, we can safely use an empty range for comparison
170 auto lower = that.impl_map_.lower_bound(key_type(key.begin, key.begin));
171
172 // If there is a preceding entry it's possible that begin is included, as all we know is that lower.begin >= key.begin
173 // or lower is at end
174 if (!that.at_impl_begin(lower)) {
175 auto prev = lower;
176 --prev;
177 // If the previous entry includes begin (and we know key.begin > prev.begin) then prev is actually lower
178 if (key.begin < prev->first.end) {
179 lower = prev;
180 }
181 }
182 return lower;
183 }
184 // Key is ill-formed
185 return that.impl_end(); // Point safely to nothing.
186 }
187
188 ImplIterator lower_bound_impl(const key_type &key) { return lower_bound_impl(*this, key); }
189
190 ImplConstIterator lower_bound_impl(const key_type &key) const { return lower_bound_impl(*this, key); }
191
192 template <typename ThisType, typename WrappedIterator = ConstCorrectImplIterator<ThisType>>
193 static WrappedIterator upper_bound_impl(ThisType &that, const key_type &key) {
194 if (key.valid()) {
195 // the upper bound is the first range that is full greater (upper.begin >= key.end
196 // we can get close by looking for the first to exclude key.end, then adjust to account for the fact that key.end is
197 // exclusive and we thus ImplMap::upper_bound may be off by one here, i.e. the previous may be the upper bound
198 auto upper = that.impl_map_.upper_bound(key_type(key.end, key.end));
199 if (!that.at_impl_end(upper) && (upper != that.impl_begin())) {
200 auto prev = upper;
201 --prev;
202 // We know key.end is >= prev.begin, the only question is whether it's ==
203 if (prev->first.begin == key.end) {
204 upper = prev;
205 }
206 }
207 return upper;
208 }
209 return that.impl_end(); // Point safely to nothing.
210 }
211
212 ImplIterator upper_bound_impl(const key_type &key) { return upper_bound_impl(*this, key); }
213
214 ImplConstIterator upper_bound_impl(const key_type &key) const { return upper_bound_impl(*this, key); }
215
216 ImplIterator impl_find(const key_type &key) { return impl_map_.find(key); }
217 ImplConstIterator impl_find(const key_type &key) const { return impl_map_.find(key); }
218 bool impl_not_found(const key_type &key) const { return impl_end() == impl_find(key); }
219
220 ImplIterator impl_end() { return impl_map_.end(); }
221 ImplConstIterator impl_end() const { return impl_map_.end(); }
222
223 ImplIterator impl_begin() { return impl_map_.begin(); }
224 ImplConstIterator impl_begin() const { return impl_map_.begin(); }
225
226 inline bool at_impl_end(const ImplIterator &pos) { return pos == impl_end(); }
227 inline bool at_impl_end(const ImplConstIterator &pos) const { return pos == impl_end(); }
228
229 inline bool at_impl_begin(const ImplIterator &pos) { return pos == impl_begin(); }
230 inline bool at_impl_begin(const ImplConstIterator &pos) const { return pos == impl_begin(); }
231
232 ImplIterator impl_erase(const ImplIterator &pos) { return impl_map_.erase(pos); }
233
234 template <typename Value>
235 ImplIterator impl_insert(const ImplIterator &hint, Value &&value) {
236 RANGE_ASSERT(impl_not_found(value.first));
237 RANGE_ASSERT(value.first.non_empty());
238 return impl_map_.emplace_hint(hint, std::forward<Value>(value));
239 }
240 ImplIterator impl_insert(const ImplIterator &hint, const key_type &key, const mapped_type &value) {
241 return impl_insert(hint, std::make_pair(key, value));
242 }
243
244 ImplIterator impl_insert(const ImplIterator &hint, const index_type &begin, const index_type &end, const mapped_type &value) {
245 return impl_insert(hint, key_type(begin, end), value);
246 }
247
248 template <typename SplitOp>
249 ImplIterator split_impl(const ImplIterator &split_it, const index_type &index, const SplitOp &) {
250 // Make sure contains the split point
251 if (!split_it->first.includes(index)) return split_it; // If we don't have a valid split point, just return the iterator
252
253 const auto range = split_it->first;
254 key_type lower_range(range.begin, index);
255 if (lower_range.empty() && SplitOp::keep_upper()) {
256 return split_it; // this is a noop we're keeping the upper half which is the same as split_it;
257 }
258 // Save the contents of it and erase it
259 auto value = std::move(split_it->second);
260 auto next_it = impl_map_.erase(split_it); // Keep this, just in case the split point results in an empty "keep" set
261
262 if (lower_range.empty() && !SplitOp::keep_upper()) {
263 // This effectively an erase...
264 return next_it;
265 }
266 // Upper range cannot be empty
267 key_type upper_range(index, range.end);
268 key_type move_range;
269 key_type copy_range;
270
271 // Were either going to keep one or both of the split pieces. If we keep both, we'll copy value to the upper,
272 // and move to the lower, and return the lower, else move to, and return the kept one.
273 if (SplitOp::keep_lower() && !lower_range.empty()) {
274 move_range = lower_range;
275 if (SplitOp::keep_upper()) {
276 copy_range = upper_range; // only need a valid copy range if we keep both.
277 }
278 } else if (SplitOp::keep_upper()) { // We're not keeping the lower split because it's either empty or not wanted
279 move_range = upper_range; // this will be non_empty as index is included ( < end) in the original range)
280 }
281
282 // we insert from upper to lower because that's what emplace_hint can do in constant time. (not log time in C++11)
283 if (!copy_range.empty()) {
284 // We have a second range to create, so do it by copy
285 RANGE_ASSERT(impl_map_.find(copy_range) == impl_map_.end());
286 next_it = impl_map_.emplace_hint(next_it, std::make_pair(copy_range, value));
287 }
288
289 if (!move_range.empty()) {
290 // Whether we keep one or both, the one we return gets value moved to it, as the other one already has a copy
291 RANGE_ASSERT(impl_map_.find(move_range) == impl_map_.end());
292 next_it = impl_map_.emplace_hint(next_it, std::make_pair(move_range, std::move(value)));
293 }
294
295 // point to the beginning of the inserted elements (or the next from the erase
296 return next_it;
297 }
298
299 // do an ranged insert that splits existing ranges at the boundaries, and writes value to any non-initialized sub-ranges
300 range<ImplIterator> infill_and_split(const key_type &bounds, const mapped_type &value, ImplIterator lower, bool split_bounds) {
301 auto pos = lower;
302 if (at_impl_end(pos)) return range<ImplIterator>(pos, pos); // defensive...
303
304 // Logic assumes we are starting at lower bound
305 RANGE_ASSERT(lower == lower_bound_impl(bounds));
306
307 // Trim/infil the beginning if needed
308 const auto first_begin = pos->first.begin;
309 if (bounds.begin > first_begin && split_bounds) {
310 pos = split_impl(pos, bounds.begin, split_op_keep_both());
311 lower = pos;
312 ++lower;
313 RANGE_ASSERT(lower == lower_bound_impl(bounds));
314 } else if (bounds.begin < first_begin) {
315 pos = impl_insert(pos, bounds.begin, first_begin, value);
316 lower = pos;
317 RANGE_ASSERT(lower == lower_bound_impl(bounds));
318 }
319
320 // in the trim case pos starts one before lower_bound, but that allows trimming a single entry range in loop.
321 // NOTE that the loop is trimming and infilling at pos + 1
322 while (!at_impl_end(pos) && pos->first.begin < bounds.end) {
323 auto last_end = pos->first.end;
324 // check for in-fill
325 ++pos;
326 if (at_impl_end(pos)) {
327 if (last_end < bounds.end) {
328 // Gap after last entry in impl_map and before end,
329 pos = impl_insert(pos, last_end, bounds.end, value);
330 ++pos; // advances to impl_end, as we're at upper boundary
331 RANGE_ASSERT(at_impl_end(pos));
332 }
333 } else if (pos->first.begin != last_end) {
334 // we have a gap between last entry and current... fill, but not beyond bounds
335 if (bounds.includes(pos->first.begin)) {
336 pos = impl_insert(pos, last_end, pos->first.begin, value);
337 // don't further advance pos, because we may need to split the next entry and thus can't skip it.
338 } else if (last_end < bounds.end) {
339 // Non-zero length final gap in-bounds
340 pos = impl_insert(pos, last_end, bounds.end, value);
341 ++pos; // advances back to the out of bounds entry which we inserted just before
342 RANGE_ASSERT(!bounds.includes(pos->first.begin));
343 }
344 } else if (pos->first.includes(bounds.end)) {
345 if (split_bounds) {
346 // extends past the end of the bounds range, snip to only include the bounded section
347 // NOTE: this splits pos, but the upper half of the split should now be considered upper_bound
348 // for the range
349 pos = split_impl(pos, bounds.end, split_op_keep_both());
350 }
351 // advance to the upper haf of the split which will be upper_bound or to next which will both be out of bounds
352 ++pos;
353 RANGE_ASSERT(!bounds.includes(pos->first.begin));
354 }
355 }
356 // Return the current position which should be the upper_bound for bounds
357 RANGE_ASSERT(pos == upper_bound_impl(bounds));
358 return range<ImplIterator>(lower, pos);
359 }
360
361 ImplIterator impl_erase_range(const key_type &bounds, ImplIterator lower) {
362 // Logic assumes we are starting at a valid lower bound
363 RANGE_ASSERT(!at_impl_end(lower));
364 RANGE_ASSERT(lower == lower_bound_impl(bounds));
365
366 // Trim/infil the beginning if needed
367 auto current = lower;
368 const auto first_begin = current->first.begin;
369 if (bounds.begin > first_begin) {
370 // Preserve the portion of lower bound excluded from bounds
371 current = split_impl(current, bounds.begin, split_op_keep_lower());
372 // Exclude the preserved portion
373 ++current;
374 RANGE_ASSERT(current == lower_bound_impl(bounds));
375 }
376
377 // Loop over completely contained entries and erase them
378 while (!at_impl_end(current) && (current->first.end <= bounds.end)) {
379 current = impl_erase(current);
380 }
381
382 if (!at_impl_end(current) && current->first.includes(bounds.end)) {
383 // last entry extends past the end of the bounds range, snip to only erase the bounded section
384 current = split_impl(current, bounds.end, split_op_keep_upper());
385 }
386
387 RANGE_ASSERT(current == upper_bound_impl(bounds));
388 return current;
389 }
390
391 template <typename ValueType, typename WrappedIterator_>
392 struct iterator_impl {
393 public:
394 friend class range_map;
395 using WrappedIterator = WrappedIterator_;
396
397 private:
398 WrappedIterator pos_;
399
400 // Create an iterator at a specific internal state -- only from the parent container
401 iterator_impl(const WrappedIterator &pos) : pos_(pos) {}
402
403 public:
404 iterator_impl() : iterator_impl(WrappedIterator()){};
405 iterator_impl(const iterator_impl &other) : pos_(other.pos_){};
406
407 iterator_impl &operator=(const iterator_impl &rhs) {
408 pos_ = rhs.pos_;
409 return *this;
410 }
411
412 inline bool operator==(const iterator_impl &rhs) const { return pos_ == rhs.pos_; }
413
414 inline bool operator!=(const iterator_impl &rhs) const { return pos_ != rhs.pos_; }
415
416 ValueType &operator*() const { return *pos_; }
417 ValueType *operator->() const { return &*pos_; }
418
419 iterator_impl &operator++() {
420 ++pos_;
421 return *this;
422 }
423
424 iterator_impl &operator--() {
425 --pos_;
426 return *this;
427 }
John Zulauf81408f12019-11-27 16:40:27 -0700428
429 // To allow for iterator -> const_iterator construction
430 // NOTE: while it breaks strict encapsulation, it does so less than friend
431 const WrappedIterator &get_pos() const { return pos_; };
John Zulauf11211402019-11-15 14:02:36 -0700432 };
433
434 public:
435 using iterator = iterator_impl<value_type, ImplIterator>;
John Zulauf81408f12019-11-27 16:40:27 -0700436
437 // The const iterator must be derived to allow the conversion from iterator, which iterator doesn't support
438 class const_iterator : public iterator_impl<const value_type, ImplConstIterator> {
439 using Base = iterator_impl<const value_type, ImplConstIterator>;
440 friend range_map;
441
442 public:
443 const_iterator(const const_iterator &other) : Base(other){};
444 const_iterator(const iterator &it) : Base(ImplConstIterator(it.get_pos())) {}
445 const_iterator() : Base() {}
446
447 private:
448 const_iterator(const ImplConstIterator &pos) : Base(pos) {}
449 };
John Zulauf11211402019-11-15 14:02:36 -0700450
451 protected:
452 inline bool at_end(const iterator &it) { return at_impl_end(it.pos_); }
453 inline bool at_end(const const_iterator &it) const { return at_impl_end(it.pos_); }
454 inline bool at_begin(const iterator &it) { return at_impl_begin(it.pos_); }
455
456 template <typename That, typename Iterator>
457 static bool is_contiguous_impl(That *const that, const key_type &range, const Iterator &lower) {
458 // Search range or intersection is empty
459 if (lower == that->impl_end() || lower->first.excludes(range)) return false;
460
461 if (lower->first.includes(range)) {
462 return true; // there is one entry that contains the whole key range
463 }
464
465 bool contiguous = true;
466 for (auto pos = lower; contiguous && pos != that->impl_end() && range.includes(pos->first.begin); ++pos) {
467 // if current doesn't cover the rest of the key range, check to see that the next is extant and abuts
468 if (pos->first.end < range.end) {
469 auto next = pos;
John Zulauff3eeba62019-11-22 15:09:07 -0700470 ++next;
John Zulauf11211402019-11-15 14:02:36 -0700471 contiguous = (next != that->impl_end()) && pos->first.is_prior_to(next->first);
472 }
473 }
474 return contiguous;
475 }
476
477 public:
478 iterator end() { return iterator(impl_map_.end()); } // policy and bounds don't matter for end
479 const_iterator end() const { return const_iterator(impl_map_.end()); } // policy and bounds don't matter for end
480 iterator begin() { return iterator(impl_map_.begin()); } // with default policy, and thus no bounds
481 const_iterator begin() const { return const_iterator(impl_map_.begin()); } // with default policy, and thus no bounds
482 const_iterator cbegin() const { return const_iterator(impl_map_.cbegin()); } // with default policy, and thus no bounds
483 const_iterator cend() const { return const_iterator(impl_map_.cend()); } // with default policy, and thus no bounds
484
485 iterator erase(const iterator &pos) {
486 RANGE_ASSERT(!at_end(pos));
487 return iterator(impl_erase(pos.pos_));
488 }
489
490 iterator erase(range<iterator> bounds) {
491 auto current = bounds.begin.pos_;
492 while (current != bounds.end.pos_) {
493 RANGE_ASSERT(!at_impl_end(current));
494 current = impl_map_.erase(current);
495 }
496 RANGE_ASSERT(current == bounds.end.pos_);
497 return current;
498 }
499
500 iterator erase(iterator first, iterator last) { return erase(range<iterator>(first, last)); }
501
502 iterator erase_range(const key_type &bounds) {
503 auto lower = lower_bound_impl(bounds);
504
505 if (at_impl_end(lower) || !bounds.intersects(lower->first)) {
506 // There is nothing in this range lower bound is above bound
507 return iterator(lower);
508 }
509 auto next = impl_erase_range(bounds, lower);
510 return iterator(next);
511 }
512
513 void clear() { impl_map_.clear(); }
514
515 iterator find(const key_type &key) { return iterator(impl_map_.find(key)); }
516
517 const_iterator find(const key_type &key) const { return const_iterator(impl_map_.find(key)); }
518
519 iterator find(const index_type &index) {
520 auto lower = lower_bound(range<index_type>(index, index + 1));
521 if (!at_end(lower) && lower->first.includes(index)) {
522 return lower;
523 }
524 return end();
525 }
526
527 const_iterator find(const index_type &index) const {
528 auto lower = lower_bound(key_type(index, index + 1));
529 if (!at_end(lower) && lower->first.includes(index)) {
530 return lower;
531 }
532 return end();
533 }
534
John Zulauf81408f12019-11-27 16:40:27 -0700535#if 0
John Zulauf11211402019-11-15 14:02:36 -0700536 // Hint for this insert range is the *lower* bound of the insert as opposed to the upper_bound used
537 // as a non-range hint
538 template <typename Value, typename Split = insert_range_split_bounds>
539 range<iterator> insert_range(const iterator &lower, Value &&value, const Split &split = Split()) {
540 const key_type &bounds = value.first;
541 // We're not robust to a bad hint, so detect it with extreme prejudice
542 // TODO: Add bad hint test to make this robust...
543 RANGE_ASSERT(lower == lower_bound(bounds));
544 range<ImplIterator> impl_bounds(lower.pos_, lower.pos_);
545
546 if (at_impl_end(impl_bounds.begin) || !bounds.intersects(impl_bounds.begin->first)) {
547 // There is nothing in this range lower bound is above bound
548 // Generate the needed range (and we're done...)
549 impl_bounds.begin = impl_insert(impl_bounds.begin, std::forward<Value>(value));
550 } else {
551 // Splitting from an occupied range, trim and infill (with value) as needed
552 RANGE_ASSERT(impl_bounds.begin->first.intersects(bounds)); // Must construct at the lower boundary of range
553 impl_bounds = infill_and_split(bounds, value.second, impl_bounds.begin, Split::split_boundaries);
554 }
555
556 return range<iterator>(iterator(impl_bounds.begin), iterator(impl_bounds.end));
557 }
558
559 template <typename Value, typename Split = insert_range_split_bounds>
560 range<iterator> insert_range(Value &&value, const Split &split = Split()) {
561 return insert_range(lower_bound(value.first), value, split);
562 }
John Zulauf81408f12019-11-27 16:40:27 -0700563#endif
John Zulauf11211402019-11-15 14:02:36 -0700564
565 iterator lower_bound(const key_type &key) { return iterator(lower_bound_impl(key)); }
566
567 const_iterator lower_bound(const key_type &key) const { return const_iterator(lower_bound_impl(key)); }
568
569 iterator upper_bound(const key_type &key) { return iterator(upper_bound_impl(key)); }
570
571 const_iterator upper_bound(const key_type &key) const { return const_iterator(upper_bound_impl(key)); }
572
573 range<iterator> bounds(const key_type &key) { return {lower_bound(key), upper_bound(key)}; }
574 range<const_iterator> cbounds(const key_type &key) const { return {lower_bound(key), upper_bound(key)}; }
575 range<const_iterator> bounds(const key_type &key) const { return cbounds(key); }
576
577 using insert_pair = std::pair<iterator, bool>;
578
579 // This is traditional no replacement insert.
John Zulauf81408f12019-11-27 16:40:27 -0700580 insert_pair insert(const value_type &value) {
John Zulauf11211402019-11-15 14:02:36 -0700581 const auto &key = value.first;
582 if (!key.non_empty()) {
583 // It's an invalid key, early bail pointing to end
584 return std::make_pair(end(), false);
585 }
586
587 // Look for range conflicts (and an insertion point, which makes the lower_bound *not* wasted work)
588 // we don't have to check upper if just check that lower doesn't intersect (which it would if lower != upper)
589 auto lower = lower_bound_impl(key);
590 if (at_impl_end(lower) || !lower->first.intersects(key)) {
591 // range is not even paritally overlapped, and lower is strictly > than key
John Zulauf81408f12019-11-27 16:40:27 -0700592 auto impl_insert = impl_map_.emplace_hint(lower, value);
John Zulauf11211402019-11-15 14:02:36 -0700593 // auto impl_insert = impl_map_.emplace(value);
594 iterator wrap_it(impl_insert);
595 return std::make_pair(wrap_it, true);
596 }
597 // We don't replace
598 return std::make_pair(iterator(lower), false);
599 };
600
John Zulauf81408f12019-11-27 16:40:27 -0700601 iterator insert(const_iterator hint, const value_type &value) {
602 bool hint_open;
603 ImplConstIterator impl_next = hint.pos_;
604 if (impl_map_.empty()) {
605 hint_open = true;
606 } else if (impl_next == impl_map_.cbegin()) {
607 hint_open = value.first.strictly_less(impl_next->first);
608 } else if (impl_next == impl_map_.cend()) {
609 auto impl_prev = impl_next;
610 --impl_prev;
611 hint_open = value.first.strictly_greater(impl_prev->first);
612 } else {
613 auto impl_prev = impl_next;
614 --impl_prev;
615 hint_open = value.first.strictly_greater(impl_prev->first) && value.first.strictly_less(impl_next->first);
616 }
617
618 if (!hint_open) {
619 // Hint was unhelpful, fall back to the non-hinted version
620 auto plain_insert = insert(value);
621 return plain_insert.first;
622 }
623
624 auto impl_insert = impl_map_.insert(impl_next, value);
625 return iterator(impl_insert);
626 }
627
628#if 0
John Zulauf11211402019-11-15 14:02:36 -0700629 iterator merge_adjacent(const range<iterator> &bounds) {
630 if (at_end(bounds.begin)) return bounds.begin;
631
632 auto anchor = bounds.begin;
633 while (anchor != bounds.end) {
634 RANGE_ASSERT(!at_end(anchor));
635 auto current = anchor;
636 auto next = current;
637 ++next;
638 // Walk from anchor to find adjoining ranges that have the same value
639 while (next != bounds.end && next->first.is_subsequent_to(current->first) && next->second == anchor->second) {
640 current = next;
641 ++next;
642 }
643 if (current != anchor) {
644 // the while loop above advanced at least onces, so we have something to merge
645 value_type merged = std::make_pair(key_type(anchor->first.begin, current->first.end), std::move(anchor->second));
646 next = erase(range<iterator>(anchor, next));
647 impl_map_.emplace_hint(next.pos_, merged);
648 }
649 // Reset the anchor for the next merge search
650 anchor = next;
651 }
652 RANGE_ASSERT(anchor == bounds.end);
653 return anchor;
654 }
655
656 iterator merge_adjacent(iterator start) { return merge_adjacent(range<iterator>(start, end())); }
657
658 iterator merge_adjacent() { return merge_adjacent(range<iterator>(begin(), end())); }
John Zulauf81408f12019-11-27 16:40:27 -0700659#endif
John Zulauf11211402019-11-15 14:02:36 -0700660
661 template <typename SplitOp>
662 iterator split(const iterator whole_it, const index_type &index, const SplitOp &split_op) {
663 auto split_it = split_impl(whole_it.pos_, index, split_op);
664 return iterator(split_it);
665 }
666
667 // The overwrite hint here is lower.... and if it's not right... this fails
668 template <typename Value>
669 iterator overwrite_range(const iterator &lower, Value &&value) {
670 // We're not robust to a bad hint, so detect it with extreme prejudice
671 // TODO: Add bad hint test to make this robust...
672 auto lower_impl = lower.pos_;
673 auto insert_hint = lower_impl;
674 if (!at_impl_end(lower_impl)) {
675 // If we're at end (and the hint is good, there's nothing to erase
676 RANGE_ASSERT(lower == lower_bound(value.first));
677 insert_hint = impl_erase_range(value.first, lower_impl);
678 }
679 auto inserted = impl_insert(insert_hint, std::forward<Value>(value));
680 return iterator(inserted);
681 }
682
683 template <typename Value>
684 iterator overwrite_range(Value &&value) {
685 auto lower = lower_bound(value.first);
686 return overwrite_range(lower, value);
687 }
688
John Zulauf81408f12019-11-27 16:40:27 -0700689#if 0
John Zulauf11211402019-11-15 14:02:36 -0700690 // Need const_iterator/const and iterator/non-const variants using a common implementation
691 bool is_contiguous(const key_type &key, const const_iterator &lower) const {
692 // We're not robust to a bad lower, so detect it with extreme prejudice
693 // TODO: Add bad test to make this robust...
694 RANGE_ASSERT(lower == lower_bound(key));
695 return is_contiguous_impl(this, lower.pos_);
696 }
697
698 bool is_contiguous(const key_type &key, const iterator &lower) {
699 // We're not robust to a bad lower, so detect it with extreme prejudice
700 // TODO: Add bad lower test to make this robust...
701 RANGE_ASSERT(lower == lower_bound(key));
702 return is_contiguous_impl(this, key, lower.pos_);
703 }
704
705 // we don't need a non-const version of this variant
706 bool is_contiguous(const key_type &key) const { return is_contiguous_impl_(this, key, lower_bound(key).pos_); }
John Zulauf81408f12019-11-27 16:40:27 -0700707#endif
John Zulauf11211402019-11-15 14:02:36 -0700708
709 bool empty() const { return impl_map_.empty(); }
710 size_t size() const { return impl_map_.size(); }
John Zulauf81408f12019-11-27 16:40:27 -0700711
712 // For configuration/debug use // Use with caution...
713 ImplMap &get_implementation_map() { return impl_map_; }
714 const ImplMap &get_implementation_map() const { return impl_map_; }
John Zulauf11211402019-11-15 14:02:36 -0700715};
716
717template <typename Container>
718using const_correct_iterator = decltype(std::declval<Container>().begin());
719
John Zulauf81408f12019-11-27 16:40:27 -0700720// The an array based small ordered map for range keys for use as the range map "ImplMap" as an alternate to std::map
721//
722// Assumes RangeKey::index_type is unsigned (TBD is it useful to generalize to unsigned?)
723// Assumes RangeKey implements begin, end, < and (TBD) from template range above
724template <typename Key, typename T, typename RangeKey = range<Key>, size_t N = 64, typename SmallIndex = uint8_t>
725class small_range_map {
726 using SmallRange = range<SmallIndex>;
727
728 public:
729 using mapped_type = T;
730 using key_type = RangeKey;
731 using value_type = std::pair<const key_type, mapped_type>;
732 using index_type = typename key_type::index_type;
733
734 using size_type = SmallIndex;
735 template <typename Map_, typename Value_>
736 struct IteratorImpl {
737 public:
738 using Map = Map_;
739 using Value = Value_;
740 friend Map;
741 Value *operator->() const { return map_->get_value(pos_); }
742 Value &operator*() const { return *(map_->get_value(pos_)); }
743 IteratorImpl &operator++() {
744 pos_ = map_->next_range(pos_);
745 return *this;
746 }
747 IteratorImpl &operator--() {
748 pos_ = map_->prev_range(pos_);
749 return *this;
750 }
751 IteratorImpl &operator=(const IteratorImpl &other) {
752 map_ = other.map_;
753 pos_ = other.pos_;
754 return *this;
755 }
756 bool operator==(const IteratorImpl &other) const {
757 if (at_end() && other.at_end()) {
758 return true; // all ends are equal
759 }
760 return (map_ == other.map_) && (pos_ == other.pos_);
761 }
762 bool operator!=(const IteratorImpl &other) const { return !(*this == other); }
763
764 // At end()
765 IteratorImpl() : map_(nullptr), pos_(N) {}
766
767 // Raw getters to allow for const_iterator conversion below
768 Map *get_map() const { return map_; }
769 SmallIndex get_pos() const { return pos_; }
770
771 bool at_end() const { return (map_ == nullptr) || (pos_ >= map_->get_limit()); }
772
773 protected:
774 IteratorImpl(Map *map, SmallIndex pos) : map_(map), pos_(pos) {}
775
776 private:
777 Map *map_;
778 SmallIndex pos_; // the begin of the current small_range
779 };
780 using iterator = IteratorImpl<small_range_map, value_type>;
781
782 // The const iterator must be derived to allow the conversion from iterator, which iterator doesn't support
783 class const_iterator : public IteratorImpl<const small_range_map, const value_type> {
784 using Base = IteratorImpl<const small_range_map, const value_type>;
785 friend small_range_map;
786
787 public:
788 const_iterator(const iterator &it) : Base(it.get_map(), it.get_pos()) {}
789 const_iterator() : Base() {}
790
791 private:
792 const_iterator(const small_range_map *map, SmallIndex pos) : Base(map, pos) {}
793 };
794
795 iterator begin() {
796 // 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)
797 return iterator(this, ranges_[0].begin);
798 }
799 const_iterator cbegin() const { return const_iterator(this, ranges_[0].begin); }
800 const_iterator begin() const { return cbegin(); }
801 iterator end() { return iterator(); }
802 const_iterator cend() const { return const_iterator(); }
803 const_iterator end() const { return cend(); }
804
805 void clear() {
806 const SmallRange clear_range(limit_, 0);
807 for (SmallIndex i = 0; i < limit_; ++i) {
808 auto &range = ranges_[i];
809 if (range.begin == i) {
810 // Clean up the backing store
811 destruct_value(i);
812 }
813 range = clear_range;
814 }
815 size_ = 0;
816 }
817
818 // Find entry with an exact key match (uncommon use case)
819 iterator find(const key_type &key) {
820 RANGE_ASSERT(in_bounds(key));
821 if (key.begin < limit_) {
822 const SmallIndex small_begin = static_cast<SmallIndex>(key.begin);
823 const auto &range = ranges_[small_begin];
824 if (range.begin == small_begin) {
825 const auto small_end = static_cast<SmallIndex>(key.end);
826 if (range.end == small_end) return iterator(this, small_begin);
827 }
828 }
829 return end();
830 }
831 const_iterator find(const key_type &key) const {
832 RANGE_ASSERT(in_bounds(key));
833 if (key.begin < limit_) {
834 const SmallIndex small_begin = static_cast<SmallIndex>(key.begin);
835 const auto &range = ranges_[small_begin];
836 if (range.begin == small_begin) {
837 const auto small_end = static_cast<SmallIndex>(key.end);
838 if (range.end == small_end) return const_iterator(this, small_begin);
839 }
840 }
841 return end();
842 }
843
844 iterator find(const index_type &index) {
845 if (index < get_limit()) {
846 const SmallIndex small_index = static_cast<SmallIndex>(index);
847 const auto &range = ranges_[small_index];
848 if (range.valid()) {
849 return iterator(this, range.begin);
850 }
851 }
852 return end();
853 }
854
855 const_iterator find(const index_type &index) const {
856 if (index < get_limit()) {
857 const SmallIndex small_index = static_cast<SmallIndex>(index);
858 const auto &range = ranges_[small_index];
859 if (range.valid()) {
860 return const_iterator(this, range.begin);
861 }
862 }
863 return end();
864 }
865
866 size_type size() const { return size_; }
867 bool empty() const { return 0 == size_; }
868
869 iterator erase(const_iterator pos) {
870 RANGE_ASSERT(pos.map_ == this);
871 return erase_impl(pos.get_pos());
872 }
873
874 iterator erase(iterator pos) {
875 RANGE_ASSERT(pos.map_ == this);
876 return erase_impl(pos.get_pos());
877 }
878
879 // Must be called with rvalue or lvalue of value_type
880 template <typename Value>
881 iterator emplace(Value &&value) {
882 const auto &key = value.first;
883 RANGE_ASSERT(in_bounds(key));
884 if (key.begin >= limit_) return end(); // Invalid key (end is checked in "is_open")
885 const SmallRange range(static_cast<SmallIndex>(key.begin), static_cast<SmallIndex>(key.end));
886 if (is_open(key)) {
887 // This needs to be the fast path, but I don't see how we can avoid the sanity checks above
888 for (auto i = range.begin; i < range.end; ++i) {
889 ranges_[i] = range;
890 }
891 // Update the next information for the previous unused slots (as stored in begin invalidly)
892 auto prev = range.begin;
893 while (prev > 0) {
894 --prev;
895 if (ranges_[prev].valid()) break;
896 ranges_[prev].begin = range.begin;
897 }
898 // Placement new into the storage interpreted as Value
899 construct_value(range.begin, value_type(std::forward<Value>(value)));
900 auto next = range.end;
901 // update the previous range information for the next unsed slots (as stored in end invalidly)
902 while (next < limit_) {
903 // End is exclusive... increment *after* update
904 if (ranges_[next].valid()) break;
905 ranges_[next].end = range.end;
906 ++next;
907 }
908 return iterator(this, range.begin);
909 } else {
910 // Can't insert into occupied ranges.
911 // if ranges_[key.begin] is valid then this is the collision (starting at .begin
912 // if it's invalid .begin points to the overlapping entry from is_open (or end if key was out of range)
913 return iterator(this, ranges_[range.begin].begin);
914 }
915 }
916
917 // As hint is going to be ignored, make it as lightweight as possible, by reference and no conversion construction
918 template <typename Value>
919 iterator emplace_hint(const const_iterator &hint, Value &&value) {
920 // We have direct access so we can drop the hint
921 return emplace(std::forward<Value>(value));
922 }
923
924 template <typename Value>
925 iterator emplace_hint(const iterator &hint, Value &&value) {
926 // We have direct access so we can drop the hint
927 return emplace(std::forward<Value>(value));
928 }
929
930 // Again, hint is going to be ignored, make it as lightweight as possible, by reference and no conversion construction
931 iterator insert(const const_iterator &hint, const value_type &value) { return emplace(value); }
932 iterator insert(const iterator &hint, const value_type &value) { return emplace(value); }
933
934 std::pair<iterator, bool> insert(const value_type &value) {
935 const auto &key = value.first;
936 RANGE_ASSERT(in_bounds(key));
937 if (key.begin >= limit_) return std::make_pair(end(), false); // Invalid key, not inserted.
938 if (is_open(key)) {
939 return std::make_pair(emplace(value), true);
940 }
941 // If invalid we point to the subsequent range that collided, if valid begin is the start of the valid range
942 const auto &collision_begin = ranges_[key.begin].begin;
943 RANGE_ASSERT(ranges_[collision_begin].valid());
944 return std::make_pair(iterator(this, collision_begin), false);
945 }
946
947 template <typename SplitOp>
948 iterator split(const iterator whole_it, const index_type &index, const SplitOp &split_op) {
949 if (!whole_it->first.includes(index)) return whole_it; // If we don't have a valid split point, just return the iterator
950
951 const auto &key = whole_it->first;
952 const auto small_key = make_small_range(key);
953 key_type lower_key(key.begin, index);
954 if (lower_key.empty() && SplitOp::keep_upper()) {
955 return whole_it; // this is a noop we're keeping the upper half which is the same as whole_it;
956 }
957
958 if ((lower_key.empty() && !SplitOp::keep_upper()) || !(SplitOp::keep_lower() || SplitOp::keep_upper())) {
959 // This effectively an erase... so erase.
960 return erase(whole_it);
961 }
962
963 // Upper range cannot be empty (because the split point would be included...
964 const auto small_lower_key = make_small_range(lower_key);
965 const SmallRange small_upper_key{small_lower_key.end, small_key.end};
966 if (SplitOp::keep_upper()) {
967 // Note: create the upper section before the lower, as processing the lower may erase it
968 RANGE_ASSERT(!small_upper_key.empty());
969 const key_type upper_key{lower_key.end, key.end};
970 if (SplitOp::keep_lower()) {
971 construct_value(small_upper_key.begin, std::make_pair(upper_key, get_value(small_key.begin)->second));
972 } else {
973 // If we aren't keeping the lower, move instead of copy
974 construct_value(small_upper_key.begin, std::make_pair(upper_key, std::move(get_value(small_key.begin)->second)));
975 }
976 for (auto i = small_upper_key.begin; i < small_upper_key.end; ++i) {
977 ranges_[i] = small_upper_key;
978 }
979 } else {
980 // rewrite "end" to the next valid range (or end)
981 RANGE_ASSERT(SplitOp::keep_lower());
982 auto next = next_range(small_key.begin);
983 rerange(small_upper_key, SmallRange(next, small_lower_key.end));
984 // for any already invalid, we just rewrite the end.
985 rerange_end(small_upper_key.end, next, small_lower_key.end);
986 }
987 SmallIndex split_index;
988 if (SplitOp::keep_lower()) {
989 resize_value(small_key.begin, lower_key.end);
990 rerange_end(small_lower_key.begin, small_lower_key.end, small_lower_key.end);
991 split_index = small_lower_key.begin;
992 } else {
993 // Remove lower and rewrite empty space
994 RANGE_ASSERT(SplitOp::keep_upper());
995 destruct_value(small_key.begin);
996
997 // Rewrite prior empty space (if any)
998 auto prev = prev_range(small_key.begin);
999 SmallIndex limit = small_lower_key.end;
1000 SmallIndex start = 0;
1001 if (small_key.begin != 0) {
1002 const auto &prev_start = ranges_[prev];
1003 if (prev_start.valid()) {
1004 // If there is a previous used range, the empty space starts after it.
1005 start = prev_start.end;
1006 } else {
1007 RANGE_ASSERT(prev == 0); // prev_range only returns invalid ranges "off the front"
1008 start = prev;
1009 }
1010 // for the section *prior* to key begin only need to rewrite the "invalid" begin (i.e. next "in use" begin)
1011 rerange_begin(start, small_lower_key.begin, limit);
1012 }
1013 // for the section being erased rewrite the invalid range reflecting the empty space
1014 rerange(small_lower_key, SmallRange(limit, start));
1015 split_index = small_lower_key.end;
1016 }
1017
1018 return iterator(this, split_index);
1019 }
1020
1021 // For the value.first range rewrite the range...
1022 template <typename Value>
1023 iterator overwrite_range(Value &&value) {
1024 const auto &key = value.first;
1025
1026 // Small map only has a restricted range supported
1027 RANGE_ASSERT(in_bounds(key));
1028 if (key.end > get_limit()) {
1029 return end();
1030 }
1031
1032 const auto small_key = make_small_range(key);
1033 clear_out_range(small_key, /* valid clear range */ true);
1034 construct_value(small_key.begin, std::forward<Value>(value));
1035 return iterator(this, small_key.begin);
1036 }
1037
1038 // We don't need a hint...
1039 template <typename Value>
1040 iterator overwrite_range(const iterator &hint, Value &&value) {
1041 return overwrite_range(std::forward<Value>(value));
1042 }
1043
1044 // For the range erase all contents within range, trimming any overlapping ranges
1045 iterator erase_range(const key_type &range) {
1046 // Small map only has a restricted range supported
1047 RANGE_ASSERT(in_bounds(range));
1048 if (range.end > get_limit() || range.empty()) {
1049 return end();
1050 }
1051 const auto empty = clear_out_range(make_small_range(range), /* valid clear range */ false);
1052 return iterator(this, empty.end);
1053 }
1054
1055 template <typename Iterator>
1056 iterator erase(const Iterator &first, const Iterator &last) {
1057 RANGE_ASSERT(this == first.map_);
1058 RANGE_ASSERT(this == last.map_);
1059 auto first_pos = !first.at_end() ? first.pos_ : limit_;
1060 auto last_pos = !last.at_end() ? last.pos_ : limit_;
1061 RANGE_ASSERT(first_pos <= last_pos);
1062 const SmallRange clear_me(first_pos, last_pos);
1063 if (!clear_me.empty()) {
1064 const SmallRange empty_range(find_empty_left(clear_me), last_pos);
1065 clear_and_set_range(empty_range.begin, empty_range.end, make_invalid_range(empty_range));
1066 }
1067 return iterator(this, last_pos);
1068 }
1069
1070 iterator lower_bound(const key_type &key) { return iterator(this, lower_bound_impl(this, key)); }
1071 const_iterator lower_bound(const key_type &key) const { return const_iterator(this, lower_bound_impl(this, key)); }
1072
1073 iterator upper_bound(const key_type &key) { return iterator(this, upper_bound_impl(this, key)); }
1074 const_iterator upper_bound(const key_type &key) const { return const_iterator(this, upper_bound_impl(this, key)); }
1075
1076 small_range_map(index_type limit = N) : size_(0), limit_(static_cast<SmallIndex>(limit)) {
1077 RANGE_ASSERT(limit <= std::numeric_limits<SmallIndex>::max());
1078 init_range();
1079 }
1080
1081 // Only valid for empty maps
1082 void set_limit(size_t limit) {
1083 RANGE_ASSERT(size_ == 0);
1084 RANGE_ASSERT(limit <= std::numeric_limits<SmallIndex>::max());
1085 limit_ = static_cast<SmallIndex>(limit);
1086 init_range();
1087 }
1088 inline index_type get_limit() const { return static_cast<index_type>(limit_); }
1089
1090 private:
1091 inline bool in_bounds(index_type index) const { return index < get_limit(); }
1092 inline bool in_bounds(const RangeKey &key) const { return key.begin < get_limit() && key.end <= get_limit(); }
1093
1094 inline SmallRange make_small_range(const RangeKey &key) const {
1095 RANGE_ASSERT(in_bounds(key));
1096 return SmallRange(static_cast<SmallIndex>(key.begin), static_cast<SmallIndex>(key.end));
1097 }
1098
1099 inline SmallRange make_invalid_range(const SmallRange &key) const { return SmallRange(key.end, key.begin); }
1100
1101 bool is_open(const key_type &key) const {
1102 // Remebering that invalid range.begin is the beginning the next used range.
1103 const auto small_key = make_small_range(key);
1104 const auto &range = ranges_[small_key.begin];
1105 return range.invalid() && small_key.end <= range.begin;
1106 }
1107 // Only call this with a valid beginning index
1108 iterator erase_impl(SmallIndex erase_index) {
1109 RANGE_ASSERT(erase_index == ranges_[erase_index].begin);
1110 auto &range = ranges_[erase_index];
1111 destruct_value(erase_index);
1112 // Need to update the ranges to accommodate the erasure
1113 SmallIndex prev = 0; // This is correct for the case erase_index is 0....
1114 if (erase_index != 0) {
1115 prev = prev_range(erase_index);
1116 // This works if prev is valid or invalid, because the invalid end will be either 0 (and correct) or the end of the
1117 // prior valid range and the valid end will be the end of the previous range (and thus correct)
1118 prev = ranges_[prev].end;
1119 }
1120 auto next = next_range(erase_index);
1121 // We have to be careful of next == limit_...
1122 if (next < limit_) {
1123 next = ranges_[next].begin;
1124 }
1125 // Rewrite both adjoining and newly empty entries
1126 SmallRange infill(next, prev);
1127 for (auto i = prev; i < next; ++i) {
1128 ranges_[i] = infill;
1129 }
1130 return iterator(this, next);
1131 }
1132 // This implements the "range lower bound logic" directly on the ranges
1133 template <typename Map>
1134 static SmallIndex lower_bound_impl(Map *const that, const key_type &key) {
1135 if (!that->in_bounds(key.begin)) return that->limit_;
1136 // If range is invalid, then begin points to the next valid (or end) with must be the lower bound
1137 // If range is valid, the begin points to a the lowest range that interects key
1138 const auto lb = that->ranges_[static_cast<SmallIndex>(key.begin)].begin;
1139 return lb;
1140 }
1141
1142 template <typename Map>
1143 static SmallIndex upper_bound_impl(Map *that, const key_type &key) {
1144 const auto limit = that->get_limit();
1145 if (key.end >= limit) return that->limit_; // at end
1146 const auto &end_range = that->ranges_[key.end];
1147 // If range is invalid, then begin points to the next valid (or end) with must be the upper bound (key < range because
1148 auto ub = end_range.begin;
1149 // If range is valid, the begin points to a range that may interects key, which is be upper iff range.begin == key.end
1150 if (end_range.valid() && (key.end > end_range.begin)) {
1151 // the ub candidate *intersects* the key, so we have to go to the next range.
1152 ub = that->next_range(end_range.begin);
1153 }
1154 return ub;
1155 }
1156
1157 // This is and inclusive "inuse", the entry itself
1158 SmallIndex find_inuse_right(const SmallRange &range) const {
1159 if (range.end >= limit_) return limit_;
1160 // if range is valid, begin is the first use (== range.end), else it's the first used after the invalid range
1161 return ranges_[range.end].begin;
1162 }
1163 // This is an exclusive "inuse", the end of the previous range
1164 SmallIndex find_inuse_left(const SmallRange &range) const {
1165 if (range.begin == 0) return 0;
1166 // if range is valid, end is the end of the first use (== range.begin), else it's the end of the in use range before the
1167 // invalid range
1168 return ranges_[range.begin - 1].end;
1169 }
1170 SmallRange find_empty(const SmallRange &range) const { return SmallRange(find_inuse_left(range), find_inuse_right(range)); }
1171
1172 // Clear out the given range, trimming as needed. The clear_range can be set as valid or invalid
1173 SmallRange clear_out_range(const SmallRange &clear_range, bool valid_clear_range) {
1174 // By copy to avoid reranging side affect
1175 auto first_range = ranges_[clear_range.begin];
1176
1177 // fast path for matching ranges...
1178 if (first_range == clear_range) {
1179 // clobber the existing value
1180 destruct_value(clear_range.begin);
1181 if (valid_clear_range) {
1182 return clear_range; // This is the overwrite fastpath for matching range
1183 } else {
1184 const auto empty_range = find_empty(clear_range);
1185 rerange(empty_range, make_invalid_range(empty_range));
1186 return empty_range;
1187 }
1188 }
1189
1190 SmallRange empty_left(clear_range.begin, clear_range.begin);
1191 SmallRange empty_right(clear_range.end, clear_range.end);
1192
1193 // The clearout is entirely within a single extant range, trim and set.
1194 if (first_range.valid() && first_range.includes(clear_range)) {
1195 // Shuffle around first_range, three cases...
1196 if (first_range.begin < clear_range.begin) {
1197 // We have a lower trimmed area to preserve.
1198 resize_value(first_range.begin, clear_range.begin);
1199 rerange_end(first_range.begin, clear_range.begin, clear_range.begin);
1200 if (first_range.end > clear_range.end) {
1201 // And an upper portion of first that needs to copy from the lower
1202 construct_value(clear_range.end, std::make_pair(key_type(clear_range.end, first_range.end),
1203 get_value(first_range.begin)->second));
1204 rerange_begin(clear_range.end, first_range.end, clear_range.end);
1205 } else {
1206 RANGE_ASSERT(first_range.end == clear_range.end);
1207 empty_right.end = find_inuse_right(clear_range);
1208 }
1209 } else {
1210 RANGE_ASSERT(first_range.end > clear_range.end);
1211 RANGE_ASSERT(first_range.begin == clear_range.begin);
1212 // Only an upper trimmed area to preserve, so move the first range value to the upper trim zone.
1213 resize_value_right(first_range, clear_range.end);
1214 rerange_begin(clear_range.end, first_range.end, clear_range.end);
1215 empty_left.begin = find_inuse_left(clear_range);
1216 }
1217 } else {
1218 if (first_range.valid()) {
1219 if (first_range.begin < clear_range.begin) {
1220 // Trim left.
1221 RANGE_ASSERT(first_range.end < clear_range.end); // we handled the "includes" case above
1222 resize_value(first_range.begin, clear_range.begin);
1223 rerange_end(first_range.begin, clear_range.begin, clear_range.begin);
1224 }
1225 } else {
1226 empty_left.begin = find_inuse_left(clear_range);
1227 }
1228
1229 // rewrite excluded portion of final range
1230 if (clear_range.end < limit_) {
1231 const auto &last_range = ranges_[clear_range.end];
1232 if (last_range.valid()) {
1233 // for a valid adjoining range we don't have to change empty_right, but we may have to trim
1234 if (last_range.begin < clear_range.end) {
1235 resize_value_right(last_range, clear_range.end);
1236 rerange_begin(clear_range.end, last_range.end, clear_range.end);
1237 }
1238 } else {
1239 // Note: invalid ranges "begin" and the next inuse range (or end)
1240 empty_right.end = last_range.begin;
1241 }
1242 }
1243 }
1244
1245 const SmallRange empty(empty_left.begin, empty_right.end);
1246 // Clear out the contents
1247 for (auto i = empty.begin; i < empty.end; ++i) {
1248 const auto &range = ranges_[i];
1249 if (range.begin == i) {
1250 RANGE_ASSERT(range.valid());
1251 // Clean up the backing store
1252 destruct_value(i);
1253 }
1254 }
1255
1256 // Rewrite the ranges
1257 if (valid_clear_range) {
1258 rerange_begin(empty_left.begin, empty_left.end, clear_range.begin);
1259 rerange(clear_range, clear_range);
1260 rerange_end(empty_right.begin, empty_right.end, clear_range.end);
1261 } else {
1262 rerange(empty, make_invalid_range(empty));
1263 }
1264 RANGE_ASSERT(empty.end == limit_ || ranges_[empty.end].valid());
1265 RANGE_ASSERT(empty.begin == 0 || ranges_[empty.begin - 1].valid());
1266 return empty;
1267 }
1268
1269 void init_range() {
1270 const SmallRange init_val(limit_, 0);
1271 for (SmallIndex i = 0; i < limit_; ++i) {
1272 ranges_[i] = init_val;
1273 in_use_[i] = false;
1274 }
1275 }
1276 value_type *get_value(SmallIndex index) {
1277 RANGE_ASSERT(index < limit_); // Must be inbounds
1278 return reinterpret_cast<value_type *>(&(backing_store_[index]));
1279 }
1280 const value_type *get_value(SmallIndex index) const {
1281 RANGE_ASSERT(index < limit_); // Must be inbounds
1282 RANGE_ASSERT(index == ranges_[index].begin); // Must be the record at begin
1283 return reinterpret_cast<const value_type *>(&(backing_store_[index]));
1284 }
1285
1286 template <typename Value>
1287 void construct_value(SmallIndex index, Value &&value) {
1288 RANGE_ASSERT(!in_use_[index]);
1289 new (get_value(index)) value_type(std::forward<Value>(value));
1290 in_use_[index] = true;
1291 ++size_;
1292 }
1293
1294 void destruct_value(SmallIndex index) {
1295 // there are times when the range and destruct logic clash... allow for double attempted deletes
1296 if (in_use_[index]) {
1297 RANGE_ASSERT(size_ > 0);
1298 --size_;
1299 get_value(index)->~value_type();
1300 in_use_[index] = false;
1301 }
1302 }
1303
1304 // No need to move around the value, when just the key is moving
1305 // Use the destructor/placement new just in case of a complex key with range's semantics
1306 // Note: Call resize before rewriting ranges_
1307 void resize_value(SmallIndex current_begin, index_type new_end) {
1308 // Destroy and rewrite the key in place
1309 RANGE_ASSERT(ranges_[current_begin].end != new_end);
1310 key_type new_key(current_begin, new_end);
1311 key_type *key = const_cast<key_type *>(&get_value(current_begin)->first);
1312 key->~key_type();
1313 new (key) key_type(new_key);
1314 }
1315
1316 inline void rerange_end(SmallIndex old_begin, SmallIndex new_end, SmallIndex new_end_value) {
1317 for (auto i = old_begin; i < new_end; ++i) {
1318 ranges_[i].end = new_end_value;
1319 }
1320 }
1321 inline void rerange_begin(SmallIndex new_begin, SmallIndex old_end, SmallIndex new_begin_value) {
1322 for (auto i = new_begin; i < old_end; ++i) {
1323 ranges_[i].begin = new_begin_value;
1324 }
1325 }
1326 inline void rerange(const SmallRange &range, const SmallRange &range_value) {
1327 for (auto i = range.begin; i < range.end; ++i) {
1328 ranges_[i] = range_value;
1329 }
1330 }
1331
1332 // for resize right need both begin and end...
1333 void resize_value_right(const SmallRange &current_range, index_type new_begin) {
1334 // Use move semantics for (potentially) heavyweight mapped_type's
1335 RANGE_ASSERT(current_range.begin != new_begin);
1336 // Move second from it's current location and update the first at the same time
1337 construct_value(static_cast<SmallIndex>(new_begin),
1338 std::make_pair(key_type(new_begin, current_range.end), std::move(get_value(current_range.begin)->second)));
1339 destruct_value(current_range.begin);
1340 }
1341
1342 // Now we can walk a range and rewrite it cleaning up any live contents
1343 void clear_and_set_range(SmallIndex rewrite_begin, SmallIndex rewrite_end, const SmallRange &new_range) {
1344 for (auto i = rewrite_begin; i < rewrite_end; ++i) {
1345 auto &range = ranges_[i];
1346 if (i == range.begin) {
1347 destruct_value(i);
1348 }
1349 range = new_range;
1350 }
1351 }
1352
1353 SmallIndex next_range(SmallIndex current) const {
1354 SmallIndex next = ranges_[current].end;
1355 // If the next range is invalid, skip to the next range, which *must* be (or be end)
1356 if ((next < limit_) && ranges_[next].invalid()) {
1357 // For invalid ranges, begin is the beginning of the next range
1358 next = ranges_[next].begin;
1359 RANGE_ASSERT(next == limit_ || ranges_[next].valid());
1360 }
1361 return next;
1362 }
1363
1364 SmallIndex prev_range(SmallIndex current) const {
1365 if (current == 0) {
1366 return 0;
1367 }
1368
1369 auto prev = current - 1;
1370 if (ranges_[prev].valid()) {
1371 // For valid ranges, the range denoted by begin (as that's where the backing store keeps values
1372 prev = ranges_[prev].begin;
1373 } else if (prev != 0) {
1374 // Invalid but not off the front, we can recur (only once) from the end of the prev range to get the answer
1375 // For invalid ranges this is the end of the previous range
1376 prev = prev_range(ranges_[prev].end);
1377 }
1378 return prev;
1379 }
1380
1381 friend iterator;
1382 friend const_iterator;
1383 // Stores range boundaries only
1384 // open ranges, stored as inverted, invalid range (begining of next, end of prev]
1385 // inuse(begin, end) for all entries on (begin, end]
1386 // Used for placement new of T for each range begin.
1387 struct alignas(alignof(value_type)) BackingStore {
1388 uint8_t data[sizeof(value_type)];
1389 };
1390
1391 SmallIndex size_;
1392 SmallIndex limit_;
1393 std::array<SmallRange, N> ranges_;
1394 std::array<BackingStore, N> backing_store_;
1395 std::array<bool, N> in_use_;
1396};
1397
John Zulauf11211402019-11-15 14:02:36 -07001398// Forward index iterator, tracking an index value and the appropos lower bound
1399// returns an index_type, lower_bound pair. Supports ++, offset, and seek affecting the index,
1400// lower bound updates as needed. As the index may specify a range for which no entry exist, dereferenced
1401// iterator includes an "valid" field, true IFF the lower_bound is not end() and contains [index, index +1)
1402//
1403// Must be explicitly invalidated when the underlying map is changed.
1404template <typename Map>
1405class cached_lower_bound_impl {
1406 using plain_map_type = typename std::remove_const<Map>::type; // Allow instatiation with const or non-const Map
1407 public:
1408 using iterator = const_correct_iterator<Map>;
1409 using key_type = typename plain_map_type::key_type;
1410 using mapped_type = typename plain_map_type::mapped_type;
1411 // Both sides of the return pair are const'd because we're returning references/pointers to the *internal* state
1412 // and we don't want and caller altering internal state.
1413 using index_type = typename Map::index_type;
1414 struct value_type {
1415 const index_type &index;
1416 const iterator &lower_bound;
1417 const bool &valid;
1418 value_type(const index_type &index_, const iterator &lower_bound_, bool &valid_)
1419 : index(index_), lower_bound(lower_bound_), valid(valid_) {}
1420 };
1421
1422 private:
1423 Map *const map_;
1424 value_type pos_;
1425
1426 index_type index_;
1427 iterator lower_bound_;
1428 bool valid_;
1429
1430 bool is_valid() const { return includes(index_); }
1431
1432 // Allow reuse of a type with const semantics
1433 void set_value(const index_type &index, const iterator &it) {
John Zulauf6066f732019-11-21 13:15:10 -07001434 RANGE_ASSERT(it == lower_bound(index));
John Zulauf11211402019-11-15 14:02:36 -07001435 index_ = index;
1436 lower_bound_ = it;
1437 valid_ = is_valid();
1438 }
John Zulauf6066f732019-11-21 13:15:10 -07001439
John Zulauf11211402019-11-15 14:02:36 -07001440 void update(const index_type &index) {
John Zulauf6066f732019-11-21 13:15:10 -07001441 RANGE_ASSERT(lower_bound_ == lower_bound(index));
1442 index_ = index;
1443 valid_ = is_valid();
John Zulauf11211402019-11-15 14:02:36 -07001444 }
John Zulauf6066f732019-11-21 13:15:10 -07001445
John Zulauf11211402019-11-15 14:02:36 -07001446 inline iterator lower_bound(const index_type &index) { return map_->lower_bound(key_type(index, index + 1)); }
1447 inline bool at_end(const iterator &it) const { return it == map_->end(); }
1448 inline bool at_end() const { return at_end(lower_bound_); }
1449
1450 bool is_lower_than(const index_type &index, const iterator &it) { return at_end(it) || (index < it->first.end); }
1451
1452 public:
1453 // includes(index) is a convenience function to test if the index would be in the currently cached lower bound
1454 bool includes(const index_type &index) const { return !at_end() && lower_bound_->first.includes(index); }
1455
1456 // The return is const because we are sharing the internal state directly.
1457 const value_type &operator*() const { return pos_; }
1458 const value_type *operator->() const { return &pos_; }
1459
1460 // Advance the cached location by 1
1461 cached_lower_bound_impl &operator++() {
1462 const index_type next = index_ + 1;
1463 if (is_lower_than(next, lower_bound_)) {
1464 update(next);
1465 } else {
1466 // if we're past pos_->second, next *must* be the new lower bound.
1467 // NOTE: that next can't be past end, so lower_bound_ isn't end.
1468 auto next_it = lower_bound_;
1469 ++next_it;
1470 set_value(next, next_it);
1471
1472 // However we *must* not be past next.
1473 RANGE_ASSERT(is_lower_than(next, next_it));
1474 }
1475
1476 return *this;
1477 }
1478
John Zulauf6066f732019-11-21 13:15:10 -07001479 // seek(index) updates lower_bound for index, updating lower_bound_ as needed.
1480 cached_lower_bound_impl &seek(const index_type &seek_to) {
1481 // Optimize seeking to forward
1482 if (index_ == seek_to) {
1483 // seek to self is a NOOP. To reset lower bound after a map change, use invalidate
1484 } else if (index_ < seek_to) {
1485 // See if the current or next ranges are the appropriate lower_bound... should be a common use case
1486 if (is_lower_than(seek_to, lower_bound_)) {
1487 // lower_bound_ is still the correct lower bound
1488 update(seek_to);
1489 } else {
1490 // Look to see if the next range is the new lower_bound (and we aren't at end)
1491 auto next_it = lower_bound_;
1492 ++next_it;
1493 if (is_lower_than(seek_to, next_it)) {
1494 // next_it is the correct new lower bound
1495 set_value(seek_to, next_it);
1496 } else {
1497 // We don't know where we are... and we aren't going to walk the tree looking for seek_to.
1498 set_value(seek_to, lower_bound(seek_to));
1499 }
1500 }
1501 } else {
1502 // General case... this is += so we're not implmenting optimized negative offset logic
1503 set_value(seek_to, lower_bound(seek_to));
1504 }
1505 return *this;
John Zulauf11211402019-11-15 14:02:36 -07001506 }
1507
1508 // Advance the cached location by offset.
1509 cached_lower_bound_impl &offset(const index_type &offset) {
1510 const index_type next = index_ + offset;
John Zulauf6066f732019-11-21 13:15:10 -07001511 return seek(next);
John Zulauf11211402019-11-15 14:02:36 -07001512 }
1513
1514 // invalidate() resets the the lower_bound_ cache, needed after insert/erase/overwrite/split operations
John Zulauf6066f732019-11-21 13:15:10 -07001515 cached_lower_bound_impl &invalidate() {
John Zulauf11211402019-11-15 14:02:36 -07001516 index_type index = index_; // copy as set modifies in place.
John Zulauf6066f732019-11-21 13:15:10 -07001517 set_value(index, lower_bound(index));
1518 return *this;
John Zulauf11211402019-11-15 14:02:36 -07001519 }
1520
1521 // Allow a hint for a *valid* lower bound for current index
1522 // TODO: if the fail-over becomes a hot-spot, the hint logic could be far more clever (looking at previous/next...)
John Zulauf6066f732019-11-21 13:15:10 -07001523 cached_lower_bound_impl &invalidate(const iterator &hint) {
John Zulauf11211402019-11-15 14:02:36 -07001524 if ((hint != map_->end()) && hint->first.includes(index_)) {
1525 auto index = index_; // by copy set modifies in place
1526 set_value(index, hint);
1527 } else {
1528 invalidate();
1529 }
John Zulauf6066f732019-11-21 13:15:10 -07001530 return *this;
John Zulauf11211402019-11-15 14:02:36 -07001531 }
1532
1533 // The offset in index type to the next change (the end of the current range, or the transition from invalid to
1534 // valid. If invalid and at_end, returns index_type(0)
1535 index_type distance_to_edge() {
1536 if (valid_) {
1537 // Distance to edge of
1538 return lower_bound_->first.end - index_;
1539 } else if (at_end()) {
1540 return index_type(0);
1541 } else {
1542 return lower_bound_->first.begin - index_;
1543 }
1544 }
1545
1546 // Default constructed object reports valid (correctly) as false, but otherwise will fail (assert) under nearly any use.
1547 cached_lower_bound_impl() : map_(nullptr), pos_(index_, lower_bound_, valid_), index_(0), lower_bound_(), valid_(false) {}
John Zulauf6066f732019-11-21 13:15:10 -07001548 cached_lower_bound_impl(Map &map, const index_type &index)
1549 : map_(&map), pos_(index_, lower_bound_, valid_), index_(index), lower_bound_(lower_bound(index)), valid_(is_valid()) {}
John Zulauf11211402019-11-15 14:02:36 -07001550};
1551
1552template <typename CachedLowerBound, typename MappedType = typename CachedLowerBound::mapped_type>
1553const MappedType &evaluate(const CachedLowerBound &clb, const MappedType &default_value) {
1554 if (clb->valid) {
1555 return clb->lower_bound->second;
1556 }
1557 return default_value;
1558}
1559
1560// Parallel iterator
1561// Traverse to range maps over the the same range, but without assumptions of aligned ranges.
1562// ++ increments to the next point where on of the two maps changes range, giving a range over which the two
1563// maps do not transition ranges
1564template <typename MapA, typename MapB, typename KeyType = typename MapA::key_type>
1565class parallel_iterator {
1566 public:
1567 using key_type = KeyType;
1568 using index_type = typename key_type::index_type;
1569
1570 // The traits keep the iterator/const_interator consistent with the constness of the map.
1571 using map_type_A = MapA;
1572 using plain_map_type_A = typename std::remove_const<MapA>::type; // Allow instatiation with const or non-const Map
1573 using key_type_A = typename plain_map_type_A::key_type;
1574 using index_type_A = typename plain_map_type_A::index_type;
1575 using iterator_A = const_correct_iterator<map_type_A>;
1576 using lower_bound_A = cached_lower_bound_impl<map_type_A>;
1577
1578 using map_type_B = MapB;
1579 using plain_map_type_B = typename std::remove_const<MapB>::type;
1580 using key_type_B = typename plain_map_type_B::key_type;
1581 using index_type_B = typename plain_map_type_B::index_type;
1582 using iterator_B = const_correct_iterator<map_type_B>;
1583 using lower_bound_B = cached_lower_bound_impl<map_type_B>;
1584
1585 // This is the value we'll always be returning, but the referenced object will be updated by the operations
1586 struct value_type {
1587 const key_type &range;
1588 const lower_bound_A &pos_A;
1589 const lower_bound_B &pos_B;
1590 value_type(const key_type &range_, const lower_bound_A &pos_A_, const lower_bound_B &pos_B_)
1591 : range(range_), pos_A(pos_A_), pos_B(pos_B_) {}
1592 };
1593
1594 private:
1595 lower_bound_A pos_A_;
1596 lower_bound_B pos_B_;
1597 key_type range_;
1598 value_type pos_;
1599 index_type compute_delta() {
1600 auto delta_A = pos_A_.distance_to_edge();
1601 auto delta_B = pos_B_.distance_to_edge();
1602 index_type delta_min;
1603
1604 // If either A or B are at end, there distance is *0*, so shouldn't be considered in the "distance to edge"
1605 if (delta_A == 0) { // lower A is at end
1606 delta_min = static_cast<index_type>(delta_B);
1607 } else if (delta_B == 0) { // lower B is at end
1608 delta_min = static_cast<index_type>(delta_A);
1609 } else {
1610 // Neither are at end, use the nearest edge, s.t. over this range A and B are both constant
1611 delta_min = std::min(static_cast<index_type>(delta_A), static_cast<index_type>(delta_B));
1612 }
1613 return delta_min;
1614 }
1615
1616 public:
1617 // Default constructed object will report range empty (for end checks), but otherwise is unsafe to use
1618 parallel_iterator() : pos_A_(), pos_B_(), range_(), pos_(range_, pos_A_, pos_B_) {}
1619 parallel_iterator(map_type_A &map_A, map_type_B &map_B, index_type index)
1620 : pos_A_(map_A, static_cast<index_type_A>(index)),
1621 pos_B_(map_B, static_cast<index_type_B>(index)),
1622 range_(index, index + compute_delta()),
1623 pos_(range_, pos_A_, pos_B_) {}
1624
1625 // Advance to the next spot one of the two maps changes
1626 parallel_iterator &operator++() {
1627 const auto start = range_.end; // we computed this the last time we set range
1628 const auto delta = range_.distance(); // we computed this the last time we set range
1629 RANGE_ASSERT(delta != 0); // Trying to increment past end
1630
1631 pos_A_.offset(static_cast<index_type_A>(delta));
1632 pos_B_.offset(static_cast<index_type_B>(delta));
1633
1634 range_ = key_type(start, start + compute_delta()); // find the next boundary (must be after offset)
1635 RANGE_ASSERT(pos_A_->index == start);
1636 RANGE_ASSERT(pos_B_->index == start);
1637
1638 return *this;
1639 }
1640
1641 // Seeks to a specific index in both maps reseting range. Cannot guarantee range.begin is on edge boundary,
1642 /// but range.end will be. Lower bound objects assumed to invalidate their cached lower bounds on seek.
1643 parallel_iterator &seek(const index_type &index) {
1644 pos_A_.seek(static_cast<index_type_A>(index));
1645 pos_B_.seek(static_cast<index_type_B>(index));
1646 range_ = key_type(index, index + compute_delta());
1647 RANGE_ASSERT(pos_A_->index == index);
1648 RANGE_ASSERT(pos_A_->index == pos_B_->index);
1649 return *this;
1650 }
1651
1652 // Invalidates the lower_bound caches, reseting range. Cannot guarantee range.begin is on edge boundary,
1653 // but range.end will be.
1654 parallel_iterator &invalidate() {
1655 const index_type start = range_.begin;
1656 seek(start);
1657 return *this;
1658 }
1659 parallel_iterator &invalidate_A() {
1660 const index_type index = range_.begin;
1661 pos_A_.seek(static_cast<index_type_A>(index));
1662 range_ = key_type(index, index + compute_delta());
1663 return *this;
1664 }
1665 parallel_iterator &invalidate_B() {
1666 const index_type index = range_.begin;
1667 pos_B_.seek(static_cast<index_type_B>(index));
1668 range_ = key_type(index, index + compute_delta());
1669 return *this;
1670 }
1671
1672 // The return is const because we are sharing the internal state directly.
1673 const value_type &operator*() const { return pos_; }
1674 const value_type *operator->() const { return &pos_; }
1675};
1676
John Zulauf11211402019-11-15 14:02:36 -07001677template <typename RangeMap, typename SourceIterator = typename RangeMap::const_iterator>
John Zulauf81408f12019-11-27 16:40:27 -07001678bool splice(RangeMap *to, const RangeMap &from, value_precedence arbiter, SourceIterator begin, SourceIterator end) {
John Zulauf11211402019-11-15 14:02:36 -07001679 if (from.empty() || (begin == end) || (begin == from.cend())) return false; // nothing to merge.
1680
1681 using ParallelIterator = parallel_iterator<RangeMap, const RangeMap>;
1682 using Key = typename RangeMap::key_type;
1683 using CachedLowerBound = cached_lower_bound_impl<RangeMap>;
1684 using ConstCachedLowerBound = cached_lower_bound_impl<const RangeMap>;
John Zulauf11211402019-11-15 14:02:36 -07001685 ParallelIterator par_it(*to, from, begin->first.begin);
1686 bool updated = false;
1687 while (par_it->range.non_empty() && par_it->pos_B->lower_bound != end) {
1688 const Key &range = par_it->range;
1689 const CachedLowerBound &to_lb = par_it->pos_A;
1690 const ConstCachedLowerBound &from_lb = par_it->pos_B;
1691 if (from_lb->valid) {
1692 auto read_it = from_lb->lower_bound;
1693 auto write_it = to_lb->lower_bound;
1694 // Because of how the parallel iterator walk, "to" is valid over the whole range or it isn't (ranges don't span
1695 // transitions between map entries or between valid and invalid ranges)
1696 if (to_lb->valid) {
1697 // Only rewrite this range if source is preferred (and the value differs)
1698 // TODO determine if equality checks are always wanted. (for example heavyweight values)
John Zulauf81408f12019-11-27 16:40:27 -07001699 if (arbiter == value_precedence::prefer_source && (write_it->second != read_it->second)) {
John Zulauf11211402019-11-15 14:02:36 -07001700 // Both ranges occupied and source is preferred and from differs from to
1701 if (write_it->first == range) {
1702 // we're writing the whole destination range, so just set the value
1703 write_it->second = read_it->second;
1704 } else {
1705 to->overwrite_range(write_it, std::make_pair(range, read_it->second));
1706 par_it.invalidate_A(); // we've changed map 'to' behind to_lb's back... let it know.
1707 }
1708 updated = true;
1709 }
1710 } else {
1711 // Insert into the gap.
John Zulauf81408f12019-11-27 16:40:27 -07001712 to->insert(write_it, std::make_pair(range, read_it->second));
John Zulauf11211402019-11-15 14:02:36 -07001713 par_it.invalidate_A(); // we've changed map 'to' behind to_lb's back... let it know.
1714 updated = true;
1715 }
1716 }
1717 ++par_it; // next range over which both 'to' and 'from' stay constant
1718 }
1719 return updated;
1720}
1721// And short hand for "from begin to end"
1722template <typename RangeMap>
John Zulauf81408f12019-11-27 16:40:27 -07001723bool splice(RangeMap *to, const RangeMap &from, value_precedence arbiter) {
John Zulauf11211402019-11-15 14:02:36 -07001724 return splice(to, from, arbiter, from.cbegin(), from.cend());
1725}
1726
John Zulauf81408f12019-11-27 16:40:27 -07001727template <typename Map, typename Range = typename Map::key_type, typename MapValue = typename Map::mapped_type>
1728bool update_range_value(Map &map, const Range &range, MapValue &&value, value_precedence precedence) {
1729 using CachedLowerBound = typename sparse_container::cached_lower_bound_impl<Map>;
1730 CachedLowerBound pos(map, range.begin);
1731
1732 bool updated = false;
1733 while (range.includes(pos->index)) {
1734 if (!pos->valid) {
1735 if (precedence == value_precedence::prefer_source) {
1736 // We can convert this into and overwrite...
1737 map.overwrite_range(pos->lower_bound, std::make_pair(range, std::forward<MapValue>(value)));
1738 return true;
1739 }
1740 // Fill in the leading space (or in the case of pos at end the trailing space
1741 const auto start = pos->index;
1742 auto it = pos->lower_bound;
1743 const auto limit = (it != map.end()) ? std::min(it->first.begin, range.end) : range.end;
1744 map.insert(it, std::make_pair(Range(start, limit), value));
1745 // We inserted before pos->lower_bound, so pos->lower_bound isn't invalid, but the associated index *is* and seek
1746 // will fix this (and move the state to valid)
1747 pos.seek(limit);
1748 updated = true;
1749 }
1750 // Note that after the "fill" operation pos may have become valid so we check again
1751 if (pos->valid) {
1752 if ((precedence == value_precedence::prefer_source) && (pos->lower_bound->second != value)) {
1753 // We've found a place where we're changing the value, at this point might as well simply over write the range
1754 // and be done with it. (save on later merge operations....
1755 map.overwrite_range(pos->lower_bound, std::make_pair(range, std::forward<MapValue>(value)));
1756 return true;
1757#if 0
1758 // Pointing to an occupied interval, we are updating the values, and the value differs from the current one
1759 auto it = pos->lower_bound;
1760 auto start = it->first.begin;
1761 if (start < range.begin) {
1762 // if the current interval starts before the range, split and skip the leading section
1763 it = map.split(it, range.begin, split_op_keep_both());
1764 pos.invalidate(it); // let the cached lower bound know we've change the portion of the map it's pointing to
1765 ++it;
1766 start = it->first.begin;
1767 assert(it->first.begin == range.begin);
1768 }
1769 auto limit = it->first.end;
1770 if (limit > range.end) {
1771 // if the current interval ends beyond the range, split and point
1772 it = map.split(it, range.end, split_op_keep_both());
1773 pos.invalidate(it); // let the cached lower bound know we've change the portion of the map it's pointing to
1774 limit = it->first.end;
1775 assert(it->first.end == range.end);
1776 }
1777 updated = true;
1778 it->second = value;
1779 pos.seek(limit);
1780#endif
1781 } else {
1782 // "prefer_dest" means don't overwrite existing values, so we'll skip this interval.
1783 // Point just past the end of this section, if it's within the given range, it will get filled next iteration
1784 // ++pos could move us past the end of range (which would exit the loop) so we don't use it.
1785 pos.seek(pos->lower_bound->first.end);
1786 }
1787 }
1788 }
1789 return updated;
1790}
1791
John Zulauf11211402019-11-15 14:02:36 -07001792} // namespace sparse_container
1793
1794#endif