pub struct BitParallelSearcher { /* private fields */ }Expand description
Pre-computed searcher for a specific pattern.
Creating the searcher has O(m) setup cost where m is pattern length. Reuse the searcher across multiple texts to amortize this cost.
§Implementation Details
Uses the Shift-Or algorithm (also known as Baeza-Yates–Gonnet algorithm). Each bit in a u64 represents whether the pattern matches up to that position.
Implementations§
Source§impl BitParallelSearcher
impl BitParallelSearcher
Sourcepub fn find_in(&self, text: &[u8]) -> Option<usize>
pub fn find_in(&self, text: &[u8]) -> Option<usize>
Search for the pattern in the given text.
§Performance
- Time: O(n) where n = text.len()
- Memory: O(1)
§Returns
Position of first match, or None if pattern not found.
§Example
use bit_parallel_search::BitParallelSearcher;
let searcher = BitParallelSearcher::new(b"fox");
let text = b"The quick brown fox";
assert_eq!(searcher.find_in(text), Some(16));Sourcepub fn find_all_in<'t>(
&self,
text: &'t [u8],
) -> impl Iterator<Item = usize> + 't
Available on crate feature std only.
pub fn find_all_in<'t>( &self, text: &'t [u8], ) -> impl Iterator<Item = usize> + 't
std only.Find all occurrences of the pattern in text.
§Performance
Same as find_in but continues searching after each match.
§Example
use bit_parallel_search::BitParallelSearcher;
let searcher = BitParallelSearcher::new(b"ab");
let text = b"ababab";
let matches: Vec<_> = searcher.find_all_in(text).collect();
assert_eq!(matches, vec![0, 2, 4]);Sourcepub fn count_in(&self, text: &[u8]) -> usize
pub fn count_in(&self, text: &[u8]) -> usize
Count occurrences without collecting positions.
More efficient than find_all_in().count() as it avoids iterator overhead.
§Example
use bit_parallel_search::BitParallelSearcher;
let searcher = BitParallelSearcher::new(b"ab");
assert_eq!(searcher.count_in(b"ababab"), 3);Sourcepub fn exists_in(&self, text: &[u8]) -> bool
pub fn exists_in(&self, text: &[u8]) -> bool
Returns true if pattern exists in text.
Equivalent to find_in(text).is_some() but may be slightly faster.
Sourcepub fn pattern_len(&self) -> usize
pub fn pattern_len(&self) -> usize
Get the pattern length.
Trait Implementations§
Source§impl Clone for BitParallelSearcher
impl Clone for BitParallelSearcher
Source§fn clone(&self) -> BitParallelSearcher
fn clone(&self) -> BitParallelSearcher
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more