Struct multimap::MultiMap

source ·
pub struct MultiMap<K, V, S = RandomState> { /* private fields */ }

Implementations§

source§

impl<K, V> MultiMap<K, V>
where K: Eq + Hash,

source

pub fn new() -> MultiMap<K, V>

Creates an empty MultiMap

Examples
use multimap::MultiMap;

let mut map: MultiMap<&str, isize> = MultiMap::new();
source

pub fn with_capacity(capacity: usize) -> MultiMap<K, V>

Creates an empty multimap with the given initial capacity.

Examples
use multimap::MultiMap;

let mut map: MultiMap<&str, isize> = MultiMap::with_capacity(20);
source§

impl<K, V, S> MultiMap<K, V, S>
where K: Eq + Hash, S: BuildHasher,

source

pub fn with_hasher(hash_builder: S) -> MultiMap<K, V, S>

Creates an empty MultiMap which will use the given hash builder to hash keys.

Examples
use multimap::MultiMap;
use std::collections::hash_map::RandomState;

let s = RandomState::new();
let mut map: MultiMap<&str, isize> = MultiMap::with_hasher(s);
source

pub fn with_capacity_and_hasher( capacity: usize, hash_builder: S ) -> MultiMap<K, V, S>

Creates an empty MultiMap with the given intial capacity and hash builder.

Examples
use multimap::MultiMap;
use std::collections::hash_map::RandomState;

let s = RandomState::new();
let mut map: MultiMap<&str, isize> = MultiMap::with_capacity_and_hasher(20, s);
source

pub fn insert(&mut self, k: K, v: V)

Inserts a key-value pair into the multimap. If the key does exist in the map then the value is pushed to that key’s vector. If the key doesn’t exist in the map a new vector with the given value is inserted.

Examples
use multimap::MultiMap;

let mut map = MultiMap::new();
map.insert("key", 42);
source

pub fn insert_many<I: IntoIterator<Item = V>>(&mut self, k: K, v: I)

Inserts multiple key-value pairs into the multimap. If the key does exist in the map then the values are extended into that key’s vector. If the key doesn’t exist in the map a new vector collected from the given values is inserted.

This may be more efficient than inserting values independently.

Examples
use multimap::MultiMap;

let mut map = MultiMap::<&str, &usize>::new();
map.insert_many("key", &[42, 43]);
source

pub fn insert_many_from_slice(&mut self, k: K, v: &[V])
where V: Clone,

Inserts multiple key-value pairs into the multimap. If the key does exist in the map then the values are extended into that key’s vector. If the key doesn’t exist in the map a new vector collected from the given values is inserted.

This may be more efficient than inserting values independently.

Examples
use multimap::MultiMap;

let mut map = MultiMap::<&str, usize>::new();
map.insert_many_from_slice("key", &[42, 43]);
source

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

Returns true if the map contains a value for the specified key.

The key may be any borrowed form of the map’s key type, but Hash and Eq on the borrowed form must match those for the key type.

Examples
use multimap::MultiMap;

let mut map = MultiMap::new();
map.insert(1, 42);
assert_eq!(map.contains_key(&1), true);
assert_eq!(map.contains_key(&2), false);
source

pub fn len(&self) -> usize

Returns the number of elements in the map.

Examples
use multimap::MultiMap;

let mut map = MultiMap::new();
map.insert(1, 42);
map.insert(2, 1337);
assert_eq!(map.len(), 2);
source

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

Removes a key from the map, returning the vector of values at the key if the key was previously in the map.

The key may be any borrowed form of the map’s key type, but Hash and Eq on the borrowed form must match those for the key type.

Examples
use multimap::MultiMap;

let mut map = MultiMap::new();
map.insert(1, 42);
map.insert(1, 1337);
assert_eq!(map.remove(&1), Some(vec![42, 1337]));
assert_eq!(map.remove(&1), None);
source

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

Returns a reference to the first item in the vector corresponding to the key.

The key may be any borrowed form of the map’s key type, but Hash and Eq on the borrowed form must match those for the key type.

Examples
use multimap::MultiMap;

let mut map = MultiMap::new();
map.insert(1, 42);
map.insert(1, 1337);
assert_eq!(map.get(&1), Some(&42));
source

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

Returns a mutable reference to the first item in the vector corresponding to the key.

The key may be any borrowed form of the map’s key type, but Hash and Eq on the borrowed form must match those for the key type.

Examples
use multimap::MultiMap;

let mut map = MultiMap::new();
map.insert(1, 42);
map.insert(1, 1337);
if let Some(v) = map.get_mut(&1) {
    *v = 99;
}
assert_eq!(map[&1], 99);
source

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

Returns a reference to the vector corresponding to the key.

The key may be any borrowed form of the map’s key type, but Hash and Eq on the borrowed form must match those for the key type.

Examples
use multimap::MultiMap;

let mut map = MultiMap::new();
map.insert(1, 42);
map.insert(1, 1337);
assert_eq!(map.get_vec(&1), Some(&vec![42, 1337]));
source

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

Returns a mutable reference to the vector corresponding to the key.

The key may be any borrowed form of the map’s key type, but Hash and Eq on the borrowed form must match those for the key type.

Examples
use multimap::MultiMap;

let mut map = MultiMap::new();
map.insert(1, 42);
map.insert(1, 1337);
if let Some(v) = map.get_vec_mut(&1) {
    (*v)[0] = 1991;
    (*v)[1] = 2332;
}
assert_eq!(map.get_vec(&1), Some(&vec![1991, 2332]));
source

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

Returns true if the key is multi-valued.

The key may be any borrowed form of the map’s key type, but Hash and Eq on the borrowed form must match those for the key type.

Examples
use multimap::MultiMap;

let mut map = MultiMap::new();
map.insert(1, 42);
map.insert(1, 1337);
map.insert(2, 2332);

assert_eq!(map.is_vec(&1), true);   // key is multi-valued
assert_eq!(map.is_vec(&2), false);  // key is single-valued
assert_eq!(map.is_vec(&3), false);  // key not in map
source

pub fn capacity(&self) -> usize

Returns the number of elements the map can hold without reallocating.

Examples
use multimap::MultiMap;

let map: MultiMap<usize, usize> = MultiMap::new();
assert!(map.capacity() >= 0);
source

pub fn is_empty(&self) -> bool

Returns true if the map contains no elements.

Examples
use multimap::MultiMap;

let mut map = MultiMap::new();
assert!(map.is_empty());
map.insert(1,42);
assert!(!map.is_empty());
source

pub fn clear(&mut self)

Clears the map, removing all key-value pairs. Keeps the allocated memory for reuse.

Examples
use multimap::MultiMap;

let mut map = MultiMap::new();
map.insert(1,42);
map.clear();
assert!(map.is_empty());
source

pub fn keys(&self) -> Keys<'_, K, Vec<V>>

An iterator visiting all keys in arbitrary order. Iterator element type is &’a K.

Examples
use multimap::MultiMap;

let mut map = MultiMap::new();
map.insert(1,42);
map.insert(1,1337);
map.insert(2,1337);
map.insert(4,1991);

let mut keys: Vec<_> = map.keys().collect();
keys.sort();
assert_eq!(keys, [&1, &2, &4]);
source

pub fn iter(&self) -> Iter<'_, K, V>

An iterator visiting all key-value pairs in arbitrary order. The iterator returns a reference to the key and the first element in the corresponding key’s vector. Iterator element type is (&’a K, &’a V).

Examples
use multimap::MultiMap;

let mut map = MultiMap::new();
map.insert(1,42);
map.insert(1,1337);
map.insert(3,2332);
map.insert(4,1991);

let mut pairs: Vec<_> = map.iter().collect();
pairs.sort_by_key(|p| p.0);
assert_eq!(pairs, [(&1, &42), (&3, &2332), (&4, &1991)]);
source

pub fn iter_mut(&mut self) -> IterMut<'_, K, V>

An iterator visiting all key-value pairs in arbitrary order. The iterator returns a reference to the key and a mutable reference to the first element in the corresponding key’s vector. Iterator element type is (&’a K, &’a mut V).

Examples
use multimap::MultiMap;

let mut map = MultiMap::new();
map.insert(1,42);
map.insert(1,1337);
map.insert(3,2332);
map.insert(4,1991);

for (_, value) in map.iter_mut() {
    *value *= *value;
}

let mut pairs: Vec<_> = map.iter_mut().collect();
pairs.sort_by_key(|p| p.0);
assert_eq!(pairs, [(&1, &mut 1764), (&3, &mut 5438224), (&4, &mut 3964081)]);
source

pub fn iter_all(&self) -> IterAll<'_, K, Vec<V>>

An iterator visiting all key-value pairs in arbitrary order. The iterator returns a reference to the key and the corresponding key’s vector. Iterator element type is (&’a K, &’a V).

Examples
use multimap::MultiMap;

let mut map = MultiMap::new();
map.insert(1,42);
map.insert(1,1337);
map.insert(3,2332);
map.insert(4,1991);

let mut pairs: Vec<_> = map.iter_all().collect();
pairs.sort_by_key(|p| p.0);
assert_eq!(pairs, [(&1, &vec![42, 1337]), (&3, &vec![2332]), (&4, &vec![1991])]);
source

pub fn iter_all_mut(&mut self) -> IterAllMut<'_, K, Vec<V>>

An iterator visiting all key-value pairs in arbitrary order. The iterator returns a reference to the key and the corresponding key’s vector. Iterator element type is (&’a K, &’a V).

Examples
use multimap::MultiMap;

let mut map = MultiMap::new();
map.insert(1,42);
map.insert(1,1337);
map.insert(3,2332);
map.insert(4,1991);

for (key, values) in map.iter_all_mut() {
    for value in values.iter_mut() {
        *value = 99;
    }
}

let mut pairs: Vec<_> = map.iter_all_mut().collect();
pairs.sort_by_key(|p| p.0);
assert_eq!(pairs, [(&1, &mut vec![99, 99]), (&3, &mut vec![99]), (&4, &mut vec![99])]);
source

pub fn flat_iter(&self) -> impl Iterator<Item = (&K, &V)>

An iterator visiting all key-value pairs in arbitrary order. The iterator returns a reference to the key and the first element in the corresponding key’s vector. Iterator element type is (&’a K, &’a V).

Examples
use multimap::MultiMap;

let mut map = MultiMap::new();
map.insert(1,42);
map.insert(1,1337);
map.insert(3,2332);
map.insert(4,1991);

for (key, value) in map.flat_iter() {
    println!("key: {:?}, val: {:?}", key, value);
}
source

pub fn flat_iter_mut(&mut self) -> impl Iterator<Item = (&K, &mut V)>

An iterator visiting all key-value pairs in arbitrary order. The iterator returns a reference to the key and the first element in the corresponding key’s vector. Iterator element type is (&’a K, &’a V).

Examples
use multimap::MultiMap;

let mut map = MultiMap::new();
map.insert(1,42);
map.insert(1,1337);
map.insert(3,2332);
map.insert(4,1991);

for (key, value) in map.flat_iter_mut() {
    println!("key: {:?}, val: {:?}", key, value);
}
source

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

Gets the specified key’s corresponding entry in the map for in-place manipulation. It’s possible to both manipulate the vector and the ‘value’ (the first value in the vector).

Examples
use multimap::MultiMap;

let mut m = MultiMap::new();
m.insert(1, 42);

{
    let mut v = m.entry(1).or_insert(43);
    assert_eq!(v, &42);
    *v = 44;
}
assert_eq!(m.entry(2).or_insert(666), &666);

{
    let mut v = m.entry(1).or_insert_vec(vec![43]);
    assert_eq!(v, &vec![44]);
    v.push(50);
}
assert_eq!(m.entry(2).or_insert_vec(vec![667]), &vec![666]);

assert_eq!(m.get_vec(&1), Some(&vec![44, 50]));
source

pub fn retain<F>(&mut self, f: F)
where F: FnMut(&K, &V) -> bool,

Retains only the elements specified by the predicate.

In other words, remove all pairs (k, v) such that f(&k,&mut v) returns false.

Examples
use multimap::MultiMap;

let mut m = MultiMap::new();
m.insert(1, 42);
m.insert(1, 99);
m.insert(2, 42);
m.retain(|&k, &v| { k == 1 && v == 42 });
assert_eq!(1, m.len());
assert_eq!(Some(&42), m.get(&1));

Trait Implementations§

source§

impl<K: Clone, V: Clone, S: Clone> Clone for MultiMap<K, V, S>

source§

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

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

source§

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

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

impl<K, V, S> Default for MultiMap<K, V, S>
where K: Eq + Hash, S: BuildHasher + Default,

source§

fn default() -> MultiMap<K, V, S>

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

impl<'a, K, V, S> Deserialize<'a> for MultiMap<K, V, S>
where K: Deserialize<'a> + Eq + Hash, V: Deserialize<'a>, S: BuildHasher + Default,

source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'a>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl<'a, K, V, S> Extend<(&'a K, &'a V)> for MultiMap<K, V, S>
where K: Eq + Hash + Copy, V: Copy, S: BuildHasher,

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<'a, K, V, S> Extend<(&'a K, &'a Vec<V>)> for MultiMap<K, V, S>
where K: Eq + Hash + Copy, V: Copy, S: BuildHasher,

source§

fn extend<T: IntoIterator<Item = (&'a K, &'a Vec<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, S> Extend<(K, V)> for MultiMap<K, V, S>
where K: Eq + Hash, S: BuildHasher,

source§

fn extend<T: IntoIterator<Item = (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, S> Extend<(K, Vec<V>)> for MultiMap<K, V, S>
where K: Eq + Hash, S: BuildHasher,

source§

fn extend<T: IntoIterator<Item = (K, Vec<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, S> FromIterator<(K, V)> for MultiMap<K, V, S>
where K: Eq + Hash, S: BuildHasher + Default,

source§

fn from_iter<T: IntoIterator<Item = (K, V)>>(iterable: T) -> MultiMap<K, V, S>

Creates a value from an iterator. Read more
source§

impl<K, V, S> FromIterator<(K, Vec<V>)> for MultiMap<K, V, S>
where K: Eq + Hash, V: Clone, S: BuildHasher + Default,

source§

fn from_iter<T: IntoIterator<Item = (K, Vec<V>)>>( iterable: T ) -> MultiMap<K, V, S>

Creates a value from an iterator. Read more
source§

impl<'a, K, V, S, Q> Index<&'a Q> for MultiMap<K, V, S>
where K: Eq + Hash + Borrow<Q>, Q: Eq + Hash + ?Sized, S: BuildHasher,

§

type Output = V

The returned type after indexing.
source§

fn index(&self, index: &Q) -> &V

Performs the indexing (container[index]) operation. Read more
source§

impl<'a, K, V, S> IntoIterator for &'a MultiMap<K, V, S>
where K: Eq + Hash, S: BuildHasher,

§

type Item = (&'a K, &'a Vec<V>)

The type of the elements being iterated over.
§

type IntoIter = Iter<'a, K, Vec<V>>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> IterAll<'a, K, Vec<V>>

Creates an iterator from a value. Read more
source§

impl<'a, K, V, S> IntoIterator for &'a mut MultiMap<K, V, S>
where K: Eq + Hash, S: BuildHasher,

§

type Item = (&'a K, &'a mut Vec<V>)

The type of the elements being iterated over.
§

type IntoIter = IterMut<'a, K, Vec<V>>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> IterAllMut<'a, K, Vec<V>>

Creates an iterator from a value. Read more
source§

impl<K, V, S> IntoIterator for MultiMap<K, V, S>
where K: Eq + Hash, S: BuildHasher,

§

type Item = (K, Vec<V>)

The type of the elements being iterated over.
§

type IntoIter = IntoIter<K, Vec<V>>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> IntoIter<K, Vec<V>>

Creates an iterator from a value. Read more
source§

impl<K, V, S> PartialEq for MultiMap<K, V, S>
where K: Eq + Hash, V: PartialEq, S: BuildHasher,

source§

fn eq(&self, other: &MultiMap<K, V, S>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<K, V, BS> Serialize for MultiMap<K, V, BS>
where K: Serialize + Eq + Hash, V: Serialize, BS: BuildHasher,

source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl<K, V, S> Eq for MultiMap<K, V, S>
where K: Eq + Hash, V: Eq, S: BuildHasher,

Auto Trait Implementations§

§

impl<K, V, S> RefUnwindSafe for MultiMap<K, V, S>

§

impl<K, V, S> Send for MultiMap<K, V, S>
where K: Send, S: Send, V: Send,

§

impl<K, V, S> Sync for MultiMap<K, V, S>
where K: Sync, S: Sync, V: Sync,

§

impl<K, V, S> Unpin for MultiMap<K, V, S>
where K: Unpin, S: Unpin, V: Unpin,

§

impl<K, V, S> UnwindSafe for MultiMap<K, V, S>
where K: UnwindSafe, S: 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> 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,

§

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>,

§

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>,

§

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.
source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,