rust_dsa

Struct GenericTrie

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

Source

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

Creates a new trie.

Source

pub fn insert<I>(&mut self, key: I, value: V) -> Option<V>
where I: IntoIterator<Item = K>, K: Hash + Eq,

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']));
Source

pub fn insert_iterator( &mut self, key: &mut dyn Iterator<Item = K>, value: V, ) -> Option<V>
where K: Hash + Eq,

Source

pub fn get(&self, key: &[K]) -> Option<&V>
where K: Hash + Eq,

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);
Source

pub fn remove(&mut self, key: &[K]) -> Option<V>
where K: Hash + Eq,

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]));
Source

pub fn get_prefix(&self, prefix: &[K]) -> Values<'_, V>
where K: Hash + Eq,

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'])
);
Source

pub fn contains_key(&self, key: &[K]) -> bool
where K: Hash + Eq,

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]));
Source

pub fn contains_prefix(&self, prefix: &[K]) -> bool
where K: Hash + Eq,

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]));
Source

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);
Source

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());
Source

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());
Source

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>

Source§

fn default() -> GenericTrie<K, V>

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

impl<K, V> PartialEq for GenericTrie<K, V>
where K: Hash + Eq, V: PartialEq,

Source§

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);
1.0.0 · 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> Eq for GenericTrie<K, V>
where K: Hash + Eq, V: Eq,

Auto Trait Implementations§

§

impl<K, V> Freeze for GenericTrie<K, V>
where V: Freeze,

§

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

§

impl<K, V> Send for GenericTrie<K, V>
where V: Send, K: Send,

§

impl<K, V> Sync for GenericTrie<K, V>
where V: Sync, K: Sync,

§

impl<K, V> Unpin for GenericTrie<K, V>
where V: Unpin, K: Unpin,

§

impl<K, V> UnwindSafe for GenericTrie<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> 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, 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.