pub enum Interval<T> {
Empty,
Degenerate(T),
Concrete {
left: T,
right: 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.
Examples
let interval = Interval::new(0., 1.);
assert_eq!(interval.low().unwrap(), 0.);
assert_eq!(interval.high().unwrap(), 1.);
assert!(!interval.is_empty());
assert!(!interval.is_degenerate());
assert!(interval.is_concrete());
assert!(interval.contains(&0.5));
assert!(!interval.contains(&2.));
let interval2: Interval<_> = Interval::from(0..=10);
assert_eq!(interval2.low().unwrap(), 0);
assert_eq!(interval2.high().unwrap(), 10);Variants§
Implementations§
source§impl<T: PartialOrd> Interval<T>
impl<T: PartialOrd> Interval<T>
sourcepub fn new(low: T, high: T) -> Self
pub fn new(low: T, high: T) -> Self
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().unwrap(), 0.);
assert_eq!(interval.high().unwrap(), 1.);
assert!(!interval.is_empty());
let interval2 = Interval::new("A", "Z");
assert_eq!(interval2.low().unwrap(), "A");
assert_eq!(interval2.high().unwrap(), "Z");
let interval3 = Interval::new(0, 0_usize);
assert_eq!(interval3.low().unwrap(), 0);
assert_eq!(interval3.high().unwrap(), 0);
assert!(interval3.is_degenerate());
let interval4 = Interval::new(1, 0);
assert!(interval4.is_empty());sourcepub fn intersects(&self, other: &Self) -> bool
pub fn intersects(&self, other: &Self) -> bool
Test whether the interval intersects another interval.
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> Interval<T>
impl<T> Interval<T>
sourcepub fn new_unordered_unchecked(left: T, right: T) -> Self
pub fn new_unordered_unchecked(left: T, right: T) -> Self
Create a new interval from its left and right bounds for unordered types. The function is unchecked and always results in a concrete interval. NB: this function is not meant for ordered types; in particular for numerical types.
Examples
#[derive(Debug)]
enum Directions { North, South, East, West};
let interval = Interval::new_unordered_unchecked(Directions::North, Directions::West);
assert!(matches!(interval.left().unwrap(), Directions::North));
assert!(matches!(interval.right().unwrap(), Directions::West));
assert!(!interval.is_empty());
assert!(!interval.is_degenerate());
assert!(interval.is_concrete());
let interval = Interval::new_unordered_unchecked(Directions::North, Directions::North);
// NB: the interval is not degenerate because the bounds equality is not checked
assert!(!interval.is_degenerate());sourcepub fn is_degenerate(&self) -> bool
pub fn is_degenerate(&self) -> bool
Test if the interval is degenerate, in the sense that it contains a single element.
sourcepub fn is_concrete(&self) -> bool
pub fn is_concrete(&self) -> bool
Test if the interval is concrete, in the sense that it contains at least two elements.
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: 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) for ordered types.
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) for ordered types.
See also Self::high() if cloning is not an issue.
source§impl<T: PartialEq> Interval<T>
impl<T: PartialEq> Interval<T>
sourcepub fn new_unordered(left: T, right: T) -> Self
pub fn new_unordered(left: T, right: T) -> Self
Create a new interval from its left and right bounds for unordered types with equality.
Examples
#[derive(Debug, PartialEq)]
enum Directions { North, South, East, West};
let interval = Interval::new_unordered(Directions::North, Directions::West);
assert_eq!(interval.left().unwrap(), &Directions::North);
assert_eq!(interval.right().unwrap(), &Directions::West);
assert!(!interval.is_empty());
assert!(!interval.is_degenerate());
assert!(interval.is_concrete());
let interval = Interval::new_unordered(Directions::North, Directions::North);
assert!(interval.is_degenerate());Trait Implementations§
source§impl<T: Ord> From<RangeInclusive<T>> for Interval<T>
impl<T: Ord> From<RangeInclusive<T>> for Interval<T>
source§fn from(range: RangeInclusive<T>) -> Self
fn from(range: RangeInclusive<T>) -> Self
source§impl<T: PartialEq> PartialEq<Interval<T>> for Interval<T>
impl<T: PartialEq> PartialEq<Interval<T>> for Interval<T>
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.
recall that interval bounds are assumed inclusive.
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);
assert_eq!(a.partial_cmp(&b), None);
assert_eq!(a.partial_cmp(&c), Some(Ordering::Less));
assert_eq!(c.partial_cmp(&a), Some(Ordering::Greater));
assert_eq!(a.partial_cmp(&d), Some(Ordering::Equal));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>
impl<T: Copy> Copy for Interval<T>
impl<T> StructuralPartialEq for Interval<T>
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§
§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.