pub struct IntervalSet<T: IntervalBound> { /* private fields */ }Expand description
A finite union of half-open intervals [start, end), kept in normal form.
Empty intervals (start == end) contribute nothing and are dropped.
§Examples
Up-time of an asset in a reporting window, where planned and unplanned stops overlap:
use u_numflow::collections::IntervalSet;
let planned = IntervalSet::from_intervals([(0.0, 24.0)]).unwrap();
let unplanned = IntervalSet::from_intervals([(10.0, 30.0)]).unwrap();
let window = IntervalSet::from_intervals([(0.0, 168.0)]).unwrap();
let down = planned.union(&unplanned); // [0, 30): the 14 h overlap counts once
assert_eq!(down.measure(), 30.0);
assert_eq!(window.difference(&down).measure(), 138.0);Implementations§
Source§impl<T: IntervalBound> IntervalSet<T>
impl<T: IntervalBound> IntervalSet<T>
Sourcepub fn from_intervals<I>(intervals: I) -> Result<Self, IntervalError>where
I: IntoIterator<Item = (T, T)>,
pub fn from_intervals<I>(intervals: I) -> Result<Self, IntervalError>where
I: IntoIterator<Item = (T, T)>,
Builds the union of (start, end) pairs, each read as [start, end).
Overlapping and touching pieces are merged; empty ones are dropped.
§Errors
IntervalError::Reversed for a pair with start > end,
IntervalError::NotFinite for a NaN or infinite bound — with the
pair’s position in intervals.
Sourcepub fn insert(&mut self, start: T, end: T) -> Result<(), IntervalError>
pub fn insert(&mut self, start: T, end: T) -> Result<(), IntervalError>
Sourcepub fn iter(&self) -> impl Iterator<Item = (T, T)> + '_
pub fn iter(&self) -> impl Iterator<Item = (T, T)> + '_
The normal-form pieces, in increasing order.
Sourcepub fn contains(&self, t: T) -> bool
pub fn contains(&self, t: T) -> bool
Whether t lies in the set (start <= t < end for some piece).
Sourcepub fn intersection(&self, other: &Self) -> Self
pub fn intersection(&self, other: &Self) -> Self
Points in both sets.
Sourcepub fn difference(&self, other: &Self) -> Self
pub fn difference(&self, other: &Self) -> Self
Points in self but not in other.
Sourcepub fn clip(&self, start: T, end: T) -> Result<Self, IntervalError>
pub fn clip(&self, start: T, end: T) -> Result<Self, IntervalError>
Trait Implementations§
Source§impl<T: Clone + IntervalBound> Clone for IntervalSet<T>
impl<T: Clone + IntervalBound> Clone for IntervalSet<T>
Source§impl<T: Debug + IntervalBound> Debug for IntervalSet<T>
impl<T: Debug + IntervalBound> Debug for IntervalSet<T>
Source§impl<T: IntervalBound> Default for IntervalSet<T>
impl<T: IntervalBound> Default for IntervalSet<T>
Source§impl<T: PartialEq + IntervalBound> PartialEq for IntervalSet<T>
impl<T: PartialEq + IntervalBound> PartialEq for IntervalSet<T>
impl<T: PartialEq + IntervalBound> StructuralPartialEq for IntervalSet<T>
Auto Trait Implementations§
impl<T> Freeze for IntervalSet<T>
impl<T> RefUnwindSafe for IntervalSet<T>
impl<T> Send for IntervalSet<T>
impl<T> Sync for IntervalSet<T>
impl<T> Unpin for IntervalSet<T>
impl<T> UnsafeUnpin for IntervalSet<T>
impl<T> UnwindSafe for IntervalSet<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more