Struct radix_trie::Trie

source ·
pub struct Trie<K, V> { /* private fields */ }
Expand description

Data-structure for storing and querying string-like keys and associated values.

Any keys which share a common prefix are stored below a single copy of that prefix. This saves space, and also allows the longest prefix of any given key to be found.

You can read more about Radix Tries on Wikipedia.

Lots of the methods on Trie return optional values - they can be composed nicely using Option::and_then.

Implementations§

source§

impl<K, V> Trie<K, V>
where K: TrieKey,

source

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

Create an empty Trie.

source

pub fn get<Q>(&self, key: &Q) -> Option<&V>
where K: Borrow<Q>, Q: TrieKey + ?Sized,

Fetch a reference to the given key’s corresponding value, if any.

The key may be any borrowed form of the trie’s key type, but TrieKey on the borrowed form must match those for the key type.

source

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

Fetch a mutable reference to the given key’s corresponding value, if any.

The key may be any borrowed form of the trie’s key type, but TrieKey on the borrowed form must match those for the key type.

source

pub fn insert(&mut self, key: K, value: V) -> Option<V>

Insert the given key-value pair, returning any previous value associated with the key.

source

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

Remove the value associated with the given key.

The key may be any borrowed form of the trie’s key type, but TrieKey on the borrowed form must match those for the key type.

source

pub fn value_mut(&mut self) -> Option<&mut V>

Get a mutable reference to the value stored at this node, if any.

source

pub fn subtrie<'a, Q>(&'a self, key: &Q) -> Option<SubTrie<'a, K, V>>
where K: Borrow<Q>, Q: TrieKey + ?Sized,

Fetch a reference to the subtrie for a given key.

The key may be any borrowed form of the trie’s key type, but TrieKey on the borrowed form must match those for the key type.

source

pub fn subtrie_mut<'a, Q>(&'a mut self, key: &Q) -> Option<SubTrieMut<'a, K, V>>
where K: Borrow<Q>, Q: TrieKey + ?Sized,

Fetch a mutable reference to the subtrie for a given key.

The key may be any borrowed form of the trie’s key type, but TrieKey on the borrowed form must match those for the key type.

source

pub fn get_ancestor<'a, Q>(&'a self, key: &Q) -> Option<SubTrie<'a, K, V>>
where K: Borrow<Q>, Q: TrieKey + ?Sized,

Fetch a reference to the closest ancestor node of the given key.

If key is encoded as byte-vector b, return the node n in the tree such that n’s key’s byte-vector is the longest possible prefix of b, and n has a value.

Invariant: result.is_some() => result.key_value.is_some().

The key may be any borrowed form of the trie’s key type, but TrieKey on the borrowed form must match those for the key type.

source

pub fn get_ancestor_value<Q>(&self, key: &Q) -> Option<&V>
where K: Borrow<Q>, Q: TrieKey + ?Sized,

Fetch the closest ancestor value for a given key.

See get_ancestor for precise semantics, this is just a shortcut.

The key may be any borrowed form of the trie’s key type, but TrieKey on the borrowed form must match those for the key type.

source

pub fn get_raw_ancestor<'a, Q>(&'a self, key: &Q) -> SubTrie<'a, K, V>
where K: Borrow<Q>, Q: TrieKey + ?Sized,

The key may be any borrowed form of the trie’s key type, but TrieKey on the borrowed form must match those for the key type

source

pub fn get_raw_descendant<'a, Q>(&'a self, key: &Q) -> Option<SubTrie<'a, K, V>>
where K: Borrow<Q>, Q: TrieKey + ?Sized,

Fetch the closest descendant for a given key.

If the key is in the trie, this is the same as subtrie.

The key may be any borrowed form of the trie’s key type, but TrieKey on the borrowed form must match those for the key type

source

pub fn map_with_default<F>(&mut self, key: K, f: F, default: V)
where F: Fn(&mut V),

Take a function f and apply it to the value stored at key.

If no value is stored at key, store default.

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, 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: TrieKey, V> Default for Trie<K, V>

source§

fn default() -> Self

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

impl<K, V> FromIterator<(K, V)> for Trie<K, V>
where K: TrieKey,

source§

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

Creates a value from an iterator. Read more
source§

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

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<'a, K, V: 'a> TrieCommon<'a, K, V> for &'a Trie<K, V>
where K: TrieKey + 'a,

source§

fn len(self) -> usize

Number of key/value pairs stored in this trie.
source§

fn children(self) -> Children<'a, K, V>

Return an iterator over the child subtries of this node.
source§

fn key(self) -> Option<&'a K>

Get the key stored at this node, if any.
source§

fn value(self) -> Option<&'a V>

Get the value stored at this node, if any.
source§

fn is_empty(self) -> bool

Determine if the Trie contains 0 key-value pairs.
source§

fn is_leaf(self) -> bool

Determine if the trie is a leaf node (has no children).
source§

fn iter(self) -> Iter<'a, K, V>

Return an iterator over the keys and values of the Trie.
source§

fn keys(self) -> Keys<'a, K, V>

Return an iterator over the keys of the Trie.
source§

fn values(self) -> Values<'a, K, V>

Return an iterator over the values of the Trie.
source§

fn prefix(self) -> &'a Nibblet

Get the prefix of this node.

Auto Trait Implementations§

§

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

§

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

§

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>

§

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

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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 T
where 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 T
where 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 T
where 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 T
where 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.