Skip to main content

Trie

Struct Trie 

Source
pub struct Trie<K, V>
where K: Clone + PartialEq + Ord, V: Clone,
{ /* private fields */ }
Expand description

A singly initialised Trie mapping key sequences to a value.

It is not permitted for values to be mapped to a key that is a prefix of another key also existing in the same Trie.

There are convenience methods provided for Trie<char, V> for when &str values are used as keys.

Implementations§

Source§

impl<K, V> Trie<K, V>
where K: Clone + PartialEq + Ord, V: Clone,

Source

pub fn try_from_iter( it: impl IntoIterator<Item = (Vec<K>, V)>, ) -> Result<Self, &'static str>

Construct a new Trie from key-value pairs.

Will panic if there are any key collisions or if there are any sequences that nest under a prefix that is already required to hold a value

Source

pub fn merge(self, other: Self) -> Result<Self, &'static str>

Merge two Tries.

If the resulting Trie would be invalid to construct directly, an error is returned.

Consumes both inputs.

Source

pub fn merge_overriding(self, other: Self) -> Result<Self, &'static str>

Merge two Tries preferring keys from other in the case of collisions.

If the resulting Trie would be invalid to construct directly, an error is returned.

Consumes both inputs.

Source

pub fn get<'a>(&'a self, key: &[K]) -> QueryResult<'a, V>

Query this Trie for a given key or key prefix

If the key maps to a leaf then the value is returned, if it maps to a sub-trie then Partial is returned to denote that the given key is a parent of one or more values. If the key is not found within the Trie then Missing is returned.

Source

pub fn get_exact<'a>(&'a self, key: &[K]) -> Option<&'a V>

Query this Trie for a given key or key prefix requiring the key to match exactly.

If the key maps to a leaf then the Some(value) is returned, otherwise None.

Source

pub fn len(&self) -> usize

The number of leaf values in this Trie

Source

pub fn is_empty(&self) -> bool

Whether this Trie is empty

Source§

impl<V> Trie<char, V>
where V: Clone,

Source

pub fn from_str_keys(pairs: Vec<(&str, V)>) -> Result<Self, &'static str>

Construct a new Trie with char internal keys from string keys.

Source

pub fn get_str<'a>(&'a self, key: &str) -> QueryResult<'a, V>

Query this Trie using a string key.

Both full and partial matches are possible.

Source

pub fn get_str_exact<'a>(&'a self, key: &str) -> Option<&'a V>

Query this Trie using a string key.

Only fll matches will be returned.

Trait Implementations§

Source§

impl<K, V> Clone for Trie<K, V>
where K: Clone + PartialEq + Ord + Clone, V: Clone + Clone,

Source§

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

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<K, V> Debug for Trie<K, V>
where K: Clone + PartialEq + Ord + Debug, V: Clone + Debug,

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>
where K: Clone + PartialEq + Ord, V: Clone,

Source§

fn default() -> Self

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

impl<K, V> Eq for Trie<K, V>
where K: Clone + PartialEq + Ord + Eq, V: Clone + Eq,

Source§

impl<K, V> PartialEq for Trie<K, V>
where K: Clone + PartialEq + Ord + PartialEq, V: Clone + PartialEq,

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<K, V> StructuralPartialEq for Trie<K, V>
where K: Clone + PartialEq + Ord, V: Clone,

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 V: Sync + Send, K: Sync + Send,

§

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

§

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

§

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

§

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

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§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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,

Source§

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

Source§

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

Source§

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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more