Skip to main content

Interval

Struct Interval 

Source
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: T

The lower endpoint.

§upper: T

The upper endpoint.

§kind: IntervalKind

Which endpoints belong to the interval.

Implementations§

Source§

impl Interval<Ex>

Source

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));
Source

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

pub fn is_empty(&self) -> Option<bool>

Is the interval known to be empty? Three-valued like contains.

Source§

impl<T> Interval<Extended<T>>

Source

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>

Source

pub const fn closed(lower: T, upper: T) -> Self

[lower, upper].

Source

pub const fn open(lower: T, upper: T) -> Self

(lower, upper).

Source

pub const fn left_open(lower: T, upper: T) -> Self

(lower, upper].

Source

pub const fn right_open(lower: T, upper: T) -> Self

[lower, upper).

Source

pub fn point(value: T) -> Self
where T: Clone,

The singleton [value, value].

Source

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

Source

pub const fn as_ref(&self) -> Interval<&T>

Borrow both endpoints.

Source

pub fn with_kind(self, kind: IntervalKind) -> Self

The same endpoints with a different kind.

Source

pub fn reversed(self) -> Self

The image under a decreasing map that has already been applied to the endpoints: swap them and swap the openness with them, so that [a, b) becomes (f(b), f(a)].

Source

pub fn into_pair(self) -> (T, T)

The endpoints as a (lower, upper) tuple, for destructuring: let (lo, hi) = ci.into_pair();. The kind is dropped.

Source§

impl<T: Clone> Interval<&T>

Source

pub fn cloned(self) -> Interval<T>

Clone both endpoints.

Source§

impl<T: PartialOrd> Interval<T>

Source

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

Is x in the interval (endpoints according to kind)?

Source

pub fn is_ordered(&self) -> bool

lower <= upper (false for NaN endpoints).

Source

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.

Source

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>

Source

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.

Source

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

Source

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.

Source§

impl<T: PartialEq> Interval<T>

Source

pub fn is_point(&self) -> bool

Is this the singleton [a, a]? Structural equality of the endpoints, so it works for Interval<Ex> too.

Source§

impl Interval<f64>

Source

pub fn is_finite(&self) -> bool

Are both endpoints finite (neither ±∞ nor NaN)?

Source

pub fn with_infinite_ends_open(self) -> Self

±∞ is not a real number, so an infinite endpoint cannot belong to the interval: this opens any infinite end ([0, ∞] → [0, ∞)), which is what stats::Support and the set layer do. Finite ends are untouched.

Source§

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

Source

pub fn width(&self) -> T

upper − lower.

Trait Implementations§

Source§

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

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

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

Source§

impl<T: Debug> Debug for Interval<T>

Source§

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

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

impl<T: Default> Default for Interval<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

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

Source§

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

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

impl<T: Eq> Eq for Interval<T>

Source§

impl<T> From<Interval<T>> for Bounds<T>

Source§

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

The closure [lower, upper] of the interval (the kind is dropped — constraint boxes are closed).

Source§

impl<T> From<Range<T>> for Interval<T>

a..b is [a, b), as in Rust.

Source§

fn from(r: Range<T>) -> Self

Converts to this type from the input type.
Source§

impl<T> From<RangeInclusive<T>> for Interval<T>

a..=b is [a, b].

Source§

fn from(r: RangeInclusive<T>) -> Self

Converts to this type from the input type.
Source§

impl<T: 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<T: PartialEq> PartialEq for Interval<T>

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl<T> 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 (const: unstable) · Source§

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

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

fn is_empty(&self) -> bool
where T: PartialOrd,

🔬This is a nightly-only experimental API. (range_bounds_is_empty)
Returns true if the range contains no items. One-sided ranges (RangeFrom, etc) always return false. Read more
Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

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

Source§

fn to_string(&self) -> String

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

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

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

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more