pub struct Trie<V> { /* private fields */ }Expand description
A trie for mapping strs to values.
§Example
use rust_dsa::Trie;
// First, we create a new trie.
let mut trie = Trie::new();
// Then we can insert keys and items.
trie.insert("foo", 1);
trie.insert("bar", 2);
trie.insert("ba", 3);
assert!(trie.contains_key("foo"));
assert!(trie.contains_key("bar"));
assert!(trie.contains_key("ba"));
// We can get the values.
assert_eq!(trie.get("foo"), Some(&1));
assert_eq!(trie.get("bar"), Some(&2));
assert_eq!(trie.get("ba"), Some(&3));
assert_eq!(trie.get("baz"), None);
// We can iterate over the values with a given prefix.
use std::collections::HashSet;
let get_prefix: HashSet<_> = trie.get_prefix("ba").collect();
assert_eq!(get_prefix, HashSet::from([&2, &3]));
// We can remove values.
let removed = trie.remove("ba");
assert_eq!(removed, Some(3));
assert!(!trie.contains_key("ba"));
assert_eq!(trie.len(), 2);Implementations§
Source§impl<V> Trie<V>
impl<V> Trie<V>
Sourcepub fn insert(&mut self, key: &str, value: V) -> Option<V>
pub fn insert(&mut self, key: &str, value: V) -> Option<V>
Inserts the key value pair into the trie.
Returns the previous value, if it exists.
§Example
use rust_dsa::Trie;
let mut trie = Trie::new();
trie.insert("cab", 0);
trie.insert("car", 0);
trie.insert("c", 0);
assert!(trie.contains_key("cab"));
assert!(trie.contains_key("car"));
assert!(trie.contains_key("c"));Sourcepub fn get(&self, key: &str) -> Option<&V>
pub fn get(&self, key: &str) -> Option<&V>
Returns a reference to the value associated with the key, if one exists.
§Example
use rust_dsa::Trie;
let mut trie = Trie::new();
trie.insert("cab", 1);
trie.insert("car", 2);
assert_eq!(trie.get("cab"), Some(&1));
assert_eq!(trie.get("car"), Some(&2));
assert_eq!(trie.get("cat"), None);Sourcepub fn remove(&mut self, key: &str) -> Option<V>
pub fn remove(&mut self, key: &str) -> Option<V>
Removes and returns the value associated with the key, if it exists.
§Example
use rust_dsa::Trie;
let mut trie = Trie::new();
trie.insert("foo", 1);
trie.insert("bar", 2);
assert_eq!(trie.get("foo"), Some(&1));
assert_eq!(trie.get("bar"), Some(&2));
let removed = trie.remove("foo");
assert_eq!(removed, Some(1));
assert!(!trie.contains_key("foo"));Sourcepub fn get_prefix(&self, prefix: &str) -> Values<'_, V>
pub fn get_prefix(&self, prefix: &str) -> Values<'_, V>
Returns an iterator over the values whose associated keys begin with prefix.
§Example
use rust_dsa::Trie;
let mut trie = Trie::new();
trie.insert("abc", 1);
trie.insert("abcd", 2);
trie.insert("ab", 3);
trie.insert("a", 4);
use std::collections::HashSet;
assert_eq!(
trie.get_prefix("ab").collect::<HashSet<_>>(),
HashSet::from([&1, &2, &3])
);Sourcepub fn contains_key(&self, key: &str) -> bool
pub fn contains_key(&self, key: &str) -> bool
Returns true if the trie contains key.
§Example
use rust_dsa::Trie;
let mut trie = Trie::new();
trie.insert("cat", 0);
assert!(trie.contains_key("cat"));
assert!(!trie.contains_key("ca"));Sourcepub fn contains_prefix(&self, prefix: &str) -> bool
pub fn contains_prefix(&self, prefix: &str) -> bool
Returns true if the trie contains a key with the given prefix.
§Example
use rust_dsa::Trie;
let mut trie = Trie::new();
trie.insert("xyz", 0);
assert!(trie.contains_prefix("xyz"));
assert!(trie.contains_prefix("xy"));
assert!(trie.contains_prefix("x"));
assert!(!trie.contains_prefix("y"));
assert!(!trie.contains_prefix("xz"));Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of elements in the trie.
§Example
use rust_dsa::Trie;
let mut trie = Trie::new();
trie.insert("abc", 1);
trie.insert("ab", 2);
trie.insert("abc", 3);
assert_eq!(trie.len(), 2);
trie.remove("ab");
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::Trie;
let mut trie = Trie::new();
trie.insert("abc", 1);
trie.insert("ab", 2);
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::Trie;
let mut trie = Trie::new();
trie.insert("abc", 1);
trie.insert("ab", 2);
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::Trie;
let mut trie = Trie::new();
trie.insert("abc", 1);
trie.insert("abcd", 2);
trie.insert("ab", 3);
trie.insert("a", 4);
use std::collections::HashSet;
assert_eq!(
trie.values().collect::<HashSet<_>>(),
HashSet::from([&1, &2, &3, &4])
);Trait Implementations§
Source§impl<V, const N: usize> From<[(&'static str, V); N]> for Trie<V>
impl<V, const N: usize> From<[(&'static str, V); N]> for Trie<V>
Source§fn from(array: [(&'static str, V); N]) -> Trie<V>
fn from(array: [(&'static str, V); N]) -> Trie<V>
Creates a trie from an array of key value pairs.
§Example
use rust_dsa::Trie;
let trie = Trie::from([("foo", 1), ("bar", 2), ("baz", 3)]);
assert_eq!(trie.get("foo"), Some(&1));
assert_eq!(trie.get("bar"), Some(&2));
assert_eq!(trie.get("baz"), Some(&3));
assert_eq!(trie.get("boo"), None);Source§impl<V> FromIterator<(&'static str, V)> for Trie<V>
impl<V> FromIterator<(&'static str, V)> for Trie<V>
impl<V> Eq for Trie<V>where
V: Eq,
Auto Trait Implementations§
impl<V> Freeze for Trie<V>where
V: Freeze,
impl<V> RefUnwindSafe for Trie<V>where
V: RefUnwindSafe,
impl<V> Send for Trie<V>where
V: Send,
impl<V> Sync for Trie<V>where
V: Sync,
impl<V> Unpin for Trie<V>where
V: Unpin,
impl<V> UnwindSafe for Trie<V>where
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