[][src]Enum intervals_general::interval::Interval

pub enum Interval<T> {
    Closed {
        bound_pair: BoundPair<T>,
    },
    Open {
        bound_pair: BoundPair<T>,
    },
    LeftHalfOpen {
        bound_pair: BoundPair<T>,
    },
    RightHalfOpen {
        bound_pair: BoundPair<T>,
    },
    UnboundedClosedRight {
        right: T,
    },
    UnboundedOpenRight {
        right: T,
    },
    UnboundedClosedLeft {
        left: T,
    },
    UnboundedOpenLeft {
        left: T,
    },
    Singleton {
        at: T,
    },
    Unbounded,
    Empty,
}

Interval enum capable of general interval representation

Where applicable, using lower bound a and upper bound b. An Interval taxonomy was pulled from proofwiki.

  • Closed -> [a, b]
  • Open -> (a,b)
  • LeftHalfOpen -> (a, b]
  • RightHalfOpen -> [a, b)
  • UnboundedClosedRight -> (-inf, a]
  • UnboundedOpenRight -> (-inf, a)
  • UnboundedClosedLeft -> [a, inf)
  • UnboundedOpenLeft -> (a, inf)
  • Singeleton -> [a]
  • Unbounded -> (-inf, inf)
  • Empty

Examples

use intervals_general::bound_pair::BoundPair;
use intervals_general::interval::Interval;
let bounds = BoundPair::new(1.0, 2.0).ok_or("invalid BoundPair")?;
let right_half_open = Interval::RightHalfOpen { bound_pair: bounds }; // [1.0, 2.0)

Variants

Closed

Fields of Closed

bound_pair: BoundPair<T>
Open

Fields of Open

bound_pair: BoundPair<T>
LeftHalfOpen

Fields of LeftHalfOpen

bound_pair: BoundPair<T>
RightHalfOpen

Fields of RightHalfOpen

bound_pair: BoundPair<T>
UnboundedClosedRight

Fields of UnboundedClosedRight

right: T
UnboundedOpenRight

Fields of UnboundedOpenRight

right: T
UnboundedClosedLeft

Fields of UnboundedClosedLeft

left: T
UnboundedOpenLeft

Fields of UnboundedOpenLeft

left: T
Singleton

Fields of Singleton

at: T
UnboundedEmpty

Methods

impl<T> Interval<T> where
    T: Copy,
    T: PartialOrd
[src]

pub fn contains(&self, other: &Interval<T>) -> bool[src]

Verify whether self contains the specified interval

Interval I1.contains(I2) if and only if:

  • The left bound of I1 is bounded and less than or equal to the left bound of I2 OR
  • the left bound of I1 is unbounded and the left bound of I2 is unbounded

AND

  • The right bound of I1 is bounded and greater than or equal to the right bound of I2 OR
  • The right bound of I1 isunbounded and the left bound of I2 is unbounded

Additionally:

  • The Empty interval does not contain the Empty interval

Examples

use intervals_general::bound_pair::BoundPair;
use intervals_general::interval::Interval;
let right_half_open = Interval::RightHalfOpen {
    bound_pair: BoundPair::new(1.0, 5.0).ok_or("invalid BoundPair")?,
};
let contained_interval = Interval::Open {
    bound_pair: BoundPair::new(1.0, 2.0).ok_or("invalid BoundPair")?,
};
let non_contained_interval = Interval::Closed {
    bound_pair: BoundPair::new(4.0, 5.0).ok_or("invalid BoundPair")?,
};
assert_eq!(right_half_open.contains(&contained_interval), true);
assert_eq!(right_half_open.contains(&non_contained_interval), false);

pub fn intersect(&self, other: &Interval<T>) -> Interval<T>[src]

Intersect an with the specified Interval

Take the intersection of self with the specified Interval.

Examples

use intervals_general::bound_pair::BoundPair;
use intervals_general::interval::Interval;

let i1 = Interval::RightHalfOpen {
    bound_pair: BoundPair::new(1, 5).ok_or("invalid BoundPair")?,
};
let i2 = Interval::Open {
    bound_pair: BoundPair::new(-1, 2).ok_or("invalid BoundPair")?,
};

assert_eq!(
    i1.intersect(&i2),
    Interval::RightHalfOpen {
        bound_pair: BoundPair::new(1, 2).ok_or("invalid BoundPair")?
    }
);

pub fn left_partial_cmp(&self, other: &Interval<T>) -> Option<Ordering>[src]

The PartialOrd::partial_cmp implementation for left Bounds

Though Intervals on some generics (e.g. integers) can supply Ord because they form a total order, unfortunately our floating point implementations break such properties. Therefore the best we can do under some generics is satisfy PartialOrd.

Examples

use intervals_general::bound_pair::BoundPair;
use intervals_general::interval::Interval;
use std::cmp::Ordering;

let right_half_open = Interval::RightHalfOpen {
    bound_pair: BoundPair::new(1.0, 5.0).ok_or("invalid BoundPair")?,
};
let contained_interval = Interval::Open {
    bound_pair: BoundPair::new(1.0, 2.0).ok_or("invalid BoundPair")?,
};

assert_eq!(
    contained_interval.left_partial_cmp(&right_half_open),
    Some(Ordering::Greater)
);

pub fn right_partial_cmp(&self, other: &Interval<T>) -> Option<Ordering>[src]

The PartialOrd::partial_cmp implementation for right Bounds

Though Intervals on some generics (e.g. integers) can supply Ord because they form a total order, unfortunately our floating point implementations break such properties. Therefore the best we can do under some generics is satisfy PartialOrd.

Examples

use intervals_general::bound_pair::BoundPair;
use intervals_general::interval::Interval;
use std::cmp::Ordering;

let right_half_open = Interval::RightHalfOpen {
    bound_pair: BoundPair::new(1.0, 5.0).ok_or("invalid BoundPair")?,
};
let contained_interval = Interval::Open {
    bound_pair: BoundPair::new(1.0, 2.0).ok_or("invalid BoundPair")?,
};

assert_eq!(
    contained_interval.right_partial_cmp(&right_half_open),
    Some(Ordering::Less)
);

pub fn width(&self) -> Option<<T as Sub>::Output> where
    T: Sub
[src]

Compute the width of the interval

Returns right - left bound, so long as finite, else None TODO How to handle overflow detection? I do not have access to check_sub due to generic? Presently for interval widths exceeding the Boundary type representation, panic occurs in debug mode and wrapping occurs in production mode.

Examples

use intervals_general::bound_pair::BoundPair;
use intervals_general::interval::Interval;

let interval = Interval::RightHalfOpen {
    bound_pair: BoundPair::new(1, 5).ok_or("invalid BoundPair")?,
};

let width: i32 = interval.width().ok_or("width was None")?;
assert_eq!(width, 4);

pub fn complement(
    &self
) -> Either<Once<Interval<T>>, Chain<Once<Interval<T>>, Once<Interval<T>>>>
[src]

Take the complement of the Interval, return one or two Intervals

The return value is iterable and contains exclusively one or two Intervals, depending upon result.

Example

use intervals_general::bound_pair::BoundPair;
use intervals_general::interval::Interval;

let mut result_it =
    Interval::Closed {
        bound_pair: BoundPair::new(1, 5).ok_or("invalid BoundPair")?,
    }
    .complement();

assert_eq!(
    result_it.next(),
    Some(Interval::UnboundedOpenRight { right: 1 })
);
assert_eq!(
    result_it.next(),
    Some(Interval::UnboundedOpenLeft{ left: 5 })
);
assert_eq!(
    result_it.next(),
    None
);

Trait Implementations

impl<T: Copy> Copy for Interval<T>[src]

impl<T: PartialEq> PartialEq<Interval<T>> for Interval<T>[src]

impl<T: Clone> Clone for Interval<T>[src]

fn clone_from(&mut self, source: &Self)
1.0.0
[src]

Performs copy-assignment from source. Read more

impl<T> Display for Interval<T> where
    T: Debug
[src]

Implement the Display trait for Intervals

Here I uses Wirth Interval Notation.

Examples

use intervals_general::bound_pair::BoundPair;
use intervals_general::interval::Interval;

let bp = BoundPair::new(1, 5).ok_or("invalid BoundPair")?;

assert_eq!(format!("{}", Interval::Closed { bound_pair: bp }), "[1..5]");
assert_eq!(
    format!("{}", Interval::UnboundedOpenRight { right: 5 }),
    "(←..5)"
);

impl<T: Debug> Debug for Interval<T>[src]

Auto Trait Implementations

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

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

Blanket Implementations

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> From for T[src]

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> BorrowMut for T where
    T: ?Sized
[src]

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.