IntervalSet

Struct IntervalSet 

Source
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 to u32.

§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>

Source

pub fn new() -> Self

Creates a new, empty IntervalSet.

§Examples
use intervalmap::IntervalSet;

let set: IntervalSet<u32> = IntervalSet::new();
assert!(set.is_empty());
Source

pub fn len(&self) -> usize

Returns the number of intervals in the set.

Source

pub fn is_empty(&self) -> bool

Returns true if the set contains no intervals.

Source

pub fn clear(&mut self)

Removes all intervals from the set.

Source

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));
Source

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);
Source

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]);
Source

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));
Source

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));
Source

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>

Source

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));
Source

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 covered
Source

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);
Source

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));
Source

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));
Source

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]);
Source

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]);
Source

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]);
Source

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]);
Source

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]);
Source

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]);
Source

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

Source§

type Output = IntervalSet<Ix>

The resulting type after applying the & operator.
Source§

fn bitand(self, other: &IntervalSet<Ix>) -> Self::Output

Performs the & operation. Read more
Source§

impl<Ix: IndexType + One + Bounded> BitAndAssign<&IntervalSet<Ix>> for IntervalSet<Ix>

Intersection assignment: a &= &b

Source§

fn bitand_assign(&mut self, other: &IntervalSet<Ix>)

Performs the &= operation. Read more
Source§

impl<Ix: IndexType + One + Bounded> BitOr<&IntervalSet<Ix>> for &IntervalSet<Ix>

Union: &a | &b

Source§

type Output = IntervalSet<Ix>

The resulting type after applying the | operator.
Source§

fn bitor(self, other: &IntervalSet<Ix>) -> Self::Output

Performs the | operation. Read more
Source§

impl<Ix: IndexType + One + Bounded> BitOrAssign<&IntervalSet<Ix>> for IntervalSet<Ix>

Union assignment: a |= &b

Source§

fn bitor_assign(&mut self, other: &IntervalSet<Ix>)

Performs the |= operation. Read more
Source§

impl<Ix: IndexType + One + Bounded> BitXor<&IntervalSet<Ix>> for &IntervalSet<Ix>

Symmetric Difference: &a ^ &b

Source§

type Output = IntervalSet<Ix>

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, other: &IntervalSet<Ix>) -> Self::Output

Performs the ^ operation. Read more
Source§

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>)

Performs the ^= operation. Read more
Source§

impl<Ix: Clone> Clone for IntervalSet<Ix>

Source§

fn clone(&self) -> IntervalSet<Ix>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<Ix: Debug> Debug for IntervalSet<Ix>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<Ix: IndexType> Default for IntervalSet<Ix>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'a, Ix: IndexType> IntoIterator for &'a IntervalSet<Ix>

Source§

type Item = Range<Ix>

The type of the elements being iterated over.
Source§

type IntoIter = IterSet<'a, Ix>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<Ix: IndexType> IntoIterator for IntervalSet<Ix>

Source§

type Item = Range<Ix>

The type of the elements being iterated over.
Source§

type IntoIter = IntoIterSet<Ix>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<Ix: IndexType + One + Bounded> Sub<&IntervalSet<Ix>> for &IntervalSet<Ix>

Difference: &a - &b

Source§

type Output = IntervalSet<Ix>

The resulting type after applying the - operator.
Source§

fn sub(self, other: &IntervalSet<Ix>) -> Self::Output

Performs the - operation. Read more
Source§

impl<Ix: IndexType + One + Bounded> SubAssign<&IntervalSet<Ix>> for IntervalSet<Ix>

Difference assignment: a -= &b

Source§

fn sub_assign(&mut self, other: &IntervalSet<Ix>)

Performs the -= operation. Read more

Auto Trait Implementations§

§

impl<Ix> Freeze for IntervalSet<Ix>

§

impl<Ix> RefUnwindSafe for IntervalSet<Ix>
where Ix: RefUnwindSafe,

§

impl<Ix> Send for IntervalSet<Ix>
where Ix: Send,

§

impl<Ix> Sync for IntervalSet<Ix>
where Ix: Sync,

§

impl<Ix> Unpin for IntervalSet<Ix>

§

impl<Ix> UnwindSafe for IntervalSet<Ix>
where Ix: RefUnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.