pub struct MultiKeyMap<K, V>{ /* private fields */ }
Expand description
A wrapper over HashMap that allows for multiple keys to point to a single element, providing some additional utilities to make working with multiple keys easier.
Implementations§
Source§impl<K, V> MultiKeyMap<K, V>
impl<K, V> MultiKeyMap<K, V>
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates an empty MultiKeyMap.
pub fn contains_key<Q>(&self, k: &Q) -> bool
Sourcepub fn insert(&mut self, k: K, v: V) -> Option<V>
pub fn insert(&mut self, k: K, v: V) -> Option<V>
inserts a new value at a given key, and returns the value at that key if
there are no other keys to that value. otherwise returns None
.
Sourcepub fn alias<Q>(&mut self, k: &Q, alias: K) -> Result<&mut V, K>
pub fn alias<Q>(&mut self, k: &Q, alias: K) -> Result<&mut V, K>
Attempts to add a new key to the element at k
. Returns the new key if k
is not
in the map.
use multi_key_map::MultiKeyMap;
let mut map = MultiKeyMap::from([
(vec!["foo".to_string()], 1),
(vec!["quux".to_string()], -2),
]);
let aliased = map.alias("foo", "bar".to_string());
assert_eq!(aliased, Ok(&mut 1));
let aliased = map.alias("baz", "xyz".to_string());
assert_eq!(aliased, Err("xyz".to_string()));
Sourcepub fn alias_many<Q>(
&mut self,
k: &Q,
aliases: Vec<K>,
) -> Result<&mut V, Vec<K>>
pub fn alias_many<Q>( &mut self, k: &Q, aliases: Vec<K>, ) -> Result<&mut V, Vec<K>>
Attempts to add multiple new keys to the element at k
. Returns the list of keys if k
is not
in the map.
use multi_key_map::MultiKeyMap;
let mut map = MultiKeyMap::from([
(vec!["foo".to_string()], 1),
]);
let aliased = map.alias_many("foo", vec!["bar".to_string(), "quux".to_string()]);
assert_eq!(aliased, Ok(&mut 1));
let aliased = map.alias_many("baz", vec!["xyz".to_string(), "syzygy".to_string()]);
assert_eq!(aliased, Err(vec!["xyz".to_string(), "syzygy".to_string()]));
Sourcepub fn values_mut(&mut self) -> impl Iterator<Item = &mut V>
pub fn values_mut(&mut self) -> impl Iterator<Item = &mut V>
An iterator visiting all elements in an arbitrary order, while allowing mutation. Equivalent to HashMap::values_mut
.
Sourcepub fn into_values(self) -> impl Iterator<Item = V>
pub fn into_values(self) -> impl Iterator<Item = V>
Consumes the map and provides an iterator over all values. Equivalent to HashMap::into_values
.
Sourcepub fn iter(&self) -> impl Iterator<Item = (&K, &V)>
pub fn iter(&self) -> impl Iterator<Item = (&K, &V)>
An iterator visiting all key-value pairs. Due to the nature of MultiKeyMap, value references may be shared across multiple keys.
Sourcepub fn insert_many(&mut self, ks: Vec<K>, v: V) -> Vec<V>
pub fn insert_many(&mut self, ks: Vec<K>, v: V) -> Vec<V>
inserts a new value, pairs it to a list of keys, and returns the values that existed at each key if there are no other keys to that value.
use multi_key_map::MultiKeyMap;
let mut map = MultiKeyMap::new();
map.insert_many(vec![1, 2],"foo".to_string());
assert_eq!(Some(&"foo".to_string()), map.get(&1));
assert_eq!(Some(&"foo".to_string()), map.get(&2));
Sourcepub fn remove<Q>(&mut self, k: &Q) -> Option<V>
pub fn remove<Q>(&mut self, k: &Q) -> Option<V>
Removes a key from the map, returning the value at that key if it existed in the map and no other keys share that value.
Trait Implementations§
Source§impl<K, V: Clone> Clone for MultiKeyMap<K, V>
impl<K, V: Clone> Clone for MultiKeyMap<K, V>
Source§fn clone(&self) -> MultiKeyMap<K, V>
fn clone(&self) -> MultiKeyMap<K, V>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl<K, V> Debug for MultiKeyMap<K, V>
impl<K, V> Debug for MultiKeyMap<K, V>
Source§impl<K, V> Default for MultiKeyMap<K, V>
impl<K, V> Default for MultiKeyMap<K, V>
Source§impl<'a, K, V> Extend<(&'a [K], &'a V)> for MultiKeyMap<K, V>
impl<'a, K, V> Extend<(&'a [K], &'a V)> for MultiKeyMap<K, V>
Source§fn extend<T: IntoIterator<Item = (&'a [K], &'a V)>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = (&'a [K], &'a V)>>(&mut self, iter: T)
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, V> Extend<(Vec<K>, V)> for MultiKeyMap<K, V>
impl<K, V> Extend<(Vec<K>, V)> for MultiKeyMap<K, V>
Source§fn extend<T: IntoIterator<Item = (Vec<K>, V)>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = (Vec<K>, V)>>(&mut self, iter: T)
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
)