1use std::borrow::Borrow;
6use std::cmp::Ordering;
7use std::collections::BTreeMap;
8use std::ops::Range;
9
10#[derive(Clone, Debug)]
16struct RangeStart<T> {
17 range: Range<T>,
18}
19
20impl<T: Copy> RangeStart<T> {
21 fn new(range: Range<T>) -> Self {
26 RangeStart { range }
27 }
28
29 fn from_point(point: T) -> Self {
33 RangeStart { range: Range { start: point, end: point } }
34 }
35}
36
37impl<T: PartialEq> PartialEq for RangeStart<T> {
39 fn eq(&self, other: &Self) -> bool {
40 self.range.start.eq(&other.range.start)
41 }
42}
43
44impl<T: Eq> Eq for RangeStart<T> {}
46
47impl<T: Ord> PartialOrd for RangeStart<T> {
49 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
50 Some(self.cmp(other))
51 }
52}
53
54impl<T: Ord> Ord for RangeStart<T> {
56 fn cmp(&self, other: &Self) -> Ordering {
57 self.range.start.cmp(&other.range.start)
58 }
59}
60
61#[derive(Debug)]
77pub struct RangeMap<K: Ord + Clone, V: Clone + Eq> {
78 map: BTreeMap<RangeStart<K>, V>,
79}
80
81impl<K, V> Default for RangeMap<K, V>
82where
83 K: Ord + Copy,
84 V: Clone + Eq,
85{
86 fn default() -> Self {
88 Self { map: Default::default() }
89 }
90}
91
92impl<K, V> RangeMap<K, V>
93where
94 K: Ord + Copy,
95 V: Clone + Eq,
96{
97 pub fn get(&self, point: K) -> Option<(&Range<K>, &V)> {
107 self.map
108 .range(..=RangeStart::from_point(point))
109 .next_back()
110 .filter(|(k, _)| k.range.contains(&point))
111 .map(|(k, v)| (&k.range, v))
112 }
113
114 pub fn insert(&mut self, mut range: Range<K>, value: V) {
127 if range.end <= range.start {
128 return;
129 }
130 self.remove(range.clone());
131
132 if let Some((prev_range, prev_value)) =
135 self.map.range(..RangeStart::from_point(range.start)).next_back()
136 {
137 let prev_range = &prev_range.range;
138 if prev_range.end == range.start && &value == prev_value {
139 range.start = prev_range.start.clone();
140 self.remove_exact_range(prev_range.clone());
141 }
142 }
143 if let Some((next_range, next_value)) =
146 self.map.get_key_value(&RangeStart::from_point(range.end))
147 {
148 let next_range = &next_range.range;
149 if next_range.start == range.end && &value == next_value {
150 range.end = next_range.end.clone();
151 self.remove_exact_range(next_range.clone());
152 }
153 }
154
155 self.insert_into_empty_range(range, value);
156 }
157
158 pub fn remove(&mut self, range: Range<K>) -> Vec<V> {
168 let mut removed_values = vec![];
169 if range.end <= range.start {
171 return removed_values;
172 }
173
174 if let Some((old_range, v)) =
179 self.get(range.start).map(|(range, v)| (range.clone(), v.clone()))
180 {
181 if let Some(value) = self.remove_exact_range(old_range.clone()) {
183 removed_values.push(value);
184 }
185
186 if old_range.end > range.end {
190 self.insert_into_empty_range(range.end.clone()..old_range.end, v.clone());
191 }
192
193 if old_range.start < range.start {
197 self.insert_into_empty_range(old_range.start..range.start.clone(), v);
198 }
199
200 }
204
205 if let Some((old_range, v)) = self
214 .map
215 .range(RangeStart::from_point(range.start)..RangeStart::from_point(range.end))
216 .next_back()
217 .filter(|(k, _)| k.range.contains(&range.end))
218 .map(|(k, v)| (k.range.clone(), v.clone()))
219 {
220 if let Some(value) = self.remove_exact_range(old_range.clone()) {
222 removed_values.push(value);
223 }
224
225 if old_range.end > range.end {
229 self.insert_into_empty_range(range.end.clone()..old_range.end, v);
230 }
231 }
232
233 let doomed: Vec<_> = self
241 .map
242 .range(RangeStart::from_point(range.start)..RangeStart::from_point(range.end))
243 .map(|(k, _)| k.clone())
244 .collect();
245
246 for key in &doomed {
247 if let Some(removed_value) = self.map.remove(key) {
248 removed_values.push(removed_value);
249 }
250 }
251 removed_values
252 }
253
254 pub fn is_empty(&self) -> bool {
255 self.map.is_empty()
256 }
257
258 pub fn iter(&self) -> impl Iterator<Item = (&Range<K>, &V)> {
260 self.map.iter().map(|(k, value)| (&k.range, value))
261 }
262
263 pub fn iter_starting_at(&self, point: K) -> impl Iterator<Item = (&Range<K>, &V)> {
265 self.map.range(RangeStart::from_point(point)..).map(|(k, value)| (&k.range, value))
266 }
267
268 pub fn iter_ending_at(&self, point: K) -> impl DoubleEndedIterator<Item = (&Range<K>, &V)> {
270 self.map.range(..RangeStart::from_point(point)).map(|(k, value)| (&k.range, value))
271 }
272
273 pub fn intersection<R>(&self, range: R) -> impl DoubleEndedIterator<Item = (&Range<K>, &V)>
275 where
276 R: Borrow<Range<K>>,
277 {
278 let range = range.borrow();
279 let start = self.get(range.start).map(|(r, _)| r.start).unwrap_or(range.start);
280 self.map
281 .range(RangeStart::from_point(start)..RangeStart::from_point(range.end))
282 .map(|(k, value)| (&k.range, value))
283 }
284
285 pub fn last_range(&self) -> Option<Range<K>> {
287 if let Some((r, _)) = self.map.last_key_value() {
288 Some(r.range.clone())
289 } else {
290 None
291 }
292 }
293
294 fn insert_into_empty_range(&mut self, range: Range<K>, value: V) {
299 self.map.insert(RangeStart::new(range), value);
300 }
301
302 fn remove_exact_range(&mut self, range: Range<K>) -> Option<V> {
307 self.map.remove(&RangeStart::new(range))
308 }
309}
310
311#[cfg(test)]
312mod test {
313 use super::*;
314
315 #[::fuchsia::test]
316 fn test_empty() {
317 let mut map = RangeMap::<u32, i32>::default();
318
319 assert!(map.get(12).is_none());
320 map.remove(10..34);
321 #[allow(clippy::reversed_empty_ranges)]
323 map.remove(34..10);
324 }
325
326 #[::fuchsia::test]
327 fn test_insert_into_empty() {
328 let mut map = RangeMap::<u32, i32>::default();
329
330 map.insert(10..34, -14);
331
332 assert_eq!((&(10..34), &-14), map.get(12).unwrap());
333 assert_eq!((&(10..34), &-14), map.get(10).unwrap());
334 assert!(map.get(9).is_none());
335 assert_eq!((&(10..34), &-14), map.get(33).unwrap());
336 assert!(map.get(34).is_none());
337 }
338
339 #[::fuchsia::test]
340 fn test_iter() {
341 let mut map = RangeMap::<u32, i32>::default();
342
343 map.insert(10..34, -14);
344 map.insert(74..92, -12);
345
346 let mut iter = map.iter();
347
348 assert_eq!(iter.next().expect("missing elem"), (&(10..34), &-14));
349 assert_eq!(iter.next().expect("missing elem"), (&(74..92), &-12));
350
351 assert!(iter.next().is_none());
352
353 let mut iter = map.iter_starting_at(10);
354 assert_eq!(iter.next().expect("missing elem"), (&(10..34), &-14));
355 let mut iter = map.iter_starting_at(11);
356 assert_eq!(iter.next().expect("missing elem"), (&(74..92), &-12));
357 let mut iter = map.iter_starting_at(74);
358 assert_eq!(iter.next().expect("missing elem"), (&(74..92), &-12));
359 let mut iter = map.iter_starting_at(75);
360 assert_eq!(iter.next(), None);
361
362 assert_eq!(map.iter_ending_at(9).collect::<Vec<_>>(), vec![]);
363 assert_eq!(map.iter_ending_at(34).collect::<Vec<_>>(), vec![(&(10..34), &-14)]);
364 assert_eq!(map.iter_ending_at(74).collect::<Vec<_>>(), vec![(&(10..34), &-14)]);
365 assert_eq!(
366 map.iter_ending_at(75).collect::<Vec<_>>(),
367 vec![(&(10..34), &-14), (&(74..92), &-12)]
368 );
369 assert_eq!(
370 map.iter_ending_at(91).collect::<Vec<_>>(),
371 vec![(&(10..34), &-14), (&(74..92), &-12)]
372 );
373 assert_eq!(
374 map.iter_ending_at(92).collect::<Vec<_>>(),
375 vec![(&(10..34), &-14), (&(74..92), &-12)]
376 );
377 }
378
379 #[::fuchsia::test]
380 fn test_remove_overlapping_edge() {
381 let mut map = RangeMap::<u32, i32>::default();
382
383 map.insert(10..34, -14);
384
385 map.remove(2..11);
386 assert_eq!((&(11..34), &-14), map.get(11).unwrap());
387
388 map.remove(33..42);
389 assert_eq!((&(11..33), &-14), map.get(12).unwrap());
390 }
391
392 #[::fuchsia::test]
393 fn test_remove_middle_splits_range() {
394 let mut map = RangeMap::<u32, i32>::default();
395
396 map.insert(10..34, -14);
397 map.remove(15..18);
398
399 assert_eq!((&(10..15), &-14), map.get(12).unwrap());
400 assert_eq!((&(18..34), &-14), map.get(20).unwrap());
401 }
402
403 #[::fuchsia::test]
404 fn test_remove_upper_half_of_split_range_leaves_lower_range() {
405 let mut map = RangeMap::<u32, i32>::default();
406
407 map.insert(10..34, -14);
408 map.remove(15..18);
409 map.insert(2..7, -21);
410 map.remove(20..42);
411
412 assert_eq!((&(2..7), &-21), map.get(5).unwrap());
413 assert_eq!((&(10..15), &-14), map.get(12).unwrap());
414 }
415
416 #[::fuchsia::test]
417 fn test_range_map_overlapping_insert() {
418 let mut map = RangeMap::<u32, i32>::default();
419
420 map.insert(2..7, -21);
421 map.insert(5..9, -42);
422 map.insert(1..3, -43);
423 map.insert(6..8, -44);
424
425 assert_eq!((&(1..3), &-43), map.get(2).unwrap());
426 assert_eq!((&(3..5), &-21), map.get(4).unwrap());
427 assert_eq!((&(5..6), &-42), map.get(5).unwrap());
428 assert_eq!((&(6..8), &-44), map.get(7).unwrap());
429 }
430
431 #[::fuchsia::test]
432 fn test_intersect_single() {
433 let mut map = RangeMap::<u32, i32>::default();
434
435 map.insert(2..7, -10);
436
437 let mut iter = map.intersection(3..4);
438 assert_eq!(iter.next(), Some((&(2..7), &-10)));
439 assert_eq!(iter.next(), None);
440
441 let mut iter = map.intersection(2..3);
442 assert_eq!(iter.next(), Some((&(2..7), &-10)));
443 assert_eq!(iter.next(), None);
444
445 let mut iter = map.intersection(1..4);
446 assert_eq!(iter.next(), Some((&(2..7), &-10)));
447 assert_eq!(iter.next(), None);
448
449 let mut iter = map.intersection(1..2);
450 assert_eq!(iter.next(), None);
451
452 let mut iter = map.intersection(6..7);
453 assert_eq!(iter.next(), Some((&(2..7), &-10)));
454 assert_eq!(iter.next(), None);
455 }
456
457 #[::fuchsia::test]
458 fn test_intersect_multiple() {
459 let mut map = RangeMap::<u32, i32>::default();
460
461 map.insert(2..7, -10);
462 map.insert(7..9, -20);
463 map.insert(10..11, -30);
464
465 let mut iter = map.intersection(3..8);
466 assert_eq!(iter.next(), Some((&(2..7), &-10)));
467 assert_eq!(iter.next(), Some((&(7..9), &-20)));
468 assert_eq!(iter.next(), None);
469
470 let mut iter = map.intersection(3..11);
471 assert_eq!(iter.next(), Some((&(2..7), &-10)));
472 assert_eq!(iter.next(), Some((&(7..9), &-20)));
473 assert_eq!(iter.next(), Some((&(10..11), &-30)));
474 assert_eq!(iter.next(), None);
475 }
476
477 #[::fuchsia::test]
478 fn test_intersect_no_gaps() {
479 let mut map = RangeMap::<u32, i32>::default();
480
481 map.insert(0..1, -10);
482 map.insert(1..2, -20);
483 map.insert(2..3, -30);
484
485 let mut iter = map.intersection(0..3);
486 assert_eq!(iter.next(), Some((&(0..1), &-10)));
487 assert_eq!(iter.next(), Some((&(1..2), &-20)));
488 assert_eq!(iter.next(), Some((&(2..3), &-30)));
489 assert_eq!(iter.next(), None);
490 }
491
492 #[test]
493 fn test_merging() {
494 let mut map = RangeMap::<u32, i32>::default();
495
496 map.insert(1..2, -10);
497 assert_eq!(map.iter().collect::<Vec<_>>(), vec![(&(1..2), &-10)]);
498 map.insert(3..4, -10);
499 assert_eq!(map.iter().collect::<Vec<_>>(), vec![(&(1..2), &-10), (&(3..4), &-10)]);
500 map.insert(2..3, -10);
501 assert_eq!(map.iter().collect::<Vec<_>>(), vec![(&(1..4), &-10)]);
502 map.insert(0..1, -10);
503 assert_eq!(map.iter().collect::<Vec<_>>(), vec![(&(0..4), &-10)]);
504 map.insert(4..5, -10);
505 assert_eq!(map.iter().collect::<Vec<_>>(), vec![(&(0..5), &-10)]);
506 map.insert(2..3, -20);
507 assert_eq!(
508 map.iter().collect::<Vec<_>>(),
509 vec![(&(0..2), &-10), (&(2..3), &-20), (&(3..5), &-10)]
510 );
511 }
512
513 #[test]
514 fn test_remove_multiple_ranges_exact_query() {
515 let first = 15..21;
516 let second = first.end..29;
517
518 let mut map = RangeMap::default();
519 map.insert(first.clone(), 1);
520 map.insert(second.clone(), 2);
521
522 assert_eq!(map.remove(first.start..second.end), &[1, 2]);
523 }
524}