pub struct Cache { /* private fields */ }
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
sourceimpl Cache
impl Cache
sourcepub fn new(dfa: &DFA) -> Cache
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.
sourcepub fn reset(&mut self, dfa: &DFA)
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};
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.find_leftmost_fwd(&mut cache, "Δ".as_bytes())?,
);
// 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.find_leftmost_fwd(&mut cache, "☃".as_bytes())?,
);
sourcepub fn clear_count(&self) -> usize
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.
sourcepub fn memory_usage(&self) -> usize
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
Auto Trait Implementations
impl RefUnwindSafe for Cache
impl Send for Cache
impl Sync for Cache
impl Unpin for Cache
impl UnwindSafe for Cache
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more