Struct patricia_tree::map::PatriciaMap[][src]

pub struct PatriciaMap<V> { /* fields omitted */ }
Expand description

A map based on a patricia tree.

Implementations

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Performs the conversion.

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

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

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

🔬 This is a nightly-only experimental API. (extend_one)

Extends a collection with exactly one element.

🔬 This is a nightly-only experimental API. (extend_one)

Reserves capacity in a collection for the given number of additional elements. Read more

Performs the conversion.

Performs the conversion.

Creates a value from an iterator. Read more

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.