Skip to main content

RangeSet

Struct RangeSet 

Source
pub struct RangeSet<T>(pub Vec<T>)
where
    T: NumericInteger;
Expand description

The set type this crate operates on, and the range type its constructors take, are irange’s. They are re-exported here so that a dependent needs neither its own dependency on irange nor a version kept in step with this crate’s. A structure holding a collection of u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128 or isize.

Tuple Fields§

§0: Vec<T>

In this collection all the elements with even index represent the lower bounds (inclusive) and all the odd index represent the upper bounds (inclusive).

Implementations§

Source§

impl<T> RangeSet<T>
where T: NumericInteger,

Source

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

Return an iterator to iterate in order over all the values contained.

§Example:
use irange::RangeSet;

let range = RangeSet::new_from_range(2..=5);

for value in range.iter() {
    print!("{value} "); // 2 3 4 5
}
Source

pub fn new_from_range<R>(range: R) -> RangeSet<T>
where R: RangeBounds<T>,

Create a new instance from the given range.

§Example:
use irange::RangeSet;

// Contains 2, 3
RangeSet::new_from_range(2..4);

// Contains 0, 1
RangeSet::<u8>::new_from_range(..2);

// Contains 0, 1, 2
RangeSet::<u32>::new_from_range(..=2);
Source

pub fn new_from_ranges(ranges: &[AnyRange<T>]) -> RangeSet<T>

Create a new instance from the given ranges.

§Example:
use irange::RangeSet;
use irange::range::AnyRange;

// Contains 3, 4, 7, 8
RangeSet::<i64>::new_from_ranges(&[AnyRange::from(3..=4), AnyRange::from(7..9)]);
Source

pub fn new_from_bounds(bounds: Vec<T>) -> Option<RangeSet<T>>

Create a new instance from the raw collection of bounds used internally: the elements with an even index are the lower bounds (inclusive) and the elements with an odd index are the upper bounds (inclusive).

Return None if the collection is not a valid representation, i.e. if it does not hold an even number of bounds, if a range is inverted, or if two consecutive ranges are not sorted, overlap, or merely touch each other (they should be merged into a single range).

§Example:
use irange::RangeSet;

// Contains 2, 3, 4, 7, 8
assert!(RangeSet::<u8>::new_from_bounds(vec![2, 4, 7, 8]).is_some());

// 4..=6 and 7..=8 are adjacent, they must be given as a single 4..=8 range
assert!(RangeSet::<u8>::new_from_bounds(vec![4, 6, 7, 8]).is_none());
Source

pub fn empty() -> RangeSet<T>

Create a new instance that does not contain any value.

§Example:
use irange::RangeSet;

// Contains nothing
RangeSet::<i32>::empty();
Source

pub fn total() -> RangeSet<T>

Create a new instance that contains all possible values.

§Example:
use irange::RangeSet;

// Contains all values that can be stored into a u8
// -> 0..=255
RangeSet::<u8>::total();

// Contains all values that can be stored into a i16
// -> -32768..=32767
RangeSet::<i16>::total();
Source

pub fn is_total(&self) -> bool

Return true if it contains all the possible values.

§Example:
use irange::RangeSet;

let total = RangeSet::<u128>::total();
assert!(total.is_total());

let range = RangeSet::<u32>::new_from_range(..=2);
assert!(!range.is_total());
Source

pub fn is_empty(&self) -> bool

Return true if it does not contain any value.

§Example:
use irange::RangeSet;

let empty = RangeSet::<u128>::empty();
assert!(empty.is_empty());

let range = RangeSet::<i64>::new_from_range(2..4);
assert!(!range.is_empty());
Source

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

Return true if it contains the given value.

§Example:
use irange::RangeSet;

let range = RangeSet::<i64>::new_from_range(2..4);
assert!(range.contains(2));
assert!(range.contains(3));
assert!(!range.contains(4));
Source

pub fn contains_all(&self, that: &RangeSet<T>) -> bool

Return true if it contains the given RangeSet.

§Example:
use irange::RangeSet;

let range1 = RangeSet::<i64>::new_from_range(11..90);
let range2 = RangeSet::<i64>::new_from_range(19..23);

assert!(range1.contains_all(&range2));
assert!(!range2.contains_all(&range1));
Source

pub fn union(&self, that: &RangeSet<T>) -> RangeSet<T>

Return the union with the given RangeSet.

§Example:
use irange::RangeSet;

let range1 = RangeSet::<i64>::new_from_range(2..4);
let range2 = RangeSet::<i64>::new_from_range(3..=5);

// Contains 2..=5
let union = range1.union(&range2);
Source

pub fn has_intersection(&self, that: &RangeSet<T>) -> bool

Return true if there is common value with the given RangeSet.

§Example:
use irange::RangeSet;

let range1 = RangeSet::<i64>::new_from_range(2..4);
let range2 = RangeSet::<i64>::new_from_range(3..=5);
let range3 = RangeSet::<i64>::new_from_range(5..13);

assert!(range1.has_intersection(&range2));
assert!(!range1.has_intersection(&range3));
assert!(range2.has_intersection(&range3));
Source

pub fn intersection(&self, that: &RangeSet<T>) -> RangeSet<T>

Return the intersection with the given RangeSet.

§Example:
use irange::RangeSet;

let range1 = RangeSet::<i64>::new_from_range(2..4);
let range2 = RangeSet::<i64>::new_from_range(3..=5);

// Contains 2..=3
let intersection = range1.intersection(&range2);
Source

pub fn complement(&self) -> RangeSet<T>

Return the complement.

§Example:
use irange::RangeSet;

let range = RangeSet::<u8>::new_from_range(2..4);

// Contains 0..=1 + 4..=255
range.complement();
Source

pub fn difference(&self, that: &RangeSet<T>) -> RangeSet<T>

Return the difference with the given RangeSet.

§Example:
use irange::RangeSet;

let range1 = RangeSet::<i64>::new_from_range(2..4);
let range2 = RangeSet::<i64>::new_from_range(3..=5);

// Contains 2
let difference = range1.difference(&range2);

Trait Implementations§

Source§

impl<T> BitAnd<&RangeSet<T>> for &RangeSet<T>
where T: NumericInteger,

Source§

fn bitand(self, that: &RangeSet<T>) -> RangeSet<T>

Compute the intersection, like RangeSet::intersection.

§Example:
use irange::RangeSet;

let a = RangeSet::new_from_range(3..=8);
let b = RangeSet::new_from_range(7..=9);
assert_eq!("[ 7..=8 ]", (&a & &b).to_string());
Source§

type Output = RangeSet<T>

The resulting type after applying the & operator.
Source§

impl<T> BitOr<&RangeSet<T>> for &RangeSet<T>
where T: NumericInteger,

Source§

fn bitor(self, that: &RangeSet<T>) -> RangeSet<T>

Compute the union, like RangeSet::union.

§Example:
use irange::RangeSet;

let a = RangeSet::new_from_range(3..=4);
let b = RangeSet::new_from_range(7..=8);
assert_eq!("[ 3..=4 7..=8 ]", (&a | &b).to_string());
Source§

type Output = RangeSet<T>

The resulting type after applying the | operator.
Source§

impl CharacterClass for RangeSet<Char>

Source§

fn new_from_range_u32<R: RangeBounds<u32>>(range: R) -> Option<Self>

Create a new instance from the given range of codepoints, or return None if a bound is not a char, i.e. if it is a surrogate or above char::MAX.

A range holding no codepoint gives the empty set.

§Example
use regex_charclass::{CharacterClass, RangeSet};

let range = RangeSet::new_from_range_u32(97..=122).unwrap();
assert_eq!("[a-z]", range.to_regex());

// An empty range, and a bound that is not a `char`.
assert!(RangeSet::new_from_range_u32(97..97).unwrap().is_empty());
assert_eq!(None, RangeSet::new_from_range_u32(0xD800..=0xD800));
Source§

fn new_from_range_char<R: RangeBounds<char>>(range: R) -> Self

Create a new instance from the given range of char.

A range holding no char gives the empty set.

§Example
use regex_charclass::{CharacterClass, RangeSet};

let range = RangeSet::new_from_range_char('a'..='z');
assert_eq!("[a-z]", range.to_regex());

assert_eq!("[a-y]", RangeSet::new_from_range_char('a'..'z').to_regex());
assert!(RangeSet::new_from_range_char('a'..'a').is_empty());
Source§

fn get_cardinality(&self) -> u32

Return the number of char values contained, at most 1,112,064.

The surrogate block holds no char, so a range spanning it does not count it.

§Example
use regex_charclass::{CharacterClass, RangeSet};

let range = RangeSet::new_from_range_char('a'..='z');
assert_eq!(26, range.get_cardinality());

assert_eq!(1_112_064, RangeSet::total().get_cardinality());
Source§

fn to_regex(&self) -> String

Return the set as a regular expression character class.

See the crate documentation for the forms this returns and the flags they assume.

§Example
use regex_charclass::{AnyRange, Char, CharacterClass, RangeSet};

let range = RangeSet::new_from_range_char('a'..='z');
assert_eq!("[a-z]", range.to_regex());

let range = RangeSet::<Char>::new_from_ranges(&[
    AnyRange::from(Char::new('0')..=Char::new('9')),
    AnyRange::from(Char::new('A')..=Char::new('F')),
    AnyRange::from(Char::new('a')..=Char::new('f')),
]);
assert_eq!("\\p{ASCII_Hex_Digit}", range.to_regex());

// A single `char` needs no class, but is escaped if it is a metacharacter.
assert_eq!("\\.", RangeSet::new_from_range_char('.'..='.').to_regex());
Source§

impl<T> Clone for RangeSet<T>
where T: Clone + NumericInteger,

Source§

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

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T> Debug for RangeSet<T>
where T: Debug + NumericInteger,

Source§

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

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

impl<T> Default for RangeSet<T>
where T: NumericInteger,

Source§

fn default() -> RangeSet<T>

Return the empty set, like RangeSet::empty.

Source§

impl<'de, T> Deserialize<'de> for RangeSet<T>
where T: NumericInteger + Deserialize<'de>,

Available on crate feature serde only.
Source§

fn deserialize<D>( deserializer: D, ) -> Result<RangeSet<T>, <D as Deserializer<'de>>::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<T> Display for RangeSet<T>
where T: NumericInteger,

Source§

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

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

impl<T> Eq for RangeSet<T>
where T: Eq + NumericInteger,

Source§

impl<T> FromIterator<AnyRange<T>> for RangeSet<T>
where T: NumericInteger,

Source§

fn from_iter<I>(iter: I) -> RangeSet<T>
where I: IntoIterator<Item = AnyRange<T>>,

Collect the given ranges into a single set, like RangeSet::new_from_ranges.

§Example:
use irange::range::AnyRange;
use irange::RangeSet;

let range: RangeSet<i64> = [3..=4, 7..=8].into_iter().map(AnyRange::from).collect();
assert_eq!("[ 3..=4 7..=8 ]", range.to_string());
Source§

impl<T> Hash for RangeSet<T>
where T: Hash + NumericInteger,

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<'a, T> IntoIterator for &'a RangeSet<T>
where T: NumericInteger,

Source§

fn into_iter(self) -> RangeSetIter<'a, T> ⓘ

Iterate in order over all the values contained, like RangeSet::iter.

§Example:
use irange::RangeSet;

let range = RangeSet::new_from_range(2..=5);

for value in &range {
    print!("{value} "); // 2 3 4 5
}
Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = RangeSetIter<'a, T>

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

impl<T> Not for &RangeSet<T>
where T: NumericInteger,

Source§

fn not(self) -> RangeSet<T>

Compute the complement, like RangeSet::complement.

§Example:
use irange::RangeSet;

let a = RangeSet::<u8>::new_from_range(3..=254);
assert_eq!("[ 0..=2 255..=255 ]", (!&a).to_string());
Source§

type Output = RangeSet<T>

The resulting type after applying the ! operator.
Source§

impl<T> Ord for RangeSet<T>
where T: Ord + NumericInteger,

Source§

fn cmp(&self, other: &RangeSet<T>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

fn clamp_to<R>(self, range: R) -> Self
where Self: Sized, R: ClampBounds<Self>,

🔬This is a nightly-only experimental API. (clamp_to)
Restrict a value to a certain range. Read more
Source§

impl<T> PartialEq for RangeSet<T>

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl<T> PartialOrd for RangeSet<T>

Source§

fn partial_cmp(&self, other: &RangeSet<T>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · 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 (const: unstable) · 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 (const: unstable) · 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 (const: unstable) · 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<T> Serialize for RangeSet<T>

Source§

fn serialize<__S>( &self, __serializer: __S, ) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<T> StructuralPartialEq for RangeSet<T>

Source§

impl<T> Sub<&RangeSet<T>> for &RangeSet<T>
where T: NumericInteger,

Source§

fn sub(self, that: &RangeSet<T>) -> RangeSet<T>

Compute the difference, like RangeSet::difference.

§Example:
use irange::RangeSet;

let a = RangeSet::new_from_range(3..=8);
let b = RangeSet::new_from_range(7..=9);
assert_eq!("[ 3..=6 ]", (&a - &b).to_string());
Source§

type Output = RangeSet<T>

The resulting type after applying the - operator.

Auto Trait Implementations§

§

impl<T> Freeze for RangeSet<T>
where Vec<T>: Freeze,

§

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

§

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

§

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

§

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

§

impl<T> UnsafeUnpin for RangeSet<T>
where Vec<T>: UnsafeUnpin,

§

impl<T> UnwindSafe for RangeSet<T>
where Vec<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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.