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>
impl<K, V> GenericPatriciaMap<K, V>
sourcepub fn new() -> Self
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);
sourcepub fn clear(&mut self)
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§impl<K: Bytes, V> GenericPatriciaMap<K, V>
impl<K: Bytes, V> GenericPatriciaMap<K, V>
sourcepub fn contains_key<Q: AsRef<K::Borrowed>>(&self, key: Q) -> bool
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"));
sourcepub fn get<Q: AsRef<K::Borrowed>>(&self, key: Q) -> Option<&V>
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);
sourcepub fn get_mut<Q: AsRef<K::Borrowed>>(&mut self, key: Q) -> Option<&mut V>
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));
sourcepub fn get_longest_common_prefix<'a, Q>(
&self,
key: &'a Q
) -> Option<(&'a K::Borrowed, &V)>where
Q: ?Sized + AsRef<K::Borrowed>,
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)));
sourcepub 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>,
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)));
sourcepub fn insert<Q: AsRef<K::Borrowed>>(&mut self, key: Q, value: V) -> Option<V>
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));
sourcepub fn remove<Q: AsRef<K::Borrowed>>(&mut self, key: Q) -> Option<V>
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);
sourcepub fn common_prefixes<'a, 'b>(
&'a self,
key: &'b [u8]
) -> CommonPrefixesIter<'a, 'b, K, V> ⓘwhere
'a: 'b,
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()));
sourcepub fn common_prefix_values<'a>(
&'a self,
key: &[u8]
) -> impl 'a + Iterator<Item = &'a V>
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()));
sourcepub fn split_by_prefix<Q: AsRef<K::Borrowed>>(&mut self, prefix: Q) -> Self
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"]);
sourcepub fn iter(&self) -> Iter<'_, K, V> ⓘ
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<_>>());
sourcepub fn iter_mut(&mut self) -> IterMut<'_, K, V> ⓘ
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));
sourcepub fn keys(&self) -> Keys<'_, K, V> ⓘ
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<_>>());
sourcepub fn values(&self) -> Values<'_, V> ⓘ
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<_>>());
sourcepub fn values_mut(&mut self) -> ValuesMut<'_, V> ⓘ
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>
impl<K: Bytes, V> GenericPatriciaMap<K, V>
sourcepub fn iter_prefix<'a, 'b>(
&'a self,
prefix: &'b K::Borrowed
) -> impl 'a + Iterator<Item = (K, &'a V)>where
'b: 'a,
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<_>>());
sourcepub fn iter_prefix_mut<'a, 'b>(
&'a mut self,
prefix: &'b K::Borrowed
) -> impl 'a + Iterator<Item = (K, &'a mut V)>where
'b: 'a,
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>
impl<K, V: Clone> Clone for GenericPatriciaMap<K, V>
source§impl<K, V> Default for GenericPatriciaMap<K, V>
impl<K, V> Default for GenericPatriciaMap<K, V>
source§impl<'de, K: Bytes, V: Deserialize<'de>> Deserialize<'de> for GenericPatriciaMap<K, V>
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>,
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>,
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)>,
fn extend<I>(&mut self, iter: I)where I: IntoIterator<Item = (Q, V)>,
source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)source§impl<K, Q, V> FromIterator<(Q, V)> for GenericPatriciaMap<K, V>where
K: Bytes,
Q: AsRef<K::Borrowed>,
impl<K, Q, V> FromIterator<(Q, V)> for GenericPatriciaMap<K, V>where K: Bytes, Q: AsRef<K::Borrowed>,
source§impl<K: Bytes, V> IntoIterator for GenericPatriciaMap<K, V>
impl<K: Bytes, V> IntoIterator for GenericPatriciaMap<K, V>
source§impl<K, V: Serialize> Serialize for GenericPatriciaMap<K, V>
impl<K, V: Serialize> Serialize for GenericPatriciaMap<K, V>
source§fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>where
S: Serializer,
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.