pub struct Interval<T: Domain>(/* private fields */);Expand description
A Set representation of a contiguous interval on N, Z, or R.
Discrete types (integers) are normalized to closed form on creation.
All bounding conditions are supported.
Most operations are supported through trait implementations.
Implementations§
Source§impl<T: Domain> Interval<T>
impl<T: Domain> Interval<T>
Sourcepub fn closed(left: T, right: T) -> Self
pub fn closed(left: T, right: T) -> Self
Returns a new closed finite Interval or Empty
[a, b] = { x in T | a <= x <= b }
§Example
use intervalsets::Interval;
use intervalsets::ops::Contains;
let x = Interval::closed(10, 20);
assert_eq!(x.contains(&10), true);
assert_eq!(x.contains(&15), true);
assert_eq!(x.contains(&20), true);
assert_eq!(x.contains(&0), false);Sourcepub fn open(left: T, right: T) -> Self
pub fn open(left: T, right: T) -> Self
Returns a new open finite Interval or Empty
For discrete data types T, open bounds are normalized to closed form. Continuous(ish) types (like f32, or chrono::DateTime) are left as is.
(a, b) = { x in T | a < x < b }
§Example
use intervalsets::Interval;
use intervalsets::ops::Contains;
let x = Interval::open(0.0, 10.0);
assert_eq!(x.contains(&0.0), false);
assert_eq!(x.contains(&5.0), true);
let y = Interval::open(0, 10);
assert_eq!(y.contains(&0), false);
assert_eq!(y.contains(&5), true);
assert_eq!(y, Interval::closed(1, 9));Sourcepub fn open_closed(left: T, right: T) -> Self
pub fn open_closed(left: T, right: T) -> Self
Returns a new left open finite Interval or Empty
(a, b] = { x in T | a < x <= b }
Sourcepub fn closed_open(left: T, right: T) -> Self
pub fn closed_open(left: T, right: T) -> Self
Returns a new right open finite Interval or Empty
[a, b) = { x in T | a <= x < b }
Sourcepub fn open_unbound(left: T) -> Self
pub fn open_unbound(left: T) -> Self
Returns a new open, right-unbound Interval
(a, ->) = { x in T | a < x }
Sourcepub fn closed_unbound(left: T) -> Self
pub fn closed_unbound(left: T) -> Self
Returns a new closed, right-unbound Interval
[a, ->) = {x in T | a <= x }
Sourcepub fn unbound_open(right: T) -> Self
pub fn unbound_open(right: T) -> Self
Returns a new open, left-unbound Interval
(a, ->) = { x in T | a < x }
Sourcepub fn unbound_closed(right: T) -> Self
pub fn unbound_closed(right: T) -> Self
Returns a new closed, left-unbound Interval
[a, ->) = { x in T | a <= x }
Sourcepub fn unbounded() -> Self
pub fn unbounded() -> Self
Returns a new unbounded Interval.
An unbounded interval contains every element in T,
as well as every set of T except the Empty set.
(<-, ->) = { x in T }
§Example
use intervalsets::Interval;
use intervalsets::ops::Contains;
let x = Interval::<f32>::unbounded();
assert_eq!(x.contains(&10.0), true);
assert_eq!(x.contains(&Interval::empty()), false);Sourcepub fn new_finite(left: Bound<T>, right: Bound<T>) -> Self
pub fn new_finite(left: Bound<T>, right: Bound<T>) -> Self
Returns a new finite Interval.
If there are no elements that satisfy both left and right bounds
then an Empty interval is returned. Otherwise the result will
be fully bounded.
§Example
use intervalsets::{Bound, Interval, Bounding};
let x = Interval::open(0, 100);
let y = Interval::new_finite(x.right().unwrap().flip(), Bound::closed(200));
assert_eq!(y, Interval::closed(100, 200));
let x = Interval::open(10, 10);
assert_eq!(x, Interval::empty());Sourcepub fn new_half_bounded(side: Side, bound: Bound<T>) -> Self
pub fn new_half_bounded(side: Side, bound: Bound<T>) -> Self
Sourcepub fn is_finite(&self) -> bool
pub fn is_finite(&self) -> bool
Returns true if the interval is either fully bounded or empty.
§Example
use intervalsets::Interval;
assert_eq!(Interval::<i32>::empty().is_finite(), true);
assert_eq!(Interval::closed(0, 10).is_finite(), true);
assert_eq!(Interval::unbound_open(10).is_finite(), false);
assert_eq!(Interval::<i32>::unbounded().is_finite(), false);Sourcepub fn is_infinite(&self) -> bool
pub fn is_infinite(&self) -> bool
Returns true if the interval approaches infinity on either side.
§Example
use intervalsets::Interval;
assert_eq!(Interval::<i32>::empty().is_infinite(), false);
assert_eq!(Interval::<i32>::closed(0, 10).is_infinite(), false);
assert_eq!(Interval::unbound_open(10).is_infinite(), true);
assert_eq!(Interval::<i32>::unbounded().is_infinite(), true);Sourcepub fn is_fully_bounded(&self) -> bool
pub fn is_fully_bounded(&self) -> bool
Return true if the interval is finite and not empty.
§Example
use intervalsets::Interval;
assert_eq!(Interval::closed(0, 10).is_fully_bounded(), true);
assert_eq!(Interval::<i32>::empty().is_fully_bounded(), false);
assert_eq!(Interval::<i32>::unbounded().is_fully_bounded(), false);Sourcepub fn is_half_bounded(&self) -> bool
pub fn is_half_bounded(&self) -> bool
Return true if the interval is unbounded on exactly one side.
§Example
use intervalsets::Interval;
assert_eq!(Interval::closed_unbound(10).is_half_bounded(), true);
assert_eq!(Interval::<i32>::unbounded().is_half_bounded(), false);
Sourcepub fn is_half_bounded_on(&self, side: Side) -> bool
pub fn is_half_bounded_on(&self, side: Side) -> bool
Returns true if the interval is unbounded on the expected side.
§Example
use intervalsets::{Interval, Side};
let x = Interval::unbound_open(10);
assert_eq!(x.is_half_bounded_on(Side::Right), true);
assert_eq!(x.is_half_bounded_on(Side::Left), false);
let x = Interval::closed_unbound(10);
assert_eq!(x.is_half_bounded_on(Side::Right), false);
assert_eq!(x.is_half_bounded_on(Side::Left), true);Sourcepub fn is_unbounded(&self) -> bool
pub fn is_unbounded(&self) -> bool
Returns true if the interval is unbounded on both sides.
§Example
use intervalsets::Interval;
use intervalsets::ops::Merged;
let x = Interval::unbound_closed(10)
.merged(&Interval::closed_unbound(-10))
.unwrap();
assert_eq!(x.is_unbounded(), true);Sourcepub fn flat_map<F>(&self, func: F) -> Self
pub fn flat_map<F>(&self, func: F) -> Self
Map the bounds of this interval to a new one or else Empty.
§Example
use intervalsets::prelude::*;
use intervalsets::{Bound, Side};
use intervalsets::numeric::Domain;
fn shift<T: Domain>(interval: Interval<T>, amount: T) -> Interval<T>
where
T: Domain + core::ops::Add<T, Output=T>
{
let shift_bound = |bound: &Bound<T>| {
bound.map(|v| v.clone() + amount.clone())
};
interval.flat_map(|left, right| {
match (left, right) {
(None, None) => Interval::unbounded(),
(None, Some(right)) => {
Interval::new_half_bounded(Side::Right, shift_bound(right))
},
(Some(left), None) => {
Interval::new_half_bounded(Side::Left, shift_bound(left))
},
(Some(left), Some(right)) => {
Interval::new_finite(shift_bound(left), shift_bound(right))
}
}
})
}
assert_eq!(shift(Interval::empty(), 10), Interval::empty());
assert_eq!(shift(Interval::closed(0, 10), 10), Interval::closed(10, 20));
assert_eq!(shift(Interval::unbound_closed(0), 10), Interval::unbound_closed(10));
assert_eq!(shift(Interval::closed_unbound(0), 10), Interval::closed_unbound(10));Sourcepub fn map_or<F, U>(&self, default: U, func: F) -> U
pub fn map_or<F, U>(&self, default: U, func: F) -> U
Map the bounds of this interval or return default if Empty.
§Example
use intervalsets::prelude::*;
fn is_finite(interval: Interval<i32>) -> bool {
interval.map_or(true, |left, right| {
matches!((left, right), (Some(_), Some(_)))
})
}
assert_eq!(is_finite(Interval::empty()), true);
assert_eq!(is_finite(Interval::closed(0, 10)), true);
assert_eq!(is_finite(Interval::unbound_closed(0)), false);
assert_eq!(is_finite(Interval::closed_unbound(0)), false);
assert_eq!(is_finite(Interval::unbounded()), false);Sourcepub fn map_or_else<F, D, U>(&self, default: D, func: F) -> U
pub fn map_or_else<F, D, U>(&self, default: D, func: F) -> U
Map the bounds of this interval or result of default fn if Empty.
§Example
use intervalsets::prelude::*;
use intervalsets::Side;
fn my_complement(interval: &Interval<i32>) -> Interval<i32> {
interval.map_or_else(Interval::unbounded, |left, right| {
match (left, right) {
(None, None) => Interval::empty(),
(None, Some(right)) => Interval::new_half_bounded(Side::Left, right.flip()),
(Some(left), None) => Interval::new_half_bounded(Side::Right, left.flip()),
(Some(left), Some(right)) => panic!("... elided ...")
}
})
}
let x = Interval::closed_unbound(0);
assert_eq!(my_complement(&x), x.complement().expect_interval());
let x = Interval::unbound_closed(0);
assert_eq!(my_complement(&x), x.complement().expect_interval());
let x = Interval::empty();
assert_eq!(my_complement(&x), x.complement().expect_interval());
let x = Interval::unbounded();
assert_eq!(my_complement(&x), x.complement().expect_interval());Sourcepub fn flat_map_finite<F>(&self, func: F) -> Self
pub fn flat_map_finite<F>(&self, func: F) -> Self
Map bounds to a new Interval if and only if fully bounded else Empty.
§Panics
This method panics if called on an unbounded interval.
§Example
use intervalsets::prelude::*;
fn shift(interval: Interval<i32>, amount: i32) -> Interval<i32> {
interval.flat_map_finite(|left, right| {
Interval::new_finite(
left.map(|v| v + amount), right.map(|v| v + amount)
)
})
}
assert_eq!(shift(Interval::empty(), 10), Interval::empty());
assert_eq!(shift(Interval::closed(0, 10), 10), Interval::closed(10, 20));use intervalsets::prelude::*;
fn shift(interval: Interval<i32>, amount: i32) -> Interval<i32> {
interval.flat_map_finite(|left, right| Interval::empty())
}
// any of these should panic:
assert_eq!(shift(Interval::unbound_closed(0), 10), Interval::empty());
assert_eq!(shift(Interval::closed_unbound(0), 10), Interval::empty());
assert_eq!(shift(Interval::unbounded(), 10), Interval::empty());Trait Implementations§
Source§impl<T: Domain> Complement for Interval<T>
impl<T: Domain> Complement for Interval<T>
type Output = IntervalSet<T>
fn complement(&self) -> Self::Output
Source§impl<T: Domain> Contains<IntervalSet<T>> for Interval<T>
impl<T: Domain> Contains<IntervalSet<T>> for Interval<T>
fn contains(&self, rhs: &IntervalSet<T>) -> bool
Source§impl<T: Domain> ConvexHull<Interval<T>> for Interval<T>
impl<T: Domain> ConvexHull<Interval<T>> for Interval<T>
Source§fn convex_hull<U: IntoIterator<Item = Interval<T>>>(iter: U) -> Self
fn convex_hull<U: IntoIterator<Item = Interval<T>>>(iter: U) -> Self
Create a new interval that covers a set of intervals
§Example
use intervalsets::Interval;
use intervalsets::ConvexHull;
let iv = Interval::convex_hull(vec![
Interval::closed(100.0, 200.0),
Interval::open(0.0, 10.0),
Interval::closed_unbound(500.0),
]);
assert_eq!(iv, Interval::open_unbound(0.0));Source§impl<T: Domain> ConvexHull<IntervalSet<T>> for Interval<T>
impl<T: Domain> ConvexHull<IntervalSet<T>> for Interval<T>
fn convex_hull<U: IntoIterator<Item = IntervalSet<T>>>(iter: U) -> Self
Source§impl<T: Domain> ConvexHull<T> for Interval<T>
impl<T: Domain> ConvexHull<T> for Interval<T>
fn convex_hull<U: IntoIterator<Item = T>>(iter: U) -> Self
Source§impl<T: Domain> Difference<Interval<T>> for IntervalSet<T>
impl<T: Domain> Difference<Interval<T>> for IntervalSet<T>
type Output = IntervalSet<T>
fn difference(&self, rhs: &Interval<T>) -> Self::Output
Source§impl<T: Domain> Difference<IntervalSet<T>> for Interval<T>
impl<T: Domain> Difference<IntervalSet<T>> for Interval<T>
type Output = IntervalSet<T>
fn difference(&self, rhs: &IntervalSet<T>) -> Self::Output
Source§impl<T: Domain> Difference for Interval<T>
impl<T: Domain> Difference for Interval<T>
type Output = IntervalSet<T>
fn difference(&self, rhs: &Interval<T>) -> Self::Output
Source§impl<T: Domain> FromIterator<Interval<T>> for IntervalSet<T>
impl<T: Domain> FromIterator<Interval<T>> for IntervalSet<T>
Source§impl<T: Domain> Intersection<Interval<T>> for IntervalSet<T>
impl<T: Domain> Intersection<Interval<T>> for IntervalSet<T>
type Output = IntervalSet<T>
fn intersection(&self, rhs: &Interval<T>) -> Self::Output
Source§impl<T: Domain> Intersection<IntervalSet<T>> for Interval<T>
impl<T: Domain> Intersection<IntervalSet<T>> for Interval<T>
type Output = IntervalSet<T>
fn intersection(&self, rhs: &IntervalSet<T>) -> Self::Output
Source§impl<T: Domain> Intersection for Interval<T>
impl<T: Domain> Intersection for Interval<T>
Source§impl<T: Domain> Intersects<Interval<T>> for IntervalSet<T>
impl<T: Domain> Intersects<Interval<T>> for IntervalSet<T>
fn intersects(&self, rhs: &Interval<T>) -> bool
fn is_disjoint_from(&self, rhs: &Rhs) -> bool
Source§impl<T: Domain> Intersects<IntervalSet<T>> for Interval<T>
impl<T: Domain> Intersects<IntervalSet<T>> for Interval<T>
fn intersects(&self, rhs: &IntervalSet<T>) -> bool
fn is_disjoint_from(&self, rhs: &Rhs) -> bool
Source§impl<T: Domain> Intersects for Interval<T>
impl<T: Domain> Intersects for Interval<T>
fn intersects(&self, rhs: &Self) -> bool
fn is_disjoint_from(&self, rhs: &Rhs) -> bool
Source§impl<T: Domain + Ord> Ord for Interval<T>
impl<T: Domain + Ord> Ord for Interval<T>
Source§impl<T: Domain + PartialOrd> PartialOrd for Interval<T>
impl<T: Domain + PartialOrd> PartialOrd for Interval<T>
Source§impl<T: Domain> SymmetricDifference<Interval<T>> for IntervalSet<T>
impl<T: Domain> SymmetricDifference<Interval<T>> for IntervalSet<T>
type Output = IntervalSet<T>
fn sym_difference(&self, rhs: &Interval<T>) -> Self::Output
Source§impl<T: Domain> SymmetricDifference<IntervalSet<T>> for Interval<T>
impl<T: Domain> SymmetricDifference<IntervalSet<T>> for Interval<T>
type Output = IntervalSet<T>
fn sym_difference(&self, rhs: &IntervalSet<T>) -> Self::Output
Source§impl<T: Domain> SymmetricDifference for Interval<T>
impl<T: Domain> SymmetricDifference for Interval<T>
type Output = IntervalSet<T>
fn sym_difference(&self, rhs: &Interval<T>) -> Self::Output
Source§impl<T: Domain> Union<IntervalSet<T>> for Interval<T>
impl<T: Domain> Union<IntervalSet<T>> for Interval<T>
type Output = IntervalSet<T>
fn union(&self, rhs: &IntervalSet<T>) -> Self::Output
impl<T: Domain + Eq> Eq for Interval<T>
impl<T: Domain> 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> 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> 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