pub struct Interval<T> {
pub lower: T,
pub upper: T,
pub kind: IntervalKind,
}Expand description
The interval between lower and upper, with the endpoints included or
excluded according to kind.
use symplex::{Interval, IntervalKind};
let ci = Interval::closed(0.108_f64, 0.603);
assert!(ci.contains(&0.3) && ci.contains(&0.603));
assert!((ci.width() - 0.495).abs() < 1e-12);
assert_eq!(ci.to_string(), "[0.108, 0.603]");
let iso = Interval::left_open(1, 2); // (1, 2]
assert!(!iso.contains(&1) && iso.contains(&2));
assert_eq!(iso.kind, IntervalKind::LeftOpen);
assert_eq!(iso.to_string(), "(1, 2]");
// Range syntax: `a..=b` is `[a, b]`, `a..b` is `[a, b)`.
assert_eq!(Interval::from(0.0..=1.0), Interval::closed(0.0, 1.0));
assert_eq!(Interval::from(0..5), Interval::right_open(0, 5));
assert!(Interval::open(3, 3).is_empty() && !Interval::closed(3, 3).is_empty());Fields§
§lower: TThe lower endpoint.
upper: TThe upper endpoint.
kind: IntervalKindWhich endpoints belong to the interval.
Implementations§
Source§impl Interval<Ex>
impl Interval<Ex>
Sourcepub fn to_set(&self) -> SetEx
pub fn to_set(&self) -> SetEx
The interval as a set expression, in the lower endpoint’s context;
the inverse of SetEx::as_intervals on one piece.
§Examples
use symplex::prelude::*;
let ctx = Context::new();
let iv = Interval::left_open(ctx.int(0), ctx.int(1)); // (0, 1]
assert_eq!(format!("{}", iv.to_set()), "(0, 1]");
assert_eq!(iv.to_set(), ctx.interval(&ctx.int(0), &ctx.int(1), IntervalKind::LeftOpen));Sourcepub fn contains(&self, x: &Ex) -> Option<bool>
pub fn contains(&self, x: &Ex) -> Option<bool>
Is x in the interval? Ex has no order, so this goes through the
set layer and is three-valued: None when the comparison with an
endpoint cannot be decided symbolically.
use symplex::prelude::*;
let ctx = Context::new();
let iv = Interval::left_open(ctx.int(0), ctx.int(1)); // (0, 1]
assert_eq!(iv.contains(&ctx.rational(1, 2)), Some(true));
assert_eq!(iv.contains(&ctx.int(0)), Some(false));
assert_eq!(iv.contains(&ctx.int(1)), Some(true));
assert_eq!(iv.contains(&ctx.symbol("t")), None);Source§impl<T> Interval<Extended<T>>
impl<T> Interval<Extended<T>>
Sourcepub const fn is_bounded(&self) -> bool
pub const fn is_bounded(&self) -> bool
Are both endpoints finite? [0, ∞) is not; [0, 1] is. (The
kind is irrelevant: an infinite end is unbounded whether the
interval is written closed or open there.)
Source§impl<T> Interval<T>
impl<T> Interval<T>
Sourcepub const fn right_open(lower: T, upper: T) -> Self
pub const fn right_open(lower: T, upper: T) -> Self
[lower, upper).
Sourcepub fn map<U>(self, f: impl FnMut(T) -> U) -> Interval<U>
pub fn map<U>(self, f: impl FnMut(T) -> U) -> Interval<U>
Apply f to both endpoints, keeping the kind. (For a decreasing
f the caller must also swap the endpoints and
reversed the kind.)
Sourcepub fn with_kind(self, kind: IntervalKind) -> Self
pub fn with_kind(self, kind: IntervalKind) -> Self
The same endpoints with a different kind.
Source§impl<T: PartialOrd> Interval<T>
impl<T: PartialOrd> Interval<T>
Sourcepub fn is_ordered(&self) -> bool
pub fn is_ordered(&self) -> bool
lower <= upper (false for NaN endpoints).
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Does the interval contain no point? [a, a] is the singleton
{a}; (a, a), (a, a], [a, a), anything with lower > upper
and anything with an unordered (NaN) endpoint are empty. Between
distinct endpoints an open interval of a discrete type may still be
empty — (1, 2) over the integers — which this does not detect.
Sourcepub fn contains_interval(&self, other: &Interval<T>) -> bool
pub fn contains_interval(&self, other: &Interval<T>) -> bool
Is every point of other in self? (Empty other counts as
contained only when it is a degenerate interval at a point of self;
callers who care should test is_empty first.)
Source§impl<T: PartialOrd + Clone> Interval<T>
impl<T: PartialOrd + Clone> Interval<T>
Sourcepub fn intersect(&self, other: &Interval<T>) -> Option<Interval<T>>
pub fn intersect(&self, other: &Interval<T>) -> Option<Interval<T>>
The intersection self ∩ other, or None when it is empty. At a
shared endpoint the open side wins ([0, 2] ∩ (0, 3) is (0, 2]);
when one interval’s upper end meets the other’s lower end the result
is the point only if both ends are closed.
Sourcepub fn hull(&self, other: &Interval<T>) -> Option<Interval<T>>
pub fn hull(&self, other: &Interval<T>) -> Option<Interval<T>>
The smallest interval containing both ([0, 1) ∪ (2, 3] hulls to
[0, 3]). At a shared endpoint the closed side wins. None only
when the endpoints cannot be ordered (a NaN endpoint).
Sourcepub fn clamp_to_closure(&self, x: T) -> T
pub fn clamp_to_closure(&self, x: T) -> T
x moved to the nearest point of a closed interval
(x.clamp(lower, upper)); for another kind the nearest point does
not exist, so this uses the closure and says so in the name.
Trait Implementations§
impl<T: Copy> Copy for Interval<T>
impl<T: Eq> Eq for Interval<T>
Source§impl<T> From<RangeInclusive<T>> for Interval<T>
a..=b is [a, b].
impl<T> From<RangeInclusive<T>> for Interval<T>
a..=b is [a, b].
Source§fn from(r: RangeInclusive<T>) -> Self
fn from(r: RangeInclusive<T>) -> Self
Source§impl<T> RangeBounds<T> for Interval<T>
impl<T> RangeBounds<T> for Interval<T>
impl<T: PartialEq> 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> UnsafeUnpin for Interval<T>where
T: UnsafeUnpin,
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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