pub struct GenericTrie<K, V> { /* private fields */ }Expand description
A trie for mapping sequences of keys to values.
§Example
use rust_dsa::GenericTrie;
// First, we create a new trie.
let mut trie = GenericTrie::new();
// Then we can insert keys and items.
trie.insert([1, 2, 3], "foo");
trie.insert([1, 2, 4], "bar");
trie.insert([1, 2, 4, 0], "baz");
assert!(trie.contains_key(&[1, 2, 3]));
assert!(trie.contains_key(&[1, 2, 4]));
assert!(trie.contains_key(&[1, 2, 4, 0]));
// We can get the values.
assert_eq!(trie.get(&[1, 2, 3]), Some(&"foo"));
assert_eq!(trie.get(&[1, 2, 4]), Some(&"bar"));
assert_eq!(trie.get(&[1, 2]), None);
// We can iterate over the values with a given prefix.
use std::collections::HashSet;
let get_prefix: HashSet<_> = trie.get_prefix(&[1, 2, 4]).collect();
assert_eq!(get_prefix, HashSet::from([&"bar", &"baz"]));
// We can remove values.
let removed = trie.remove(&[1, 2, 3]);
assert_eq!(removed, Some("foo"));
assert!(!trie.contains_key(&[1, 2, 3]));
assert_eq!(trie.len(), 2);Implementations§
Source§impl<K, V> GenericTrie<K, V>
impl<K, V> GenericTrie<K, V>
Sourcepub fn new() -> GenericTrie<K, V>
pub fn new() -> GenericTrie<K, V>
Creates a new trie.
Sourcepub fn insert<I>(&mut self, key: I, value: V) -> Option<V>
pub fn insert<I>(&mut self, key: I, value: V) -> Option<V>
Inserts the key value pair into the trie, cloning each element in the key.
Returns the previous value, if it exists.
§Example
use rust_dsa::GenericTrie;
let mut trie = GenericTrie::new();
trie.insert(['c', 'a', 'b'], 0);
trie.insert(['c', 'a', 'r'], 0);
trie.insert(['c'], 0);
assert!(trie.contains_key(&['c', 'a', 'b']));
assert!(trie.contains_key(&['c', 'a', 'r']));
assert!(trie.contains_key(&['c']));pub fn insert_iterator( &mut self, key: &mut dyn Iterator<Item = K>, value: V, ) -> Option<V>
Sourcepub fn get(&self, key: &[K]) -> Option<&V>
pub fn get(&self, key: &[K]) -> Option<&V>
Returns a reference to the value associated with the key, if one exists.
§Example
use rust_dsa::GenericTrie;
let mut trie = GenericTrie::new();
trie.insert(['c', 'a', 'b'], 1);
trie.insert(['c', 'a', 'r'], 2);
assert_eq!(trie.get(&['c', 'a', 'b']), Some(&1));
assert_eq!(trie.get(&['c', 'a', 'r']), Some(&2));
assert_eq!(trie.get(&['c', 'a', 't']), None);Sourcepub fn remove(&mut self, key: &[K]) -> Option<V>
pub fn remove(&mut self, key: &[K]) -> Option<V>
Removes and returns the value associated with the key, if it exists.
§Example
use rust_dsa::GenericTrie;
let mut trie = GenericTrie::new();
trie.insert([1, 2, 3], 'a');
trie.insert([1, 2, 4], 'b');
assert_eq!(trie.get(&[1, 2, 3]), Some(&'a'));
assert_eq!(trie.get(&[1, 2, 4]), Some(&'b'));
let removed = trie.remove(&[1, 2, 3]);
assert_eq!(removed, Some('a'));
assert!(!trie.contains_key(&[1, 2, 3]));Sourcepub fn get_prefix(&self, prefix: &[K]) -> Values<'_, V>
pub fn get_prefix(&self, prefix: &[K]) -> Values<'_, V>
Returns an iterator over the values whose associated keys begin with prefix.
§Example
use rust_dsa::GenericTrie;
let mut trie = GenericTrie::new();
trie.insert([1, 2, 3], 'a');
trie.insert([1, 2, 3, 4], 'b');
trie.insert([1, 2], 'c');
trie.insert([1], 'd');
use std::collections::HashSet;
assert_eq!(
trie.get_prefix(&[1, 2]).collect::<HashSet<_>>(),
HashSet::from([&'a', &'b', &'c'])
);Sourcepub fn contains_key(&self, key: &[K]) -> bool
pub fn contains_key(&self, key: &[K]) -> bool
Returns true if the trie contains key.
§Example
use rust_dsa::GenericTrie;
let mut trie = GenericTrie::new();
trie.insert([true, true, false], 0);
assert!(trie.contains_key(&[true, true, false]));
assert!(!trie.contains_key(&[true, false]));Sourcepub fn contains_prefix(&self, prefix: &[K]) -> bool
pub fn contains_prefix(&self, prefix: &[K]) -> bool
Returns true if the trie contains a key with the given prefix.
§Example
use rust_dsa::GenericTrie;
let mut trie = GenericTrie::new();
trie.insert([true, true, false], 0);
assert!(trie.contains_prefix(&[true, true, false]));
assert!(trie.contains_prefix(&[true, true]));
assert!(trie.contains_prefix(&[true]));
assert!(!trie.contains_prefix(&[false]));
assert!(!trie.contains_prefix(&[true, false]));Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of elements in the trie.
§Example
use rust_dsa::GenericTrie;
let mut trie = GenericTrie::new();
trie.insert([1, 2, 3], 'a');
trie.insert([1, 2], 'b');
trie.insert([1, 2, 3], 'c');
assert_eq!(trie.len(), 2);
trie.remove(&[1, 2]);
assert_eq!(trie.len(), 1);Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the trie is empty.
§Example
use rust_dsa::GenericTrie;
let mut trie = GenericTrie::new();
trie.insert([1, 2, 3], 'a');
trie.insert([1, 2], 'b');
assert!(!trie.is_empty());
trie.clear();
assert!(trie.is_empty());Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the trie.
§Example
use rust_dsa::GenericTrie;
let mut trie = GenericTrie::new();
trie.insert([1, 2, 3], 'a');
trie.insert([1, 2], 'b');
assert!(!trie.is_empty());
trie.clear();
assert!(trie.is_empty());Sourcepub fn values(&self) -> Values<'_, V>
pub fn values(&self) -> Values<'_, V>
Returns an iterator over the trie’s values.
§Example
use rust_dsa::GenericTrie;
let mut trie = GenericTrie::new();
trie.insert([1, 2, 3], 'a');
trie.insert([1, 2, 3, 4], 'b');
trie.insert([1, 2], 'c');
trie.insert([1], 'd');
use std::collections::HashSet;
assert_eq!(
trie.values().collect::<HashSet<_>>(),
HashSet::from([&'a', &'b', &'c', &'d'])
);Trait Implementations§
Source§impl<K, V> Default for GenericTrie<K, V>
impl<K, V> Default for GenericTrie<K, V>
Source§fn default() -> GenericTrie<K, V>
fn default() -> GenericTrie<K, V>
Returns the “default value” for a type. Read more
Source§impl<K, V> PartialEq for GenericTrie<K, V>
impl<K, V> PartialEq for GenericTrie<K, V>
Source§fn eq(&self, other: &GenericTrie<K, V>) -> bool
fn eq(&self, other: &GenericTrie<K, V>) -> bool
Returns true if the tries are equal.
§Example
use rust_dsa::GenericTrie;
let mut a = GenericTrie::new();
a.insert(['a', 'b', 'c'], 1);
a.insert(['a', 'x'], 2);
let mut b = GenericTrie::new();
b.insert(['a', 'b', 'c'], 1);
b.insert(['a', 'x'], 2);
b.insert(['z'], 3);
assert!(a != b);
b.remove(&['z']);
assert!(a == b);impl<K, V> Eq for GenericTrie<K, V>
Auto Trait Implementations§
impl<K, V> Freeze for GenericTrie<K, V>where
V: Freeze,
impl<K, V> RefUnwindSafe for GenericTrie<K, V>where
V: RefUnwindSafe,
K: RefUnwindSafe,
impl<K, V> Send for GenericTrie<K, V>
impl<K, V> Sync for GenericTrie<K, V>
impl<K, V> Unpin for GenericTrie<K, V>
impl<K, V> UnwindSafe for GenericTrie<K, V>where
K: UnwindSafe,
V: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more