Interval

Struct Interval 

Source
pub struct Interval<T: Domain>(/* private fields */);
Expand description

A Set representation of a contiguous interval on N, Z, or R.

Discrete types (integers) are normalized to closed form on creation.

All bounding conditions are supported.

Most operations are supported through trait implementations.

Implementations§

Source§

impl<T: Domain> Interval<T>

Source

pub fn empty() -> Self

Returns a new Empty Interval

{} = {x | x not in T }

§Example
use intervalsets::Interval;
use intervalsets::ops::Contains;

let x = Interval::<i32>::empty();
assert_eq!(x.contains(&10), false);
Source

pub fn closed(left: T, right: T) -> Self

Returns a new closed finite Interval or Empty

[a, b] = { x in T | a <= x <= b }

§Example
use intervalsets::Interval;
use intervalsets::ops::Contains;

let x = Interval::closed(10, 20);
assert_eq!(x.contains(&10), true);
assert_eq!(x.contains(&15), true);
assert_eq!(x.contains(&20), true);
assert_eq!(x.contains(&0), false);
Source

pub fn open(left: T, right: T) -> Self

Returns a new open finite Interval or Empty

For discrete data types T, open bounds are normalized to closed form. Continuous(ish) types (like f32, or chrono::DateTime) are left as is.

(a, b) = { x in T | a < x < b }

§Example
use intervalsets::Interval;
use intervalsets::ops::Contains;

let x = Interval::open(0.0, 10.0);
assert_eq!(x.contains(&0.0), false);
assert_eq!(x.contains(&5.0), true);

let y = Interval::open(0, 10);
assert_eq!(y.contains(&0), false);
assert_eq!(y.contains(&5), true);
assert_eq!(y, Interval::closed(1, 9));
Source

pub fn open_closed(left: T, right: T) -> Self

Returns a new left open finite Interval or Empty

(a, b] = { x in T | a < x <= b }

Source

pub fn closed_open(left: T, right: T) -> Self

Returns a new right open finite Interval or Empty

[a, b) = { x in T | a <= x < b }

Source

pub fn open_unbound(left: T) -> Self

Returns a new open, right-unbound Interval

(a, ->) = { x in T | a < x }

Source

pub fn closed_unbound(left: T) -> Self

Returns a new closed, right-unbound Interval

[a, ->) = {x in T | a <= x }

Source

pub fn unbound_open(right: T) -> Self

Returns a new open, left-unbound Interval

(a, ->) = { x in T | a < x }

Source

pub fn unbound_closed(right: T) -> Self

Returns a new closed, left-unbound Interval

[a, ->) = { x in T | a <= x }

Source

pub fn unbounded() -> Self

Returns a new unbounded Interval.

An unbounded interval contains every element in T, as well as every set of T except the Empty set.

(<-, ->) = { x in T }

§Example
use intervalsets::Interval;
use intervalsets::ops::Contains;

let x = Interval::<f32>::unbounded();
assert_eq!(x.contains(&10.0), true);
assert_eq!(x.contains(&Interval::empty()), false);
Source

pub fn new_finite(left: Bound<T>, right: Bound<T>) -> Self

Returns a new finite Interval.

If there are no elements that satisfy both left and right bounds then an Empty interval is returned. Otherwise the result will be fully bounded.

§Example
use intervalsets::{Bound, Interval, Bounding};

let x = Interval::open(0, 100);
let y = Interval::new_finite(x.right().unwrap().flip(), Bound::closed(200));
assert_eq!(y, Interval::closed(100, 200));

let x = Interval::open(10, 10);
assert_eq!(x, Interval::empty());
Source

pub fn new_half_bounded(side: Side, bound: Bound<T>) -> Self

Returns a ew half bounded Interval.

§Example
use intervalsets::{Interval, Bound, Bounding, Side};
use intervalsets::ops::Complement;

let x = Interval::unbound_open(0);
let y = Interval::new_half_bounded(Side::Left, x.right().unwrap().flip());
assert_eq!(x.complement(), y.into());
Source

pub fn is_finite(&self) -> bool

Returns true if the interval is either fully bounded or empty.

§Example
use intervalsets::Interval;

assert_eq!(Interval::<i32>::empty().is_finite(), true);
assert_eq!(Interval::closed(0, 10).is_finite(), true);

assert_eq!(Interval::unbound_open(10).is_finite(), false);
assert_eq!(Interval::<i32>::unbounded().is_finite(), false);
Source

pub fn is_infinite(&self) -> bool

Returns true if the interval approaches infinity on either side.

§Example
use intervalsets::Interval;

assert_eq!(Interval::<i32>::empty().is_infinite(), false);
assert_eq!(Interval::<i32>::closed(0, 10).is_infinite(), false);

assert_eq!(Interval::unbound_open(10).is_infinite(), true);
assert_eq!(Interval::<i32>::unbounded().is_infinite(), true);
Source

pub fn is_fully_bounded(&self) -> bool

Return true if the interval is finite and not empty.

§Example
use intervalsets::Interval;

assert_eq!(Interval::closed(0, 10).is_fully_bounded(), true);

assert_eq!(Interval::<i32>::empty().is_fully_bounded(), false);
assert_eq!(Interval::<i32>::unbounded().is_fully_bounded(), false);
Source

pub fn is_half_bounded(&self) -> bool

Return true if the interval is unbounded on exactly one side.

§Example
use intervalsets::Interval;

assert_eq!(Interval::closed_unbound(10).is_half_bounded(), true);
assert_eq!(Interval::<i32>::unbounded().is_half_bounded(), false);
Source

pub fn is_half_bounded_on(&self, side: Side) -> bool

Returns true if the interval is unbounded on the expected side.

§Example
use intervalsets::{Interval, Side};

let x = Interval::unbound_open(10);
assert_eq!(x.is_half_bounded_on(Side::Right), true);
assert_eq!(x.is_half_bounded_on(Side::Left), false);

let x = Interval::closed_unbound(10);
assert_eq!(x.is_half_bounded_on(Side::Right), false);
assert_eq!(x.is_half_bounded_on(Side::Left), true);
Source

pub fn is_unbounded(&self) -> bool

Returns true if the interval is unbounded on both sides.

§Example
use intervalsets::Interval;
use intervalsets::ops::Merged;

let x = Interval::unbound_closed(10)
            .merged(&Interval::closed_unbound(-10))
            .unwrap();

assert_eq!(x.is_unbounded(), true);
Source

pub fn flat_map<F>(&self, func: F) -> Self
where F: FnOnce(Option<&Bound<T>>, Option<&Bound<T>>) -> Self,

Map the bounds of this interval to a new one or else Empty.

§Example
use intervalsets::prelude::*;
use intervalsets::{Bound, Side};
use intervalsets::numeric::Domain;

fn shift<T: Domain>(interval: Interval<T>, amount: T) -> Interval<T>
where
    T: Domain + core::ops::Add<T, Output=T>
{
    let shift_bound = |bound: &Bound<T>| {
        bound.map(|v| v.clone() + amount.clone())
    };

    interval.flat_map(|left, right| {
        match (left, right) {
            (None, None) => Interval::unbounded(),
            (None, Some(right)) => {
                Interval::new_half_bounded(Side::Right, shift_bound(right))
            },
            (Some(left), None) => {
                Interval::new_half_bounded(Side::Left, shift_bound(left))
            },
            (Some(left), Some(right)) => {
                Interval::new_finite(shift_bound(left), shift_bound(right))
            }
        }
    })
}

assert_eq!(shift(Interval::empty(), 10), Interval::empty());
assert_eq!(shift(Interval::closed(0, 10), 10), Interval::closed(10, 20));
assert_eq!(shift(Interval::unbound_closed(0), 10), Interval::unbound_closed(10));
assert_eq!(shift(Interval::closed_unbound(0), 10), Interval::closed_unbound(10));
Source

pub fn map_or<F, U>(&self, default: U, func: F) -> U
where F: FnOnce(Option<&Bound<T>>, Option<&Bound<T>>) -> U,

Map the bounds of this interval or return default if Empty.

§Example
use intervalsets::prelude::*;

fn is_finite(interval: Interval<i32>) -> bool {
    interval.map_or(true, |left, right| {
        matches!((left, right), (Some(_), Some(_)))
    })
}

assert_eq!(is_finite(Interval::empty()), true);
assert_eq!(is_finite(Interval::closed(0, 10)), true);
assert_eq!(is_finite(Interval::unbound_closed(0)), false);
assert_eq!(is_finite(Interval::closed_unbound(0)), false);
assert_eq!(is_finite(Interval::unbounded()), false);
Source

pub fn map_or_else<F, D, U>(&self, default: D, func: F) -> U
where D: FnOnce() -> U, F: FnOnce(Option<&Bound<T>>, Option<&Bound<T>>) -> U,

Map the bounds of this interval or result of default fn if Empty.

§Example
use intervalsets::prelude::*;
use intervalsets::Side;

fn my_complement(interval: &Interval<i32>) -> Interval<i32> {
    interval.map_or_else(Interval::unbounded, |left, right| {
        match (left, right) {
            (None, None) => Interval::empty(),
            (None, Some(right)) => Interval::new_half_bounded(Side::Left, right.flip()),
            (Some(left), None) => Interval::new_half_bounded(Side::Right, left.flip()),
            (Some(left), Some(right)) => panic!("... elided ...")
        }
    })
}

let x = Interval::closed_unbound(0);
assert_eq!(my_complement(&x), x.complement().expect_interval());

let x = Interval::unbound_closed(0);
assert_eq!(my_complement(&x), x.complement().expect_interval());

let x = Interval::empty();
assert_eq!(my_complement(&x), x.complement().expect_interval());

let x = Interval::unbounded();
assert_eq!(my_complement(&x), x.complement().expect_interval());
Source

pub fn flat_map_finite<F>(&self, func: F) -> Self
where F: FnOnce(&Bound<T>, &Bound<T>) -> Self,

Map bounds to a new Interval if and only if fully bounded else Empty.

§Panics

This method panics if called on an unbounded interval.

§Example
use intervalsets::prelude::*;

fn shift(interval: Interval<i32>, amount: i32) -> Interval<i32> {
    interval.flat_map_finite(|left, right| {
        Interval::new_finite(
            left.map(|v| v + amount), right.map(|v| v + amount)
        )
    })
}
assert_eq!(shift(Interval::empty(), 10), Interval::empty());
assert_eq!(shift(Interval::closed(0, 10), 10), Interval::closed(10, 20));
use intervalsets::prelude::*;

fn shift(interval: Interval<i32>, amount: i32) -> Interval<i32> {
    interval.flat_map_finite(|left, right| Interval::empty())
}

// any of these should panic:
assert_eq!(shift(Interval::unbound_closed(0), 10), Interval::empty());
assert_eq!(shift(Interval::closed_unbound(0), 10), Interval::empty());
assert_eq!(shift(Interval::unbounded(), 10), Interval::empty());

Trait Implementations§

Source§

impl<T: Domain> Bounding<T> for Interval<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 Interval<T>

Source§

fn clone(&self) -> Interval<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 Interval<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 Interval<T>

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Create a new interval that covers a set of intervals

§Example
use intervalsets::Interval;
use intervalsets::ConvexHull;

let iv = Interval::convex_hull(vec![
    Interval::closed(100.0, 200.0),
    Interval::open(0.0, 10.0),
    Interval::closed_unbound(500.0),
]);
assert_eq!(iv, Interval::open_unbound(0.0));
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: Domain> ConvexHull<T> for Interval<T>

Source§

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

Source§

impl<T> Count for Interval<T>
where T: Countable, T::Output: LibZero,

Source§

impl<T: Debug + Domain> Debug for Interval<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 Interval<T>

Source§

type Output = IntervalSet<T>

Source§

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

Source§

impl<T: Display + Domain> Display for Interval<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 Interval<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 Interval<T>

Source§

type Output = Interval<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 Interval<T>

Source§

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

Source§

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

Source§

impl<T: Domain> MaybeEmpty for Interval<T>

Source§

fn is_empty(&self) -> bool

Source§

impl<T: Domain> Merged for Interval<T>

Source§

type Output = Interval<T>

Source§

fn merged(&self, rhs: &Self) -> Option<Self::Output>

Source§

impl<T: Domain + Ord> Ord for Interval<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 Interval<T>

Source§

fn eq(&self, other: &Interval<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 Interval<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 Interval<T>

Source§

type Output = IntervalSet<T>

Source§

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

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 Interval<T>

Source§

type Output = IntervalSet<T>

Source§

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

Source§

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

Source§

type Output = Out

Source§

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

Source§

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

Source§

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

Auto Trait Implementations§

§

impl<T> Freeze for Interval<T>
where T: Freeze,

§

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

§

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

§

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

§

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

§

impl<T> UnwindSafe for Interval<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.