pub struct IntervalSet<T: Domain> { /* private fields */ }Expand description
A Set in N, Z, or R consisting of disjoint contiguous intervals.
§Invariants
- All stored intervals are normalized.
- We do not enforce this here because it should be
an invariant of Interval
already.
- We do not enforce this here because it should be
an invariant of Interval
- No stored interval may be the empty set.
- Emptiness is represented by storing no intervals.
- Normalized
Interval<T>should have a total ordering w/o empty sets.
- All intervals are stored in ascending order.
- All stored intervals are disjoint subsets of T.
- Stored intervals should not be adjacent.
- This can only be assured for T: Eq + Ord
- Stored intervals should not be adjacent.
Implementations§
Source§impl<T: Domain> IntervalSet<T>
impl<T: Domain> IntervalSet<T>
Sourcepub fn new(intervals: Vec<Interval<T>>) -> Self
pub fn new(intervals: Vec<Interval<T>>) -> Self
Create a new Set of intervals and enforce invariants.
pub fn satisfies_invariants(intervals: &Vec<Interval<T>>) -> bool
Sourcepub fn new_unchecked(intervals: Vec<Interval<T>>) -> Self
pub fn new_unchecked(intervals: Vec<Interval<T>>) -> Self
Creates a new IntervalSet without checking invariants.
The invariants check and enforcement step can be expensive, O(nlog(n)), since it sorts all elements. If an operation is certain that it has left the invariants in tact it can create a new IntervalSet directly.
Behavior is undefined if invariants are violated!
Sourcepub fn convex_hull(&self) -> Interval<T>
pub fn convex_hull(&self) -> Interval<T>
Creates an Interval that forms a convex hull for this Set.
This should be equivalent to using ConvexHull,
but much more efficient and convenient.
This function call relies on invariants.
§Example
use intervalsets::prelude::*;
let set = IntervalSet::from_iter([
Interval::closed(100, 110),
Interval::closed(0, 10),
]);
assert_eq!(set.convex_hull(), Interval::closed(0, 110));
// ConvexHull trait equivalent
assert_eq!(Interval::convex_hull([set]), Interval::closed(0, 110));Sourcepub fn expect_interval(self) -> Interval<T>
pub fn expect_interval(self) -> Interval<T>
Take the only Interval in this Set. self is consumed.
This is useful for operations that could return
multiple intervals such as Union.
§Panics
This method panics if there is not exactly one subset.
§Example
use intervalsets::prelude::*;
let interval = Interval::closed(0, 10);
let iset = IntervalSet::from(interval.clone());
let unwrapped = iset.expect_interval(); // iset moved
assert_eq!(interval, unwrapped);
let a = Interval::closed(0, 10)
.union(&Interval::closed(5, 15))
.expect_interval();
assert_eq!(a, Interval::closed(0, 15));
let a = IntervalSet::<i32>::from_iter([]);
assert_eq!(a.expect_interval(), Interval::<i32>::empty());use intervalsets::prelude::*;
let a = Interval::closed(0, 10)
.union(&Interval::closed(100, 110))
.expect_interval();Sourcepub fn iter(&self) -> impl Iterator<Item = &Interval<T>>
pub fn iter(&self) -> impl Iterator<Item = &Interval<T>>
Returns a new iterator over the subsets in ascending order.
Sourcepub fn collect_map<F>(&self, func: F) -> Self
pub fn collect_map<F>(&self, func: F) -> Self
Returns a new IntervalSet mapped from this Set’s subsets.
The mapping function is given a mutable vector in which to collect as many or as few new intervals as desired regardless of the intermediate types in question.
§Example
use intervalsets::prelude::*;
let x = Interval::closed(0, 10)
.union(&Interval::closed(20, 40))
.union(&Interval::closed(50, 100));
let mapped = x.collect_map(|mut collect, subset| {
if subset.count().finite() > 30 {
collect.push(subset.clone())
}
});
assert_eq!(mapped, IntervalSet::from(Interval::closed(50, 100)));
let mask = Interval::closed(5, 25);
let mapped = x.collect_map(|mut collect, subset| {
collect.push(mask.intersection(subset));
});
assert_eq!(mapped, IntervalSet::from_iter([
Interval::closed(5, 10),
Interval::closed(20, 25)
]));
let mask_set = IntervalSet::from_iter([
Interval::closed(20, 30),
Interval::closed(50, 60),
]);
let mapped = x.collect_map(|mut collect, subset| {
for interval in subset.difference(&mask_set) {
collect.push(interval)
}
});
assert_eq!(mapped, IntervalSet::from_iter([
Interval::closed(0, 10),
Interval::closed(31, 40),
Interval::closed(61, 100),
]));Sourcepub fn flat_map<F>(&self, func: F) -> Self
pub fn flat_map<F>(&self, func: F) -> Self
Returns a new IntervalSet mapped from this Set`s subsets.
use intervalsets::prelude::*;
let x = Interval::closed(0, 10)
.union(&Interval::closed(20, 40))
.union(&Interval::closed(50, 100));
let mapped = x.flat_map(|subset| {
if subset.count().finite() > 30 {
subset.clone().into()
} else {
Interval::empty().into()
}
});
assert_eq!(mapped, IntervalSet::from(Interval::closed(50, 100)));Trait Implementations§
Source§impl<T: Domain> Bounding<T> for IntervalSet<T>
impl<T: Domain> Bounding<T> for IntervalSet<T>
Source§impl<T: Clone + Domain> Clone for IntervalSet<T>
impl<T: Clone + Domain> Clone for IntervalSet<T>
Source§fn clone(&self) -> IntervalSet<T>
fn clone(&self) -> IntervalSet<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<T: Domain> Complement for IntervalSet<T>
impl<T: Domain> Complement for IntervalSet<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<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, Out> Count for IntervalSet<T>
impl<T, Out> Count for IntervalSet<T>
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 IntervalSet<T>
impl<T: Domain> Difference for IntervalSet<T>
type Output = IntervalSet<T>
fn difference(&self, rhs: &IntervalSet<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 IntervalSet<T>
impl<T: Domain> Intersection for IntervalSet<T>
type Output = IntervalSet<T>
fn intersection(&self, rhs: &Self) -> Self::Output
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 IntervalSet<T>
impl<T: Domain> Intersects for IntervalSet<T>
fn intersects(&self, rhs: &Self) -> bool
fn is_disjoint_from(&self, rhs: &Rhs) -> bool
Source§impl<T: Domain> IntoIterator for IntervalSet<T>
impl<T: Domain> IntoIterator for IntervalSet<T>
Source§impl<T: Domain> MaybeEmpty for IntervalSet<T>
impl<T: Domain> MaybeEmpty for IntervalSet<T>
Source§impl<T: Domain + Ord> Ord for IntervalSet<T>
impl<T: Domain + Ord> Ord for IntervalSet<T>
Source§impl<T: Domain + PartialOrd> PartialOrd for IntervalSet<T>
impl<T: Domain + PartialOrd> PartialOrd for IntervalSet<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 IntervalSet<T>
impl<T: Domain> SymmetricDifference for IntervalSet<T>
type Output = IntervalSet<T>
fn sym_difference(&self, rhs: &IntervalSet<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
Source§impl<T: Domain> Union for IntervalSet<T>
impl<T: Domain> Union for IntervalSet<T>
Source§impl<T, Out> Width for IntervalSet<T>
impl<T, Out> Width for IntervalSet<T>
impl<T: Domain + Eq> Eq for IntervalSet<T>
impl<T: Domain> StructuralPartialEq for IntervalSet<T>
Auto Trait Implementations§
impl<T> Freeze for IntervalSet<T>
impl<T> RefUnwindSafe for IntervalSet<T>where
T: RefUnwindSafe,
impl<T> Send for IntervalSet<T>where
T: Send,
impl<T> Sync for IntervalSet<T>where
T: Sync,
impl<T> Unpin for IntervalSet<T>where
T: Unpin,
impl<T> UnwindSafe for IntervalSet<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