pub struct CharCollection { /* private fields */ }
Expand description

A collection of chars (i.e. Unicode code points), used for storing large continuous ranges efficiently.

Lookups and insertions are O(log R), where R is the number of disjoint ranges in the collection.

The easiest way to create instances is using the char_collect! macro.

use char_collection::CharCollection;

let mut collection: CharCollection = char_collect!('a'..='d', 'x'..='z');
char_collection += 'e';
char_collection += chars!('p'..='t');
assert_eq!(
    collection.iter_ranges().collect(),
    vec![chars!('a'..='e'), chars!('p'..='t'), chars!('x'..='z')]);

assert!(collection.contains(&'c'));
assert!(collection.contains_range(chars!('q'..='s')));
assert!(!collection.contains(&'9'));

collection -= chars!('t'..='y');
assert_eq!(
    collection.iter_ranges().collect(),
    vec![chars!('a'..='e', chars!('p'..'s'), chars!('z'..='z'))]);

TODO(kpozin): Implement IntoIter.

Implementations§

source§

impl CharCollection

source

pub fn new() -> CharCollection

Create a new, empty CharCollection.

source

pub fn from_sorted_ranges<T>(ranges: T) -> Result<CharCollection, Error>
where T: IntoIterator<Item = CharRange>,

Create a new CharCollection from a list of disjoint, non-adjacent CharRanges, pre-sorted in ascending code point order.

This factory method is primarily intended for use in deserializing valid representations of CharCollections. Will return an error if ranges are out of order, overlapping, or adjacent.

source

pub fn from_sorted_chars<T>(chars: T) -> Result<CharCollection, Error>
where T: IntoIterator<Item = char>,

Create a new CharCollection from a list of chars, pre-sorted in ascending code point order.

This factory method is primarily intended for use in deserializing valid representations of CharCollections. Will return an error if chars are out of order or contain duplicates.

source

pub fn iter(&self) -> impl Iterator<Item = char> + '_

Iterate over all the chars in the collection.

source

pub fn contains(&self, ch: &char) -> bool

Test whether the collection contains a specific char.

The time complexity is O(log R), where R is the number of ranges in the collection.

source

pub fn contains_range(&self, range: &CharRange) -> bool

Test whether the collection contains an entire range of characters.

The time complexity is O(log R), where R is the number of ranges in the collection.

source

pub fn insert<V: MultiCharRange>(&mut self, to_add: &V) -> &mut Self

Insert a char or other collection of chars into this collection.

Returns &mut self for easy chaining.

The time complexity is O(T log(R + T)), where R is the number of ranges in this collection and T is the number of ranges in to_add.

source

pub fn append(&mut self, ch: char) -> Result<&mut Self, Error>

Appends a char to the end of the existing collection. Panics if the given char is not higher than the highest code point in the existing collection.

Returns &mut self for easy chaining.

The time complexity is O(1).

source

pub fn append_range(&mut self, range: CharRange) -> Result<&mut Self, Error>

Appends a CharRange to the end of the existing collection. Panics if the given range is not higher than the highest code point in the existing collection. (The new range may be adjacent to the previous highest range, but may not overlap.)

Returns &mut self for easy chaining.

The time complexity is O(1).

source

pub fn remove<V: MultiCharRange>(&mut self, to_remove: &V) -> &mut Self

Remove a char or other collection of chars from this collection.

Returns &mut self for easy chaining.

The time complexity is O(T log(R + T)), where R is the number of ranges in this collection and T is the number of ranges in to_remove.

source

pub fn clear(&mut self) -> &mut Self

Remove all entries from this collection.

Returns &mut self for easy chaining.

source

pub fn union<V: MultiCharRange>(&self, rhs: &V) -> CharCollection

Return the set union of this collection and another one.

The time complexity is O(min(R, T) log(R + T)), where R is the number of ranges in this collection and T is the number of ranges in rhs.

source

pub fn intersection<V: MultiCharRange>(&self, rhs: &V) -> CharCollection

Return the set intersection of this collection and another one.

The time complexity is O(min(R, T) log(R + T)), where R is the number of ranges in this collection and T is the number of ranges in rhs.

source

pub fn difference<V: MultiCharRange>(&self, rhs: &V) -> CharCollection

Return the (non-symmetric) set difference of this collection and another one.

The time complexity is O(T log(R + T)), where R is the number of ranges in this collection and T is the number of ranges in rhs.

source

pub fn complement(&self) -> CharCollection

Return the set complement of this collection (over the universe of chars).

The time complexity is O(R), where R is the number of ranges in this collection.

Trait Implementations§

source§

impl<V: MultiCharRange> Add<V> for CharCollection

§

type Output = CharCollection

The resulting type after applying the + operator.
source§

fn add(self, rhs: V) -> Self::Output

Performs the + operation. Read more
source§

impl<V: MultiCharRange> AddAssign<V> for CharCollection

source§

fn add_assign(&mut self, rhs: V)

Performs the += operation. Read more
source§

impl<V: MultiCharRange> BitAnd<V> for CharCollection

§

type Output = CharCollection

The resulting type after applying the & operator.
source§

fn bitand(self, rhs: V) -> Self::Output

Performs the & operation. Read more
source§

impl<V: MultiCharRange> BitAndAssign<V> for CharCollection

source§

fn bitand_assign(&mut self, rhs: V)

Performs the &= operation. Read more
source§

impl<V: MultiCharRange> BitOr<V> for CharCollection

§

type Output = CharCollection

The resulting type after applying the | operator.
source§

fn bitor(self, rhs: V) -> Self::Output

Performs the | operation. Read more
source§

impl<V: MultiCharRange> BitOrAssign<V> for CharCollection

source§

fn bitor_assign(&mut self, rhs: V)

Performs the |= operation. Read more
source§

impl Clone for CharCollection

source§

fn clone(&self) -> CharCollection

Returns a copy 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 Debug for CharCollection

source§

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

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

impl Default for CharCollection

source§

fn default() -> CharCollection

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

impl<T: MultiCharRange> From<&T> for CharCollection

source§

fn from(source: &T) -> Self

Converts to this type from the input type.
source§

impl Hash for CharCollection

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 MultiCharRange for CharCollection

source§

fn iter_ranges<'a>(&'a self) -> Box<dyn Iterator<Item = CharRange> + 'a>

Iterate over the disjoint, non-adjacent [CharRange]s in the collection in ascending order.
source§

fn range_count(&self) -> usize

The number of ranges in the collection.
source§

impl Not for CharCollection

§

type Output = CharCollection

The resulting type after applying the ! operator.
source§

fn not(self) -> Self::Output

Performs the unary ! operation. Read more
source§

impl PartialEq for CharCollection

source§

fn eq(&self, other: &CharCollection) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<V: MultiCharRange> Sub<V> for CharCollection

§

type Output = CharCollection

The resulting type after applying the - operator.
source§

fn sub(self, rhs: V) -> Self::Output

Performs the - operation. Read more
source§

impl<V: MultiCharRange> SubAssign<V> for CharCollection

source§

fn sub_assign(&mut self, rhs: V)

Performs the -= operation. Read more
source§

impl Eq for CharCollection

source§

impl StructuralPartialEq for CharCollection

Auto Trait Implementations§

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

§

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

§

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

§

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.