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>
impl<K: Eq + Clone, V> PrefixMap<K, V>
Sourcepub fn new() -> PrefixMap<K, V>
pub fn new() -> PrefixMap<K, V>
Creates an empty PrefixMap
.
§Examples
use prefix_tree::PrefixMap;
let mut map: PrefixMap<u8, i32> = PrefixMap::new();
Sourcepub fn contains_key<Q>(&self, key: Q) -> bool
pub fn contains_key<Q>(&self, key: Q) -> bool
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);
Sourcepub fn clear(&mut self)
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());
Sourcepub fn get<Q>(&self, key: Q) -> Option<&V>
pub fn get<Q>(&self, key: Q) -> Option<&V>
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);
Sourcepub fn get_mut<Q>(&mut self, key: Q) -> Option<&mut V>
pub fn get_mut<Q>(&mut self, key: Q) -> Option<&mut V>
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));
Sourcepub fn insert<Q>(&mut self, key: Q, value: V) -> Option<V>
pub fn insert<Q>(&mut self, key: Q, value: V) -> Option<V>
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));
Sourcepub fn remove<Q>(&mut self, key: Q) -> Option<V>
pub fn remove<Q>(&mut self, key: Q) -> Option<V>
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);
Sourcepub fn is_empty(&self) -> bool
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);
Sourcepub fn len(&self) -> usize
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);
Sourcepub fn iter(&self) -> Iter<'_, K, V>
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);
}
Sourcepub fn keys(&self) -> Keys<'_, K, V>
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]]);
Sourcepub fn values(&self) -> Values<'_, K, V>
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§
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>where
V: RefUnwindSafe,
K: RefUnwindSafe,
impl<K, V> Send for PrefixMap<K, V>
impl<K, V> Sync for PrefixMap<K, V>
impl<K, V> Unpin for PrefixMap<K, V>
impl<K, V> UnwindSafe for PrefixMap<K, V>where
V: UnwindSafe,
K: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more