Struct regex_automata::util::sparse_set::SparseSet

source ·
pub(crate) struct SparseSet {
    len: usize,
    dense: Vec<StateID>,
    sparse: Vec<StateID>,
}
Expand description

A sparse set used for representing ordered NFA states.

This supports constant time addition and membership testing. Clearing an entire set can also be done in constant time. Iteration yields elements in the order in which they were inserted.

The data structure is based on: https://research.swtch.com/sparse Note though that we don’t actually use uninitialized memory. We generally reuse sparse sets, so the initial allocation cost is bareable. However, its other properties listed above are extremely useful.

Fields§

§len: usize

The number of elements currently in this set.

§dense: Vec<StateID>

Dense contains the ids in the order in which they were inserted.

§sparse: Vec<StateID>

Sparse maps ids to their location in dense.

A state ID is in the set if and only if sparse[id] < len && id == dense[sparse[id]].

Note that these are indices into ‘dense’. It’s a little weird to use StateID here, but we know our length can never exceed the bounds of StateID (enforced by ‘resize’) and StateID will be at most 4 bytes where as a usize is likely double that in most cases.

Implementations§

source§

impl SparseSet

source

pub(crate) fn new(capacity: usize) -> SparseSet

Create a new sparse set with the given capacity.

Sparse sets have a fixed size and they cannot grow. Attempting to insert more distinct elements than the total capacity of the set will result in a panic.

This panics if the capacity given is bigger than StateID::LIMIT.

source

pub(crate) fn resize(&mut self, new_capacity: usize)

Resizes this sparse set to have the new capacity given.

This set is automatically cleared.

This panics if the capacity given is bigger than StateID::LIMIT.

source

pub(crate) fn capacity(&self) -> usize

Returns the capacity of this set.

The capacity represents a fixed limit on the number of distinct elements that are allowed in this set. The capacity cannot be changed.

source

pub(crate) fn len(&self) -> usize

Returns the number of elements in this set.

source

pub(crate) fn is_empty(&self) -> bool

Returns true if and only if this set is empty.

source

pub(crate) fn insert(&mut self, id: StateID) -> bool

Insert the state ID value into this set and return true if the given state ID was not previously in this set.

This operation is idempotent. If the given value is already in this set, then this is a no-op.

If more than capacity ids are inserted, then this panics.

This is marked as inline(always) since the compiler won’t inline it otherwise, and it’s a fairly hot piece of code in DFA determinization.

source

pub(crate) fn contains(&self, id: StateID) -> bool

Returns true if and only if this set contains the given value.

source

pub(crate) fn clear(&mut self)

Clear this set such that it has no members.

source

pub(crate) fn iter(&self) -> SparseSetIter<'_>

source

pub(crate) fn memory_usage(&self) -> usize

Returns the heap memory usage, in bytes, used by this sparse set.

Trait Implementations§

source§

impl Clone for SparseSet

source§

fn clone(&self) -> SparseSet

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 SparseSet

source§

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

Formats the value using the given formatter. Read more

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.