Struct patricia_tree::map::PatriciaMap
[−]
[src]
pub struct PatriciaMap<V> { /* fields omitted */ }
A map based on a patricia tree.
Methods
impl<V> PatriciaMap<V>
[src]
pub fn new() -> Self
[src]
Makes a new empty PatriciaMap
instance.
Examples
use patricia_tree::PatriciaMap; let mut map = PatriciaMap::new(); assert!(map.is_empty()); map.insert("foo", 10); assert_eq!(map.len(), 1); assert_eq!(map.get("foo"), Some(&10)); map.remove("foo"); assert_eq!(map.get("foo"), None);
pub fn clear(&mut self)
[src]
Clears this map, removing all values.
Examples
use patricia_tree::PatriciaMap; let mut map = PatriciaMap::new(); map.insert("foo", 1); map.clear(); assert!(map.is_empty());
pub fn contains_key<K: AsRef<[u8]>>(&self, key: K) -> bool
[src]
Returns true
if this map contains a value for the specified key.
Examples
use patricia_tree::PatriciaMap; let mut map = PatriciaMap::new(); map.insert("foo", 1); assert!(map.contains_key("foo")); assert!(!map.contains_key("bar"));
pub fn get<K: AsRef<[u8]>>(&self, key: K) -> Option<&V>
[src]
Returns a reference to the value corresponding to the key.
Examples
use patricia_tree::PatriciaMap; let mut map = PatriciaMap::new(); map.insert("foo", 1); assert_eq!(map.get("foo"), Some(&1)); assert_eq!(map.get("bar"), None);
pub fn get_mut<K: AsRef<[u8]>>(&mut self, key: K) -> Option<&mut V>
[src]
Returns a mutable reference to the value corresponding to the key.
Examples
use patricia_tree::PatriciaMap; let mut map = PatriciaMap::new(); map.insert("foo", 1); map.get_mut("foo").map(|v| *v = 2); assert_eq!(map.get("foo"), Some(&2));
pub fn get_longest_common_prefix<'a, K: ?Sized>(
&self,
key: &'a K
) -> Option<(&'a [u8], &V)> where
K: AsRef<[u8]>,
[src]
&self,
key: &'a K
) -> Option<(&'a [u8], &V)> where
K: AsRef<[u8]>,
Finds the longest common prefix of key
and the keys in this map,
and returns a reference to the entry whose key matches the prefix.
Examples
use patricia_tree::PatriciaMap; let mut map = PatriciaMap::new(); map.insert("foo", 1); map.insert("foobar", 2); assert_eq!(map.get_longest_common_prefix("fo"), None); assert_eq!(map.get_longest_common_prefix("foo"), Some(("foo".as_bytes(), &1))); assert_eq!(map.get_longest_common_prefix("fooba"), Some(("foo".as_bytes(), &1))); assert_eq!(map.get_longest_common_prefix("foobar"), Some(("foobar".as_bytes(), &2))); assert_eq!(map.get_longest_common_prefix("foobarbaz"), Some(("foobar".as_bytes(), &2)));
pub fn insert<K: AsRef<[u8]>>(&mut self, key: K, value: V) -> Option<V>
[src]
Inserts a key-value pair into this map.
If the map did not have this key present, None
is returned.
If the map did have this key present, the value is updated, and the old value is returned.
Examples
use patricia_tree::PatriciaMap; let mut map = PatriciaMap::new(); assert_eq!(map.insert("foo", 1), None); assert_eq!(map.get("foo"), Some(&1)); assert_eq!(map.insert("foo", 2), Some(1)); assert_eq!(map.get("foo"), Some(&2));
pub fn remove<K: AsRef<[u8]>>(&mut self, key: K) -> Option<V>
[src]
Removes a key from this map, returning the value at the key if the key was previously in it.
Examples
use patricia_tree::PatriciaMap; let mut map = PatriciaMap::new(); map.insert("foo", 1); assert_eq!(map.remove("foo"), Some(1)); assert_eq!(map.remove("foo"), None);
pub fn len(&self) -> usize
[src]
Returns the number of elements in this map.
Examples
use patricia_tree::PatriciaMap; let mut map = PatriciaMap::new(); map.insert("foo", 1); map.insert("bar", 2); assert_eq!(map.len(), 2);
pub fn is_empty(&self) -> bool
[src]
Returns true
if this map contains no elements.
Examples
use patricia_tree::PatriciaMap; let mut map = PatriciaMap::new(); assert!(map.is_empty()); map.insert("foo", 1); assert!(!map.is_empty()); map.clear(); assert!(map.is_empty());
ⓘImportant traits for Iter<'a, V>pub fn iter(&self) -> Iter<V>
[src]
Gets an iterator over the entries of this map, sorted by key.
Examples
use patricia_tree::PatriciaMap; let map: PatriciaMap<_> = vec![("foo", 1), ("bar", 2), ("baz", 3)].into_iter().collect(); assert_eq!(vec![(Vec::from("bar"), &2), ("baz".into(), &3), ("foo".into(), &1)], map.iter().collect::<Vec<_>>());
ⓘImportant traits for IterMut<'a, V>pub fn iter_mut(&mut self) -> IterMut<V>
[src]
Gets a mutable iterator over the entries of this map, soretd by key.
Examples
use patricia_tree::PatriciaMap; let mut map: PatriciaMap<_> = vec![("foo", 1), ("bar", 2), ("baz", 3)].into_iter().collect(); for (_, v) in map.iter_mut() { *v += 10; } assert_eq!(map.get("bar"), Some(&12));
ⓘImportant traits for Keys<'a, V>pub fn keys(&self) -> Keys<V>
[src]
Gets an iterator over the keys of this map, in sorted order.
Examples
use patricia_tree::PatriciaMap; let map: PatriciaMap<_> = vec![("foo", 1), ("bar", 2), ("baz", 3)].into_iter().collect(); assert_eq!(vec![Vec::from("bar"), "baz".into(), "foo".into()], map.keys().collect::<Vec<_>>());
ⓘImportant traits for Values<'a, V>pub fn values(&self) -> Values<V>
[src]
Gets an iterator over the values of this map, in order by key.
Examples
use patricia_tree::PatriciaMap; let map: PatriciaMap<_> = vec![("foo", 1), ("bar", 2), ("baz", 3)].into_iter().collect(); assert_eq!(vec![2, 3, 1], map.values().cloned().collect::<Vec<_>>());
ⓘImportant traits for ValuesMut<'a, V>pub fn values_mut(&mut self) -> ValuesMut<V>
[src]
Gets a mutable iterator over the values of this map, in order by key.
Examples
use patricia_tree::PatriciaMap; let mut map: PatriciaMap<_> = vec![("foo", 1), ("bar", 2), ("baz", 3)].into_iter().collect(); for v in map.values_mut() { *v += 10; } assert_eq!(vec![12, 13, 11], map.values().cloned().collect::<Vec<_>>());
Trait Implementations
impl<V: Clone> Clone for PatriciaMap<V>
[src]
fn clone(&self) -> PatriciaMap<V>
[src]
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0[src]
Performs copy-assignment from source
. Read more
impl<V: Debug> Debug for PatriciaMap<V>
[src]
fn fmt(&self, f: &mut Formatter) -> Result
[src]
Formats the value using the given formatter. Read more
impl<V> Default for PatriciaMap<V>
[src]
impl<V> IntoIterator for PatriciaMap<V>
[src]
type Item = (Vec<u8>, V)
The type of the elements being iterated over.
type IntoIter = IntoIter<V>
Which kind of iterator are we turning this into?
fn into_iter(self) -> Self::IntoIter
[src]
Creates an iterator from a value. Read more
impl<K, V> FromIterator<(K, V)> for PatriciaMap<V> where
K: AsRef<[u8]>,
[src]
K: AsRef<[u8]>,
fn from_iter<I>(iter: I) -> Self where
I: IntoIterator<Item = (K, V)>,
[src]
I: IntoIterator<Item = (K, V)>,
Creates a value from an iterator. Read more
impl<K, V> Extend<(K, V)> for PatriciaMap<V> where
K: AsRef<[u8]>,
[src]
K: AsRef<[u8]>,
fn extend<I>(&mut self, iter: I) where
I: IntoIterator<Item = (K, V)>,
[src]
I: IntoIterator<Item = (K, V)>,
Extends a collection with the contents of an iterator. Read more
impl<V> From<Node<V>> for PatriciaMap<V>
[src]
impl<V> From<PatriciaMap<V>> for Node<V>
[src]
fn from(f: PatriciaMap<V>) -> Self
[src]
Performs the conversion.