RangeList

Struct RangeList 

Source
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,

Source

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

pub fn from_sorted_elements<T>(iter: T) -> RangeList<E>
where T: IntoIterator<Item = E>, E: DiscreteElement + Clone,

Construct a RangeList from an iterator of elements that are known to be yielded in sorted (increasing) order, but where duplicates might still exist.

§Warning

This function will panic if the iterator yields a larger element than one yielded previously.

Source

pub fn from_sorted_ranges<T>(iter: T) -> RangeList<E>
where T: IntoIterator<Item = RangeInclusive<E>>, E: Any + Clone,

Construct a RangeList from an iterator of inclusive ranges that are known to be yielded in sorted (increasing) order, but where ranges might still need to be merged.

§Warning

This function will panic if the iterator yields a strictly larger range than the previous one.

Source

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

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.

Source

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

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

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

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

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

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> Clone for RangeList<E>
where E: Clone + PartialOrd,

Source§

fn clone(&self) -> RangeList<E>

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<E> Debug for RangeList<E>
where E: Debug + PartialOrd,

Source§

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

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

impl<E> Default for RangeList<E>
where E: PartialOrd,

Source§

fn default() -> RangeList<E>

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

impl<E> Display for RangeList<E>
where E: Debug + PartialOrd,

Source§

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

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

impl<E> From<&RangeInclusive<E>> for RangeList<E>
where E: Clone + PartialOrd,

Source§

fn from(value: &RangeInclusive<E>) -> RangeList<E>

Converts to this type from the input type.
Source§

impl<E> From<RangeInclusive<E>> for RangeList<E>
where E: Clone + PartialOrd,

Source§

fn from(value: RangeInclusive<E>) -> RangeList<E>

Converts to this type from the input type.
Source§

impl<E, R> FromIterator<R> for RangeList<E>
where E: Any + Clone + PartialOrd, R: Into<RangeInclusive<E>>,

Source§

fn from_iter<T>(iter: T) -> RangeList<E>
where T: IntoIterator<Item = R>,

Creates a value from an iterator. Read more
Source§

impl<E> Hash for RangeList<E>
where E: Hash + PartialOrd,

Source§

fn hash<__H>(&self, state: &mut __H)
where __H: Hasher,

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<E> IntervalIterator<E> for RangeList<E>
where E: PartialOrd + Clone,

Source§

type IntervalIter = <RangeList<E> as IntoIterator>::IntoIter

The type of the interval iterator.
Source§

fn intervals(&self) -> <RangeList<E> as IntervalIterator<E>>::IntervalIter

Returns an iterator over the ordered intervals.
Source§

fn card(&self) -> Option<usize>
where E: DiscreteElement,

Returns the number of elements contained within the RangeList. Read more
Source§

fn contains(&self, elem: &E) -> bool

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

fn diff<O, R>(&self, other: &O) -> R

Compute RangeList without any of the elements in the ranges of other. Read more
Source§

fn disjoint<O>(&self, other: &O) -> bool
where O: IntervalIterator<E> + ?Sized,

Returns whether self and other are disjoint sets
Source§

fn intersect<O, R>(&self, other: &O) -> R

Return the set intersection of two interval iterators.
Source§

fn subset<O>(&self, other: &O) -> bool
where O: IntervalIterator<E> + ?Sized,

Returns whether self is a subset of other
Source§

fn superset<O>(&self, other: &O) -> bool
where O: IntervalIterator<E> + ?Sized,

Returns whether self is a superset of other
Source§

fn union<O, R>(&self, other: &O) -> R

Return the set union of two interval iterators.
Source§

impl<'a, E> IntoIterator for &'a RangeList<E>
where E: PartialOrd,

Source§

type IntoIter = Map<Iter<'a, (E, E)>, fn(&'a (E, E)) -> RangeInclusive<&'a E>>

Which kind of iterator are we turning this into?
Source§

type Item = RangeInclusive<&'a E>

The type of the elements being iterated over.
Source§

fn into_iter(self) -> <&'a RangeList<E> as IntoIterator>::IntoIter

Creates an iterator from a value. Read more
Source§

impl<E> IntoIterator for RangeList<E>
where E: PartialOrd + Clone,

Source§

type IntoIter = Map<IntoIter<(E, E)>, fn((E, E)) -> RangeInclusive<E>>

Which kind of iterator are we turning this into?
Source§

type Item = RangeInclusive<E>

The type of the elements being iterated over.
Source§

fn into_iter(self) -> <RangeList<E> as IntoIterator>::IntoIter

Creates an iterator from a value. Read more
Source§

impl<E> PartialEq for RangeList<E>
where E: PartialEq + PartialOrd,

Source§

fn eq(&self, other: &RangeList<E>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<E> PartialOrd for RangeList<E>
where E: PartialOrd,

Source§

fn partial_cmp(&self, other: &RangeList<E>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<E> Eq for RangeList<E>
where E: Eq + PartialOrd,

Source§

impl<E> StructuralPartialEq for RangeList<E>
where E: PartialOrd,

Auto Trait Implementations§

§

impl<E> Freeze for RangeList<E>

§

impl<E> RefUnwindSafe for RangeList<E>
where E: RefUnwindSafe,

§

impl<E> Send for RangeList<E>
where E: Send,

§

impl<E> Sync for RangeList<E>
where E: Sync,

§

impl<E> Unpin for RangeList<E>
where E: Unpin,

§

impl<E> UnwindSafe for RangeList<E>
where E: 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, 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> 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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.