Struct qp_trie::Trie

source ·
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>

source

pub fn new() -> Trie<K, V>

Create a new, empty trie.

source

pub fn iter(&self) -> Iter<'_, K, V>

Iterate over all elements in the trie.

source

pub fn iter_mut(&mut self) -> IterMut<'_, K, V>

Iterate over all elements in the trie, given a mutable reference to the associated value.

source

pub fn keys(&self) -> Keys<'_, K, V>

Iterate over all keys in the trie.

source

pub fn values(&self) -> Values<'_, K, V>

Iterate over all values in the trie.

source

pub fn values_mut(&mut self) -> ValuesMut<'_, K, V>

Iterate over all values in the trie, mutably.

source

pub fn clear(&mut self)

Remove all entries from the trie, leaving it empty.

source

pub fn is_empty(&self) -> bool

Returns true if the trie has no entries.

source§

impl<K: Borrow<[u8]>, V> Trie<K, V>

source

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.

source

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.

source

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.

source

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.

source

pub fn count(&self) -> usize

Count the number of entries in the tree.

source

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.

source

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.

source

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.

source

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.

source

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

pub fn remove_prefix<Q>(&mut self, prefix: &Q) -> Trie<K, V>where K: Borrow<Q>, Q: Borrow<[u8]> + ?Sized,

Remove all elements beginning with a given prefix from the trie, producing a subtrie containing the removed elements.

source

pub fn entry(&mut self, key: K) -> Entry<'_, K, V>

Get the corresponding entry for the given key.

source§

impl<V> Trie<BString, V>

source

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.

source

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.

source

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.

source

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.

source

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.

source

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.

source

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.

source

pub fn remove_str<Q>(&mut self, key: &Q) -> Option<V>where Q: Borrow<str> + ?Sized,

Convenience function for removing with a string.

source

pub fn remove_prefix_str<Q>(&mut self, prefix: &Q) -> Trie<BString, V>where Q: Borrow<str> + ?Sized,

Convenience function for removing a prefix with a string.

Trait Implementations§

source§

impl<K: Clone, V: Clone> Clone for Trie<K, V>

source§

fn clone(&self) -> Trie<K, V>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<K: Debug + ToOwned, V: Debug> Debug for Trie<K, V>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<K, V> Default for Trie<K, V>

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

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)>,

Extends a collection with the contents of an iterator. Read more
source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
source§

impl<K: Borrow<[u8]>, V> FromIterator<(K, V)> for Trie<K, V>

source§

fn from_iter<I>(iterable: I) -> Trie<K, V>where I: IntoIterator<Item = (K, V)>,

Creates a value from an iterator. Read more
source§

impl<'a, K, V, Q> Index<&'a Q> for Trie<K, V>where K: Borrow<Q> + Borrow<[u8]>, Q: Borrow<[u8]> + ?Sized,

§

type Output = V

The returned type after indexing.
source§

fn index(&self, key: &Q) -> &V

Performs the indexing (container[index]) operation. Read more
source§

impl<'a, K, V, Q> IndexMut<&'a Q> for Trie<K, V>where K: Borrow<Q> + Borrow<[u8]>, Q: Borrow<[u8]> + ?Sized,

source§

fn index_mut(&mut self, key: &Q) -> &mut V

Performs the mutable indexing (container[index]) operation. Read more
source§

impl<K, V> IntoIterator for Trie<K, V>

§

type IntoIter = IntoIter<K, V>

Which kind of iterator are we turning this into?
§

type Item = (K, V)

The type of the elements being iterated over.
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<K: PartialEq, V: PartialEq> PartialEq for Trie<K, V>

source§

fn eq(&self, other: &Trie<K, V>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<K: Eq, V: Eq> Eq for Trie<K, V>

source§

impl<K, V> StructuralEq for Trie<K, V>

source§

impl<K, V> StructuralPartialEq for Trie<K, V>

Auto Trait Implementations§

§

impl<K, V> RefUnwindSafe for Trie<K, V>where K: RefUnwindSafe, V: RefUnwindSafe,

§

impl<K, V> Send for Trie<K, V>where K: Send, V: Send,

§

impl<K, V> Sync for Trie<K, V>where K: Sync, V: Sync,

§

impl<K, V> Unpin for Trie<K, V>where K: Unpin, V: Unpin,

§

impl<K, V> UnwindSafe for Trie<K, V>where K: UnwindSafe, V: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.