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,
impl<T> RangeSet<T>where
T: NumericInteger,
Sourcepub fn iter(&self) -> RangeSetIter<'_, T> ⓘ
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
}Sourcepub fn new_from_range<R>(range: R) -> RangeSet<T>where
R: RangeBounds<T>,
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);Sourcepub fn new_from_ranges(ranges: &[AnyRange<T>]) -> RangeSet<T>
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)]);Sourcepub fn new_from_bounds(bounds: Vec<T>) -> Option<RangeSet<T>>
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());Sourcepub fn empty() -> RangeSet<T>
pub fn empty() -> RangeSet<T>
Create a new instance that does not contain any value.
§Example:
use irange::RangeSet;
// Contains nothing
RangeSet::<i32>::empty();Sourcepub fn total() -> RangeSet<T>
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();Sourcepub fn is_total(&self) -> bool
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());Sourcepub fn is_empty(&self) -> bool
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());Sourcepub fn contains(&self, value: T) -> bool
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));Sourcepub fn contains_all(&self, that: &RangeSet<T>) -> bool
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));Sourcepub fn union(&self, that: &RangeSet<T>) -> RangeSet<T>
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);Sourcepub fn has_intersection(&self, that: &RangeSet<T>) -> bool
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));Sourcepub fn intersection(&self, that: &RangeSet<T>) -> RangeSet<T>
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);Sourcepub fn complement(&self) -> RangeSet<T>
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();Sourcepub fn difference(&self, that: &RangeSet<T>) -> RangeSet<T>
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,
impl<T> BitAnd<&RangeSet<T>> for &RangeSet<T>where
T: NumericInteger,
Source§impl<T> BitOr<&RangeSet<T>> for &RangeSet<T>where
T: NumericInteger,
impl<T> BitOr<&RangeSet<T>> for &RangeSet<T>where
T: NumericInteger,
Source§impl CharacterClass for RangeSet<Char>
impl CharacterClass for RangeSet<Char>
Source§fn new_from_range_u32<R: RangeBounds<u32>>(range: R) -> Option<Self>
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
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
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
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> Default for RangeSet<T>where
T: NumericInteger,
impl<T> Default for RangeSet<T>where
T: NumericInteger,
Source§fn default() -> RangeSet<T>
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.
impl<'de, T> Deserialize<'de> for RangeSet<T>where
T: NumericInteger + Deserialize<'de>,
serde only.Source§fn deserialize<D>(
deserializer: D,
) -> Result<RangeSet<T>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D,
) -> Result<RangeSet<T>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
Source§impl<T> Display for RangeSet<T>where
T: NumericInteger,
impl<T> Display for RangeSet<T>where
T: NumericInteger,
impl<T> Eq for RangeSet<T>where
T: Eq + NumericInteger,
Source§impl<T> FromIterator<AnyRange<T>> for RangeSet<T>where
T: NumericInteger,
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>>,
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<'a, T> IntoIterator for &'a RangeSet<T>where
T: NumericInteger,
impl<'a, T> IntoIterator for &'a RangeSet<T>where
T: NumericInteger,
Source§fn into_iter(self) -> RangeSetIter<'a, T> ⓘ
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
}