pub struct Trie<V> { /* private fields */ }Expand description
A trie (prefix tree) data structure
Generic over the value type V. If no value is needed, use ().
§Example
use polyglot_sql::trie::{Trie, TrieResult};
let mut trie = Trie::new();
trie.insert("cat", 1);
trie.insert("car", 2);
assert_eq!(trie.in_trie("cat"), (TrieResult::Exists, Some(&1)));
assert_eq!(trie.in_trie("ca").0, TrieResult::Prefix);
assert_eq!(trie.in_trie("dog").0, TrieResult::Failed);Implementations§
Source§impl<V> Trie<V>
impl<V> Trie<V>
Sourcepub fn insert(&mut self, key: &str, value: V)
pub fn insert(&mut self, key: &str, value: V)
Insert a key-value pair into the trie
§Arguments
key- The key to insert (a string slice)value- The value to associate with the key
Sourcepub fn get(&self, key: &str) -> Option<&V>
pub fn get(&self, key: &str) -> Option<&V>
Get the value associated with a key
Returns None if the key doesn’t exist or only exists as a prefix.
Sourcepub fn in_trie(&self, key: &str) -> (TrieResult, Option<&V>)
pub fn in_trie(&self, key: &str) -> (TrieResult, Option<&V>)
Check if a key exists in the trie
Returns a tuple of (TrieResult, Option<&V>) where:
TrieResult::Failed- key not foundTrieResult::Prefix- key is a prefix of an existing keyTrieResult::Exists- key exists in trie
When the result is Exists, the Option will contain the value.
Sourcepub fn in_trie_char(&self, ch: char) -> (TrieResult, Option<&Trie<V>>)
pub fn in_trie_char(&self, ch: char) -> (TrieResult, Option<&Trie<V>>)
Check if a key exists in the trie, following one character at a time
This is useful for streaming/incremental matching. Returns:
TrieResult::Failed- character not found from current positionTrieResult::Prefix- character found, but not at end of a wordTrieResult::Exists- character found and at end of a word
Also returns the subtrie at this position (if any).
Trait Implementations§
Auto Trait Implementations§
impl<V> Freeze for Trie<V>where
V: Freeze,
impl<V> RefUnwindSafe for Trie<V>where
V: RefUnwindSafe,
impl<V> Send for Trie<V>where
V: Send,
impl<V> Sync for Trie<V>where
V: Sync,
impl<V> Unpin for Trie<V>where
V: Unpin,
impl<V> UnwindSafe for Trie<V>where
V: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more