Struct regex_automata::nfa::thompson::literal_trie::State

source ·
struct State {
    transitions: Vec<Transition>,
    chunks: Vec<(usize, usize)>,
}
Expand description

A state in a trie.

This uses a sparse representation. Since we don’t use literal tries for searching, and ultimately (and compilation requires visiting every transition anyway), we use a sparse representation for transitions. This means we save on memory, at the expense of ‘LiteralTrie::add’ being perhaps a bit slower.

While ‘transitions’ is pretty standard as far as tries goes, the ‘chunks’ piece here is more unusual. In effect, ‘chunks’ defines a partitioning of ‘transitions’, where each chunk corresponds to a distinct set of transitions. The key invariant is that a transition in one chunk cannot be moved to another chunk. This is the secret sauce that preserve leftmost-first match semantics.

A new chunk is added whenever we mark a state as a match state. Once a new chunk is added, the old active chunk is frozen and is never mutated again. The new chunk becomes the active chunk, which is defined as ‘&transitions[chunks.last().map_or(0, |c| c.1)..]’. Thus, a state where ‘chunks’ is empty actually contains one chunk. Thus, every state contains at least one (possibly empty) chunk.

A “leaf” state is a state that has no outgoing transitions (so ‘transitions’ is empty). Note that there is no way for a leaf state to be a non-matching state. (Although while building the trie, within ‘add’, a leaf state may exist while not containing any matches. But this invariant is only broken within ‘add’. Once ‘add’ returns, the invariant is upheld.)

Fields§

§transitions: Vec<Transition>§chunks: Vec<(usize, usize)>

Implementations§

source§

impl State

source

fn add_match(&mut self)

Mark this state as a match state and freeze the active chunk such that it can not be further mutated.

source

fn is_leaf(&self) -> bool

Returns true if and only if this state is a leaf state. That is, a state that has no outgoing transitions.

source

fn chunks(&self) -> StateChunksIter<'_>

Returns an iterator over all of the chunks (including the currently active chunk) in this state. Since the active chunk is included, the iterator is guaranteed to always yield at least one chunk (although the chunk may be empty).

source

fn active_chunk(&self) -> &[Transition]

Returns the active chunk as a slice of transitions.

source

fn active_chunk_start(&self) -> usize

Returns the index into ‘transitions’ where the active chunk starts.

Trait Implementations§

source§

impl Clone for State

source§

fn clone(&self) -> State

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 State

source§

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

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

impl Default for State

source§

fn default() -> State

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

Auto Trait Implementations§

§

impl Freeze for State

§

impl RefUnwindSafe for State

§

impl Send for State

§

impl Sync for State

§

impl Unpin for State

§

impl UnwindSafe for State

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.