regex_automata::hybrid::dfa

Struct Cache

Source
pub struct Cache { /* private fields */ }
Available on crate feature hybrid only.
Expand description

A cache represents a partially computed DFA.

A cache is the key component that differentiates a classical DFA and a hybrid NFA/DFA (also called a “lazy DFA”). Where a classical DFA builds a complete transition table that can handle all possible inputs, a hybrid NFA/DFA starts with an empty transition table and builds only the parts required during search. The parts that are built are stored in a cache. For this reason, a cache is a required parameter for nearly every operation on a DFA.

Caches can be created from their corresponding DFA via DFA::create_cache. A cache can only be used with either the DFA that created it, or the DFA that was most recently used to reset it with Cache::reset. Using a cache with any other DFA may result in panics or incorrect results.

Implementations§

Source§

impl Cache

Source

pub fn new(dfa: &DFA) -> Cache

Create a new cache for the given lazy DFA.

The cache returned should only be used for searches for the given DFA. If you want to reuse the cache for another DFA, then you must call Cache::reset with that DFA.

Source

pub fn reset(&mut self, dfa: &DFA)

Reset this cache such that it can be used for searching with the given lazy DFA (and only that DFA).

A cache reset permits reusing memory already allocated in this cache with a different lazy DFA.

Resetting a cache sets its “clear count” to 0. This is relevant if the lazy DFA has been configured to “give up” after it has cleared the cache a certain number of times.

Any lazy state ID generated by the cache prior to resetting it is invalid after the reset.

§Example

This shows how to re-purpose a cache for use with a different DFA.

use regex_automata::{hybrid::dfa::DFA, HalfMatch, Input};

let dfa1 = DFA::new(r"\w")?;
let dfa2 = DFA::new(r"\W")?;

let mut cache = dfa1.create_cache();
assert_eq!(
    Some(HalfMatch::must(0, 2)),
    dfa1.try_search_fwd(&mut cache, &Input::new("Δ"))?,
);

// Using 'cache' with dfa2 is not allowed. It may result in panics or
// incorrect results. In order to re-purpose the cache, we must reset
// it with the DFA we'd like to use it with.
//
// Similarly, after this reset, using the cache with 'dfa1' is also not
// allowed.
cache.reset(&dfa2);
assert_eq!(
    Some(HalfMatch::must(0, 3)),
    dfa2.try_search_fwd(&mut cache, &Input::new("☃"))?,
);
Source

pub fn search_start(&mut self, at: usize)

Initializes a new search starting at the given position.

If a previous search was unfinished, then it is finished automatically and a new search is begun.

Note that keeping track of search progress is not necessary for correct implementations of search using a lazy DFA. Keeping track of search progress is only necessary if you want the Config::minimum_bytes_per_state configuration knob to work.

Source

pub fn search_update(&mut self, at: usize)

Updates the current search to indicate that it has search to the current position.

No special care needs to be taken for reverse searches. Namely, the position given may be less than the starting position of the search.

§Panics

This panics if no search has been started by Cache::search_start.

Source

pub fn search_finish(&mut self, at: usize)

Indicates that a search has finished at the given position.

§Panics

This panics if no search has been started by Cache::search_start.

Source

pub fn search_total_len(&self) -> usize

Returns the total number of bytes that have been searched since this cache was last cleared.

This is useful for determining the efficiency of the cache. For example, the lazy DFA uses this value in conjunction with the Config::minimum_bytes_per_state knob to help determine whether it should quit searching.

This always returns 0 if search progress isn’t being tracked. Note that the lazy DFA search routines in this crate always track search progress.

Source

pub fn clear_count(&self) -> usize

Returns the total number of times this cache has been cleared since it was either created or last reset.

This is useful for informational purposes or if you want to change search strategies based on the number of times the cache has been cleared.

Source

pub fn memory_usage(&self) -> usize

Returns the heap memory usage, in bytes, of this cache.

This does not include the stack size used up by this cache. To compute that, use std::mem::size_of::<Cache>().

Trait Implementations§

Source§

impl Clone for Cache

Source§

fn clone(&self) -> Cache

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 Cache

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Cache

§

impl RefUnwindSafe for Cache

§

impl Send for Cache

§

impl Sync for Cache

§

impl Unpin for Cache

§

impl UnwindSafe for Cache

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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,

Source§

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

Source§

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

Source§

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.