Struct iset::set::IntervalSet

source ·
pub struct IntervalSet<T, Ix: IndexType = DefaultIx> { /* private fields */ }
Expand description

Set with interval keys (ranges x..y). Newtype over IntervalMap<T, ()>. See IntervalMap for more information.

use iset::interval_set;
let mut set = interval_set!{ 0.4..1.5, 0.1..0.5, 5.0..7.0 };
assert!(set.insert(-1.0..0.2));
// false because the interval is already in the set.
assert!(!set.insert(0.1..0.5));

assert!(set.contains(5.0..7.0));
assert!(set.remove(5.0..7.0));

// Iterate over intervals that overlap `0.2..0.8`.
let a: Vec<_> = set.iter(0.2..0.8).collect();
assert_eq!(a, &[0.1..0.5, 0.4..1.5]);

// Iterate over intervals that overlap a point 0.5.
let b: Vec<_> = set.overlap(0.5).collect();
assert_eq!(b, &[0.4..1.5]);

// Will panic:
// set.insert(0.0..core::f64::NAN);
// set.overlap(core::f64::NAN);

// It is still possible to use infinity.
const INF: f64 = core::f64::INFINITY;
set.insert(0.0..INF);
let c: Vec<_> = set.overlap(0.5).collect();
assert_eq!(c, &[0.0..INF, 0.4..1.5]);

println!("{:?}", set);
// {-1.0..0.2, 0.0..inf, 0.1..0.5, 0.4..1.5}
assert_eq!(set.range().unwrap(), -1.0..INF);
assert_eq!(set.smallest().unwrap(), -1.0..0.2);
assert_eq!(set.largest().unwrap(), 0.4..1.5);

There are no mutable iterators over IntervalSet as keys should be immutable.

In contrast to the IntervalMap, IntervalSet does not have a force_insert, and completely forbids duplicate intervals.

Implementations§

source§

impl<T: PartialOrd + Copy> IntervalSet<T>

source

pub fn new() -> Self

Creates an empty IntervalSet with default index type DefaultIx.

source§

impl<T: PartialOrd + Copy, Ix: IndexType> IntervalSet<T, Ix>

source

pub fn with_capacity(capacity: usize) -> Self

Creates an empty IntervalSet with capacity.

source

pub fn from_sorted<I>(iter: I) -> Selfwhere I: Iterator<Item = Range<T>>,

Creates an interval set from a sorted iterator over intervals. Takes O(N).

Panics if the intervals are not sorted or if there are equal intervals.

source

pub fn len(&self) -> usize

Returns the number of elements in the set.

source

pub fn is_empty(&self) -> bool

Returns true if the set contains no elements.

source

pub fn clear(&mut self)

Clears the set, removing all values. This method has no effect on the allocated capacity.

source

pub fn shrink_to_fit(&mut self)

Shrinks inner contents.

source

pub fn insert(&mut self, interval: Range<T>) -> bool

Inserts an interval x..y to the set. If the set did not have this interval present, true is returned. Takes O(log N).

Panics if interval is empty (start >= end) or contains a value that cannot be compared (such as NAN).

source

pub fn contains(&self, interval: Range<T>) -> bool

Check if the interval set contains interval (exact match). Takes O(log N).

Panics if interval is empty (start >= end) or contains a value that cannot be compared (such as NAN).

source

pub fn remove(&mut self, interval: Range<T>) -> bool

Removes the interval from the set. Returns true if the interval was present in the set. Takes O(log N).

Panics if interval is empty (start >= end) or contains a value that cannot be compared (such as NAN).

source

pub fn range(&self) -> Option<Range<T>>

Returns a range of interval keys in the set, takes O(1). Returns None if the set is empty. out.start is the minimal start of all intervals in the set, and out.end is the maximal end of all intervals in the set.

source

pub fn smallest(&self) -> Option<Range<T>>

Returns the smallest interval in the set (in lexicographical order). Takes O(log N). Returns None if the set is empty.

source

pub fn remove_smallest(&mut self) -> Option<Range<T>>

Removes and returns the smallest interval in the set (in lexicographical order). Takes O(log N). Returns None if the set is empty.

source

pub fn largest(&self) -> Option<Range<T>>

Returns the largest interval in the set (in lexicographical order). Takes O(log N). Returns None if the set is empty.

source

pub fn remove_largest(&mut self) -> Option<Range<T>>

Removes and returns the largest interval in the set (in lexicographical order). Takes O(log N). Returns None if the set is empty.

source

pub fn has_overlap<R>(&self, query: R) -> boolwhere R: RangeBounds<T>,

Checks, if the query overlaps any intervals in the interval set. Equivalent to set.iter(query).next().is_some(), but much faster.

source

pub fn iter<'a, R>(&'a self, query: R) -> Intervals<'a, T, (), R, Ix> where R: RangeBounds<T>,

Iterates over intervals x..y that overlap the query. Takes O(log N + K) where K is the size of the output. Output is sorted by intervals.

Panics if interval is empty or contains a value that cannot be compared (such as NAN).

source

pub fn overlap<'a>( &'a self, point: T ) -> Intervals<'a, T, (), RangeInclusive<T>, Ix>

Iterates over intervals x..y that overlap the point. Same as iter(point..=point). See iter for more details.

source

pub fn into_iter<R>(self, query: R) -> IntoIntervals<T, (), R, Ix> where R: RangeBounds<T>,

Consumes IntervalSet and iterates over intervals x..y that overlap the query. See iter for more details.

source

pub fn unsorted_iter<'a>(&'a self) -> UnsIntervals<'a, T, (), Ix>

Creates an unsorted iterator over all intervals x..y. Slightly faster than the sorted iterator, although both take O(N).

source

pub fn unsorted_into_iter(self) -> UnsIntoIntervals<T, (), Ix>

Consumes IntervalSet and creates an unsorted iterator over all intervals x..y.

source§

impl<T, Ix> IntervalSet<T, Ix>where T: PartialOrd + Copy + Default + AddAssign + Sub<Output = T>, Ix: IndexType,

source

pub fn covered_len<R>(&self, query: R) -> Twhere R: RangeBounds<T>,

Calculates the total length of the query that is covered by intervals in the map. Takes O(log N + K) where K is the number of intervals that overlap query.

See IntervalMap::covered_len for more details.

Trait Implementations§

source§

impl<T: Clone, Ix: Clone + IndexType> Clone for IntervalSet<T, Ix>

source§

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

Returns a copy 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<T: PartialOrd + Copy + Debug, Ix: IndexType> Debug for IntervalSet<T, Ix>

source§

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

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

impl<T: PartialOrd + Copy, Ix: IndexType> Default for IntervalSet<T, Ix>

source§

fn default() -> Self

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

impl<T: PartialOrd + Copy> FromIterator<Range<T>> for IntervalSet<T>

Construct IntervalSet from ranges x..y.

source§

fn from_iter<I: IntoIterator<Item = Range<T>>>(iter: I) -> Self

Creates a value from an iterator. Read more
source§

impl<T: PartialOrd + Copy, Ix: IndexType> IntoIterator for IntervalSet<T, Ix>

§

type IntoIter = IntoIntervals<T, (), RangeFull, Ix>

Which kind of iterator are we turning this into?
§

type Item = Range<T>

The type of the elements being iterated over.
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

§

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

§

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

§

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

§

impl<T, Ix> Unpin for IntervalSet<T, Ix>where Ix: Unpin, T: Unpin,

§

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

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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 Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.