RangeSet

Struct RangeSet 

Source
pub struct RangeSet<T> { /* private fields */ }
Expand description

A set of values represented using ranges.

A RangeSet is similar to any other kind of set, such as HashSet, with the difference being that the values in the set are represented using ranges rather than storing each value individually.

§Invariants

RangeSet enforces the following invariants on the ranges it contains:

  • The ranges are sorted.
  • The ranges are non-adjacent.
  • The ranges are non-intersecting.
  • The ranges are non-empty.

This is enforced in the constructor, and guaranteed to hold after applying any operation on a range or set.

§Examples

use rangeset::*;

let a = 10..20;

// Difference
assert_eq!(a.difference(&(15..25)), RangeSet::from([10..15]));
assert_eq!(a.difference(&(12..15)), RangeSet::from([(10..12), (15..20)]));

// Union
assert_eq!(a.union(&(15..25)), RangeSet::from([10..25]));
assert_eq!(a.union(&(0..0)), RangeSet::from([10..20]));

// Comparison
assert!(a.is_subset(&(0..30)));
assert!(a.is_disjoint(&(0..10)));
assert_eq!(a.clone(), RangeSet::from(a));

Implementations§

Source§

impl<T> RangeSet<T>

Source

pub fn into_inner(self) -> Vec<Range<T>>

Returns the ranges of the set.

Source

pub fn is_empty(&self) -> bool

Returns true if the set is empty.

Source

pub fn len_ranges(&self) -> usize

Returns the number of ranges in the set.

Source

pub fn clear(&mut self)

Clears the set, removing all ranges.

Source§

impl<T: Copy + Ord> RangeSet<T>

Source

pub fn new(ranges: &[Range<T>]) -> Self
where Self: Union<Range<T>, Output = Self>,

Returns a new RangeSet from the given ranges.

The RangeSet is constructed by computing the union of the given ranges.

Source

pub fn iter(&self) -> RangeSetIter<'_, T>

Returns an iterator over the values in the set.

Source

pub fn iter_ranges(&self) -> RangeIter<'_, T>

Returns an iterator over the ranges in the set.

Source

pub fn contains(&self, value: &T) -> bool

Returns true if the set contains the given value.

Source

pub fn min(&self) -> Option<T>

Returns the minimum value in the set, or None if the set is empty.

Source

pub fn end(&self) -> Option<T>

Returns the end of right-most range in the set, or None if the set is empty.

§Note

This is the non-inclusive bound of the right-most range. See RangeSet::max for the maximum value in the set.

Source§

impl<T: Copy + Ord + Step + Sub<Output = T>> RangeSet<T>

Source

pub fn max(&self) -> Option<T>

Returns the maximum value in the set, or None if the set is empty.

Source

pub fn split_off(&mut self, at: &T) -> Self

Splits the set into two at the provided value.

Returns a new set containing all the existing elements >= at. After the call, the original set will be left containing the elements < at.

§Panics

Panics if at is not in the set.

Source§

impl<T: Copy + Ord + Sub<Output = T>> RangeSet<T>

Source

pub fn shift_left(&mut self, offset: &T)

Shifts every range in the set to the left by the provided offset.

§Panics

Panics if the shift causes an underflow.

Source§

impl<T: Copy + Ord + Add<Output = T>> RangeSet<T>

Source

pub fn shift_right(&mut self, offset: &T)

Shifts every range in the set to the right by the provided offset.

§Panics

Panics if the the shift causes an overflow.

Source§

impl<T: Copy + Ord> RangeSet<T>
where Range<T>: ExactSizeIterator<Item = T>,

Source

pub fn len(&self) -> usize

Returns the number of values in the set.

Trait Implementations§

Source§

impl<T: Copy + Ord> BitAnd<&Range<T>> for RangeSet<T>

Source§

type Output = RangeSet<T>

The resulting type after applying the & operator.
Source§

fn bitand(self, other: &Range<T>) -> Self::Output

Performs the & operation. Read more
Source§

impl<T: Copy + Ord> BitAnd<&RangeSet<T>> for Range<T>

Source§

type Output = RangeSet<T>

The resulting type after applying the & operator.
Source§

fn bitand(self, other: &RangeSet<T>) -> Self::Output

Performs the & operation. Read more
Source§

impl<T: Copy + Ord> BitAnd<&RangeSet<T>> for RangeSet<T>

Source§

type Output = RangeSet<T>

The resulting type after applying the & operator.
Source§

fn bitand(self, other: &RangeSet<T>) -> Self::Output

Performs the & operation. Read more
Source§

impl<T: Copy + Ord> BitAnd<Range<T>> for RangeSet<T>

Source§

type Output = RangeSet<T>

The resulting type after applying the & operator.
Source§

fn bitand(self, other: Range<T>) -> Self::Output

Performs the & operation. Read more
Source§

impl<T: Copy + Ord> BitAnd<RangeSet<T>> for Range<T>

Source§

type Output = RangeSet<T>

The resulting type after applying the & operator.
Source§

fn bitand(self, other: RangeSet<T>) -> Self::Output

Performs the & operation. Read more
Source§

impl<T: Copy + Ord> BitAnd for RangeSet<T>

Source§

type Output = RangeSet<T>

The resulting type after applying the & operator.
Source§

fn bitand(self, other: RangeSet<T>) -> Self::Output

Performs the & operation. Read more
Source§

impl<T: Copy + Ord> BitAndAssign<&Range<T>> for RangeSet<T>

Source§

fn bitand_assign(&mut self, other: &Range<T>)

Performs the &= operation. Read more
Source§

impl<T: Copy + Ord> BitAndAssign<&RangeSet<T>> for RangeSet<T>

Source§

fn bitand_assign(&mut self, other: &RangeSet<T>)

Performs the &= operation. Read more
Source§

impl<T: Copy + Ord> BitAndAssign<Range<T>> for RangeSet<T>

Source§

fn bitand_assign(&mut self, other: Range<T>)

Performs the &= operation. Read more
Source§

impl<T: Copy + Ord> BitAndAssign for RangeSet<T>

Source§

fn bitand_assign(&mut self, other: RangeSet<T>)

Performs the &= operation. Read more
Source§

impl<T: Copy + Ord> BitOr<&Range<T>> for RangeSet<T>

Source§

type Output = RangeSet<T>

The resulting type after applying the | operator.
Source§

fn bitor(self, other: &Range<T>) -> Self::Output

Performs the | operation. Read more
Source§

impl<T: Copy + Ord> BitOr<&RangeSet<T>> for RangeSet<T>

Source§

type Output = RangeSet<T>

The resulting type after applying the | operator.
Source§

fn bitor(self, other: &RangeSet<T>) -> Self::Output

Performs the | operation. Read more
Source§

impl<T: Copy + Ord> BitOr<Range<T>> for RangeSet<T>

Source§

type Output = RangeSet<T>

The resulting type after applying the | operator.
Source§

fn bitor(self, other: Range<T>) -> Self::Output

Performs the | operation. Read more
Source§

impl<T: Copy + Ord> BitOr for RangeSet<T>

Source§

type Output = RangeSet<T>

The resulting type after applying the | operator.
Source§

fn bitor(self, other: RangeSet<T>) -> Self::Output

Performs the | operation. Read more
Source§

impl<T: Copy + Ord> BitOrAssign<&Range<T>> for RangeSet<T>

Source§

fn bitor_assign(&mut self, other: &Range<T>)

Performs the |= operation. Read more
Source§

impl<T: Copy + Ord> BitOrAssign<&RangeSet<T>> for RangeSet<T>

Source§

fn bitor_assign(&mut self, other: &RangeSet<T>)

Performs the |= operation. Read more
Source§

impl<T: Copy + Ord> BitOrAssign<Range<T>> for RangeSet<T>

Source§

fn bitor_assign(&mut self, other: Range<T>)

Performs the |= operation. Read more
Source§

impl<T: Copy + Ord> BitOrAssign for RangeSet<T>

Source§

fn bitor_assign(&mut self, other: RangeSet<T>)

Performs the |= operation. Read more
Source§

impl<T: Copy + Ord> BitXor<&Range<T>> for RangeSet<T>

Source§

type Output = RangeSet<T>

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, rhs: &Range<T>) -> Self::Output

Performs the ^ operation. Read more
Source§

impl<T: Copy + Ord> BitXor<&RangeSet<T>> for RangeSet<T>

Source§

type Output = RangeSet<T>

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, rhs: &RangeSet<T>) -> Self::Output

Performs the ^ operation. Read more
Source§

impl<T: Copy + Ord> BitXor<Range<T>> for RangeSet<T>

Source§

type Output = RangeSet<T>

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, rhs: Range<T>) -> Self::Output

Performs the ^ operation. Read more
Source§

impl<T: Copy + Ord> BitXor<RangeSet<T>> for &RangeSet<T>

Source§

type Output = RangeSet<T>

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, rhs: RangeSet<T>) -> Self::Output

Performs the ^ operation. Read more
Source§

impl<T: Copy + Ord> BitXor for RangeSet<T>

Source§

type Output = RangeSet<T>

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, rhs: RangeSet<T>) -> Self::Output

Performs the ^ operation. Read more
Source§

impl<T: Copy + Ord> BitXorAssign<&RangeSet<T>> for RangeSet<T>

Source§

fn bitxor_assign(&mut self, rhs: &RangeSet<T>)

Performs the ^= operation. Read more
Source§

impl<T: Copy + Ord> BitXorAssign for RangeSet<T>

Source§

fn bitxor_assign(&mut self, rhs: RangeSet<T>)

Performs the ^= operation. Read more
Source§

impl<T: Clone> Clone for RangeSet<T>

Source§

fn clone(&self) -> RangeSet<T>

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<T> Cover<RangeSet<T>> for RangeSet<T>
where T: Copy + Ord + 'static, Range<T>: ExactSizeIterator<Item = T>,

Source§

fn find_cover<'a>( &self, others: impl IntoIterator<Item = &'a RangeSet<T>>, ) -> (Vec<usize>, RangeSet<T>)
where RangeSet<T>: 'a,

Finds the positions of the fewest sets from others which exactly cover self. Read more
Source§

fn cover<'a>( &self, others: impl IntoIterator<Item = &'a RangeSet<T>>, ) -> (Vec<&'a RangeSet<T>>, RangeSet<T>)
where RangeSet<T>: 'a,

Finds the fewest sets from others which exactly cover self. Read more
Source§

fn cover_by<'a, U>( &self, others: impl IntoIterator<Item = &'a U>, f: impl Fn(&U) -> &RangeSet<T>, ) -> (Vec<&'a U>, RangeSet<T>)
where T: 'a,

Finds the fewest sets from others which exactly cover self. Read more
Source§

impl<T: Debug> Debug for RangeSet<T>

Source§

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

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

impl<T: Copy + Ord> Default for RangeSet<T>

Source§

fn default() -> Self

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

impl<T: Copy + Ord> Difference<Range<T>> for RangeSet<T>

Source§

type Output = RangeSet<T>

Source§

fn difference(&self, other: &Range<T>) -> Self::Output

Returns the set difference of self and other.
Source§

impl<T: Copy + Ord> Difference<RangeSet<T>> for Range<T>

Source§

type Output = RangeSet<T>

Source§

fn difference(&self, other: &RangeSet<T>) -> Self::Output

Returns the set difference of self and other.
Source§

impl<T: Copy + Ord> Difference<RangeSet<T>> for RangeSet<T>

Source§

type Output = RangeSet<T>

Source§

fn difference(&self, other: &RangeSet<T>) -> Self::Output

Returns the set difference of self and other.
Source§

impl<T: Copy + Ord> DifferenceMut<Range<T>> for RangeSet<T>

Source§

fn difference_mut(&mut self, other: &Range<T>)

Subtracts other from self.
Source§

impl<T: Copy + Ord> DifferenceMut<RangeSet<T>> for RangeSet<T>

Source§

fn difference_mut(&mut self, other: &RangeSet<T>)

Subtracts other from self.
Source§

impl<T: Copy + Ord> Disjoint<Range<T>> for RangeSet<T>

Source§

fn is_disjoint(&self, other: &Range<T>) -> bool

Returns true if the range is disjoint with other.
Source§

impl<T: Copy + Ord> Disjoint<RangeSet<T>> for Range<T>

Source§

fn is_disjoint(&self, other: &RangeSet<T>) -> bool

Returns true if the range is disjoint with other.
Source§

impl<T: Copy + Ord> Disjoint<RangeSet<T>> for RangeSet<T>

Source§

fn is_disjoint(&self, other: &RangeSet<T>) -> bool

Returns true if the range is disjoint with other.
Source§

impl<T: Copy + Ord> From<&[Range<T>]> for RangeSet<T>

Source§

fn from(ranges: &[Range<T>]) -> Self

Converts to this type from the input type.
Source§

impl<const N: usize, T: Copy + Ord> From<[Range<T>; N]> for RangeSet<T>

Source§

fn from(ranges: [Range<T>; N]) -> Self

Converts to this type from the input type.
Source§

impl<T: Copy + Ord> From<Range<T>> for RangeSet<T>

Source§

fn from(range: Range<T>) -> Self

Converts to this type from the input type.
Source§

impl<T> From<RangeSet<T>> for Vec<Range<T>>

Source§

fn from(ranges: RangeSet<T>) -> Self

Converts to this type from the input type.
Source§

impl<T: Copy + Ord> From<Vec<Range<T>>> for RangeSet<T>

Source§

fn from(ranges: Vec<Range<T>>) -> Self

Converts to this type from the input type.
Source§

impl<T: Hash> Hash for RangeSet<T>

Source§

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

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<T: Copy + Ord> Intersection<Range<T>> for RangeSet<T>

Source§

type Output = RangeSet<T>

Source§

fn intersection(&self, other: &Range<T>) -> Self::Output

Returns the set intersection of self and other.
Source§

impl<T: Copy + Ord> Intersection<RangeSet<T>> for Range<T>

Source§

type Output = RangeSet<T>

Source§

fn intersection(&self, other: &RangeSet<T>) -> Self::Output

Returns the set intersection of self and other.
Source§

impl<T: Copy + Ord> Intersection<RangeSet<T>> for RangeSet<T>

Source§

type Output = RangeSet<T>

Source§

fn intersection(&self, other: &RangeSet<T>) -> Self::Output

Returns the set intersection of self and other.
Source§

impl<T: Copy + Ord> PartialEq<Range<T>> for &RangeSet<T>

Source§

fn eq(&self, other: &Range<T>) -> 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<T: Copy + Ord> PartialEq<Range<T>> for RangeSet<T>

Source§

fn eq(&self, other: &Range<T>) -> 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<T: Copy + Ord> PartialEq<RangeSet<T>> for &Range<T>

Source§

fn eq(&self, other: &RangeSet<T>) -> 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<T: Copy + Ord> PartialEq<RangeSet<T>> for Range<T>

Source§

fn eq(&self, other: &RangeSet<T>) -> 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<T: PartialEq> PartialEq for RangeSet<T>

Source§

fn eq(&self, other: &RangeSet<T>) -> 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<T: Copy + Ord> Sub<&Range<T>> for RangeSet<T>

Source§

type Output = RangeSet<T>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: &Range<T>) -> Self::Output

Performs the - operation. Read more
Source§

impl<T: Copy + Ord> Sub<&RangeSet<T>> for RangeSet<T>

Source§

type Output = RangeSet<T>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: &RangeSet<T>) -> Self::Output

Performs the - operation. Read more
Source§

impl<T: Copy + Ord> Sub<Range<T>> for RangeSet<T>

Source§

type Output = RangeSet<T>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Range<T>) -> Self::Output

Performs the - operation. Read more
Source§

impl<T: Copy + Ord> Sub for RangeSet<T>

Source§

type Output = RangeSet<T>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: RangeSet<T>) -> Self::Output

Performs the - operation. Read more
Source§

impl<T: Copy + Ord> SubAssign<&Range<T>> for RangeSet<T>

Source§

fn sub_assign(&mut self, rhs: &Range<T>)

Performs the -= operation. Read more
Source§

impl<T: Copy + Ord> SubAssign<&RangeSet<T>> for RangeSet<T>

Source§

fn sub_assign(&mut self, rhs: &RangeSet<T>)

Performs the -= operation. Read more
Source§

impl<T: Copy + Ord> SubAssign<Range<T>> for RangeSet<T>

Source§

fn sub_assign(&mut self, rhs: Range<T>)

Performs the -= operation. Read more
Source§

impl<T: Copy + Ord> SubAssign for RangeSet<T>

Source§

fn sub_assign(&mut self, rhs: RangeSet<T>)

Performs the -= operation. Read more
Source§

impl<T: Copy + Ord> Subset<Range<T>> for RangeSet<T>

Source§

fn is_subset(&self, other: &Range<T>) -> bool

Returns true if self is a subset of other.
Source§

impl<T: Copy + Ord> Subset<RangeSet<T>> for Range<T>

Source§

fn is_subset(&self, other: &RangeSet<T>) -> bool

Returns true if self is a subset of other.
Source§

impl<T: Copy + Ord> Subset<RangeSet<T>> for RangeSet<T>

Source§

fn is_subset(&self, other: &RangeSet<T>) -> bool

Returns true if self is a subset of other.
Source§

impl<T: Copy + Ord> SymmetricDifference<Range<T>> for RangeSet<T>

Source§

type Output = RangeSet<T>

Source§

fn symmetric_difference(&self, other: &Range<T>) -> Self::Output

Returns the set symmetric difference of self and other.
Source§

impl<T: Copy + Ord> SymmetricDifference<RangeSet<T>> for RangeSet<T>

Source§

type Output = RangeSet<T>

Source§

fn symmetric_difference(&self, other: &RangeSet<T>) -> Self::Output

Returns the set symmetric difference of self and other.
Source§

impl<T: Copy + Ord> SymmetricDifferenceMut<Range<T>> for RangeSet<T>

Source§

fn symmetric_difference_mut(&mut self, other: &Range<T>)

Replaces self with the set symmetric difference of self and other.
Source§

impl<T: Copy + Ord> SymmetricDifferenceMut<RangeSet<T>> for RangeSet<T>

Source§

fn symmetric_difference_mut(&mut self, other: &RangeSet<T>)

Replaces self with the set symmetric difference of self and other.
Source§

impl<T: Copy + Ord> ToRangeSet<T> for RangeSet<T>

Source§

fn to_range_set(&self) -> RangeSet<T>

Returns a corresponding range set.
Source§

impl<T: Copy + Ord> TryFrom<RangeSet<T>> for Range<T>

Source§

fn try_from(set: RangeSet<T>) -> Result<Self, Self::Error>

Attempts to convert a RangeSet into a single Range, returning the set if it does not contain exactly one range.

Source§

type Error = RangeSet<T>

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

impl<T: Copy + Ord> Union<Range<T>> for RangeSet<T>

Source§

type Output = RangeSet<T>

Source§

fn union(&self, other: &Range<T>) -> Self::Output

Returns the set union of self and other.
Source§

impl<T: Copy + Ord> Union<RangeSet<T>> for Range<T>

Source§

type Output = RangeSet<T>

Source§

fn union(&self, other: &RangeSet<T>) -> Self::Output

Returns the set union of self and other.
Source§

impl<T: Copy + Ord> Union<RangeSet<T>> for RangeSet<T>

Source§

type Output = RangeSet<T>

Source§

fn union(&self, other: &RangeSet<T>) -> Self::Output

Returns the set union of self and other.
Source§

impl<T: Copy + Ord> UnionMut<Range<T>> for RangeSet<T>

Source§

fn union_mut(&mut self, other: &Range<T>)

Replaces self with the set union of self and other.
Source§

impl<T: Copy + Ord> UnionMut<RangeSet<T>> for RangeSet<T>

Source§

fn union_mut(&mut self, other: &RangeSet<T>)

Replaces self with the set union of self and other.
Source§

impl<T: Eq> Eq for RangeSet<T>

Source§

impl<T> StructuralPartialEq for RangeSet<T>

Auto Trait Implementations§

§

impl<T> Freeze for RangeSet<T>

§

impl<T> RefUnwindSafe for RangeSet<T>
where T: RefUnwindSafe,

§

impl<T> Send for RangeSet<T>
where T: Send,

§

impl<T> Sync for RangeSet<T>
where T: Sync,

§

impl<T> Unpin for RangeSet<T>
where T: Unpin,

§

impl<T> UnwindSafe for RangeSet<T>
where T: 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, 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.