pub struct RangeList<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: PartialOrd> RangeList<E>
impl<E: PartialOrd> RangeList<E>
Sourcepub fn first_position_bound(&self, bound: &Bound<E>) -> Option<usize>
pub fn first_position_bound(&self, bound: &Bound<E>) -> Option<usize>
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_elements<T: IntoIterator<Item = E>>(iter: T) -> Self
pub fn from_elements<T: IntoIterator<Item = E>>(iter: T) -> Self
Sourcepub fn from_sorted_elements<T: IntoIterator<Item = E>>(iter: T) -> Self
pub fn from_sorted_elements<T: IntoIterator<Item = E>>(iter: T) -> Self
Sourcepub fn from_sorted_ranges<T: IntoIterator<Item = RangeInclusive<E>>>(
iter: T,
) -> Self
pub fn from_sorted_ranges<T: IntoIterator<Item = RangeInclusive<E>>>( iter: T, ) -> Self
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>
pub fn last_position_bound(&self, bound: &Bound<E>) -> Option<usize>
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>
👎Deprecated since 0.5.0: use min instead
pub fn lower_bound(&self) -> Option<&E>
use min instead
Returns the lower bound of the range list, or None if the range list
is empty.
Sourcepub fn max(&self) -> Option<&E>
pub fn max(&self) -> Option<&E>
Returns the maximum element of the range list, or None if the range
list is empty.
§Examples
assert_eq!(RangeList::from_iter([1..=4]).max(), Some(&4));
assert_eq!(RangeList::from_iter([1..=4, 6..=7, -5..=-3]).max(), Some(&7));
assert_eq!(RangeList::<i64>::default().max(), None);Sourcepub fn min(&self) -> Option<&E>
pub fn min(&self) -> Option<&E>
Returns the minimum element of the range list, or None if the range
list is empty.
§Examples
assert_eq!(RangeList::from_iter([1..=4]).min(), Some(&1));
assert_eq!(RangeList::from_iter([1..=4, 6..=7, -5..=-3]).min(), Some(&-5));
assert_eq!(RangeList::<i64>::default().min(), None);Sourcepub fn position(&self, elem: &E) -> Option<usize>where
E: Step,
pub fn position(&self, elem: &E) -> Option<usize>where
E: Step,
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,
👎Deprecated since 0.5.0: use tighten_min instead
pub fn set_lower_bound(&mut self, lower_bound: E)where
E: Debug,
use tighten_min instead
Tightens the lower bound of the range list, removing any (partial) ranges that are below the new lower bound.
Sourcepub fn set_upper_bound(&mut self, upper_bound: E)
👎Deprecated since 0.5.0: use tighten_max instead
pub fn set_upper_bound(&mut self, upper_bound: E)
use tighten_max instead
Tightens the upper bound of the range list, removing any (partial) ranges that are above the new upper bound.
Sourcepub fn tighten_max(&mut self, max: E)
pub fn tighten_max(&mut self, max: E)
Tightens the maximum of the range list, removing any (partial) ranges that are above the new maximum.
Note that no action is taken if max is greater than or equal to the
current maximum.
§Examples
let mut r = RangeList::from_iter([-5..=-3, 1..=4, 6..=7]);
r.tighten_max(3);
assert_eq!(r.max(), Some(&3));
assert_eq!(r.iter().collect::<Vec<_>>(), vec![-5..=-3, 1..=3]);Sourcepub fn tighten_min(&mut self, min: E)where
E: Debug,
pub fn tighten_min(&mut self, min: E)where
E: Debug,
Tightens the minimum of the range list, removing any (partial) ranges that are below the new minimum.
Note that no action is taken if min is less than or equal to the
current minimum.
§Examples
let mut r = RangeList::from_iter([-5..=-3, 1..=4, 6..=7]);
r.tighten_min(2);
assert_eq!(r.min(), Some(&2));
assert_eq!(r.iter().collect::<Vec<_>>(), vec![2..=4, 6..=7]);Sourcepub fn upper_bound(&self) -> Option<&E>
👎Deprecated since 0.5.0: use max instead
pub fn upper_bound(&self) -> Option<&E>
use max instead
Returns the upper bound of the range list, or None if the range list
is empty.
Trait Implementations§
Source§impl<E: PartialOrd> Default for RangeList<E>
impl<E: PartialOrd> Default for RangeList<E>
impl<E: Eq + PartialOrd> Eq for RangeList<E>
Source§impl<E: Clone + PartialOrd> From<&RangeInclusive<E>> for RangeList<E>
impl<E: Clone + PartialOrd> From<&RangeInclusive<E>> for RangeList<E>
Source§fn from(value: &RangeInclusive<E>) -> Self
fn from(value: &RangeInclusive<E>) -> Self
Source§impl<E: Clone + PartialOrd> From<RangeInclusive<E>> for RangeList<E>
impl<E: Clone + PartialOrd> From<RangeInclusive<E>> for RangeList<E>
Source§fn from(value: RangeInclusive<E>) -> Self
fn from(value: RangeInclusive<E>) -> Self
Source§impl<E, R> FromIterator<R> for RangeList<E>
impl<E, R> FromIterator<R> for RangeList<E>
Source§fn from_iter<T: IntoIterator<Item = R>>(iter: T) -> Self
fn from_iter<T: IntoIterator<Item = R>>(iter: T) -> Self
Source§impl<E: PartialOrd + Clone> IntervalIterator<E> for RangeList<E>
impl<E: PartialOrd + Clone> IntervalIterator<E> for RangeList<E>
Source§type IntervalIter = <RangeList<E> as IntoIterator>::IntoIter
type IntervalIter = <RangeList<E> as IntoIterator>::IntoIter
Source§fn intervals(&self) -> Self::IntervalIter
fn intervals(&self) -> Self::IntervalIter
Source§fn card(&self) -> Option<usize>where
E: Step,
fn card(&self) -> Option<usize>where
E: Step,
Source§fn diff<O, R>(&self, other: &O) -> R
fn diff<O, R>(&self, other: &O) -> R
other. Read moreSource§fn disjoint<O: IntervalIterator<E> + ?Sized>(&self, other: &O) -> bool
fn disjoint<O: IntervalIterator<E> + ?Sized>(&self, other: &O) -> bool
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: IntervalIterator<E> + ?Sized>(&self, other: &O) -> bool
fn subset<O: IntervalIterator<E> + ?Sized>(&self, other: &O) -> bool
self is a subset of otherSource§fn superset<O: IntervalIterator<E> + ?Sized>(&self, other: &O) -> bool
fn superset<O: IntervalIterator<E> + ?Sized>(&self, other: &O) -> bool
self is a superset of other