segmap/map.rs
1use alloc::{collections::BTreeMap, vec::Vec};
2use core::{
3 cmp::{max, Ordering},
4 fmt::Debug,
5 hash::{Hash, Hasher},
6 ops::{Bound, Index, RangeBounds},
7};
8
9use crate::segment::{Segment, Start};
10pub(crate) use key::Key;
11
12pub mod iterators;
13mod key;
14
15#[cfg(test)]
16mod tests;
17
18/// # SegmentMap
19///
20/// A map of non-overlapping ranges to values. Inserted ranges will be merged
21/// with adjacent ranges if they have the same value.
22///
23/// Internally, [`SegmentMap`] is represented by a [`BTreeMap`] in which the keys
24/// are represented by a concrete [`Range`] type, sorted by their start values.
25///
26/// # Examples
27///
28/// TODO
29///
30/// # Entry API
31///
32/// TODO
33///
34#[derive(Clone)]
35pub struct SegmentMap<K, V> {
36 pub(crate) map: BTreeMap<Key<K>, V>,
37
38 /// Reuseable storage for working set of keys
39 /// (many insertions/deletions will allocate less)
40 ///
41 /// TODO Performance Improvement:
42 /// This (and successor key collection) could be more streamlined with a
43 /// few strategically placed `unsafe` blocks
44 pub(crate) store: Vec<Key<K>>,
45}
46
47impl<K, V> SegmentMap<K, V> {
48 /// Makes a new, empty `SegmentMap`.
49 ///
50 /// Does not allocate anything on its own.
51 ///
52 /// # Examples
53 ///
54 /// Basic usage:
55 ///
56 /// ```
57 /// # use segmap::*;
58 /// let mut map = SegmentMap::new();
59 ///
60 /// // entries can now be inserted into the empty map
61 /// map.insert(0..1, "a");
62 /// ```
63 pub fn new() -> Self
64 where
65 K: Ord,
66 {
67 SegmentMap {
68 map: BTreeMap::new(),
69 store: Vec::new(),
70 }
71 }
72
73 /// Makes a new, empty [`SegmentMap`] with a value for the full range.
74 ///
75 /// # Examples
76 ///
77 /// Basic usage:
78 ///
79 /// ```
80 /// # use segmap::*;
81 /// let mut map = SegmentMap::with_value(true);
82 ///
83 /// // All values will return something
84 /// assert_eq!(map[&0], true);
85 /// assert_eq!(map[&10], true);
86 /// assert_eq!(map[&12345678], true);
87 /// ```
88 pub fn with_value(value: V) -> Self
89 where
90 K: Ord,
91 {
92 let mut inner = BTreeMap::new();
93 inner.insert(Key(Segment::full()), value);
94 SegmentMap {
95 map: inner,
96 store: Vec::new(),
97 }
98 }
99
100 /// Clears the map, removing all elements.
101 ///
102 /// This also resets the capacity of the internal `store`.
103 ///
104 /// # Examples
105 ///
106 /// Basic usage:
107 ///
108 /// ```
109 /// # use segmap::*;
110 /// let mut a = SegmentMap::new();
111 /// a.insert(0..1, "a");
112 /// a.clear();
113 /// assert!(a.is_empty());
114 /// ```
115 pub fn clear(&mut self) {
116 self.map.clear();
117 self.store = Vec::new(); // Reset capacity
118 }
119
120 /// Resets the capacity of `store`
121 pub fn shrink_to_fit(&mut self) {
122 self.store = Vec::new(); // Reset capacity
123 }
124
125 /// Returns a reference to the value corresponding to the given point,
126 /// if the point is covered by any range in the map.
127 ///
128 /// # Examples
129 ///
130 /// Basic usage:
131 ///
132 /// ```
133 /// # use segmap::*;
134 /// let mut map = SegmentMap::new();
135 /// map.insert(0..1, "a");
136 /// assert_eq!(map.get(&0), Some(&"a"));
137 /// assert_eq!(map.get(&2), None);
138 /// ```
139 pub fn get(&self, at: &K) -> Option<&V>
140 where
141 K: Clone + Ord,
142 {
143 self.get_range_value(at).map(|(_range, value)| value)
144 }
145
146 /// Returns the range-value pair (as a pair of references) corresponding
147 /// to the given point, if the point is covered by any range in the map.
148 ///
149 /// # Examples
150 ///
151 /// ```
152 /// # use segmap::*;
153 /// let mut map = SegmentMap::new();
154 /// map.insert(0..1, "a");
155 /// assert_eq!(map.get_range_value(&0), Some((&Segment::from(0..1), &"a")));
156 /// assert_eq!(map.get_range_value(&2), None);
157 /// ```
158 pub fn get_range_value(&self, at: &K) -> Option<(&Segment<K>, &V)>
159 where
160 K: Clone + Ord,
161 {
162 // The only stored range that could contain the given key is the
163 // last stored range whose start is less than or equal to this key.
164 self.map
165 .range(..=(Start(Bound::Included(at.clone()))))
166 .rev()
167 .map(|(w, v)| (&w.0, v))
168 .next()
169 .filter(|(range, _)| range.contains(at))
170 }
171
172 // TODO: entry api for get_range_value_mut (since we can't control when &mut V is changed to do a merge/coalesce)
173
174 /// Returns `true` if any range in the map covers the specified point.
175 ///
176 /// # Examples
177 ///
178 /// Basic usage:
179 ///
180 /// ```
181 /// # use segmap::*;
182 /// let mut map = SegmentMap::new();
183 /// map.insert(0..1, "a");
184 /// assert_eq!(map.contains(&0), true);
185 /// assert_eq!(map.contains(&2), false);
186 /// ```
187 pub fn contains(&self, point: &K) -> bool
188 where
189 K: Clone + Ord,
190 {
191 self.get_range_value(point).is_some()
192 }
193
194 /// Get the widest bounds covered by the ranges in this map
195 ///
196 /// **NOTE**: This is not necessarily (or likely) a contiguous range!
197 /// # Examples
198 ///
199 /// ```
200 /// # use segmap::*;
201 /// let mut map = SegmentMap::new();
202 /// map.insert(0..9, "a");
203 /// map.insert(15..30, "b");
204 /// map.insert(35..90, "c");
205 ///
206 /// assert_eq!(map.bounds(), Some(Segment::from(0..90).as_ref()));
207 /// ```
208 pub fn bounds(&self) -> Option<Segment<&K>> {
209 let mut iter = self.map.iter();
210 iter.next().map(|(first, _)| {
211 if let Some((last, _)) = iter.next_back() {
212 // 2 or more items, use widest possible bounds
213 Segment {
214 start: first.0.start.as_ref(),
215 end: last.0.end.as_ref(),
216 }
217 } else {
218 // 1 item, use it's bounds
219 first.0.as_ref()
220 }
221 })
222 }
223
224 /// Get the lowest bound covered by the ranges in this map
225 ///
226 /// # Examples
227 ///
228 /// ```
229 /// # use segmap::*;
230 /// let mut map = SegmentMap::new();
231 /// map.insert(0..9, "a");
232 /// map.insert(15..30, "b");
233 /// map.insert(35..90, "c");
234 ///
235 /// assert_eq!(map.lower_bound(), Some(&Bound::Included(0)));
236 /// ```
237 pub fn lower_bound(&self) -> Option<&Bound<K>> {
238 self.map.iter().next().map(|(range, _)| &range.0.start.0)
239 }
240
241 /// Get the highest bound covered by the ranges in this map
242 ///
243 /// # Examples
244 ///
245 /// ```
246 /// # use segmap::*;
247 /// let mut map = SegmentMap::new();
248 /// map.insert(0..9, "a");
249 /// map.insert(15..30, "b");
250 /// map.insert(35..90, "c");
251 ///
252 /// assert_eq!(map.upper_bound(), Some(&Bound::Excluded(90)));
253 /// ```
254 pub fn upper_bound(&self) -> Option<&Bound<K>> {
255 self.map.iter().next_back().map(|(range, _)| &range.0.end.0)
256 }
257
258 /// Retains only the elements specified by the predicate.
259 ///
260 /// In other words, remove all pairs `(k, v)` such that `f(&k, &mut v)`
261 /// returns `false`.
262 ///
263 /// # Examples
264 ///
265 /// ```
266 /// # use segmap::*;
267 /// let mut map = SegmentMap::new();
268 /// map.set(0..5, true);
269 /// map.set(5..10, false);
270 /// map.set(10..15, true);
271 /// map.set(15..20, false);
272 /// map.set(20..25, true);
273 ///
274 /// // Keep only the ranges with even numbered starts
275 /// map.retain(|k, _| k.start_value().unwrap() % 2 == 0);
276 ///
277 /// assert!(map[&0] == true);
278 /// assert!(map[&10] == true);
279 /// assert!(map[&12] == true);
280 /// assert!(map[&20] == true);
281 /// assert!(map[&24] == true);
282 ///
283 /// assert!(map.get(&15).is_none());
284 /// ```
285 ///
286 pub fn retain<F>(&mut self, mut f: F)
287 where
288 K: Ord,
289 F: FnMut(&Segment<K>, &mut V) -> bool,
290 {
291 self.map.retain(|k, v| f(&k.0, v))
292 }
293
294 /// Insert a value for the specified range
295 ///
296 /// If the inserted range completely overlaps any existing range in the map,
297 /// the existing range (or ranges) will be replaced by the inserted range.
298 ///
299 /// If the inserted range partially overlaps any existing range in the map,
300 /// the existing ranges will be truncated to non-overlapping regions.
301 ///
302 /// If the inserted range overlaps or is touching an existing range that
303 /// maps to the same value, the ranges will be merged into one contiguous
304 /// range
305 ///
306 /// # Returns
307 ///
308 /// Much like other maps ([`BTreeMap::insert`] or [`HashMap::insert`]),
309 /// insert returns the overwritten values (if any existed). Because multiple
310 /// ranges might be overwritten, another map will be constructed with those
311 /// values.
312 ///
313 /// **Note**: This will allocate a new underlying [`SegmentMap`], though, so an
314 /// option is used in case no ranges were overwritten. If you don't care
315 /// about the overwritten values, use [`SegmentMap::set_range`] instead.
316 ///
317 /// # Examples
318 ///
319 /// ## Basic Usage
320 ///
321 /// ```
322 /// # use segmap::*;
323 /// let mut map = SegmentMap::new();
324 /// map.insert(0..4, "a");
325 /// assert_eq!(map.is_empty(), false);
326 ///
327 /// map.insert(2..6, "b");
328 /// assert_eq!(map[&1], "a");
329 /// assert_eq!(map[&3], "b");
330 /// ```
331 ///
332 /// ## Overwriting
333 ///
334 /// ```
335 /// # use segmap::*;
336 /// let mut map = SegmentMap::new();
337 /// map.insert(0..10, "a");
338 /// let out = map.insert(3..6, "b").unwrap();
339 ///
340 /// assert_eq!(map[&2], "a");
341 /// assert_eq!(map[&4], "b");
342 /// assert_eq!(map[&7], "a");
343 /// assert!(out.into_iter().eq(vec![(Segment::from(3..6), "a")]));
344 ///
345 /// ```
346 ///
347 /// ## Coalescing
348 ///
349 /// ```
350 /// # use segmap::*;
351 /// let mut map = SegmentMap::new();
352 /// map.insert(0..10, "a");
353 /// map.insert(10..20, "a");
354 ///
355 /// assert!(map.into_iter().eq(vec![(Segment::from(0..20), "a")]));
356 ///
357 /// ```
358 ///
359 /// # See Also
360 ///
361 /// - [`SegmentMap::set`] if you don't want to return the values
362 /// overwritten
363 /// - [`SegmentMap::insert_if_empty`] if you only want to insert the value if
364 /// no overlaps occur
365 /// - [`SegmentMap::insert_in_gaps`] if you only want to insert the value for
366 /// the empty parts of the range, not overwriting any values.
367 ///
368 pub fn insert<R>(&mut self, range: R, value: V) -> Option<Self>
369 where
370 R: core::ops::RangeBounds<K>,
371 K: Clone + Ord,
372 V: Clone + Eq,
373 {
374 // assert!(range.start_bound() <= range.end_bound());
375 let range = Segment::from(&range);
376 let mut removed_ranges = MaybeMap::Uninitialized;
377 self.insert_internal(range, value, &mut removed_ranges);
378 removed_ranges.into()
379 }
380
381 /// Set a value for the specified range, overwriting any existing subset
382 /// ranges. This is the same as [`SegmentMap::insert`], but without a return
383 /// value, so overlapping ranges will be truncated and adjacent ranges with
384 /// the same value will be merged.
385 ///
386 /// # Examples
387 ///
388 /// ## Basic Usage
389 ///
390 /// ```
391 /// # use segmap::*;
392 /// let mut map = SegmentMap::new();
393 /// map.set(0..4, "a");
394 /// assert_eq!(map.is_empty(), false);
395 ///
396 /// map.set(2..6, "b");
397 /// assert_eq!(map[&1], "a");
398 /// assert_eq!(map[&3], "b");
399 /// ```
400 ///
401 /// ## Overwriting
402 ///
403 /// ```
404 /// # use segmap::*;
405 /// let mut map = SegmentMap::new();
406 /// map.set(0..10, "a");
407 /// map.set(3..6, "b");
408 ///
409 /// assert_eq!(map[&2], "a");
410 /// assert_eq!(map[&4], "b");
411 /// assert_eq!(map[&7], "a");
412 ///
413 /// ```
414 ///
415 /// ## Coalescing
416 ///
417 /// ```
418 /// # use segmap::*;
419 /// let mut map = SegmentMap::new();
420 /// map.set(0..10, "a");
421 /// map.set(10..20, "a");
422 ///
423 /// assert!(map.into_iter().eq(vec![(Segment::from(0..20), "a")]))
424 ///
425 /// ```
426 ///
427 /// # See Also
428 ///
429 /// - [`SegmentMap::insert`] if you want the value overwritten
430 /// - [`SegmentMap::insert_if_empty`] if you only want to insert the value if
431 /// no overlaps occur
432 /// - [`SegmentMap::insert_in_gaps`] if you only want to insert the value for
433 /// the empty parts of the range, not overwriting any values.
434 ///
435 pub fn set<R>(&mut self, range: R, value: V)
436 where
437 R: core::ops::RangeBounds<K>,
438 K: Clone + Ord,
439 V: Clone + Eq,
440 {
441 let range = Segment::from(&range);
442 let mut removed_ranges = MaybeMap::Never;
443 self.insert_internal(range, value, &mut removed_ranges);
444 }
445
446 /// Insert a value into the map, only if there are no existing overlapping
447 /// ranges. Returns the given value if it wasn't inserted.
448 ///
449 /// # Examples
450 ///
451 /// ```
452 /// # use segmap::*;
453 /// let mut map = SegmentMap::new();
454 /// assert!(map.insert_if_empty(0..5, true).is_none());
455 /// assert!(map.insert_if_empty(5..10, true).is_none());
456 /// assert!(map.insert_if_empty(3..6, true).is_some());
457 ///
458 /// ```
459 ///
460 /// # See Also
461 ///
462 /// - [`SegmentMap::insert`] or [`SegmentMap::set`] if you want to overwrite
463 /// existing values
464 /// - [`SegmentMap::insert_in_gaps`] if you only want to insert the value for
465 /// the empty parts of the range
466 ///
467 pub fn insert_if_empty<R>(&mut self, range: R, value: V) -> Option<V>
468 where
469 R: core::ops::RangeBounds<K>,
470 K: Clone + Ord,
471 V: Clone + Eq,
472 {
473 // Get the ranges before and after this one
474 let range = Segment::from(&range);
475
476 // Check for any overlaps
477 let overlapped = if let Some(upper_bound) = range.end.after() {
478 self.map.range(..=upper_bound.cloned())
479 } else {
480 self.map.range::<Key<K>, _>(..)
481 }
482 .rev()
483 .take_while(|(k, _)| k.0.overlaps(&range))
484 .next()
485 .is_none();
486
487 // If no overlaps, we can insert directly into the map
488 if overlapped {
489 self.map.insert(Key(range), value);
490 None
491 } else {
492 Some(value)
493 }
494 }
495
496 /// Insert a value for empty regions (gaps) in the specified range. If
497 /// values exist for ranges overlapping this range, those values will be
498 /// preserved. As such, this method may add multiple ranges for the given
499 /// value.
500 ///
501 /// # Examples
502 ///
503 /// ```
504 /// # use segmap::*;
505 /// let mut map = SegmentMap::new();
506 /// map.set(5..10, "a");
507 /// map.set(15..20, "a");
508 /// map.insert_in_gaps(0..30, "b");
509 ///
510 /// assert!(map.into_iter().eq(vec![
511 /// (Segment::from(0..5), "b"),
512 /// (Segment::from(5..10), "a"),
513 /// (Segment::from(10..15), "b"),
514 /// (Segment::from(15..20), "a"),
515 /// (Segment::from(20..30), "b"),
516 /// ]));
517 ///
518 /// ```
519 ///
520 /// # See Also
521 ///
522 /// - [`SegmentMap::insert`] or [`SegmentMap::set`] if you want to overwrite
523 /// existing values
524 /// - [`SegmentMap::insert_if_empty`] if you only want to insert the value if
525 /// no overlaps occur
526 /// - [`SegmentMap::with_value`] if you'd instead like to construct your map
527 /// with a default value for all possible ranges
528 ///
529 pub fn insert_in_gaps<R>(&mut self, range: R, value: V)
530 where
531 R: core::ops::RangeBounds<K>,
532 K: Clone + Ord,
533 V: Clone + Eq,
534 {
535 let mut range = Segment::from(&range);
536
537 // In case this is an empty map, exit early
538 if self.map.is_empty() {
539 self.map.insert(Key(range), value);
540 return;
541 }
542
543 // Similar to insert, we need to see if any preceeding ranges overlap
544 // or touch this one
545
546 let leftmost = self
547 .map
548 .range(..=range.start.clone())
549 .rev()
550 .take_while(|(r, _)| r.0.touches(&range))
551 .last()
552 .map(|(k, v)| (k.clone(), v));
553
554 if let Some((leftmost_touching_range, leftmost_touching_value)) = leftmost {
555 // And merge if they have the same value
556 if value.eq(leftmost_touching_value) {
557 range.start = leftmost_touching_range.0.start.clone(); // Min is implied
558 if leftmost_touching_range.0.end > range.end {
559 range.end = leftmost_touching_range.0.end.clone();
560 }
561 self.map.remove(&leftmost_touching_range);
562 } else if leftmost_touching_range.0.end < range.end {
563 // If this range extends past the end of the previous range,
564 // truncate this range.
565 range.start = leftmost_touching_range.0.bound_after().unwrap().cloned();
566 } else {
567 // Otherwise, we've exhausted the insertion range and don't need
568 // to add anything
569 return;
570 }
571 }
572
573 // Get successors of this insertion range. Both are treated the same
574 // (unlike in insert)
575 self.store.clear();
576 self.store.extend(
577 if let Some(bound_after) = range.bound_after().map(|b| b.cloned()) {
578 self.map.range(range.start.clone()..=bound_after)
579 } else {
580 self.map.range(range.start.clone()..)
581 }
582 .map(|(k, _)| k.clone()),
583 );
584
585 // Keep marching along the insertion range and insert gaps as we find them
586 for successor in self.store.drain(..) {
587 let successor_value = self.map.get(&successor).unwrap();
588 // If we can merge ranges, do so
589 if value.eq(successor_value) {
590 let (removed_range, _) = self.map.remove_entry(&successor).unwrap();
591 range.end = max(removed_range.0.end, range.end);
592 } else {
593 // Otherwise, we may need to insert a gap. We can only
594 // insert if the range starts before the successor
595 // (it shouldn't ever by greater, but could be equal)
596 if successor.0.start > range.start {
597 self.map.insert(
598 Key(Segment {
599 start: range.start.clone(),
600 end: successor
601 .0
602 .bound_before()
603 .expect("Unexpected unbounded start")
604 .cloned(),
605 }),
606 value.clone(),
607 );
608 }
609
610 // After inserting the gap, move the start of the range to
611 // the end of this successor, if it exists. If not, this
612 // successor extends to the end and we're done.
613 if let Some(next_gap_start) = successor.0.bound_after() {
614 range.start = next_gap_start.cloned();
615 } else {
616 // Shouldn't actually be necessary, as it would be the last itme
617 break;
618 }
619 }
620 }
621
622 // Any leftover range can then be inserted as a "last gap"
623 self.map.insert(Key(range), value);
624 }
625
626 /// Remove all values in a given range, returning the removed values.
627 /// Overlapping ranges will be truncated at the bounds of this range.
628 ///
629 /// **Note**: This will allocate another [`SegmentMap`] for returning the
630 /// removed ranges, so if you don't care about them, use
631 /// [`SegmentMap::clear_range`] instead
632 ///
633 /// # Examples
634 ///
635 /// ```
636 /// # use segmap::*;
637 /// let mut map = SegmentMap::new();
638 /// map.insert(0..=10, 5);
639 /// let removed = map.remove(2..4).unwrap();
640 ///
641 /// assert_eq!(map[&0], 5);
642 /// assert!(map.get(&2).is_none());
643 /// assert!(map.get(&3).is_none());
644 /// assert_eq!(map[&4], 5);
645 /// assert_eq!(map[&10], 5);
646 ///
647 /// assert_eq!(removed[&2], 5);
648 /// assert_eq!(removed[&3], 5);
649 /// ```
650 ///
651 /// # See Also
652 ///
653 /// - [`SegmentMap::clear_range`] if you don't care about the removed values
654 ///
655 pub fn remove<R: core::ops::RangeBounds<K>>(&mut self, range: R) -> Option<Self>
656 where
657 K: Clone + Ord,
658 V: Clone,
659 {
660 let mut removed_ranges = MaybeMap::Uninitialized;
661 self.remove_internal(Segment::from(&range), &mut removed_ranges);
662 removed_ranges.into()
663 }
664
665 // Unset all values in a given range. Overlapping ranges will be truncated at the bounds of this range
666
667 /// Remove all values in a given range. Overlapping ranges will be truncated
668 /// at the bounds of this range.
669 ///
670 /// # Examples
671 ///
672 /// ```
673 /// # use segmap::*;
674 /// let mut map = SegmentMap::new();
675 /// map.insert(0..=10, 5);
676 /// map.clear_range(2..4);
677 ///
678 /// assert_eq!(map[&0], 5);
679 /// assert!(map.get(&2).is_none());
680 /// assert!(map.get(&3).is_none());
681 /// assert_eq!(map[&4], 5);
682 /// assert_eq!(map[&10], 5);
683 /// ```
684 ///
685 /// # See Also
686 ///
687 /// - [`SegmentMap::remove`] if you want the removed values
688 ///
689 pub fn clear_range<R: core::ops::RangeBounds<K>>(&mut self, range: R)
690 where
691 K: Clone + Ord,
692 V: Clone,
693 {
694 let mut removed_ranges = MaybeMap::Never;
695 self.remove_internal(Segment::from(&range), &mut removed_ranges);
696 }
697
698 /// Moves all elements from `other` into `Self`, leaving `other` empty.
699 ///
700 /// Note thate `V` must be `Clone` in case any ranges need to be split
701 ///
702 /// # Examples
703 ///
704 /// ```
705 /// # use segmap::*;
706 /// let mut a = SegmentMap::new();
707 /// a.insert(0..1, "a");
708 /// a.insert(1..2, "b");
709 /// a.insert(2..3, "c");
710 ///
711 /// let mut b = SegmentMap::new();
712 /// b.insert(2..3, "d");
713 /// b.insert(3..4, "e");
714 /// b.insert(4..5, "f");
715 ///
716 /// a.append(&mut b);
717 ///
718 /// assert_eq!(a.len(), 5);
719 /// assert_eq!(b.len(), 0);
720 ///
721 /// assert_eq!(a[&0], "a");
722 /// assert_eq!(a[&1], "b");
723 /// assert_eq!(a[&2], "d");
724 /// assert_eq!(a[&3], "e");
725 /// assert_eq!(a[&4], "f");
726 /// ```
727 pub fn append(&mut self, other: &mut Self)
728 where
729 K: Clone + Ord,
730 V: Clone + Eq,
731 {
732 // self.bounds().is_none() implies an empty map
733 match (self.bounds(), other.bounds()) {
734 // Other is empty, nothing to append
735 (_, None) => {
736 // NoOp
737 }
738
739 // Self is empty, swap it with other
740 (None, _) => core::mem::swap(self, other),
741
742 // Overlapping ranges, we must insert each range in other
743 (Some(a), Some(b)) if a.overlaps(&b) => {
744 for (range, value) in core::mem::take(&mut other.map) {
745 self.set(range.0, value)
746 }
747 }
748
749 // If there isn't any overlap, we can safely insert all of the
750 // items in other directly into the inner map
751 (Some(_), Some(_)) => {
752 for (range, value) in core::mem::take(&mut other.map) {
753 self.map.insert(range, value);
754 }
755 }
756 }
757 }
758
759 /// Split the map into two at the given bound. Returns everything including
760 /// and after that bound.
761 ///
762 /// # Examples
763 ///
764 /// # Basic Usage
765 ///
766 /// ```
767 /// # use segmap::*;
768 /// let mut a = SegmentMap::new();
769 /// a.insert(0..1, "a");
770 /// a.insert(1..2, "b");
771 /// a.insert(2..3, "c");
772 /// a.insert(3..4, "d");
773 ///
774 /// let b = a.split_off(Bound::Included(2));
775 ///
776 /// assert!(a.into_iter().eq(vec![
777 /// (Segment::from(0..1), "a"),
778 /// (Segment::from(1..2), "b"),
779 /// ]));
780 /// assert!(b.into_iter().eq(vec![
781 /// (Segment::from(2..3), "c"),
782 /// (Segment::from(3..4), "d"),
783 /// ]));
784 /// ```
785 ///
786 /// ## Mixed Bounds
787 ///
788 /// ```
789 /// # use segmap::*;
790 /// let mut a = SegmentMap::new();
791 /// a.insert(0..1, "a");
792 /// a.insert(1..2, "b");
793 /// a.insert(2..3, "c");
794 /// a.insert(3..4, "d");
795 /// a.insert(4..5, "e");
796 /// a.insert(5..6, "f");
797 /// a.insert(6..7, "g");
798 ///
799 /// let c = a.split_off(Bound::Excluded(4));
800 /// let b = a.split_off(Bound::Included(2));
801 ///
802 /// assert_eq!(a.len(), 2);
803 /// assert_eq!(b.len(), 3);
804 /// assert_eq!(c.len(), 3);
805 ///
806 /// assert_eq!(a[&0], "a");
807 /// assert_eq!(a[&1], "b");
808 /// assert!(a.get(&2).is_none());
809 ///
810 /// assert!(b.get(&1).is_none());
811 /// assert_eq!(b[&2], "c");
812 /// assert_eq!(b[&3], "d");
813 /// assert_eq!(b[&4], "e");
814 /// assert!(b.get(&5).is_none());
815 ///
816 /// // `c` also has a a range (4, 5), which is inaccessible with integers.
817 /// // if we were using floats, `c[4.5]` would be `"e"`.
818 /// assert!(c.get(&4).is_none());
819 /// assert_eq!(c[&5], "f");
820 /// assert_eq!(c[&6], "g");
821 /// assert!(c.get(&7).is_none());
822 /// ```
823 ///
824 pub fn split_off(&mut self, at: Bound<K>) -> Self
825 where
826 K: Clone + Ord,
827 V: Clone,
828 {
829 let at = Start(at);
830 if self.is_empty() {
831 return Self::new();
832 }
833
834 // Split non-overlapping items
835 let mut other = self.map.split_off(&at);
836
837 // If there are still items in the lower map, check if the last range
838 // crosses the boundary into the upper map
839 // No split should be necessary if `at` is unbounded
840 if let Some(split_range) = self.map.iter().next_back().map(|(k, _)| k.clone()) {
841 // These should all be together and available if there's a split
842 if let Some((left_end, at_value)) = at.before().zip(at.value()) {
843 if split_range.0.contains(at_value) {
844 // This should always unwrap, because we know the key exists
845 let value = self.map.remove(&split_range).unwrap();
846
847 // Reinsert truncated range in each
848 self.map.insert(
849 Key(Segment {
850 start: split_range.0.start.clone(),
851 end: left_end.cloned(),
852 }),
853 value.clone(),
854 );
855 other.insert(
856 Key(Segment {
857 start: at.clone(),
858 end: split_range.0.end,
859 }),
860 value,
861 );
862 }
863 }
864 }
865
866 Self {
867 map: other,
868 store: Vec::new(),
869 }
870 }
871
872 // TODO: split_off_range
873 // /// Split the map into two, removing from `self` and returning everything in the given range
874 // pub fn split_off_range<R>(&mut self, range: R) where R: RangeBounds, K: Clone + Ord, V: Clone {
875
876 // }
877
878 /// Internal implementation for [`insert`], [`set`], and similar
879 fn insert_internal(
880 &mut self,
881 mut range: Segment<K>,
882 value: V,
883 removed_ranges: &mut MaybeMap<K, V>,
884 ) where
885 K: Clone + Ord,
886 V: Clone + Eq,
887 {
888 // In case this is an empty map, exit early
889 if self.map.is_empty() {
890 self.map.insert(Key(range), value);
891 return;
892 }
893
894 // Get ranges starting at or before the new range that touch it. The
895 // iterator here should yeild:
896 // - None if no ranges touch the new range
897 // - The first previous range that touches or overlaps the new range
898 // - The range two previous if the new range starts right at a previous
899 // range (overlapping at the start) and touches one more previous range
900 // (like 0..3, 3..5, when inserting 3..4)
901 //
902 // We want to have the leftmost touching range (rather than just
903 // overlapping) in case we can combine the ranges when they have equal
904 // values
905 let leftmost = self
906 .map
907 .range(..=range.start.clone())
908 .rev()
909 .take_while(|(r, _)| r.0.touches(&range))
910 .last()
911 .map(|(k, v)| (k.clone(), v));
912
913 if let Some((leftmost_touching_range, leftmost_touching_value)) = leftmost {
914 if value.eq(leftmost_touching_value) {
915 // Remove the touching range and use it's start value (and maybe
916 // it's end, in the case of an overlap)
917 range.start = leftmost_touching_range.0.start.clone(); // Min is implied
918 if leftmost_touching_range.0.end > range.end {
919 range.end = leftmost_touching_range.0.end.clone();
920 }
921 self.map.remove(&leftmost_touching_range);
922 } else if range.overlaps(&leftmost_touching_range.0) {
923 // Split an overlapping range to preserve non-overlapped values
924 self.split_key(&leftmost_touching_range, &range, removed_ranges);
925 }
926 // Otherwise, touches (with a different value) but doesn't overlap,
927 // leave the existing range alone.
928 }
929
930 // After making the adjustment above, are there any following ranges
931 // that overlap with the new range?
932 //
933 // Or, following ranges that touch the end of this range (thus, bound_after)
934 //
935 // If there is no bound after the inserted range (i.e. the new range is
936 // unbounded on the right), all successor ranges can just be removed.
937 if let Some(bound_after) = range.bound_after().map(|b| b.cloned()) {
938 // Just store keys, so we don't clone values
939 self.store.clear();
940 self.store.extend(
941 self.map
942 .range(range.start.clone()..=bound_after.clone())
943 .map(|(k, _)| k.clone()),
944 );
945
946 for successor in self.store.drain(..) {
947 if let alloc::collections::btree_map::Entry::Occupied(entry) =
948 self.map.entry(successor)
949 {
950 if entry.get().eq(&value) {
951 // If values are the same, merge the ranges (and don't
952 // consider the successor part removed).
953 //
954 // For merging, we don't care if this is a touching or
955 // overlapping range, just that we may need to extend the
956 // end of the inserted range to merge with it.
957 let (successor, _) = entry.remove_entry();
958 if successor.0.end > range.end {
959 range.end = successor.0.end;
960 }
961 } else if entry.key().0.start < bound_after {
962 // Otherwise, if the range is overlapping (not just
963 // touching), it will need to be partially or fully removed
964 let (mut successor, successor_value) = entry.remove_entry();
965
966 // If overlapping, we need to split and reinsert it
967 if successor.0.end > range.end {
968 self.map.insert(
969 Key(Segment {
970 start: bound_after,
971 end: core::mem::replace(
972 &mut successor.0.end,
973 range.end.clone(),
974 ),
975 }),
976 successor_value.clone(),
977 );
978 removed_ranges.insert(successor, successor_value);
979 break;
980 } else {
981 // Store the removed portion
982 removed_ranges.insert(successor, successor_value);
983 }
984 }
985 // Otherwise (touching and different value), leave the successor alone
986 }
987 }
988 } else {
989 // No upper bound, all following ranges are removed or merged
990 let successors = self
991 .map
992 .range(range.start.clone()..)
993 .map(|(k, _)| k.clone())
994 .collect::<alloc::vec::Vec<_>>();
995
996 for successor in successors {
997 let v = self.map.remove(&successor).unwrap();
998 if value != v {
999 removed_ranges.insert(successor, v);
1000 }
1001 }
1002 }
1003
1004 // Finally, insert the new range and return the removed ranges
1005 self.map.insert(Key(range), value);
1006 }
1007
1008 /// Remove a specified range (`range_to_remove`) from an area of the map
1009 /// overlapped by the range defined by `key`.
1010 ///
1011 /// This is a helper function designed for use in `insert_internal`
1012 /// and `remove_internal`. This method does no checking for overlaps, and
1013 /// assumes that the ranges do!
1014 fn split_key(
1015 &mut self,
1016 key: &Key<K>,
1017 range_to_remove: &Segment<K>,
1018 removed_ranges: &mut MaybeMap<K, V>,
1019 ) where
1020 K: Clone + Ord,
1021 V: Clone,
1022 {
1023 // Unwrap here is fine, since the callers of this should have already
1024 // determined that the key exists
1025 let (mut removed_range, value) = self.map.remove_entry(key).unwrap();
1026
1027 // Insert a split of the range to the left (if necessary)
1028 if removed_range.0.start < range_to_remove.start {
1029 self.map.insert(
1030 Key(Segment {
1031 start: core::mem::replace(
1032 &mut removed_range.0.start,
1033 range_to_remove.start.clone(),
1034 ),
1035 end: range_to_remove.bound_before().unwrap().cloned(), // From above inequality, this must not be unbound
1036 }),
1037 value.clone(),
1038 );
1039 }
1040
1041 // Insert a split of the range to the right (if necessary)
1042 if removed_range.0.end > range_to_remove.end {
1043 self.map.insert(
1044 Key(Segment {
1045 start: range_to_remove.bound_after().unwrap().cloned(), // same as above
1046 end: core::mem::replace(&mut removed_range.0.end, range_to_remove.end.clone()),
1047 }),
1048 value.clone(),
1049 );
1050 }
1051 removed_ranges.insert(removed_range, value);
1052 }
1053
1054 pub(crate) fn remove_internal(&mut self, range: Segment<K>, removed_ranges: &mut MaybeMap<K, V>)
1055 where
1056 K: Clone + Ord,
1057 V: Clone,
1058 {
1059 // Return early if we can
1060 if self.map.is_empty() {
1061 return;
1062 }
1063
1064 // Get the first range before this one
1065 let previous_range = self
1066 .map
1067 .range(..=range.start.clone())
1068 .rev()
1069 .next()
1070 .map(|(k, _)| k.clone());
1071 if let Some(previous_range) = previous_range {
1072 // Split an overlapping range to preserve non-overlapped values
1073 if range.overlaps(&previous_range.0) {
1074 self.split_key(&previous_range, &range, removed_ranges);
1075 }
1076 }
1077
1078 // Check if there are any ranges starting inside the range to remove.
1079 // Unlike insert, we don't care about touching ranges because we
1080 // won't be merging them.
1081 self.store.clear();
1082 self.store.extend(
1083 if let Some(after) = range.bound_after().map(|b| b.cloned()) {
1084 self.map.range(range.start.clone()..=after)
1085 } else {
1086 self.map.range(range.start.clone()..)
1087 }
1088 .map(|(k, _)| k.clone()),
1089 );
1090
1091 for mut successor in self.store.drain(..) {
1092 let value = self.map.remove(&successor).unwrap();
1093
1094 // Must be the last range
1095 if successor.0.end > range.end {
1096 self.map.insert(
1097 Key(Segment {
1098 start: range.bound_after().unwrap().cloned(), // Implicitly not none due to less than successor end
1099 end: successor.0.end.clone(),
1100 }),
1101 value.clone(),
1102 );
1103 successor.0.end = range.end;
1104 removed_ranges.insert(successor, value);
1105 break;
1106 } else {
1107 removed_ranges.insert(successor, value);
1108 }
1109 }
1110 }
1111}
1112
1113// We can't just derive this automatically, because that would
1114// expose irrelevant (and private) implementation details.
1115// Instead implement it in the same way that the underlying BTreeMap does.
1116impl<K: Debug, V: Debug> Debug for SegmentMap<K, V>
1117where
1118 K: Ord + Clone,
1119 V: Eq + Clone,
1120{
1121 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
1122 f.debug_map().entries(self.iter()).finish()
1123 }
1124}
1125
1126impl<K: Hash, V: Hash> Hash for SegmentMap<K, V> {
1127 fn hash<H: Hasher>(&self, state: &mut H) {
1128 for elt in self {
1129 elt.hash(state);
1130 }
1131 }
1132}
1133
1134impl<K: Ord, V> Default for SegmentMap<K, V> {
1135 fn default() -> Self {
1136 Self::new()
1137 }
1138}
1139
1140impl<K: PartialEq, V: PartialEq> PartialEq for SegmentMap<K, V> {
1141 fn eq(&self, other: &Self) -> bool {
1142 self.len() == other.len() && self.iter().zip(other).all(|(a, b)| a == b)
1143 }
1144}
1145impl<K: Eq, V: Eq> Eq for SegmentMap<K, V> {}
1146impl<K: PartialOrd + Ord, V: PartialOrd> PartialOrd for SegmentMap<K, V> {
1147 #[inline]
1148 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
1149 self.map.iter().partial_cmp(other.map.iter())
1150 }
1151}
1152impl<K: Ord, V: Ord> Ord for SegmentMap<K, V> {
1153 #[inline]
1154 fn cmp(&self, other: &Self) -> Ordering {
1155 self.map.iter().cmp(other.map.iter())
1156 }
1157}
1158
1159impl<K, V> Index<&K> for SegmentMap<K, V>
1160where
1161 K: Clone + Ord,
1162 V: Eq + Clone,
1163{
1164 type Output = V;
1165
1166 /// Returns a reference to the value corresponding to the supplied key.
1167 ///
1168 /// # Panics
1169 ///
1170 /// Panics if the key is not present in the `SegmentMap`.
1171 #[inline]
1172 fn index(&self, key: &K) -> &V {
1173 self.get(key).expect("no entry found for key")
1174 }
1175}
1176
1177pub(crate) enum MaybeMap<K, V> {
1178 // Never do anything
1179 Never,
1180
1181 // Hold whether the map would contain elements
1182 None,
1183 Some,
1184
1185 // Hold actual elements
1186 Uninitialized,
1187 Map(BTreeMap<Key<K>, V>),
1188}
1189impl<K: Ord, V> MaybeMap<K, V> {
1190 fn insert(&mut self, key: Key<K>, value: V) {
1191 match self {
1192 MaybeMap::Never | MaybeMap::Some => {} //NoOp
1193 MaybeMap::None => *self = MaybeMap::Some,
1194 MaybeMap::Uninitialized => {
1195 let mut map = BTreeMap::new();
1196 map.insert(key, value);
1197 *self = MaybeMap::Map(map);
1198 }
1199 MaybeMap::Map(map) => {
1200 map.insert(key, value);
1201 }
1202 }
1203 }
1204}
1205
1206impl<K, V> From<MaybeMap<K, V>> for Option<SegmentMap<K, V>> {
1207 fn from(map: MaybeMap<K, V>) -> Self {
1208 if let MaybeMap::Map(map) = map {
1209 Some(SegmentMap {
1210 map,
1211 store: Vec::new(),
1212 })
1213 } else {
1214 None
1215 }
1216 }
1217}
1218impl<K, V> From<MaybeMap<K, V>> for bool {
1219 fn from(map: MaybeMap<K, V>) -> Self {
1220 matches!(map, MaybeMap::Some | MaybeMap::Map(_))
1221 }
1222}