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>
impl<T: PartialOrd + Copy> IntervalSet<T>
sourcepub fn new() -> Self
pub fn new() -> Self
Creates an empty IntervalSet with default index type DefaultIx.
source§impl<T: PartialOrd + Copy, Ix: IndexType> IntervalSet<T, Ix>
impl<T: PartialOrd + Copy, Ix: IndexType> IntervalSet<T, Ix>
sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Creates an empty IntervalSet with capacity
.
sourcepub fn from_sorted<I>(iter: I) -> Selfwhere
I: Iterator<Item = Range<T>>,
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.
sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the set, removing all values. This method has no effect on the allocated capacity.
sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrinks inner contents.
sourcepub fn insert(&mut self, interval: Range<T>) -> bool
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
).
sourcepub fn contains(&self, interval: Range<T>) -> bool
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
).
sourcepub fn remove(&mut self, interval: Range<T>) -> bool
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
).
sourcepub fn range(&self) -> Option<Range<T>>
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.
sourcepub fn smallest(&self) -> Option<Range<T>>
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.
sourcepub fn remove_smallest(&mut self) -> Option<Range<T>>
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.
sourcepub fn largest(&self) -> Option<Range<T>>
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.
sourcepub fn remove_largest(&mut self) -> Option<Range<T>>
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.
sourcepub fn has_overlap<R>(&self, query: R) -> boolwhere
R: RangeBounds<T>,
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.
sourcepub fn iter<'a, R>(&'a self, query: R) -> Intervals<'a, T, (), R, Ix> ⓘwhere
R: RangeBounds<T>,
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
).
sourcepub fn overlap<'a>(
&'a self,
point: T
) -> Intervals<'a, T, (), RangeInclusive<T>, Ix> ⓘ
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.
sourcepub fn into_iter<R>(self, query: R) -> IntoIntervals<T, (), R, Ix> ⓘwhere
R: RangeBounds<T>,
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.
sourcepub fn unsorted_iter<'a>(&'a self) -> UnsIntervals<'a, T, (), Ix> ⓘ
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).
sourcepub fn unsorted_into_iter(self) -> UnsIntoIntervals<T, (), Ix> ⓘ
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,
impl<T, Ix> IntervalSet<T, Ix>where T: PartialOrd + Copy + Default + AddAssign + Sub<Output = T>, Ix: IndexType,
sourcepub fn covered_len<R>(&self, query: R) -> Twhere
R: RangeBounds<T>,
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>
impl<T: Clone, Ix: Clone + IndexType> Clone for IntervalSet<T, Ix>
source§fn clone(&self) -> IntervalSet<T, Ix>
fn clone(&self) -> IntervalSet<T, Ix>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl<T: PartialOrd + Copy + Debug, Ix: IndexType> Debug for IntervalSet<T, Ix>
impl<T: PartialOrd + Copy + Debug, Ix: IndexType> Debug for IntervalSet<T, Ix>
source§impl<T: PartialOrd + Copy, Ix: IndexType> Default for IntervalSet<T, Ix>
impl<T: PartialOrd + Copy, Ix: IndexType> Default for IntervalSet<T, Ix>
source§impl<T: PartialOrd + Copy> FromIterator<Range<T>> for IntervalSet<T>
impl<T: PartialOrd + Copy> FromIterator<Range<T>> for IntervalSet<T>
Construct IntervalSet from ranges x..y
.