IntervalSet

Struct IntervalSet 

Source
pub struct IntervalSet<T: Domain> { /* private fields */ }
Expand description

A Set in N, Z, or R consisting of disjoint contiguous intervals.

§Invariants

  • All stored intervals are normalized.
    • We do not enforce this here because it should be an invariant of Interval already.
  • No stored interval may be the empty set.
    • Emptiness is represented by storing no intervals.
    • Normalized Interval<T> should have a total ordering w/o empty sets.
  • All intervals are stored in ascending order.
  • All stored intervals are disjoint subsets of T.
    • Stored intervals should not be adjacent.
      • This can only be assured for T: Eq + Ord

Implementations§

Source§

impl<T: Domain> IntervalSet<T>

Source

pub fn empty() -> Self

Create a new empty IntervalSet

Source

pub fn new(intervals: Vec<Interval<T>>) -> Self

Create a new Set of intervals and enforce invariants.

Source

pub fn satisfies_invariants(intervals: &Vec<Interval<T>>) -> bool

Source

pub fn new_unchecked(intervals: Vec<Interval<T>>) -> Self

Creates a new IntervalSet without checking invariants.

The invariants check and enforcement step can be expensive, O(nlog(n)), since it sorts all elements. If an operation is certain that it has left the invariants in tact it can create a new IntervalSet directly.

Behavior is undefined if invariants are violated!

Source

pub fn convex_hull(&self) -> Interval<T>

Creates an Interval that forms a convex hull for this Set.

This should be equivalent to using ConvexHull, but much more efficient and convenient.

This function call relies on invariants.

§Example
use intervalsets::prelude::*;

let set = IntervalSet::from_iter([
    Interval::closed(100, 110),
    Interval::closed(0, 10),
]);
assert_eq!(set.convex_hull(), Interval::closed(0, 110));

// ConvexHull trait equivalent
assert_eq!(Interval::convex_hull([set]), Interval::closed(0, 110));
Source

pub fn expect_interval(self) -> Interval<T>

Take the only Interval in this Set. self is consumed.

This is useful for operations that could return multiple intervals such as Union.

§Panics

This method panics if there is not exactly one subset.

§Example
use intervalsets::prelude::*;

let interval = Interval::closed(0, 10);
let iset = IntervalSet::from(interval.clone());
let unwrapped = iset.expect_interval(); // iset moved
assert_eq!(interval, unwrapped);

let a = Interval::closed(0, 10)
    .union(&Interval::closed(5, 15))
    .expect_interval();
assert_eq!(a, Interval::closed(0, 15));

let a = IntervalSet::<i32>::from_iter([]);
assert_eq!(a.expect_interval(), Interval::<i32>::empty());
use intervalsets::prelude::*;

let a = Interval::closed(0, 10)
    .union(&Interval::closed(100, 110))
    .expect_interval();
Source

pub fn intervals(&self) -> &Vec<Interval<T>>

Return an immutable reference to the subsets.

Source

pub fn iter(&self) -> impl Iterator<Item = &Interval<T>>

Returns a new iterator over the subsets in ascending order.

Source

pub fn collect_map<F>(&self, func: F) -> Self
where F: Fn(&mut Vec<Interval<T>>, &Interval<T>),

Returns a new IntervalSet mapped from this Set’s subsets.

The mapping function is given a mutable vector in which to collect as many or as few new intervals as desired regardless of the intermediate types in question.

§Example
use intervalsets::prelude::*;

let x = Interval::closed(0, 10)
    .union(&Interval::closed(20, 40))
    .union(&Interval::closed(50, 100));

let mapped = x.collect_map(|mut collect, subset| {
    if subset.count().finite() > 30 {
        collect.push(subset.clone())
    }
});

assert_eq!(mapped, IntervalSet::from(Interval::closed(50, 100)));

let mask = Interval::closed(5, 25);
let mapped = x.collect_map(|mut collect, subset| {
    collect.push(mask.intersection(subset));
});
assert_eq!(mapped, IntervalSet::from_iter([
    Interval::closed(5, 10),
    Interval::closed(20, 25)
]));

let mask_set = IntervalSet::from_iter([
    Interval::closed(20, 30),
    Interval::closed(50, 60),
]);
let mapped = x.collect_map(|mut collect, subset| {
    for interval in subset.difference(&mask_set) {
        collect.push(interval)
    }
});
assert_eq!(mapped, IntervalSet::from_iter([
    Interval::closed(0, 10),
    Interval::closed(31, 40),
    Interval::closed(61, 100),
]));
Source

pub fn flat_map<F>(&self, func: F) -> Self
where F: Fn(&Interval<T>) -> Self,

Returns a new IntervalSet mapped from this Set`s subsets.

use intervalsets::prelude::*;

let x = Interval::closed(0, 10)
    .union(&Interval::closed(20, 40))
    .union(&Interval::closed(50, 100));

let mapped = x.flat_map(|subset| {
    if subset.count().finite() > 30 {
        subset.clone().into()
    } else {
        Interval::empty().into()
    }
});

assert_eq!(mapped, IntervalSet::from(Interval::closed(50, 100)));

Trait Implementations§

Source§

impl<T: Domain> Bounding<T> for IntervalSet<T>

Source§

fn bound(&self, side: Side) -> Option<&Bound<T>>

Source§

fn left(&self) -> Option<&Bound<T>>

Source§

fn right(&self) -> Option<&Bound<T>>

Source§

fn lval(&self) -> Option<&T>

Source§

fn rval(&self) -> Option<&T>

Source§

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

Source§

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

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<T: Domain> Complement for IntervalSet<T>

Source§

impl<T: Domain> Contains<Interval<T>> for IntervalSet<T>

Source§

fn contains(&self, rhs: &Interval<T>) -> bool

Source§

impl<T: Domain> Contains<IntervalSet<T>> for Interval<T>

Source§

fn contains(&self, rhs: &IntervalSet<T>) -> bool

Source§

impl<T: Domain> Contains<T> for IntervalSet<T>

Source§

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

Source§

impl<T: Domain> Contains for IntervalSet<T>

Source§

fn contains(&self, rhs: &Self) -> bool

Source§

impl<T: Domain> ConvexHull<IntervalSet<T>> for Interval<T>

Source§

fn convex_hull<U: IntoIterator<Item = IntervalSet<T>>>(iter: U) -> Self

Source§

impl<T, Out> Count for IntervalSet<T>
where T: Countable<Output = Out>, Out: LibZero + Clone + Add<Out, Output = Out>,

Source§

type Output = Out

Source§

fn count(&self) -> Measurement<Self::Output>

Source§

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

Source§

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

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

impl<T: Domain> Difference<Interval<T>> for IntervalSet<T>

Source§

type Output = IntervalSet<T>

Source§

fn difference(&self, rhs: &Interval<T>) -> Self::Output

Source§

impl<T: Domain> Difference<IntervalSet<T>> for Interval<T>

Source§

type Output = IntervalSet<T>

Source§

fn difference(&self, rhs: &IntervalSet<T>) -> Self::Output

Source§

impl<T: Domain> Difference for IntervalSet<T>

Source§

type Output = IntervalSet<T>

Source§

fn difference(&self, rhs: &IntervalSet<T>) -> Self::Output

Source§

impl<T: Display + Domain> Display for IntervalSet<T>

Source§

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

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

impl<T: Domain> From<Interval<T>> for IntervalSet<T>

Source§

fn from(value: Interval<T>) -> Self

Converts to this type from the input type.
Source§

impl<T: Domain> FromIterator<Interval<T>> for IntervalSet<T>

Source§

fn from_iter<U: IntoIterator<Item = Interval<T>>>(iter: U) -> Self

Creates a value from an iterator. Read more
Source§

impl<T: Hash + Domain> Hash for IntervalSet<T>

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T: Domain> Intersection<Interval<T>> for IntervalSet<T>

Source§

type Output = IntervalSet<T>

Source§

fn intersection(&self, rhs: &Interval<T>) -> Self::Output

Source§

impl<T: Domain> Intersection<IntervalSet<T>> for Interval<T>

Source§

type Output = IntervalSet<T>

Source§

fn intersection(&self, rhs: &IntervalSet<T>) -> Self::Output

Source§

impl<T: Domain> Intersection for IntervalSet<T>

Source§

type Output = IntervalSet<T>

Source§

fn intersection(&self, rhs: &Self) -> Self::Output

Source§

impl<T: Domain> Intersects<Interval<T>> for IntervalSet<T>

Source§

fn intersects(&self, rhs: &Interval<T>) -> bool

Source§

fn is_disjoint_from(&self, rhs: &Rhs) -> bool

Source§

impl<T: Domain> Intersects<IntervalSet<T>> for Interval<T>

Source§

fn intersects(&self, rhs: &IntervalSet<T>) -> bool

Source§

fn is_disjoint_from(&self, rhs: &Rhs) -> bool

Source§

impl<T: Domain> Intersects for IntervalSet<T>

Source§

fn intersects(&self, rhs: &Self) -> bool

Source§

fn is_disjoint_from(&self, rhs: &Rhs) -> bool

Source§

impl<T: Domain> IntoIterator for IntervalSet<T>

Source§

type Item = Interval<T>

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<<IntervalSet<T> as IntoIterator>::Item>

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<T: Domain> MaybeEmpty for IntervalSet<T>

Source§

fn is_empty(&self) -> bool

Source§

impl<T: Domain + Ord> Ord for IntervalSet<T>

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

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

Source§

fn eq(&self, other: &IntervalSet<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: Domain + PartialOrd> PartialOrd for IntervalSet<T>

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T: Domain> SymmetricDifference<Interval<T>> for IntervalSet<T>

Source§

type Output = IntervalSet<T>

Source§

fn sym_difference(&self, rhs: &Interval<T>) -> Self::Output

Source§

impl<T: Domain> SymmetricDifference<IntervalSet<T>> for Interval<T>

Source§

impl<T: Domain> SymmetricDifference for IntervalSet<T>

Source§

impl<T: Domain> Union<Interval<T>> for IntervalSet<T>

Source§

type Output = IntervalSet<T>

Source§

fn union(&self, rhs: &Interval<T>) -> Self::Output

Source§

impl<T: Domain> Union<IntervalSet<T>> for Interval<T>

Source§

type Output = IntervalSet<T>

Source§

fn union(&self, rhs: &IntervalSet<T>) -> Self::Output

Source§

impl<T: Domain> Union for IntervalSet<T>

Source§

type Output = IntervalSet<T>

Source§

fn union(&self, rhs: &Self) -> Self::Output

Source§

impl<T, Out> Width for IntervalSet<T>
where T: Domain + Sub<T, Output = Out>, Out: LibZero + Add<Out, Output = Out> + Clone,

Source§

type Output = Out

Source§

fn width(&self) -> Measurement<Self::Output>

Source§

impl<T: Domain + Eq> Eq for IntervalSet<T>

Source§

impl<T: Domain> StructuralPartialEq for IntervalSet<T>

Auto Trait Implementations§

§

impl<T> Freeze for IntervalSet<T>

§

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

§

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

§

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

§

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

§

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

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.