Struct multimap::MultiMap [] [src]

pub struct MultiMap<K, V> {
    // some fields omitted
}

A MultiMap implementation which is just a wrapper around std::collections::HashMap. See HashMap's documentation for more details.

Some of the methods are just thin wrappers, some methods does change a little semantics and some methods are new (doesn't have an equivalent in HashMap.)

The MultiMap is generic for the key (K) and the value (V). Internally the values are stored in a generic Vector.

Examples

use multimap::MultiMap;

// create a new MultiMap. An explicit type signature can be omitted because of the
// type inference.
let mut queries = MultiMap::new();

// insert some queries.
queries.insert("urls", "http://rust-lang.org");
queries.insert("urls", "http://mozilla.org");
queries.insert("urls", "http://wikipedia.org");
queries.insert("id", "42");
queries.insert("name", "roger");

// check if there's any urls.
println!("Are there any urls in the multimap? {:?}.",
    if queries.contains_key(&("urls")) {"Yes"} else {"No"} );

// get the first item in a key's vector.
assert_eq!(queries.get(&("urls")), Some(&("http://rust-lang.org")));

// get all the urls.
assert_eq!(queries.get_vec(&("urls")),
    Some(&vec!["http://rust-lang.org", "http://mozilla.org", "http://wikipedia.org"]));

// iterate over all keys and the first value in the key's vector.
for (key, value) in queries.iter() {
    println!("key: {:?}, val: {:?}", key, value);
}

// iterate over all keys and the key's vector.
for (key, values) in queries.iter_all() {
    println!("key: {:?}, values: {:?}", key, values);
}

// the different methods for getting value(s) from the multimap.
let mut map = MultiMap::new();

map.insert("key1", 42);
map.insert("key1", 1337);

assert_eq!(map[&("key1")], 42);
assert_eq!(map.get(&("key1")), Some(&42));
assert_eq!(map.get_vec(&("key1")), Some(&vec![42, 1337]));

Methods

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

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

Creates an empty MultiMap

Examples

use multimap::MultiMap;

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

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

Inserts a key-value pair into the multimap. If the key does exists in the map then the key is pushed to that key's vector. If the key doesn't exists 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);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

fn keys<'a>(&'a self) -> Keys<'a, 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(2,1337);
map.insert(4,1991);

for key in map.keys() {
    println!("{:?}", key);
}

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

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

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

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

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

for (key, values) in map.iter_all() {
    println!("key: {:?}, values: {:?}", key, values);
}

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

for (key, values) in map.iter_all() {
    println!("key: {:?}, values: {:?}", key, values);
}

Trait Implementations

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

type Output = V

The returned type after indexing

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

The method for the indexing (Foo[Bar]) operation