use intrusive_collections::Bound as IBound; use intrusive_collections::{KeyAdapter, intrusive_adapter}; use intrusive_collections::{RBTreeLink, rbtree::RBTree}; use std::cmp::{max, min}; use std::ops::{Add, Bound, RangeBounds, Sub}; #[derive(Debug)] pub struct NaiveRange { link: RBTreeLink, pub start: T, pub end: T, } intrusive_adapter!( pub NaiveRangeAdapter = Box>: NaiveRange { link: RBTreeLink } ); impl<'a, T: Ord + Clone> KeyAdapter<'a> for NaiveRangeAdapter { type Key = T; fn get_key(&self, node: &'a NaiveRange) -> T { node.start.clone() } } impl NaiveRange { fn new(start: T, end: T) -> Self { Self { link: RBTreeLink::new(), start, end, } } } pub struct RangeSet { pub store: RBTree>, } fn normalize_range, T: Copy + Ord + Add + From>(r: &R) -> NaiveRange { let start = match r.start_bound() { Bound::Included(x) => *x, Bound::Excluded(x) => *x + T::from(1), Bound::Unbounded => panic!(), }; let end = match r.end_bound() { Bound::Included(x) => *x + T::from(1), Bound::Excluded(x) => *x, Bound::Unbounded => panic!(), }; NaiveRange::new(start, end) } impl + Sub + From> Default for RangeSet { fn default() -> Self { Self::new() } } impl + Sub + From> RangeSet { pub fn new() -> Self { Self { store: RBTree::new(NaiveRangeAdapter::::new()), } } pub fn add>(&mut self, r: &R) { //normalize r to Range let mut new_r = normalize_range(r); // Find the position of nearest entry >= and <= than new_r's start let mut cur = self.store.lower_bound_mut(IBound::Included(&new_r.start)); while let Some(val) = cur.get() && new_r.end >= val.start { // then remove them and update our new range new_r.start = min(new_r.start, val.start); new_r.end = max(new_r.end, val.end); cur.remove(); // moves the cursor ahead } // Move the cursor back one (this will be the element before our insertion point, 'below' start) cur.move_prev(); while let Some(val) = cur.get() && new_r.start <= val.end { new_r.start = min(new_r.start, val.start); new_r.end = max(new_r.end, val.end); cur.remove(); cur.move_prev(); } // We are already in position, we just checked the previous element and it's before us // since we fell out of the loop, so we need to go after this position cur.insert_after(Box::new(new_r)); } } #[cfg(test)] mod tests { use super::*; #[test] fn test_ranges() { let mut set = RangeSet::new(); // edge cases are tricky to construct, run the aoc2025 day 5 puzzle and compare :) set.add(&(0..10)); set.add(&(100..1000)); set.add(&(50..100)); set.add(&(50..100)); set.add(&(1001..2000)); set.add(&(2000..2001)); set.add(&(-10..-2)); set.add(&(-1..-1)); let end = set.store.iter().map(|r| (r.start, r.end)).collect::>(); assert_eq!(end, [(-10, 10), (50, 2001)]) } }