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§
Implementations§
source§impl<T: PartialOrd> Interval<T>
impl<T: PartialOrd> Interval<T>
sourcepub fn new(low: T, high: T) -> Result<Self, IntervalError>
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));sourcepub fn new_upper(low: T) -> Self
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);sourcepub fn new_lower(high: T) -> Self
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.));sourcepub fn is_two_sided(&self) -> bool
pub fn is_two_sided(&self) -> bool
Test whether the interval is two-sided.
sourcepub fn is_one_sided(&self) -> bool
pub fn is_one_sided(&self) -> bool
Test whether the interval is one-sided.
sourcepub fn contains(&self, x: &T) -> bool
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.));sourcepub fn intersects(&self, other: &Self) -> bool
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));sourcepub fn is_included_in(&self, other: &Self) -> bool
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§impl<T: PartialOrd + PartialEq> Interval<T>
impl<T: PartialOrd + PartialEq> Interval<T>
sourcepub fn is_degenerate(&self) -> bool
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>
impl<T: PartialOrd + Clone> Interval<T>
sourcepub fn low(&self) -> Option<T>
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.
sourcepub fn high(&self) -> Option<T>
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>
impl<T: Float> Interval<T>
sourcepub fn low_f(&self) -> T
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.
sourcepub fn high_f(&self) -> T
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.
pub fn relative_to(&self, reference: &Interval<T>) -> Interval<T>
source§impl<T: PrimInt + Signed> Interval<T>
impl<T: PrimInt + Signed> Interval<T>
source§impl<T: PrimInt + Unsigned> Interval<T>
impl<T: PrimInt + Unsigned> Interval<T>
source§impl<T: PartialOrd> Interval<T>
impl<T: PartialOrd> Interval<T>
sourcepub fn low_as_ref(&self) -> Option<&T>
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.
sourcepub fn high_as_ref(&self) -> Option<&T>
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.
Trait Implementations§
source§impl<T: PartialOrd> From<RangeToInclusive<T>> for Interval<T>
impl<T: PartialOrd> From<RangeToInclusive<T>> for Interval<T>
source§fn from(range: RangeToInclusive<T>) -> Self
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> PartialEq<Interval<T>> for Interval<T>where
T: PartialOrd + PartialEq,
impl<T> PartialEq<Interval<T>> for Interval<T>where T: PartialOrd + PartialEq,
source§impl<T: PartialOrd> PartialOrd<Interval<T>> for Interval<T>
impl<T: PartialOrd> PartialOrd<Interval<T>> for Interval<T>
source§fn partial_cmp(&self, other: &Self) -> Option<Ordering>
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 le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self and other) and is used by the <=
operator. Read moresource§impl<T: PartialOrd> RangeBounds<T> for Interval<T>
impl<T: PartialOrd> RangeBounds<T> for Interval<T>
source§impl<T: PartialOrd> TryFrom<(Option<T>, Option<T>)> for Interval<T>
impl<T: PartialOrd> TryFrom<(Option<T>, Option<T>)> for Interval<T>
source§fn try_from(value: (Option<T>, Option<T>)) -> Result<Self, Self::Error>
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
type Error = IntervalError
source§impl<T: PartialOrd> TryFrom<(T, T)> for Interval<T>
impl<T: PartialOrd> TryFrom<(T, T)> for Interval<T>
source§fn try_from(value: (T, T)) -> Result<Self, Self::Error>
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
type Error = IntervalError
source§impl<T: Ord> TryFrom<RangeInclusive<T>> for Interval<T>
impl<T: Ord> TryFrom<RangeInclusive<T>> for Interval<T>
source§fn try_from(range: RangeInclusive<T>) -> Result<Self, Self::Error>
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
type Error = IntervalError
impl<T: PartialOrd + Copy> Copy for Interval<T>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere SS: SubsetOf<SP>,
§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read more§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.