Class in collections::map coil-stdlib

HashMap

class HashMap<K, V>
Import
use collections::map::{HashMap};
Source: src/collections/map.hy

A hash map from keys of type K to values of type V.

Keys need Eq and Hash (the primitive types have both; #[derive(Eq, Hash)] adds them to your own types). Inserting an existing key replaces its value. Iteration (for e in m, m.keys(), m.values()) visits entries in insertion order, skipping removed ones.

get takes a fallback instead of returning an Option; use contains first when the fallback could be a real value.

Fields#

FieldTypeDescription
keysVec<K>Key slots, including removed ones (see live). Prefer keys().
valsVec<V>Value slots parallel to keys. Prefer values().
liveVec<int>1 where the slot holds an entry, 0 where it was removed.

Associated functions#

NameDescription
with_capacityCreates an empty map with room for about cap buckets before it grows.
newCreates an empty map.

Methods#

NameDescription
sizeReturns the number of entries.
is_emptyReturns true when the map has no entries.
capacityReturns the number of buckets.
clearRemoves every entry, keeping the bucket count.
keysReturns the keys, in insertion order.
valuesReturns the values, in the insertion order of their keys.
iterReturns an iterator over the entries, in insertion order.
insertStores v under k, replacing any value already there.
containsReturns true when the map has an entry for k.
get_orReturns the value stored under k, or fallback when there is none.
getReturns the value stored under k, or None when there is none.
removeRemoves the entry for k.

Trait implementations#

Invariants#

Checked according to --contracts (see Contracts).

invariant self.len >= 0 && self.len <= self.keys.len()
invariant self.cap >= 8 && self.heads.len() == self.cap
invariant self.vals.len() == self.keys.len() && self.next.len() == self.keys.len()
invariant self.live.len() == self.keys.len()

Examples#

use collections::map::{HashMap};
use tests::assert;
let ages: HashMap<string, int> = HashMap::new();
ages.insert("ada", 36);
ages.insert("alan", 41);
ages.insert("ada", 37);
assert!(ages.size() == 2);
assert!(ages.get_or("ada", 0) == 37);
assert!(ages.get_or("grace", -1) == -1);

let total = 0;
for e in ages {
    total = total + e.value;
}
assert!(total == 78);

See also#

↑ ↓ navigate ↵ open