2019-12-26 00:17:57 -05:00
|
|
|
pub struct SinglePattern {
|
2020-01-01 22:14:40 -05:00
|
|
|
pub seq: &'static [u8],
|
|
|
|
pub table: &'static [usize],
|
2019-12-26 00:17:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SinglePattern {
|
|
|
|
pub fn match_against(&self, haystack: &[u8]) -> Option<usize> {
|
|
|
|
let mut hay_idx = 0usize;
|
|
|
|
let mut pat_idx = 0usize;
|
|
|
|
while hay_idx < haystack.len() {
|
|
|
|
if self.seq[pat_idx] == haystack[hay_idx] {
|
|
|
|
pat_idx += 1;
|
|
|
|
hay_idx += 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
if pat_idx == self.seq.len() {
|
|
|
|
return Some(hay_idx - pat_idx);
|
|
|
|
};
|
|
|
|
|
|
|
|
if hay_idx < haystack.len() && self.seq[pat_idx] != haystack[hay_idx] {
|
|
|
|
if pat_idx != 0 {
|
|
|
|
pat_idx = self.table[pat_idx - 1];
|
|
|
|
} else {
|
|
|
|
hay_idx += 1;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2019-12-29 05:00:20 -05:00
|
|
|
|
2020-01-05 08:57:07 -05:00
|
|
|
pub trait ITrieNode<V: 'static + Copy> {
|
|
|
|
fn get_value(&self) -> Option<V>;
|
|
|
|
fn get_child(&self, c: u8) -> Option<&dyn ITrieNode<V>>;
|
2019-12-29 05:00:20 -05:00
|
|
|
}
|
2020-01-06 02:28:01 -05:00
|
|
|
|
|
|
|
pub struct TrieLeafNode<V: 'static + Copy> {
|
|
|
|
pub value: Option<V>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<V: 'static + Copy> ITrieNode<V> for TrieLeafNode<V> {
|
|
|
|
fn get_value(&self) -> Option<V> {
|
|
|
|
self.value
|
|
|
|
}
|
|
|
|
|
2020-01-06 02:54:46 -05:00
|
|
|
fn get_child(&self, _: u8) -> Option<&dyn ITrieNode<V>> {
|
2020-01-06 02:28:01 -05:00
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|