Type Definition patricia_tree::map::StringPatriciaMap

source ·
pub type StringPatriciaMap<V> = GenericPatriciaMap<String, V>;
Expand description

Patricia tree based map with String as key.

Implementations§

source§

impl<K, V> GenericPatriciaMap<K, V>

source

pub fn new() -> Self

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

pub fn clear(&mut self)

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

pub fn len(&self) -> usize

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

pub fn is_empty(&self) -> bool

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());
source§

impl<K: Bytes, V> GenericPatriciaMap<K, V>

source

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

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

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

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

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

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

pub fn get_longest_common_prefix<'a, Q>( &self, key: &'a Q ) -> Option<(&'a K::Borrowed, &V)>where Q: ?Sized + AsRef<K::Borrowed>,

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

pub fn get_longest_common_prefix_mut<'a, Q>( &mut self, key: &'a Q ) -> Option<(&'a K::Borrowed, &mut V)>where Q: ?Sized + AsRef<K::Borrowed>,

Finds the longest common prefix of key and the keys in this map, and returns a mutable 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_mut("fo"), None);
assert_eq!(map.get_longest_common_prefix_mut("foo"), Some(("foo".as_bytes(), &mut 1)));
*map.get_longest_common_prefix_mut("foo").unwrap().1 = 3;
assert_eq!(map.get_longest_common_prefix_mut("fooba"), Some(("foo".as_bytes(), &mut 3)));
assert_eq!(map.get_longest_common_prefix_mut("foobar"), Some(("foobar".as_bytes(), &mut 2)));
*map.get_longest_common_prefix_mut("foobar").unwrap().1 = 4;
assert_eq!(map.get_longest_common_prefix_mut("foobarbaz"), Some(("foobar".as_bytes(), &mut 4)));
source

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

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

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

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

pub fn common_prefixes<'a, 'b>( &'a self, key: &'b [u8] ) -> CommonPrefixesIter<'a, 'b, K, V> where 'a: 'b,

Returns an iterator that collects all entries in the map up to a certain key.

Example
use patricia_tree::PatriciaMap;

let mut t = PatriciaMap::new();
t.insert("a", vec!["a"]);
t.insert("x", vec!["x"]);
t.insert("ab", vec!["b"]);
t.insert("abc", vec!["c"]);
t.insert("abcd", vec!["d"]);
t.insert("abcdf", vec!["f"]);
assert!(t
    .common_prefixes(b"abcde")
    .map(|(_, v)| v)
    .flatten()
    .eq(vec![&"a", &"b", &"c", &"d"].into_iter()));
source

pub fn common_prefix_values<'a>( &'a self, key: &[u8] ) -> impl 'a + Iterator<Item = &'a V>

Returns an iterator that collects all values of entries in the map up to a certain key.

Example
use patricia_tree::PatriciaMap;
let mut t = PatriciaMap::new();
t.insert("a", vec!["a"]);
t.insert("x", vec!["x"]);
t.insert("ab", vec!["b"]);
t.insert("abc", vec!["c"]);
t.insert("abcd", vec!["d"]);
t.insert("abcdf", vec!["f"]);
assert!(t
    .common_prefix_values(b"abcde")
    .flatten()
    .eq(vec![&"a", &"b", &"c", &"d"].into_iter()));
source

pub fn split_by_prefix<Q: AsRef<K::Borrowed>>(&mut self, prefix: Q) -> Self

Splits the map into two at the given prefix.

The returned map contains all the entries of which keys are prefixed by prefix.

Examples
use patricia_tree::PatriciaMap;

let mut a = PatriciaMap::new();
a.insert("rust", 1);
a.insert("ruby", 2);
a.insert("bash", 3);
a.insert("erlang", 4);
a.insert("elixir", 5);

let b = a.split_by_prefix("e");
assert_eq!(a.len(), 3);
assert_eq!(b.len(), 2);

assert_eq!(a.keys().collect::<Vec<_>>(), [b"bash", b"ruby", b"rust"]);
assert_eq!(b.keys().collect::<Vec<_>>(), [b"elixir", b"erlang"]);
source

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

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<_>>());
source

pub fn iter_mut(&mut self) -> IterMut<'_, K, V>

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

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

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<_>>());
source

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

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<_>>());
source

pub fn values_mut(&mut self) -> ValuesMut<'_, V>

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<_>>());
source§

impl<K: Bytes, V> GenericPatriciaMap<K, V>

source

pub fn iter_prefix<'a, 'b>( &'a self, prefix: &'b K::Borrowed ) -> impl 'a + Iterator<Item = (K, &'a V)>where 'b: 'a,

Gets an iterator over the entries having the given prefix 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)],
           map.iter_prefix(b"ba").collect::<Vec<_>>());
source

pub fn iter_prefix_mut<'a, 'b>( &'a mut self, prefix: &'b K::Borrowed ) -> impl 'a + Iterator<Item = (K, &'a mut V)>where 'b: 'a,

Gets a mutable iterator over the entries having the given prefix of this map, sorted by key.

Examples
use patricia_tree::PatriciaMap;

let mut map: PatriciaMap<_> =
    vec![("foo", 1), ("bar", 2), ("baz", 3)].into_iter().collect();
assert_eq!(vec![(Vec::from("bar"), &mut 2), ("baz".into(), &mut 3)],
           map.iter_prefix_mut(b"ba").collect::<Vec<_>>());

Trait Implementations§

source§

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

source§

fn clone(&self) -> Self

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: Bytes + Debug, V: Debug> Debug for GenericPatriciaMap<K, V>

source§

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

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

impl<K, V> Default for GenericPatriciaMap<K, V>

source§

fn default() -> Self

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

impl<'de, K: Bytes, V: Deserialize<'de>> Deserialize<'de> for GenericPatriciaMap<K, V>

source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where D: Deserializer<'de>,

In order to serialize a [PatriciaMap], make sure you installed the crate with the feature serde.

For example, in your Cargo.toml:

[dependencies]
patricia_tree = { version = "*", features = ["serde"] }

Read more about serialization / deserialization at the serde crate.

source§

impl<K, Q, V> Extend<(Q, V)> for GenericPatriciaMap<K, V>where K: Bytes, Q: AsRef<K::Borrowed>,

source§

fn extend<I>(&mut self, iter: I)where I: IntoIterator<Item = (Q, V)>,

Extends a collection with the contents of an iterator. Read more
source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
source§

impl<K, Q, V> FromIterator<(Q, V)> for GenericPatriciaMap<K, V>where K: Bytes, Q: AsRef<K::Borrowed>,

source§

fn from_iter<I>(iter: I) -> Selfwhere I: IntoIterator<Item = (Q, V)>,

Creates a value from an iterator. Read more
source§

impl<K: Bytes, V> IntoIterator for GenericPatriciaMap<K, V>

§

type Item = (K, V)

The type of the elements being iterated over.
§

type IntoIter = IntoIter<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, V: Serialize> Serialize for GenericPatriciaMap<K, V>

source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>where S: Serializer,

In order to serialize a [PatriciaMap], make sure you installed the crate with the feature serde.

For example, in your Cargo.toml:

[dependencies]
patricia_tree = { version = "*", features = ["serde"] }

Read more about serialization / deserialization at the serde crate.

Auto Trait Implementations§

§

impl<K, V> RefUnwindSafe for GenericPatriciaMap<K, V>where K: RefUnwindSafe, V: RefUnwindSafe,

§

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

§

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

§

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

§

impl<K, V> UnwindSafe for GenericPatriciaMap<K, V>where K: UnwindSafe, V: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.
source§

impl<T> DeserializeOwned for Twhere T: for<'de> Deserialize<'de>,