Struct std::hashmap::HashMap
pub struct HashMap<K, V> { // some fields omitted }
A hash map implementation which uses linear probing along with the SipHash hash function for internal state. This means that the order of all hash maps is randomized by keying each hash map randomly on creation.
It is required that the keys implement the Eq
and Hash
traits, although
this can frequently be achieved by just implementing the Eq
and
IterBytes
traits as Hash
is automatically implemented for types that
implement IterBytes
.
Fields
Methods
impl<K: Hash + Eq, V> HashMap<K, V>
fn new() -> HashMap<K, V>
Create an empty HashMap
fn with_capacity(capacity: uint) -> HashMap<K, V>
Create an empty HashMap with space for at least capacity
elements in the hash table.
fn with_capacity_and_keys(k0: u64, k1: u64, capacity: uint) -> HashMap<K, V>
Create an empty HashMap with space for at least capacity
elements, using k0
and k1
as the keys.
Warning: k0
and k1
are normally randomly generated, and
are designed to allow HashMaps to be resistant to attacks that
cause many collisions and very poor performance. Setting them
manually using this function can expose a DoS attack vector.
fn reserve_at_least(&mut self, n: uint)
Reserve space for at least n
elements in the hash table.
fn mangle<'a, A>(&'a mut self, k: K, a: A, not_found: |&K, A| -> V, found: |&K, &mut V, A|) -> &'a mut V
Modify and return the value corresponding to the key in the map, or insert and return a new value if it doesn't exist.
This method allows for all insertion behaviours of a hashmap,
see methods like insert
, find_or_insert
and
insert_or_update_with
for less general and more friendly
variations of this.
Example
use std::hashmap::HashMap;
// map some strings to vectors of strings
let mut map = HashMap::<~str, ~[~str]>::new();
map.insert(~"a key", ~[~"value"]);
map.insert(~"z key", ~[~"value"]);
let new = ~[~"a key", ~"b key", ~"z key"];
for k in new.move_iter() {
map.mangle(k, ~"new value",
// if the key doesn't exist in the map yet, add it in
// the obvious way.
|_k, v| ~[v],
// if the key does exist either prepend or append this
// new value based on the first letter of the key.
|key, already, new| {
if key.starts_with("z") {
already.unshift(new);
} else {
already.push(new);
}
});
}
for (k, v) in map.iter() {
println!("{} -> {:?}", *k, *v);
}
fn find_or_insert<'a>(&'a mut self, k: K, v: V) -> &'a mut V
Return the value corresponding to the key in the map, or insert and return the value if it doesn't exist.
fn find_or_insert_with<'a>(&'a mut self, k: K, f: |&K| -> V) -> &'a mut V
Return the value corresponding to the key in the map, or create, insert, and return a new value if it doesn't exist.
fn insert_or_update_with<'a>(&'a mut self, k: K, v: V, f: |&K, &mut V|) -> &'a mut V
Insert a key-value pair into the map if the key is not already present. Otherwise, modify the existing value for the key. Returns the new or modified value for the key.
fn get<'a>(&'a self, k: &K) -> &'a V
Retrieves a value for the given key, failing if the key is not present.
fn get_mut<'a>(&'a mut self, k: &K) -> &'a mut V
Retrieves a (mutable) value for the given key, failing if the key is not present.
fn contains_key_equiv<Q: Hash + Equiv<K>>(&self, key: &Q) -> bool
Return true if the map contains a value for the specified key, using equivalence
fn find_equiv<'a, Q: Hash + Equiv<K>>(&'a self, k: &Q) -> Option<&'a V>
Return the value corresponding to the key in the map, using equivalence
fn keys<'a>(&'a self) -> HashMapKeyIterator<'a, K, V>
An iterator visiting all keys in arbitrary order. Iterator element type is &'a K.
fn values<'a>(&'a self) -> HashMapValueIterator<'a, K, V>
An iterator visiting all values in arbitrary order. Iterator element type is &'a V.
fn iter<'a>(&'a self) -> HashMapIterator<'a, K, V>
An iterator visiting all key-value pairs in arbitrary order. Iterator element type is (&'a K, &'a V).
fn mut_iter<'a>(&'a mut self) -> HashMapMutIterator<'a, K, V>
An iterator visiting all key-value pairs in arbitrary order, with mutable references to the values. Iterator element type is (&'a K, &'a mut V).
fn move_iter(self) -> HashMapMoveIterator<K, V>
Creates a consuming iterator, that is, one that moves each key-value pair out of the map in arbitrary order. The map cannot be used after calling this.
impl<K: Hash + Eq, V: Clone> HashMap<K, V>
fn find_copy(&self, k: &K) -> Option<V>
Like find
, but returns a copy of the value.
fn get_copy(&self, k: &K) -> V
Like get
, but returns a copy of the value.
Trait Implementations
impl<A: ToStr + Hash + Eq, B: ToStr> ToStr for HashMap<A, B>
fn to_str(&self) -> ~str
Converts the value of self
to an owned string
impl<K: Hash + Eq, V> Container for HashMap<K, V>
fn len(&self) -> uint
Return the number of elements in the map
fn is_empty(&self) -> bool
Return true if the container contains no elements
impl<K: Hash + Eq, V> Mutable for HashMap<K, V>
fn clear(&mut self)
Clear the map, removing all key-value pairs.
impl<K: Hash + Eq, V> Map<K, V> for HashMap<K, V>
fn find<'a>(&'a self, k: &K) -> Option<&'a V>
Return a reference to the value corresponding to the key
fn contains_key(&self, key: &K) -> bool
Return true if the map contains a value for the specified key
impl<K: Hash + Eq, V> MutableMap<K, V> for HashMap<K, V>
fn find_mut<'a>(&'a mut self, k: &K) -> Option<&'a mut V>
Return a mutable reference to the value corresponding to the key
fn swap(&mut self, k: K, v: V) -> Option<V>
Insert a key-value pair from the map. If the key already had a value present in the map, that value is returned. Otherwise None is returned.
fn pop(&mut self, k: &K) -> Option<V>
Removes a key from the map, returning the value at the key if the key was previously in the map.
fn insert(&mut self, key: K, value: V) -> bool
Insert a key-value pair into the map. An existing value for a key is replaced by the new value. Return true if the key did not already exist in the map.
fn remove(&mut self, key: &K) -> bool
Remove a key-value pair from the map. Return true if the key was present in the map, otherwise false.
impl<K: Hash + Eq, V: Eq> Eq for HashMap<K, V>
impl<K: Hash + Eq + Clone, V: Clone> Clone for HashMap<K, V>
fn clone(&self) -> HashMap<K, V>
Returns a copy of the value. The contents of owned pointers are copied to maintain uniqueness, while the contents of managed pointers are not copied.
fn clone_from(&mut self, source: &Self)
Perform copy-assignment from source
.
a.clone_from(&b)
is equivalent to a = b.clone()
in functionality,
but can be overridden to reuse the resources of a
to avoid unnecessary
allocations.
impl<K: Eq + Hash, V> FromIterator<(K, V)> for HashMap<K, V>
fn from_iterator<T: Iterator<(K, V)>>(iter: &mut T) -> HashMap<K, V>
Build a container with elements from an external iterator.
impl<K: Eq + Hash, V> Extendable<(K, V)> for HashMap<K, V>
fn extend<T: Iterator<(K, V)>>(&mut self, iter: &mut T)
Extend a container with the elements yielded by an iterator
impl<K: Eq + Hash, V> Default for HashMap<K, V>
fn default() -> HashMap<K, V>
Return the "default value" for a type.