pub struct IntervalSet<Ix = u32> { /* private fields */ }Expand description
A set of non-overlapping intervals with automatic merging.
IntervalSet stores a collection of non-overlapping intervals. When inserting
intervals that overlap or are adjacent, they are automatically merged into
a single interval.
§Type Parameters
Ix- The index type for interval bounds. Defaults tou32.
§Examples
use intervalmap::IntervalSet;
let mut set = IntervalSet::new();
set.insert(0..10);
set.insert(5..15); // Merges with [0, 10) to form [0, 15)
assert!(set.contains(7));
assert!(!set.contains(20));Implementations§
Source§impl<Ix: IndexType> IntervalSet<Ix>
impl<Ix: IndexType> IntervalSet<Ix>
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new, empty IntervalSet.
§Examples
use intervalmap::IntervalSet;
let set: IntervalSet<u32> = IntervalSet::new();
assert!(set.is_empty());Sourcepub fn contains(&self, point: Ix) -> bool
pub fn contains(&self, point: Ix) -> bool
Returns true if the set contains an interval covering the given point.
§Examples
use intervalmap::IntervalSet;
let mut set = IntervalSet::new();
set.insert(0..10);
assert!(set.contains(5));
assert!(!set.contains(10));Sourcepub fn get_interval(&self, point: Ix) -> Option<Range<Ix>>
pub fn get_interval(&self, point: Ix) -> Option<Range<Ix>>
Returns the interval containing the given point, if any.
§Examples
use intervalmap::IntervalSet;
let mut set = IntervalSet::new();
set.insert(5..15);
assert_eq!(set.get_interval(10), Some(5..15));
assert_eq!(set.get_interval(20), None);Sourcepub fn iter(&self) -> IterSet<'_, Ix>
pub fn iter(&self) -> IterSet<'_, Ix>
Returns an iterator over the intervals in the set.
Intervals are yielded in ascending order by start position.
§Examples
use intervalmap::IntervalSet;
let mut set = IntervalSet::new();
set.insert(0..10);
set.insert(20..30);
let intervals: Vec<_> = set.iter().collect();
assert_eq!(intervals, vec![0..10, 20..30]);Sourcepub fn first(&self) -> Option<Range<Ix>>
pub fn first(&self) -> Option<Range<Ix>>
Returns the first (lowest) interval, or None if empty.
This is an O(log n) operation.
§Examples
use intervalmap::IntervalSet;
let mut set = IntervalSet::new();
assert_eq!(set.first(), None);
set.insert(10..20);
set.insert(0..5);
assert_eq!(set.first(), Some(0..5));Sourcepub fn last(&self) -> Option<Range<Ix>>
pub fn last(&self) -> Option<Range<Ix>>
Returns the last (highest) interval, or None if empty.
This is an O(log n) operation.
§Examples
use intervalmap::IntervalSet;
let mut set = IntervalSet::new();
assert_eq!(set.last(), None);
set.insert(0..5);
set.insert(10..20);
assert_eq!(set.last(), Some(10..20));Sourcepub fn span(&self) -> Option<Range<Ix>>
pub fn span(&self) -> Option<Range<Ix>>
Returns the bounding interval that spans from the start of the first
interval to the end of the last interval, or None if empty.
This is an O(log n) operation.
§Examples
use intervalmap::IntervalSet;
let mut set = IntervalSet::new();
assert_eq!(set.span(), None);
set.insert(5..10);
set.insert(20..30);
assert_eq!(set.span(), Some(5..30));Source§impl<Ix: IndexType + One + Bounded> IntervalSet<Ix>
impl<Ix: IndexType + One + Bounded> IntervalSet<Ix>
Sourcepub fn overlaps<R: RangeBounds<Ix>>(&self, range: R) -> bool
pub fn overlaps<R: RangeBounds<Ix>>(&self, range: R) -> bool
Returns true if any interval in the set overlaps with the given range.
This is an O(log n) operation.
§Examples
use intervalmap::IntervalSet;
let mut set = IntervalSet::new();
set.insert(10..20);
assert!(set.overlaps(15..25));
assert!(set.overlaps(5..15));
assert!(!set.overlaps(0..5));
assert!(!set.overlaps(25..30));Sourcepub fn covers<R: RangeBounds<Ix>>(&self, range: R) -> bool
pub fn covers<R: RangeBounds<Ix>>(&self, range: R) -> bool
Returns true if the entire range is covered by intervals in the set.
This is an O(log n + k) operation where k is the number of intervals overlapping the range.
§Examples
use intervalmap::IntervalSet;
let mut set = IntervalSet::new();
set.insert(0..10);
set.insert(10..20);
assert!(set.covers(5..15));
assert!(set.covers(0..20));
assert!(!set.covers(0..25)); // 20..25 not coveredSourcepub fn insert<R: RangeBounds<Ix>>(&mut self, range: R)
pub fn insert<R: RangeBounds<Ix>>(&mut self, range: R)
Inserts an interval into the set.
If the interval overlaps or is adjacent to existing intervals, they will be merged.
§Examples
use intervalmap::IntervalSet;
let mut set = IntervalSet::new();
set.insert(0..10);
set.insert(10..20); // Merges into [0, 20)
assert_eq!(set.len(), 1);Sourcepub fn remove<R: RangeBounds<Ix>>(&mut self, range: R)
pub fn remove<R: RangeBounds<Ix>>(&mut self, range: R)
Removes an interval from the set.
Any existing intervals that overlap with the removed range will be split, with the overlapping portions removed.
§Examples
use intervalmap::IntervalSet;
let mut set = IntervalSet::new();
set.insert(0..20);
set.remove(5..15);
// Result: [0,5) and [15,20)
assert!(set.contains(3));
assert!(!set.contains(10));
assert!(set.contains(17));Sourcepub fn union(&self, other: &Self) -> Self
pub fn union(&self, other: &Self) -> Self
Returns a new set containing all intervals from both sets.
Overlapping and adjacent intervals are merged automatically.
§Examples
use intervalmap::interval_set;
let a = interval_set!{ 0..10, 20..30 };
let b = interval_set!{ 5..25 };
let union = a.union(&b);
assert_eq!(union.len(), 1);
assert!(union.contains(15));Sourcepub fn intersection(&self, other: &Self) -> Self
pub fn intersection(&self, other: &Self) -> Self
Returns a new set containing only intervals covered by both sets.
§Examples
use intervalmap::interval_set;
let a = interval_set!{ 0..10, 20..30 };
let b = interval_set!{ 5..25 };
let intersection = a.intersection(&b);
let intervals: Vec<_> = intersection.iter().collect();
assert_eq!(intervals, vec![5..10, 20..25]);Sourcepub fn difference(&self, other: &Self) -> Self
pub fn difference(&self, other: &Self) -> Self
Returns a new set containing intervals from self that are not in other.
§Examples
use intervalmap::interval_set;
let a = interval_set!{ 0..20 };
let b = interval_set!{ 5..15 };
let difference = a.difference(&b);
let intervals: Vec<_> = difference.iter().collect();
assert_eq!(intervals, vec![0..5, 15..20]);Sourcepub fn symmetric_difference(&self, other: &Self) -> Self
pub fn symmetric_difference(&self, other: &Self) -> Self
Returns a new set containing intervals that are in either set, but not both.
§Examples
use intervalmap::interval_set;
let a = interval_set!{ 0..10 };
let b = interval_set!{ 5..15 };
let sym_diff = a.symmetric_difference(&b);
let intervals: Vec<_> = sym_diff.iter().collect();
assert_eq!(intervals, vec![0..5, 10..15]);Sourcepub fn gaps(&self) -> Gaps<'_, Ix>
pub fn gaps(&self) -> Gaps<'_, Ix>
Returns an iterator over the gaps between intervals in the set.
This iterates over the ranges that are NOT covered by any interval in the set, but only between the first and last intervals. It does not yield unbounded gaps before the first interval or after the last.
§Examples
use intervalmap::IntervalSet;
let mut set = IntervalSet::new();
set.insert(0..10);
set.insert(20..30);
set.insert(40..50);
let gaps: Vec<_> = set.gaps().collect();
assert_eq!(gaps, vec![10..20, 30..40]);Sourcepub fn iter_overlapping<R: RangeBounds<Ix>>(
&self,
range: R,
) -> IterOverlapping<'_, Ix>
pub fn iter_overlapping<R: RangeBounds<Ix>>( &self, range: R, ) -> IterOverlapping<'_, Ix>
Returns an iterator over intervals that overlap with the given range.
This is an O(log n + k) operation where k is the number of overlapping intervals.
§Examples
use intervalmap::IntervalSet;
let mut set = IntervalSet::new();
set.insert(0..10);
set.insert(20..30);
set.insert(40..50);
let overlapping: Vec<_> = set.iter_overlapping(15..45).collect();
assert_eq!(overlapping, vec![20..30, 40..50]);Sourcepub fn retain<F: FnMut(&Range<Ix>) -> bool>(&mut self, f: F)
pub fn retain<F: FnMut(&Range<Ix>) -> bool>(&mut self, f: F)
Keeps only the intervals matching the predicate.
§Examples
use intervalmap::IntervalSet;
let mut set = IntervalSet::new();
set.insert(0..10);
set.insert(20..30);
set.insert(40..50);
// Keep only intervals that start before 25
set.retain(|r| r.start < 25);
let intervals: Vec<_> = set.iter().collect();
assert_eq!(intervals, vec![0..10, 20..30]);Sourcepub fn split_at(&mut self, point: Ix)
pub fn split_at(&mut self, point: Ix)
Splits all intervals at the given point.
If an interval contains the point, it will be split into two intervals: one ending at the point and one starting at the point.
§Examples
use intervalmap::IntervalSet;
let mut set = IntervalSet::new();
set.insert(0..20);
set.split_at(10);
let intervals: Vec<_> = set.iter().collect();
assert_eq!(intervals, vec![0..10, 10..20]);Trait Implementations§
Source§impl<Ix: IndexType + One + Bounded> BitAnd<&IntervalSet<Ix>> for &IntervalSet<Ix>
Intersection: &a & &b
impl<Ix: IndexType + One + Bounded> BitAnd<&IntervalSet<Ix>> for &IntervalSet<Ix>
Intersection: &a & &b
Source§type Output = IntervalSet<Ix>
type Output = IntervalSet<Ix>
& operator.Source§impl<Ix: IndexType + One + Bounded> BitAndAssign<&IntervalSet<Ix>> for IntervalSet<Ix>
Intersection assignment: a &= &b
impl<Ix: IndexType + One + Bounded> BitAndAssign<&IntervalSet<Ix>> for IntervalSet<Ix>
Intersection assignment: a &= &b
Source§fn bitand_assign(&mut self, other: &IntervalSet<Ix>)
fn bitand_assign(&mut self, other: &IntervalSet<Ix>)
&= operation. Read moreSource§impl<Ix: IndexType + One + Bounded> BitOr<&IntervalSet<Ix>> for &IntervalSet<Ix>
Union: &a | &b
impl<Ix: IndexType + One + Bounded> BitOr<&IntervalSet<Ix>> for &IntervalSet<Ix>
Union: &a | &b
Source§type Output = IntervalSet<Ix>
type Output = IntervalSet<Ix>
| operator.Source§impl<Ix: IndexType + One + Bounded> BitOrAssign<&IntervalSet<Ix>> for IntervalSet<Ix>
Union assignment: a |= &b
impl<Ix: IndexType + One + Bounded> BitOrAssign<&IntervalSet<Ix>> for IntervalSet<Ix>
Union assignment: a |= &b
Source§fn bitor_assign(&mut self, other: &IntervalSet<Ix>)
fn bitor_assign(&mut self, other: &IntervalSet<Ix>)
|= operation. Read moreSource§impl<Ix: IndexType + One + Bounded> BitXor<&IntervalSet<Ix>> for &IntervalSet<Ix>
Symmetric Difference: &a ^ &b
impl<Ix: IndexType + One + Bounded> BitXor<&IntervalSet<Ix>> for &IntervalSet<Ix>
Symmetric Difference: &a ^ &b
Source§type Output = IntervalSet<Ix>
type Output = IntervalSet<Ix>
^ operator.Source§impl<Ix: IndexType + One + Bounded> BitXorAssign<&IntervalSet<Ix>> for IntervalSet<Ix>
Symmetric difference assignment: a ^= &b
impl<Ix: IndexType + One + Bounded> BitXorAssign<&IntervalSet<Ix>> for IntervalSet<Ix>
Symmetric difference assignment: a ^= &b
Source§fn bitxor_assign(&mut self, other: &IntervalSet<Ix>)
fn bitxor_assign(&mut self, other: &IntervalSet<Ix>)
^= operation. Read moreSource§impl<Ix: Clone> Clone for IntervalSet<Ix>
impl<Ix: Clone> Clone for IntervalSet<Ix>
Source§fn clone(&self) -> IntervalSet<Ix>
fn clone(&self) -> IntervalSet<Ix>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<Ix: Debug> Debug for IntervalSet<Ix>
impl<Ix: Debug> Debug for IntervalSet<Ix>
Source§impl<Ix: IndexType> Default for IntervalSet<Ix>
impl<Ix: IndexType> Default for IntervalSet<Ix>
Source§impl<'a, Ix: IndexType> IntoIterator for &'a IntervalSet<Ix>
impl<'a, Ix: IndexType> IntoIterator for &'a IntervalSet<Ix>
Source§impl<Ix: IndexType> IntoIterator for IntervalSet<Ix>
impl<Ix: IndexType> IntoIterator for IntervalSet<Ix>
Source§impl<Ix: IndexType + One + Bounded> Sub<&IntervalSet<Ix>> for &IntervalSet<Ix>
Difference: &a - &b
impl<Ix: IndexType + One + Bounded> Sub<&IntervalSet<Ix>> for &IntervalSet<Ix>
Difference: &a - &b
Source§type Output = IntervalSet<Ix>
type Output = IntervalSet<Ix>
- operator.Source§impl<Ix: IndexType + One + Bounded> SubAssign<&IntervalSet<Ix>> for IntervalSet<Ix>
Difference assignment: a -= &b
impl<Ix: IndexType + One + Bounded> SubAssign<&IntervalSet<Ix>> for IntervalSet<Ix>
Difference assignment: a -= &b
Source§fn sub_assign(&mut self, other: &IntervalSet<Ix>)
fn sub_assign(&mut self, other: &IntervalSet<Ix>)
-= operation. Read more