Skip to main content

IntervalSet

Struct IntervalSet 

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

Source

pub fn new() -> Self

The empty set.

Source

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.

Source

pub fn insert(&mut self, start: T, end: T) -> Result<(), IntervalError>

Adds [start, end) to the set.

§Errors

As from_intervals, with index 0.

Source

pub fn iter(&self) -> impl Iterator<Item = (T, T)> + '_

The normal-form pieces, in increasing order.

Source

pub fn as_slice(&self) -> &[(T, T)]

The normal-form pieces as a slice.

Source

pub fn len(&self) -> usize

Number of disjoint pieces.

Source

pub fn is_empty(&self) -> bool

Whether the set contains no point.

Source

pub fn measure(&self) -> T::Length

Total length of the set — each point counted once.

Source

pub fn contains(&self, t: T) -> bool

Whether t lies in the set (start <= t < end for some piece).

Source

pub fn union(&self, other: &Self) -> Self

Points in either set.

Source

pub fn intersection(&self, other: &Self) -> Self

Points in both sets.

Source

pub fn difference(&self, other: &Self) -> Self

Points in self but not in other.

Source

pub fn clip(&self, start: T, end: T) -> Result<Self, IntervalError>

The part of the set inside [start, end).

§Errors

As from_intervals, with index 0.

Trait Implementations§

Source§

impl<T: Clone + IntervalBound> Clone for IntervalSet<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T: Debug + IntervalBound> Debug for IntervalSet<T>

Source§

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

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

impl<T: IntervalBound> Default for IntervalSet<T>

Source§

fn default() -> Self

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

impl<T: PartialEq + IntervalBound> PartialEq for IntervalSet<T>

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<T: PartialEq + IntervalBound> StructuralPartialEq for IntervalSet<T>

Auto Trait Implementations§

§

impl<T> Freeze for IntervalSet<T>
where Vec<(T, T)>: Freeze,

§

impl<T> RefUnwindSafe for IntervalSet<T>

§

impl<T> Send for IntervalSet<T>
where Vec<(T, T)>: Send,

§

impl<T> Sync for IntervalSet<T>
where Vec<(T, T)>: Sync,

§

impl<T> Unpin for IntervalSet<T>
where Vec<(T, T)>: Unpin,

§

impl<T> UnsafeUnpin for IntervalSet<T>

§

impl<T> UnwindSafe for IntervalSet<T>

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.