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
impl SparseSet
sourcepub(crate) fn new(capacity: usize) -> SparseSet
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
.
sourcepub(crate) fn resize(&mut self, new_capacity: usize)
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
.
sourcepub(crate) fn capacity(&self) -> usize
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.
sourcepub(crate) fn insert(&mut self, id: StateID) -> bool
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.
sourcepub(crate) fn contains(&self, id: StateID) -> bool
pub(crate) fn contains(&self, id: StateID) -> bool
Returns true if and only if this set contains the given value.
pub(crate) fn iter(&self) -> SparseSetIter<'_> ⓘ
sourcepub(crate) fn memory_usage(&self) -> usize
pub(crate) fn memory_usage(&self) -> usize
Returns the heap memory usage, in bytes, used by this sparse set.