Struct PrefixMap

Source
pub struct PrefixMap<K, V> { /* private fields */ }
Expand description

A map implemented with prefix tree.

Implementations§

Source§

impl<K: Eq + Clone, V> PrefixMap<K, V>

Source

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

Creates an empty PrefixMap.

§Examples
use prefix_tree::PrefixMap;

let mut map: PrefixMap<u8, i32> = PrefixMap::new();
Source

pub fn contains_key<Q>(&self, key: Q) -> bool
where Q: AsRef<[K]>,

Returns true if the map contains a value for the specified key.

§Examples
use prefix_tree::PrefixMap;

let mut map: PrefixMap<u8, i32> = PrefixMap::new();
map.insert("foo", 1);
assert_eq!(map.contains_key("foo"), true);
assert_eq!(map.contains_key("bar"), false);
Source

pub fn clear(&mut self)

Clears the map, removing all key-value pairs.

§Examples
use prefix_tree::PrefixMap;

let mut map: PrefixMap<u8, i32> = PrefixMap::new();
map.insert("foo", 1);
map.clear();
assert!(map.is_empty());
Source

pub fn get<Q>(&self, key: Q) -> Option<&V>
where Q: AsRef<[K]>,

Returns a reference to the value corresponding to the key.

§Examples
use prefix_tree::PrefixMap;

let mut map: PrefixMap<u8, i32> = PrefixMap::new();
map.insert("foo", 1);
assert_eq!(map.get("foo"), Some(&1));
assert_eq!(map.get("bar"), None);
Source

pub fn get_mut<Q>(&mut self, key: Q) -> Option<&mut V>
where Q: AsRef<[K]>,

Returns a mutable reference to the value corresponding to the key.

§Examples
use prefix_tree::PrefixMap;

let mut map: PrefixMap<u8, i32> = PrefixMap::new();
map.insert("foo", 1);
if let Some(x) = map.get_mut("foo") {
    *x = 2;
}
assert_eq!(map.get("foo"), Some(&2));
Source

pub fn insert<Q>(&mut self, key: Q, value: V) -> Option<V>
where Q: AsRef<[K]>,

Inserts a key-value pair into the map.

§Examples
use prefix_tree::PrefixMap;

let mut map: PrefixMap<u8, i32> = PrefixMap::new();
assert_eq!(map.insert("a", 42), None);
assert_eq!(map.is_empty(), false);
assert_eq!(map.insert("a", 5), Some(42));
assert_eq!(map.get("a"), Some(&5));
Source

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

Removes a key from the map, returning the value at the key if the key was previously in the map.

§Examples
use prefix_tree::PrefixMap;

let mut map: PrefixMap<u8, i32> = PrefixMap::new();
assert_eq!(map.insert("a", 42), None);
assert_eq!(map.remove("a"), Some(42));
assert_eq!(map.get("a"), None);
Source

pub fn is_empty(&self) -> bool

Returns true if the map contains no elements.

§Examples
use prefix_tree::PrefixMap;

let mut map: PrefixMap<u8, i32> = PrefixMap::new();
assert_eq!(map.is_empty(), true);
map.insert("foo", 1);
assert_eq!(map.is_empty(), false);
Source

pub fn len(&self) -> usize

Returns the number of elements in the map.

§Examples
use prefix_tree::PrefixMap;

let mut map: PrefixMap<u8, i32> = PrefixMap::new();
assert_eq!(map.len(), 0);
map.insert("foo", 1);
assert_eq!(map.len(), 1);
Source

pub fn iter(&self) -> Iter<'_, K, V>

Gets an iterator over the entries of the map, in arbitrary order.

§Examples
use prefix_tree::PrefixMap;

let mut map: PrefixMap<u8, i32> = PrefixMap::new();
map.insert("1", 9);
map.insert("2", 8);
map.insert("3", 7);

for (key, value) in map.iter() {
    println!("{:?}: {:?}", key, value);
}
Source

pub fn keys(&self) -> Keys<'_, K, V>

Gets an iterator over the keys of the map, in arbitrary order.

§Examples
use prefix_tree::PrefixMap;

let mut map: PrefixMap<i32, i32> = PrefixMap::new();
map.insert([1], 2);
map.insert([2], 3);

assert_eq!(map.keys().collect::<Vec<_>>(), vec![vec![1], vec![2]]);
Source

pub fn values(&self) -> Values<'_, K, V>

Gets an iterator over the values of the map, in arbitrary order.

§Examples
use prefix_tree::PrefixMap;

let mut map: PrefixMap<i32, i32> = PrefixMap::new();
map.insert([1], 2);
map.insert([2], 3);

assert_eq!(map.values().cloned().collect::<Vec<_>>(), vec![2, 3]);

Trait Implementations§

Source§

impl<K: Clone, V: Clone> Clone for PrefixMap<K, V>

Source§

fn clone(&self) -> PrefixMap<K, V>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<K: Debug, V: Debug> Debug for PrefixMap<K, V>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<K: Default, V: Default> Default for PrefixMap<K, V>

Source§

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

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

impl<'a, K: 'a + Eq + Clone, V: 'a> FromIterator<(&'a [K], V)> for PrefixMap<K, V>

Source§

fn from_iter<I>(iter: I) -> PrefixMap<K, V>
where I: IntoIterator<Item = (&'a [K], V)>,

Creates a value from an iterator. Read more
Source§

impl<K: Eq + Clone + Hash, V: Hash> Hash for PrefixMap<K, V>

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<K: Eq + Clone, V, Q: AsRef<[K]>> Index<Q> for PrefixMap<K, V>

Source§

type Output = V

The returned type after indexing.
Source§

fn index(&self, index: Q) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<'a, K: 'a + Eq + Clone, V: 'a> IntoIterator for &'a PrefixMap<K, V>

Source§

type Item = (Vec<K>, &'a V)

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, K, V>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<K: Eq + Clone, V: Eq> PartialEq for PrefixMap<K, V>

Source§

fn eq(&self, other: &PrefixMap<K, 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<K: Eq + Clone, V: Eq> Eq for PrefixMap<K, V>

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<K, V> UnwindSafe for PrefixMap<K, V>
where V: UnwindSafe, K: 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.