pub struct RangeList<E>where
E: PartialOrd,{ /* private fields */ }Expand description
A sorted collection of inclusive ranges that can be used to represent non-continuous sets of values.
§Warning
Although RangeList can be constructed for elements that do not implement
std::cmp::Ord, but do implement std::cmp::PartialOrd, constructor
methods, such as the FromIterator implementation, will panic if the used
boundary values cannot be sorted. This requirement allows the usage of types
like f64, as long as the user can guarantee that values that cannot be
ordered, like NaN, will not appear.
Implementations§
Source§impl<E> RangeList<E>where
E: PartialOrd,
impl<E> RangeList<E>where
E: PartialOrd,
Sourcepub fn first_position_bound(&self, bound: &Bound<E>) -> Option<usize>where
E: Clone + DiscreteElement,
pub fn first_position_bound(&self, bound: &Bound<E>) -> Option<usize>where
E: Clone + DiscreteElement,
Returns the Self::position pointing at the smallest element greater
than (or equal to) the given bound.
Passing Bound::Included(x) will return the position of the smallest
element greater than or equal to x, or None if all elements are
smaller than x.
Passing Bound::Excluded(x) will return the position of the smallest
element greater than x, or None if all elements are smaller than or
equal to x.
Passing Bound::Unbounded will return None.
§Examples
let rl = RangeList::from_iter([1..=4, 6..=8]);
assert_eq!(rl.first_position_bound(&Bound::Included(-1)), Some(0));
assert_eq!(rl.first_position_bound(&Bound::Included(1)), Some(0));
assert_eq!(rl.first_position_bound(&Bound::Excluded(1)), Some(1));
assert_eq!(rl.first_position_bound(&Bound::Included(4)), Some(3));
assert_eq!(rl.first_position_bound(&Bound::Excluded(4)), Some(4));
assert_eq!(rl.first_position_bound(&Bound::Included(8)), Some(6));
assert_eq!(rl.first_position_bound(&Bound::Included(9)), None);Sourcepub fn from_sorted_elements<T>(iter: T) -> RangeList<E>
pub fn from_sorted_elements<T>(iter: T) -> RangeList<E>
Sourcepub fn from_sorted_ranges<T>(iter: T) -> RangeList<E>
pub fn from_sorted_ranges<T>(iter: T) -> RangeList<E>
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the range list contains no items.
§Examples
assert!(!RangeList::from_iter([3..=4]).is_empty());
assert!(RangeList::<i64>::default().is_empty());
assert!(RangeList::from_iter([3..=2]).is_empty());Sourcepub fn iter<'a>(
&'a self,
) -> Map<<&'a RangeList<E> as IntoIterator>::IntoIter, fn(RangeInclusive<&'a E>) -> RangeInclusive<E>>where
E: Copy,
pub fn iter<'a>(
&'a self,
) -> Map<<&'a RangeList<E> as IntoIterator>::IntoIter, fn(RangeInclusive<&'a E>) -> RangeInclusive<E>>where
E: Copy,
Returns an Copying iterator for the ranges in the set.
Sourcepub fn last_position_bound(&self, bound: &Bound<E>) -> Option<usize>where
E: Clone + DiscreteElement,
pub fn last_position_bound(&self, bound: &Bound<E>) -> Option<usize>where
E: Clone + DiscreteElement,
Returns the Self::position pointing at the largest element smaller
than (or equal to) the given bound.
Passing Bound::Included(x) will return the position of the largest
element smaller than or equal to x, or None if all elements are larger
x.
Passing Bound::Excluded(x) will return the position of the largest
element smaller than x, or None if all elements are larger than or
equal to x.
Passing Bound::Unbounded will return None.
§Examples
let rl = RangeList::from_iter([1..=4, 6..=8]);
assert_eq!(rl.last_position_bound(&Bound::Included(1)), Some(0));
assert_eq!(rl.last_position_bound(&Bound::Included(4)), Some(3));
assert_eq!(rl.last_position_bound(&Bound::Excluded(4)), Some(2));
assert_eq!(rl.last_position_bound(&Bound::Included(9)), Some(7));
assert_eq!(rl.last_position_bound(&Bound::Excluded(9)), Some(7));
assert_eq!(rl.last_position_bound(&Bound::Included(-1)), None);
assert_eq!(rl.last_position_bound(&Bound::Excluded(1)), None);Sourcepub fn lower_bound(&self) -> Option<&E>
pub fn lower_bound(&self) -> Option<&E>
Returns the lower bound of the range list, or None if the range list is
empty.
§Examples
assert_eq!(RangeList::from_iter([1..=4]).lower_bound(), Some(&1));
assert_eq!(RangeList::from_iter([1..=4, 6..=7, -5..=-3]).lower_bound(), Some(&-5));
assert_eq!(RangeList::<i64>::default().lower_bound(), None);Sourcepub fn position(&self, elem: &E) -> Option<usize>where
E: DiscreteElement,
pub fn position(&self, elem: &E) -> Option<usize>where
E: DiscreteElement,
Returns how many elements precede the given element in the RangeList, or
None if the element does not occur in the RangeList.
§Examples
let rl = RangeList::from_iter([1..=4, 6..=8]);
assert_eq!(rl.position(&1), Some(0));
assert_eq!(rl.position(&4), Some(3));
assert_eq!(rl.position(&6), Some(4));
assert_eq!(rl.position(&7), Some(5));
assert_eq!(rl.position(&-4), None);Sourcepub fn set_lower_bound(&mut self, lower_bound: E)where
E: Debug,
pub fn set_lower_bound(&mut self, lower_bound: E)where
E: Debug,
Tightens the lower bound of the range list, removing any (partial) ranges that are below the new lower bound.
Note that no action is taken if the new lower bound is less than or equal to the current lower bound.
§Examples
let mut r = RangeList::from_iter([-5..=-3, 1..=4, 6..=7]);
r.set_lower_bound(2);
assert_eq!(r.lower_bound(), Some(&2));
assert_eq!(r.iter().collect::<Vec<_>>(), vec![2..=4, 6..=7]);Sourcepub fn set_upper_bound(&mut self, upper_bound: E)
pub fn set_upper_bound(&mut self, upper_bound: E)
Tightens the upper bound of the range list, removing any (partial) ranges that are above the new upper bound.
Note that no action is taken if the new upper bound is greater than or equal to the current upper bound.
§Examples
let mut r = RangeList::from_iter([-5..=-3, 1..=4, 6..=7]);
r.set_upper_bound(3);
assert_eq!(r.upper_bound(), Some(&3));
assert_eq!(r.iter().collect::<Vec<_>>(), vec![-5..=-3, 1..=3]);Sourcepub fn upper_bound(&self) -> Option<&E>
pub fn upper_bound(&self) -> Option<&E>
Returns the upper bound of the range list, or None if the range list is
empty
§Examples
assert_eq!(RangeList::from_iter([1..=4]).upper_bound(), Some(&4));
assert_eq!(RangeList::from_iter([1..=4, 6..=7, -5..=-3]).upper_bound(), Some(&7));
assert_eq!(RangeList::<i64>::default().upper_bound(), None);Trait Implementations§
Source§impl<E> Default for RangeList<E>where
E: PartialOrd,
impl<E> Default for RangeList<E>where
E: PartialOrd,
Source§impl<E> From<&RangeInclusive<E>> for RangeList<E>where
E: Clone + PartialOrd,
impl<E> From<&RangeInclusive<E>> for RangeList<E>where
E: Clone + PartialOrd,
Source§fn from(value: &RangeInclusive<E>) -> RangeList<E>
fn from(value: &RangeInclusive<E>) -> RangeList<E>
Source§impl<E> From<RangeInclusive<E>> for RangeList<E>where
E: Clone + PartialOrd,
impl<E> From<RangeInclusive<E>> for RangeList<E>where
E: Clone + PartialOrd,
Source§fn from(value: RangeInclusive<E>) -> RangeList<E>
fn from(value: RangeInclusive<E>) -> RangeList<E>
Source§impl<E, R> FromIterator<R> for RangeList<E>
impl<E, R> FromIterator<R> for RangeList<E>
Source§impl<E> IntervalIterator<E> for RangeList<E>where
E: PartialOrd + Clone,
impl<E> IntervalIterator<E> for RangeList<E>where
E: PartialOrd + Clone,
Source§type IntervalIter = <RangeList<E> as IntoIterator>::IntoIter
type IntervalIter = <RangeList<E> as IntoIterator>::IntoIter
Source§fn intervals(&self) -> <RangeList<E> as IntervalIterator<E>>::IntervalIter
fn intervals(&self) -> <RangeList<E> as IntervalIterator<E>>::IntervalIter
Source§fn card(&self) -> Option<usize>where
E: DiscreteElement,
fn card(&self) -> Option<usize>where
E: DiscreteElement,
Source§fn diff<O, R>(&self, other: &O) -> R
fn diff<O, R>(&self, other: &O) -> R
other. Read moreSource§fn disjoint<O>(&self, other: &O) -> boolwhere
O: IntervalIterator<E> + ?Sized,
fn disjoint<O>(&self, other: &O) -> boolwhere
O: IntervalIterator<E> + ?Sized,
self and other are disjoint setsSource§fn intersect<O, R>(&self, other: &O) -> R
fn intersect<O, R>(&self, other: &O) -> R
Source§fn subset<O>(&self, other: &O) -> boolwhere
O: IntervalIterator<E> + ?Sized,
fn subset<O>(&self, other: &O) -> boolwhere
O: IntervalIterator<E> + ?Sized,
self is a subset of otherSource§fn superset<O>(&self, other: &O) -> boolwhere
O: IntervalIterator<E> + ?Sized,
fn superset<O>(&self, other: &O) -> boolwhere
O: IntervalIterator<E> + ?Sized,
self is a superset of other