[−][src]Struct sequence_trie::SequenceTrie
A SequenceTrie
is recursively defined as a value and a map containing child Tries.
Typically, Tries are used to store strings, which can be thought of as lists of char
s.
Generalising this to any key type, a Trie is a data structure storing values for keys
which are themselves lists. Let the parts of such a list-key be called "key fragments".
In our representation of a Trie, K
denotes the type of the key fragments.
The nesting of child Tries creates a tree structure which can be traversed by mapping
key fragments onto nodes. The structure is similar to a k-ary tree, except that the children
are stored in HashMap
s, and there is no bound on the number of children a single node may
have (effectively k = ∞). In a SequenceTrie
with char
key fragments, the key
['a', 'b', 'c']
might correspond to something like this:
SequenceTrie {
value: Some(0),
children: 'a' => SequenceTrie {
value: Some(1),
children: 'b' => SequenceTrie {
value: None,
children: 'c' => SequenceTrie {
value: Some(3),
children: Nil
}
}
}
}
Values are stored optionally at each node because inserting a value for a list-key only inserts
a value for the last fragment of the key. The intermediate prefix nodes are created with value
None
if they do not exist already.
The above SequenceTrie
could be created using the following sequence of operations:
let mut trie: SequenceTrie<char, i32> = SequenceTrie::new(); trie.insert(&['a', 'b', 'c'], 3); trie.insert(&[], 0); trie.insert(&['a'], 1);
The order of insertion is never important.
One interesting thing about Tries is that every key is a descendant of the root, which itself
has no key fragment. Although this is a rather trivial observation, it means that every key
corresponds to a non-empty sequence of prefix nodes in the tree. This observation is the
motivation for the get_prefix_nodes
method, which returns the nodes corresponding to the longest
prefix of a given key.
The empty list key, []
, always corresponds to the root node of the Trie.
The Sequence Trie Invariant
All leaf nodes have non-trivial values (not equal to None
). This invariant is maintained by
the insertion and removal methods and can be relied upon.
Implementations
impl<K, V> SequenceTrie<K, V> where
K: TrieKey,
[src]
K: TrieKey,
pub fn new() -> SequenceTrie<K, V>
[src]
Creates a new SequenceTrie
node with no value and an empty child map.
impl<K, V, S> SequenceTrie<K, V, S> where
K: TrieKey,
S: BuildHasher + Default + Clone,
[src]
K: TrieKey,
S: BuildHasher + Default + Clone,
pub fn with_hasher(hash_builder: S) -> SequenceTrie<K, V, S>
[src]
impl<K, V, S> SequenceTrie<K, V, S> where
K: TrieKey,
S: BuildHasher + Default + Clone,
[src]
K: TrieKey,
S: BuildHasher + Default + Clone,
pub fn value(&self) -> Option<&V>
[src]
Retrieve the value stored at this node.
pub fn value_mut(&mut self) -> Option<&mut V>
[src]
Retrieve a mutable reference to the value stored at this node.
pub fn is_empty(&self) -> bool
[src]
Checks if this node is empty.
A node is considered empty when it has no value and no children.
pub fn is_leaf(&self) -> bool
[src]
Checks if this node has no descendants.
pub fn insert<'key, I, Q: 'key + ?Sized>(
&mut self,
key: I,
value: V
) -> Option<V> where
I: IntoIterator<Item = &'key Q>,
Q: ToOwned<Owned = K>,
K: Borrow<Q>,
[src]
&mut self,
key: I,
value: V
) -> Option<V> where
I: IntoIterator<Item = &'key Q>,
Q: ToOwned<Owned = K>,
K: Borrow<Q>,
Inserts a key and value into the SequenceTrie.
Returns None
if the key did not already correspond to a value, otherwise the old value is
returned.
pub fn insert_owned<I>(&mut self, key: I, value: V) -> Option<V> where
I: IntoIterator<Item = K>,
[src]
I: IntoIterator<Item = K>,
Version of insert
that takes an owned sequence of key fragments.
This function is used internally by insert
.
pub fn get<'key, I, Q: ?Sized>(&self, key: I) -> Option<&V> where
I: IntoIterator<Item = &'key Q>,
K: Borrow<Q>,
Q: TrieKey + 'key,
[src]
I: IntoIterator<Item = &'key Q>,
K: Borrow<Q>,
Q: TrieKey + 'key,
Finds a reference to a key's value, if it has one.
pub fn get_node<'key, I, Q: ?Sized>(
&self,
key: I
) -> Option<&SequenceTrie<K, V, S>> where
I: IntoIterator<Item = &'key Q>,
K: Borrow<Q>,
Q: TrieKey + 'key,
[src]
&self,
key: I
) -> Option<&SequenceTrie<K, V, S>> where
I: IntoIterator<Item = &'key Q>,
K: Borrow<Q>,
Q: TrieKey + 'key,
Finds a reference to a key's node, if it has one.
pub fn get_mut<'key, I, Q: ?Sized>(&mut self, key: I) -> Option<&mut V> where
I: IntoIterator<Item = &'key Q>,
K: Borrow<Q>,
Q: TrieKey + 'key,
[src]
I: IntoIterator<Item = &'key Q>,
K: Borrow<Q>,
Q: TrieKey + 'key,
Finds a mutable reference to a key's value, if it has one.
pub fn get_node_mut<'key, I, Q: ?Sized>(
&mut self,
key: I
) -> Option<&mut SequenceTrie<K, V, S>> where
I: IntoIterator<Item = &'key Q>,
K: Borrow<Q>,
Q: TrieKey + 'key,
[src]
&mut self,
key: I
) -> Option<&mut SequenceTrie<K, V, S>> where
I: IntoIterator<Item = &'key Q>,
K: Borrow<Q>,
Q: TrieKey + 'key,
Finds a mutable reference to a key's node, if it has one.
pub fn get_prefix_nodes<'key, I, Q: ?Sized>(
&self,
key: I
) -> Vec<&SequenceTrie<K, V, S>> where
I: 'key + IntoIterator<Item = &'key Q>,
K: Borrow<Q>,
Q: TrieKey + 'key,
[src]
&self,
key: I
) -> Vec<&SequenceTrie<K, V, S>> where
I: 'key + IntoIterator<Item = &'key Q>,
K: Borrow<Q>,
Q: TrieKey + 'key,
Finds the longest prefix of nodes which match the given key.
pub fn get_ancestor<'key, I, Q: ?Sized>(&self, key: I) -> Option<&V> where
I: 'key + IntoIterator<Item = &'key Q>,
K: Borrow<Q>,
Q: TrieKey + 'key,
[src]
I: 'key + IntoIterator<Item = &'key Q>,
K: Borrow<Q>,
Q: TrieKey + 'key,
Finds the value of the nearest ancestor with a non-empty value, if one exists.
If all ancestors have empty (None
) values, None
is returned.
pub fn get_ancestor_node<'key, I, Q: ?Sized>(
&self,
key: I
) -> Option<&SequenceTrie<K, V, S>> where
I: 'key + IntoIterator<Item = &'key Q>,
K: Borrow<Q>,
Q: TrieKey + 'key,
[src]
&self,
key: I
) -> Option<&SequenceTrie<K, V, S>> where
I: 'key + IntoIterator<Item = &'key Q>,
K: Borrow<Q>,
Q: TrieKey + 'key,
Finds the nearest ancestor with a non-empty value, if one exists.
If all ancestors have empty (None
) values, None
is returned.
pub fn remove<'key, I, Q: ?Sized>(&mut self, key: I) where
I: IntoIterator<Item = &'key Q>,
K: Borrow<Q>,
Q: TrieKey + 'key,
[src]
I: IntoIterator<Item = &'key Q>,
K: Borrow<Q>,
Q: TrieKey + 'key,
Removes the node corresponding to the given key.
This operation is like the reverse of insert
in that
it also deletes extraneous nodes on the path from the root.
If the key node has children, its value is set to None
and no further
action is taken. If the key node is a leaf, then it and its ancestors with
empty values and no other children are deleted. Deletion proceeds up the tree
from the key node until a node with a non-empty value or children is reached.
If the key doesn't match a node in the Trie, no action is taken.
pub fn map<F>(&mut self, f: F) where
F: Fn(&Self) -> Option<V>,
[src]
F: Fn(&Self) -> Option<V>,
Recursively apply a function to every node in the trie.
Nodes are visited "bottom-up" (children before parent).
If f
returns a value, it replaces the value at that node.
Otherwise, the node's value remains unchanged.
pub fn iter(&self) -> Iter<'_, K, V, S>ⓘNotable traits for Iter<'a, K, V, S>
impl<'a, K, V, S> Iterator for Iter<'a, K, V, S> where
K: TrieKey,
S: BuildHasher + Default, type Item = KeyValuePair<'a, K, V>;
[src]
Notable traits for Iter<'a, K, V, S>
impl<'a, K, V, S> Iterator for Iter<'a, K, V, S> where
K: TrieKey,
S: BuildHasher + Default, type Item = KeyValuePair<'a, K, V>;
Returns an iterator over all the key-value pairs in the collection.
pub fn keys(&self) -> Keys<'_, K, V, S>ⓘ
[src]
Returns an iterator over all the keys in the trie.
pub fn values(&self) -> Values<'_, K, V, S>ⓘ
[src]
Returns an iterator over all the values stored in the trie.
pub fn prefix_iter<'trie, 'key, I, Q: ?Sized>(
&'trie self,
key: I
) -> PrefixIter<'trie, 'key, K, V, Q, I::IntoIter, S>ⓘNotable traits for PrefixIter<'trie, 'key, K, V, Q, I, S>
impl<'trie, 'key, K, V, Q: ?Sized, I, S> Iterator for PrefixIter<'trie, 'key, K, V, Q, I, S> where
K: 'trie + TrieKey,
V: 'trie,
I: 'key + Iterator<Item = &'key Q>,
K: Borrow<Q>,
Q: TrieKey + 'key,
S: BuildHasher + Default, type Item = &'trie SequenceTrie<K, V, S>;
where
I: IntoIterator<Item = &'key Q>,
K: Borrow<Q>,
Q: TrieKey + 'key,
[src]
&'trie self,
key: I
) -> PrefixIter<'trie, 'key, K, V, Q, I::IntoIter, S>ⓘ
Notable traits for PrefixIter<'trie, 'key, K, V, Q, I, S>
impl<'trie, 'key, K, V, Q: ?Sized, I, S> Iterator for PrefixIter<'trie, 'key, K, V, Q, I, S> where
K: 'trie + TrieKey,
V: 'trie,
I: 'key + Iterator<Item = &'key Q>,
K: Borrow<Q>,
Q: TrieKey + 'key,
S: BuildHasher + Default, type Item = &'trie SequenceTrie<K, V, S>;
I: IntoIterator<Item = &'key Q>,
K: Borrow<Q>,
Q: TrieKey + 'key,
Returns an iterator over the longest prefix of nodes which match the given key.
pub fn children(&self) -> Vec<&Self>
[src]
Return all the children of this node, in an arbitrary order.
pub fn children_with_keys<'a>(&'a self) -> Vec<(&'a K, &'a Self)>
[src]
Children of this node, with their associated keys in arbitrary order.
Trait Implementations
impl<K: Clone, V: Clone, S: Clone> Clone for SequenceTrie<K, V, S> where
K: TrieKey,
S: BuildHasher + Default,
[src]
K: TrieKey,
S: BuildHasher + Default,
fn clone(&self) -> SequenceTrie<K, V, S>
[src]
fn clone_from(&mut self, source: &Self)
1.0.0[src]
impl<K: Debug, V: Debug, S: Debug> Debug for SequenceTrie<K, V, S> where
K: TrieKey,
S: BuildHasher + Default,
[src]
K: TrieKey,
S: BuildHasher + Default,
impl<K, V, S> Default for SequenceTrie<K, V, S> where
K: TrieKey,
S: BuildHasher + Default + Clone,
[src]
K: TrieKey,
S: BuildHasher + Default + Clone,
impl<K, V, S> Eq for SequenceTrie<K, V, S> where
K: TrieKey,
V: Eq,
S: BuildHasher + Default,
[src]
K: TrieKey,
V: Eq,
S: BuildHasher + Default,
impl<K, V, S> PartialEq<SequenceTrie<K, V, S>> for SequenceTrie<K, V, S> where
K: TrieKey,
V: PartialEq,
S: BuildHasher + Default,
[src]
K: TrieKey,
V: PartialEq,
S: BuildHasher + Default,
Auto Trait Implementations
impl<K, V, S> RefUnwindSafe for SequenceTrie<K, V, S> where
K: RefUnwindSafe,
S: RefUnwindSafe,
V: RefUnwindSafe,
K: RefUnwindSafe,
S: RefUnwindSafe,
V: RefUnwindSafe,
impl<K, V, S> Send for SequenceTrie<K, V, S> where
K: Send,
S: Send,
V: Send,
K: Send,
S: Send,
V: Send,
impl<K, V, S> Sync for SequenceTrie<K, V, S> where
K: Sync,
S: Sync,
V: Sync,
K: Sync,
S: Sync,
V: Sync,
impl<K, V, S> Unpin for SequenceTrie<K, V, S> where
K: Unpin,
S: Unpin,
V: Unpin,
K: Unpin,
S: Unpin,
V: Unpin,
impl<K, V, S> UnwindSafe for SequenceTrie<K, V, S> where
K: UnwindSafe,
S: UnwindSafe,
V: UnwindSafe,
K: UnwindSafe,
S: UnwindSafe,
V: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,