pub struct Trie<K, V> { /* private fields */ }
Expand description
A QP-trie. QP stands for - depending on who you ask - either “quelques-bits popcount” or “quad-bit popcount”. In any case, the fact of the matter is that this is a compressed radix trie with a branching factor of 16. It acts as a key-value map where the keys are any value which can be converted to a slice of bytes.
The following example uses the provided string wrapper. Unfortunately, String
/str
cannot be
used directly because they do not implement Borrow<[u8]>
(as they do not hash the same way as
a byte slice.) As a stopgap, qp_trie::wrapper::{BString, BStr}
are provided, as are the
.whatever_str()
convenience methods on qp_trie::Trie<BString, _>
.
Example
let mut trie = Trie::new();
trie.insert_str("abbc", 1);
trie.insert_str("abcd", 2);
trie.insert_str("bcde", 3);
trie.insert_str("bdde", 4);
trie.insert_str("bddf", 5);
// This will print the following string:
//
// `{"abbc": 1, "abcd": 2, "bcde": 3, "bdde": 4, "bddf": 5}`
println!("{:?}", trie);
assert_eq!(trie.get_str("abcd"), Some(&2));
assert_eq!(trie.get_str("bcde"), Some(&3));
// We can take subtries, removing all elements of the trie with a given prefix.
let mut subtrie = trie.remove_prefix_str("b");
assert_eq!(trie.get_str("abbc"), Some(&1));
assert_eq!(trie.get_str("abcd"), Some(&2));
assert_eq!(trie.get_str("bcde"), None);
assert_eq!(trie.get_str("bdde"), None);
assert_eq!(trie.get_str("bddf"), None);
assert_eq!(subtrie.get_str("abbc"), None);
assert_eq!(subtrie.get_str("abcd"), None);
assert_eq!(subtrie.get_str("bcde"), Some(&3));
assert_eq!(subtrie.get_str("bdde"), Some(&4));
assert_eq!(subtrie.get_str("bddf"), Some(&5));
// We can remove elements:
assert_eq!(trie.remove_str("abbc"), Some(1));
assert_eq!(trie.get_str("abbc"), None);
// We can mutate values:
*subtrie.get_mut_str("bdde").unwrap() = 0;
assert_eq!(subtrie.get_str("bdde"), Some(&0));
Implementations§
source§impl<K, V> Trie<K, V>
impl<K, V> Trie<K, V>
source§impl<K: Borrow<[u8]>, V> Trie<K, V>
impl<K: Borrow<[u8]>, V> Trie<K, V>
sourcepub fn iter_prefix<'a, Q>(&'a self, prefix: &Q) -> Iter<'a, K, V> ⓘwhere
K: Borrow<Q>,
Q: Borrow<[u8]> + ?Sized,
pub fn iter_prefix<'a, Q>(&'a self, prefix: &Q) -> Iter<'a, K, V> ⓘwhere K: Borrow<Q>, Q: Borrow<[u8]> + ?Sized,
Iterate over all elements with a given prefix.
sourcepub fn iter_prefix_mut<'a, Q>(&'a mut self, prefix: &Q) -> IterMut<'a, K, V> ⓘwhere
K: Borrow<Q>,
Q: Borrow<[u8]> + ?Sized,
pub fn iter_prefix_mut<'a, Q>(&'a mut self, prefix: &Q) -> IterMut<'a, K, V> ⓘwhere K: Borrow<Q>, Q: Borrow<[u8]> + ?Sized,
Iterate over all elements with a given prefix, but given a mutable reference to the associated value.
sourcepub fn subtrie<'a, Q>(&'a self, prefix: &Q) -> SubTrie<'a, K, V>where
K: Borrow<Q>,
Q: Borrow<[u8]> + ?Sized,
pub fn subtrie<'a, Q>(&'a self, prefix: &Q) -> SubTrie<'a, K, V>where K: Borrow<Q>, Q: Borrow<[u8]> + ?Sized,
Get an immutable view into the trie, providing only values keyed with the given prefix.
sourcepub fn longest_common_prefix<'a, Q>(&'a self, key: &Q) -> &'a K::Splitwhere
K: Borrow<Q> + Break,
Q: Borrow<[u8]> + ?Sized,
pub fn longest_common_prefix<'a, Q>(&'a self, key: &Q) -> &'a K::Splitwhere K: Borrow<Q> + Break, Q: Borrow<[u8]> + ?Sized,
Get the longest common prefix of all the nodes in the trie and the given key.
sourcepub fn contains_key<Q>(&self, key: &Q) -> boolwhere
K: Borrow<Q>,
Q: Borrow<[u8]> + ?Sized,
pub fn contains_key<Q>(&self, key: &Q) -> boolwhere K: Borrow<Q>, Q: Borrow<[u8]> + ?Sized,
Returns true if there is an entry for the given key.
sourcepub fn get<'a, Q>(&'a self, key: &Q) -> Option<&'a V>where
K: Borrow<Q>,
Q: Borrow<[u8]> + ?Sized,
pub fn get<'a, Q>(&'a self, key: &Q) -> Option<&'a V>where K: Borrow<Q>, Q: Borrow<[u8]> + ?Sized,
Get an immutable reference to the value associated with a given key, if it is in the tree.
sourcepub fn get_mut<'a, Q>(&'a mut self, key: &Q) -> Option<&'a mut V>where
K: Borrow<Q>,
Q: Borrow<[u8]> + ?Sized,
pub fn get_mut<'a, Q>(&'a mut self, key: &Q) -> Option<&'a mut V>where K: Borrow<Q>, Q: Borrow<[u8]> + ?Sized,
Get a mutable reference to the value associated with a given key, if it is in the tree.
sourcepub fn insert(&mut self, key: K, val: V) -> Option<V>
pub fn insert(&mut self, key: K, val: V) -> Option<V>
Insert a key/value pair into the trie, returning the old value if an entry already existed.
sourcepub fn remove<Q>(&mut self, key: &Q) -> Option<V>where
K: Borrow<Q>,
Q: Borrow<[u8]> + ?Sized,
pub fn remove<Q>(&mut self, key: &Q) -> Option<V>where K: Borrow<Q>, Q: Borrow<[u8]> + ?Sized,
Remove the key/value pair associated with a given key from the trie, returning
Some(val)
if a corresponding key/value pair was found.
source§impl<V> Trie<BString, V>
impl<V> Trie<BString, V>
sourcepub fn iter_prefix_str<'a, Q>(&'a self, key: &Q) -> Iter<'a, BString, V> ⓘwhere
Q: Borrow<str> + ?Sized,
pub fn iter_prefix_str<'a, Q>(&'a self, key: &Q) -> Iter<'a, BString, V> ⓘwhere Q: Borrow<str> + ?Sized,
Convenience function for iterating over suffixes with a string.
sourcepub fn iter_prefix_mut_str<'a, Q>(
&'a mut self,
key: &Q
) -> IterMut<'a, BString, V> ⓘwhere
Q: Borrow<str> + ?Sized,
pub fn iter_prefix_mut_str<'a, Q>( &'a mut self, key: &Q ) -> IterMut<'a, BString, V> ⓘwhere Q: Borrow<str> + ?Sized,
Convenience function for iterating over suffixes with a string.
sourcepub fn subtrie_str<'a, Q>(&'a self, prefix: &Q) -> SubTrie<'a, BString, V>where
Q: Borrow<str> + ?Sized,
pub fn subtrie_str<'a, Q>(&'a self, prefix: &Q) -> SubTrie<'a, BString, V>where Q: Borrow<str> + ?Sized,
Convenience function for viewing subtries wit a string prefix.
sourcepub fn contains_key_str<Q>(&self, key: &Q) -> boolwhere
Q: Borrow<str> + ?Sized,
pub fn contains_key_str<Q>(&self, key: &Q) -> boolwhere Q: Borrow<str> + ?Sized,
Returns true if there is an entry for the given string key.
sourcepub fn get_str<'a, Q>(&'a self, key: &Q) -> Option<&'a V>where
Q: Borrow<str> + ?Sized,
pub fn get_str<'a, Q>(&'a self, key: &Q) -> Option<&'a V>where Q: Borrow<str> + ?Sized,
Convenience function for getting with a string.
sourcepub fn get_mut_str<'a, Q>(&'a mut self, key: &Q) -> Option<&'a mut V>where
Q: Borrow<str> + ?Sized,
pub fn get_mut_str<'a, Q>(&'a mut self, key: &Q) -> Option<&'a mut V>where Q: Borrow<str> + ?Sized,
Convenience function for getting mutably with a string.
sourcepub fn insert_str<Q>(&mut self, key: &Q, val: V) -> Option<V>where
Q: Borrow<str> + ?Sized,
pub fn insert_str<Q>(&mut self, key: &Q, val: V) -> Option<V>where Q: Borrow<str> + ?Sized,
Convenience function for inserting with a string.
Trait Implementations§
source§impl<K: Borrow<[u8]>, V> Extend<(K, V)> for Trie<K, V>
impl<K: Borrow<[u8]>, V> Extend<(K, V)> for Trie<K, V>
source§fn extend<I>(&mut self, iterable: I)where
I: IntoIterator<Item = (K, V)>,
fn extend<I>(&mut self, iterable: I)where I: IntoIterator<Item = (K, V)>,
source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)