rust_dsa

Struct Trie

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

Source

pub fn new() -> Trie<V>

Creates a new trie.

Source

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

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

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

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

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

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

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

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

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

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> Default for Trie<V>

Source§

fn default() -> Trie<V>

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

impl<V, const N: usize> From<[(&'static str, V); N]> for Trie<V>

Source§

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>

Source§

fn from_iter<T: IntoIterator<Item = (&'static str, V)>>(iter: T) -> Trie<V>

Creates a value from an iterator. Read more
Source§

impl<V> PartialEq for Trie<V>
where V: PartialEq,

Source§

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

Tests for self and other values to be equal, and is used by ==.
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<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> 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.