Struct MultiKeyMap

Source
pub struct MultiKeyMap<K, V>
where K: Hash + Eq,
{ /* 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>
where K: Hash + Eq,

Source

pub fn new() -> Self

Creates an empty MultiKeyMap.

Source

pub fn contains_key<Q>(&self, k: &Q) -> bool
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Source

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.

Source

pub fn alias<Q>(&mut self, k: &Q, alias: K) -> Result<&mut V, K>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

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

pub fn alias_many<Q>( &mut self, k: &Q, aliases: Vec<K>, ) -> Result<&mut V, Vec<K>>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

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

pub fn keys(&self) -> impl Iterator<Item = &K>

An iterator visiting all keys in an arbitrary order. Equivalent to HashMap::keys.

Source

pub fn values(&self) -> impl Iterator<Item = &V>

An iterator visiting all elements in an arbitrary order. Equivalent to HashMap::values.

Source

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.

Source

pub fn into_keys(self) -> impl Iterator<Item = K>

Consumes the map and provides an iterator over all keys. Equivalent to HashMap::into_keys.

Source

pub fn into_values(self) -> impl Iterator<Item = V>

Consumes the map and provides an iterator over all values. Equivalent to HashMap::into_values.

Source

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.

Source

pub fn get<Q>(&self, k: &Q) -> Option<&V>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Returns a shared reference to the value of the key. Equivalent to HashMap::get.

Source

pub fn get_mut<Q>(&mut self, k: &Q) -> Option<&mut V>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Returns a mutable reference to the value of the key. Equivalent to HashMap::get_mut.

use multi_key_map::MultiKeyMap;
let mut map = MultiKeyMap::new();
map.insert(1, "foo".to_string());
let x = map.get_mut(&1).unwrap();
*x = "bar".to_string();
assert_eq!(Some(&"bar".to_string()), map.get(&1));
Source

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

pub fn remove<Q>(&mut self, k: &Q) -> Option<V>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

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.

Source

pub fn remove_many<Q, const N: usize>(&mut self, ks: [&Q; N]) -> Vec<V>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Removes a list of keys from the map, returning the values at each key if they existed in the map and no other keys shared that value.

Source

pub fn entry(&mut self, k: K) -> Entry<'_, K, V>

Equivalent to HashMap::entry.

Trait Implementations§

Source§

impl<K, V: Clone> Clone for MultiKeyMap<K, V>
where K: Hash + Eq + Clone,

Source§

fn clone(&self) -> MultiKeyMap<K, V>

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, V> Debug for MultiKeyMap<K, V>
where K: Hash + Eq + Debug, V: Debug,

Source§

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

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

impl<K, V> Default for MultiKeyMap<K, V>
where K: Hash + Eq,

Source§

fn default() -> Self

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

impl<'a, K, V> Extend<(&'a [K], &'a V)> for MultiKeyMap<K, V>
where K: Hash + Eq + Copy, V: Copy,

Source§

fn extend<T: IntoIterator<Item = (&'a [K], &'a V)>>(&mut self, iter: T)

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, V> Extend<(Vec<K>, V)> for MultiKeyMap<K, V>
where K: Hash + Eq,

Source§

fn extend<T: IntoIterator<Item = (Vec<K>, V)>>(&mut self, iter: T)

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, V, const N: usize> From<[(Vec<K>, V); N]> for MultiKeyMap<K, V>
where K: Hash + Eq,

Source§

fn from(arr: [(Vec<K>, V); N]) -> Self

Converts to this type from the input type.
Source§

impl<K, V> FromIterator<(Vec<K>, V)> for MultiKeyMap<K, V>
where K: Hash + Eq,

Source§

fn from_iter<T: IntoIterator<Item = (Vec<K>, V)>>(iter: T) -> Self

Creates a value from an iterator. Read more
Source§

impl<K, V> PartialEq for MultiKeyMap<K, V>
where K: Hash + Eq, V: PartialEq,

Source§

fn eq(&self, rhs: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<K, V> Eq for MultiKeyMap<K, V>
where K: Hash + Eq, V: Eq,

Auto Trait Implementations§

§

impl<K, V> Freeze for MultiKeyMap<K, V>

§

impl<K, V> RefUnwindSafe for MultiKeyMap<K, V>

§

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

§

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

§

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

§

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

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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 T
where 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 T
where T: Clone,

Source§

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

Source§

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

Source§

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.