Enum stats_ci::Interval

source ·
pub enum Interval<T>where
    T: PartialOrd,{
    TwoSided(T, T),
    UpperOneSided(T),
    LowerOneSided(T),
}
Expand description

Interval over a partially ordered type (NB: floating point numbers are partially ordered because of NaN). The interval is defined by its lower and upper bounds. One-sided intervals (with a single concrete bound) are also supported.

Examples

use stats_ci::*;

let interval = Interval::new(0., 10.)?;
assert_eq!(interval.low(), Some(0.));
assert_eq!(interval.high(), Some(10.));
assert!(interval.contains(&5.));
assert!(!interval.contains(&20.));

let interval = Interval::try_from(0..=10)?;
assert_eq!(interval.low(), Some(0));
assert_eq!(interval.high(), Some(10));

Variants§

§

TwoSided(T, T)

§

UpperOneSided(T)

§

LowerOneSided(T)

Implementations§

source§

impl<T: PartialOrd> Interval<T>

source

pub fn new(low: T, high: T) -> Result<Self, IntervalError>

Create a new interval from its left and right bounds for ordered types with equality.

Examples
let interval = Interval::new(0., 1.)?;
assert_eq!(interval.low(), Some(0.));
assert_eq!(interval.high(), Some(1.));
let interval2 = Interval::new("A", "Z")?;
assert_eq!(interval2.low(), Some("A"));
assert_eq!(interval2.high(), Some("Z"));
let interval3 = Interval::new(0, 0_usize)?;
assert_eq!(interval3.low(), Some(0));
assert_eq!(interval3.high(), Some(0));
source

pub fn new_upper(low: T) -> Self

Create a new upper one-sided interval from its left bound. The interval is defined as [low, +∞). The bound is included in the interval.

Examples

The interval below represents [0., +∞).

let interval = Interval::new_upper(0.);
assert_eq!(interval.low(), Some(0.));
assert_eq!(interval.high(), None);
source

pub fn new_lower(high: T) -> Self

Create a new lower one-sided interval from its right bound. The interval is defined as (-∞, high]. The bound is included in the interval.

Examples

The interval below represents (-∞, 1.]

let interval = Interval::new_lower(1.);
assert_eq!(interval.low(), None);
assert_eq!(interval.high(), Some(1.));
source

pub fn is_two_sided(&self) -> bool

Test whether the interval is two-sided.

source

pub fn is_one_sided(&self) -> bool

Test whether the interval is one-sided.

source

pub fn is_upper(&self) -> bool

Test whether the interval is an upper one-sided interval.

source

pub fn is_lower(&self) -> bool

Test whether the interval is a lower one-sided interval.

source

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

Test whether the interval contains a value.

Examples
let interval = Interval::new(0., 1.)?;
assert!(interval.contains(&0.5));
assert!(!interval.contains(&2.));
source

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

Test whether the interval intersects another interval. Two intervals are considered to intersect even if they only have a single point in common (e.g., one of their bounds).

Examples
let interval = Interval::new(0., 1.)?;
let interval2 = Interval::new(0.5, 1.5)?;
assert!(interval.intersects(&interval2));
let interval3 = Interval::new(2., 3.)?;
assert!(!interval.intersects(&interval3));
source

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

Test whether the interval is included in another interval.

The inclusion is not strict, i.e. an interval is included in itself.

source

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

Test whether the interval includes another interval.

The inclusion is not strict, i.e. an interval includes itself.

source

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

Get the left bound of the interval (if any).

source

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

Get the right bound of the interval (if any).

source§

impl<T: PartialOrd + PartialEq> Interval<T>

source

pub fn is_degenerate(&self) -> bool

Test whether the interval is degenerate. A degenerate interval is an interval with a single point. For example, the interval [0, 0] is degenerate.

source§

impl<T: PartialOrd + Clone> Interval<T>

source

pub fn low(&self) -> Option<T>

Get the lower bound of the interval (if any) for partially ordered types.

This function clones the bound. If cloning is an issue, use Self::low_as_ref() instead.

source

pub fn high(&self) -> Option<T>

Get the upper bound of the interval (if any) for partially ordered types.

This function clones the bound. If cloning is an issue, use Self::high_as_ref() instead.

source§

impl<T: Float> Interval<T>

source

pub fn low_f(&self) -> T

Get the lower bound of the interval (if any) for floating point types. This function returns the negative infinite value for T for lower one-sided intervals.

source

pub fn high_f(&self) -> T

Get the upper bound of the interval (if any) for floating point types. This function returns the infinite value for T for upper one-sided intervals.

source

pub fn relative_to(&self, reference: &Interval<T>) -> Interval<T>

source§

impl<T: PrimInt + Signed> Interval<T>

source

pub fn low_i(&self) -> T

Get the lower bound of the interval (if any) for signed integer types. This function returns the minimal value for T for lower one-sided intervals.

source

pub fn high_i(&self) -> T

Get the upper bound of the interval (if any) for signed integer types. This function returns the maximal value for T for upper one-sided intervals.

source§

impl<T: PrimInt + Unsigned> Interval<T>

source

pub fn low_u(&self) -> T

Get the lower bound of the interval (if any) for unsigned integer types. This function returns 0 for lower one-sided intervals.

source

pub fn high_u(&self) -> T

Get the upper bound of the interval (if any) for unsigned integer types. This function returns the maximum value for T for upper one-sided intervals.

source§

impl<T: PartialOrd> Interval<T>

source

pub fn low_as_ref(&self) -> Option<&T>

Get a reference to the lower bound of the interval (if any).

See also Self::low() if cloning is not an issue.

source

pub fn high_as_ref(&self) -> Option<&T>

Get a reference to the upper bound of the interval (if any).

See also Self::high() if cloning is not an issue.

source§

impl<T: PartialOrd + Sub<Output = T> + Zero + Clone> Interval<T>

source

pub fn width(&self) -> Option<T>

Compute the width of the interval. If the interval is one-sided, the function returns None.

Trait Implementations§

source§

impl<F: Add<F, Output = F> + PartialOrd + Copy> Add<F> for Interval<F>

§

type Output = Interval<F>

The resulting type after applying the + operator.
source§

fn add(self, rhs: F) -> Self::Output

Performs the + operation. Read more
source§

impl<T: PartialOrd> AsRef<Interval<T>> for Interval<T>

source§

fn as_ref(&self) -> &Self

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl<T: PartialOrd + Clone> Clone for Interval<T>

source§

fn clone(&self) -> Self

Returns a copy 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> Debug for Interval<T>where T: PartialOrd + Debug,

source§

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

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

impl<T: PartialOrd + Display> Display for Interval<T>

source§

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

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

impl<F: Div<F, Output = F> + PartialOrd + Copy> Div<F> for Interval<F>

§

type Output = Interval<F>

The resulting type after applying the / operator.
source§

fn div(self, rhs: F) -> Self::Output

Performs the / operation. Read more
source§

impl<T: PartialOrd + Clone> From<Interval<T>> for (Option<T>, Option<T>)

source§

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

Convert an interval to a tuple of optional bounds. The first element of the tuple is the lower bound, the second element is the upper bound. If the interval is one-sided, one of the bounds is None,

source§

impl From<Interval<f32>> for (f32, f32)

source§

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

Converts to this type from the input type.
source§

impl From<Interval<f64>> for (f64, f64)

source§

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

Converts to this type from the input type.
source§

impl From<Interval<i128>> for (i128, i128)

source§

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

Converts to this type from the input type.
source§

impl From<Interval<i16>> for (i16, i16)

source§

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

Converts to this type from the input type.
source§

impl From<Interval<i32>> for (i32, i32)

source§

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

Converts to this type from the input type.
source§

impl From<Interval<i64>> for (i64, i64)

source§

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

Converts to this type from the input type.
source§

impl From<Interval<i8>> for (i8, i8)

source§

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

Converts to this type from the input type.
source§

impl From<Interval<isize>> for (isize, isize)

source§

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

Converts to this type from the input type.
source§

impl From<Interval<u128>> for (u128, u128)

source§

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

Converts to this type from the input type.
source§

impl From<Interval<u16>> for (u16, u16)

source§

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

Converts to this type from the input type.
source§

impl From<Interval<u32>> for (u32, u32)

source§

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

Converts to this type from the input type.
source§

impl From<Interval<u64>> for (u64, u64)

source§

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

Converts to this type from the input type.
source§

impl From<Interval<u8>> for (u8, u8)

source§

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

Converts to this type from the input type.
source§

impl From<Interval<usize>> for (usize, usize)

source§

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

Converts to this type from the input type.
source§

impl<T: PartialOrd> From<RangeFrom<T>> for Interval<T>

source§

fn from(range: RangeFrom<T>) -> Self

Create an upper one-sided interval from a range starting from a given value.

use stats_ci::Interval;
use std::ops::RangeFrom;
let interval = Interval::from(1..);
assert_eq!(interval, Interval::new_upper(1));
source§

impl<T: PartialOrd> From<RangeToInclusive<T>> for Interval<T>

source§

fn from(range: RangeToInclusive<T>) -> Self

Create a lower one-sided interval from a range ending at a given value.

use stats_ci::Interval;
use std::ops::RangeToInclusive;
let interval = Interval::from(..=1);
assert_eq!(interval, Interval::new_lower(1));
source§

impl<T: PartialOrd + Hash> 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<F: Mul<F, Output = F> + PartialOrd + Copy> Mul<F> for Interval<F>

§

type Output = Interval<F>

The resulting type after applying the * operator.
source§

fn mul(self, rhs: F) -> Self::Output

Performs the * operation. Read more
source§

impl<T> PartialEq<Interval<T>> for Interval<T>where T: PartialOrd + PartialEq,

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T: PartialOrd> PartialOrd<Interval<T>> for Interval<T>

source§

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

Compare two intervals. Given two intervals a and b, a < b if and only if the upper bound of a is less than the lower bound of b. Although interval bounds are inclusive, two intervals that overlap only at a single bound are considered ordered. E.g., intervals [x,y] is considered less than [a,b] if y==a and x<b.

Examples
let a = Interval::new(0, 10)?;
let b = Interval::new(10, 20)?;
let c = Interval::new(11, 20)?;
let d = Interval::new(0, 10)?;
let e = Interval::new_upper(10);
assert_eq!(a.partial_cmp(&b), Some(Ordering::Less));
assert_eq!(a.partial_cmp(&c), Some(Ordering::Less));
assert_eq!(a.partial_cmp(&d), Some(Ordering::Equal));
assert_eq!(a.partial_cmp(&e), Some(Ordering::Less));
assert_eq!(c.partial_cmp(&a), Some(Ordering::Greater));
assert_eq!(b.partial_cmp(&c), None);
1.0.0 · source§

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

This method 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

This method 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

This method 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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<T: PartialOrd> RangeBounds<T> for Interval<T>

source§

fn start_bound(&self) -> Bound<&T>

Start index bound. Read more
source§

fn end_bound(&self) -> Bound<&T>

End index bound. Read more
1.35.0 · source§

fn contains<U>(&self, item: &U) -> boolwhere T: PartialOrd<U>, U: PartialOrd<T> + ?Sized,

Returns true if item is contained in the range. Read more
source§

impl<F: Sub<F, Output = F> + PartialOrd + Copy> Sub<F> for Interval<F>

§

type Output = Interval<F>

The resulting type after applying the - operator.
source§

fn sub(self, rhs: F) -> Self::Output

Performs the - operation. Read more
source§

impl<T: PartialOrd> TryFrom<(Option<T>, Option<T>)> for Interval<T>

source§

fn try_from(value: (Option<T>, Option<T>)) -> Result<Self, Self::Error>

Create a new interval from a tuple of optional bounds. The first element of the tuple is the lower bound, the second element is the upper bound. If one of the bounds is None, the interval is one-sided. If both bounds are None, an error is returned.

§

type Error = IntervalError

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

impl<T: PartialOrd> TryFrom<(T, T)> for Interval<T>

source§

fn try_from(value: (T, T)) -> Result<Self, Self::Error>

Create a new interval from a tuple of bounds. The first element of the tuple is the lower bound, the second element is the upper bound. If the lower bound is greater than the upper bound, an error is returned.

§

type Error = IntervalError

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

impl<T: Ord> TryFrom<RangeInclusive<T>> for Interval<T>

source§

fn try_from(range: RangeInclusive<T>) -> Result<Self, Self::Error>

Create an interval from an inclusive range. The range must be non-empty or the function will return an error.

use stats_ci::Interval;
use std::ops::RangeInclusive;
let interval = Interval::try_from(1..=2);
assert!(interval.is_ok());
assert_eq!(interval.unwrap(), Interval::new(1, 2).unwrap());
§

type Error = IntervalError

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

impl<T: PartialOrd + Copy> Copy for Interval<T>

source§

impl<T> StructuralPartialEq for Interval<T>where T: PartialOrd,

Auto Trait Implementations§

§

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 Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. 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 Twhere 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> Same<T> for T

§

type Output = T

Should always be Self
§

impl<SS, SP> SupersetOf<SS> for SPwhere SS: SubsetOf<SP>,

§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
source§

impl<T> ToOwned for Twhere T: Clone,

§

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 Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V

source§

impl<T> Scalar for Twhere T: 'static + Clone + PartialEq<T> + Debug,