pub struct Trie { /* private fields */ }Expand description
A standard trie form that often provides the fastest queries.
Implementations§
Source§impl Trie
impl Trie
Sourcepub fn from_keys<I, K>(keys: I) -> Result<Self>
pub fn from_keys<I, K>(keys: I) -> Result<Self>
Creates a new Trie from input keys.
Values in [0..n-1] will be associated with keys in the lexicographical order,
where n is the number of keys.
§Arguments
keys: Sorted list of string keys.
§Errors
CrawdadError will be returned when
keysis empty,keyscontains empty strings,keyscontains duplicate keys,- the scale of
keysexceeds the expected one, or - the scale of the resulting trie exceeds the expected one.
§Examples
use crawdad::Trie;
let keys = vec!["世界", "世界中", "国民"];
let trie = Trie::from_keys(keys).unwrap();
assert_eq!(trie.num_elems(), 8);Sourcepub fn from_records<I, K>(records: I) -> Result<Self>
pub fn from_records<I, K>(records: I) -> Result<Self>
Creates a new Trie from input records.
§Arguments
records: Sorted list of key-value pairs.
§Errors
CrawdadError will be returned when
recordsis empty,recordscontains empty strings,recordscontains duplicate keys,- the scale of
keysexceeds the expected one, or - the scale of the resulting trie exceeds the expected one.
§Examples
use crawdad::Trie;
let records = vec![("世界", 2), ("世界中", 3), ("国民", 2)];
let trie = Trie::from_records(records).unwrap();
assert_eq!(trie.num_elems(), 8);Sourcepub fn serialize_to_vec(&self) -> Vec<u8>
pub fn serialize_to_vec(&self) -> Vec<u8>
Sourcepub fn deserialize_from_slice(source: &[u8]) -> (Self, &[u8])
pub fn deserialize_from_slice(source: &[u8]) -> (Self, &[u8])
Deserializes the data structure from a given byte slice.
§Arguments
source- A source byte slice.
§Returns
A tuple of the data structure and the slice not used for the deserialization.
§Examples
use crawdad::Trie;
let keys = vec!["世界", "世界中", "国民"];
let trie = Trie::from_keys(&keys).unwrap();
let bytes = trie.serialize_to_vec();
let (other, _) = Trie::deserialize_from_slice(&bytes);
assert_eq!(trie.io_bytes(), other.io_bytes());Sourcepub fn exact_match<I>(&self, key: I) -> Option<u32>where
I: IntoIterator<Item = char>,
pub fn exact_match<I>(&self, key: I) -> Option<u32>where
I: IntoIterator<Item = char>,
Sourcepub const fn common_prefix_search<I>(
&self,
haystack: I,
) -> CommonPrefixSearchIter<'_, I> ⓘ
pub const fn common_prefix_search<I>( &self, haystack: I, ) -> CommonPrefixSearchIter<'_, I> ⓘ
Returns an iterator for common prefix search.
The iterator reports all occurrences of keys starting from an input haystack, where an occurrence consists of its associated value and ending positoin in characters.
§Examples
You can find all occurrences of keys in a haystack by performing common prefix searches at all starting positions.
use crawdad::Trie;
let keys = vec!["世界", "世界中", "国民"];
let trie = Trie::from_keys(&keys).unwrap();
let haystack: Vec<char> = "国民が世界中にて".chars().collect();
let mut matches = vec![];
for i in 0..haystack.len() {
for (v, j) in trie.common_prefix_search(haystack[i..].iter().copied()) {
matches.push((v, i..i + j));
}
}
assert_eq!(
matches,
vec![(2, 0..2), (0, 3..5), (1, 3..6)]
);Sourcepub fn heap_bytes(&self) -> usize
pub fn heap_bytes(&self) -> usize
Returns the total amount of heap used by this automaton in bytes.
Sourcepub fn io_bytes(&self) -> usize
pub fn io_bytes(&self) -> usize
Returns the total amount of bytes to serialize the data structure.