HashMap<K, V> structv0.1.0
pub struct HashMap<K, V> {
// private fields
}
Move-only: it owns its entries and storage and prohibits copying.
Type parameters
| Name | Description |
|---|---|
K | the key type |
V | the value type |
Constructors
HashMap
pub func HashMap(
allocator: Allocator,
hash: func(K, uint64, uint64) -> uint64,
equals: func(K, K) -> bool
) -> HashMap<K, V>
An empty map that has not allocated anything, with a seed drawn from the operating system.
allocator is borrowed for the call and kept. A program making many maps should draw one seed with
TableSeed::Random and use WithSeed, since each draw here is a system call.
| Name | Type | Description |
|---|---|---|
allocator | Allocator | where the storage comes from |
hash | func(K, uint64, uint64) -> uint64 | how a key is hashed, taking the key and the two seed words |
equals | func(K, K) -> bool | whether two keys are the same |
Returns: an empty collection that has taken no storage yet
Associated functions
WithSeed
pub func WithSeed(
allocator: Allocator,
seed: TableSeed,
hash: func(K, uint64, uint64) -> uint64,
equals: func(K, K) -> bool
) -> HashMap<K, V>
An empty map that has not allocated anything, hashing with seed.
allocator is borrowed for the call and kept; seed is read by value. Two maps sharing a seed hash alike,
which is what a caller drawing one seed for many maps wants and what a test needing the same table twice
needs.
| Name | Type | Description |
|---|---|---|
allocator | Allocator | where the storage comes from |
seed | TableSeed | the two words that key the hash, which decide the iteration order |
hash | func(K, uint64, uint64) -> uint64 | how a key is hashed, taking the key and the two seed words |
equals | func(K, K) -> bool | whether two keys are the same |
Returns: an empty map keyed with that seed
WithCapacity
pub func WithCapacity(
allocator: Allocator,
capacity: uint,
hash: func(K, uint64, uint64) -> uint64,
equals: func(K, K) -> bool
) -> HashMap<K, V>
An empty map with room for capacity entries already taken.
The capacity actually taken is rounded up to a power of two above the load factor, so it is larger than asked
for. Terminates the program when the storage cannot be had.
| Name | Type | Description |
|---|---|---|
allocator | Allocator | where the storage comes from |
capacity | uint | how many elements to make room for |
hash | func(K, uint64, uint64) -> uint64 | how a key is hashed, taking the key and the two seed words |
equals | func(K, K) -> bool | whether two keys are the same |
Returns: an empty map with room for that many entries already taken
Methods
Length
pub func Length(&self) -> uint
How many entries the map holds.
Returns: how many entries it holds
Capacity
pub func Capacity(&self) -> uint
How many slots the table has. Not how many entries fit: a quarter of the slots are kept free, because a table run fuller spends its time displacing entries rather than storing them.
Returns: how many entries fit before the next growth
IsEmpty
pub func IsEmpty(&self) -> bool
Whether the map holds no entries.
Returns: whether it holds nothing
ContainsKey
pub func ContainsKey(&self, key: K) -> bool
Whether the map has an entry for key.
self is borrowed; key is read by value.
Runs in constant expected time and allocates nothing.
| Name | Type | Description |
|---|---|---|
key | K | the key to look up |
Returns: whether an entry with that key is present
Get
pub func Get(&self, key: K) -> V?
The value stored under key, or none when there is no such entry.
self is borrowed; the value is returned by copy.
| Name | Type | Description |
|---|---|---|
key | K | the key to look up |
Returns: a copy of the value, or none when the key is absent
Find
pub func Find(&self, key: K) -> &V?
The value stored under key, borrowed where it sits, or none when there is no such entry.
self stays borrowed while the reference is in use, which is how a value that owns something is read without
being taken, and nothing can be inserted or removed under it.
| Name | Type | Description |
|---|---|---|
key | K | the key to look up |
Returns: a reference to the value, or none when the key is absent
FindMutable
pub func FindMutable(&var self, key: K) -> &var V?
The value stored under key, borrowed to be changed in place, or none when there is no such entry.
self stays borrowed exclusively while the reference is in use. Assigning through it replaces the value and
destroys the one that was there.
| Name | Type | Description |
|---|---|---|
key | K | the key to look up |
Returns: a writable reference to the value, or none when the key is absent
Insert
pub func Insert(&var self, key: K, value: V)
Stores value under key, replacing whatever was there.
self is borrowed mutably; both are moved in. A key already present keeps its stored key and takes the new
value, and the value it had is destroyed — use Remove first where the old one is wanted.
Terminates the program when the table cannot grow; TryInsert reports that instead.
Runs in constant expected time.
| Name | Type | Description |
|---|---|---|
key | K | the key to look up |
value | V | the value to act on |
TryInsert
pub func TryInsert(
&var self,
key: K,
value: V
) throws AllocError
Stores value under key, replacing whatever was there, or throws why the table could not grow.
self is borrowed mutably; both are moved in, and stored exactly as Insert stores them. A failure leaves
the map exactly as it was, and both arguments are then destroyed here.
Runs in constant expected time.
| Name | Type | Description |
|---|---|---|
key | K | the key to look up |
value | V | the value to act on |
Returns: nothing, or throws the allocator's refusal
Remove
pub func Remove(&var self, key: K) -> V?
Takes the entry for key out and hands back its value.
self is borrowed mutably. The value is moved out rather than destroyed, so one that owns something leaves
intact; the key is destroyed, having no other owner. Reports none when there is no such entry.
The gap closes behind the entry, so the table is left exactly as if it had never been inserted.
Runs in constant expected time.
| Name | Type | Description |
|---|---|---|
key | K | the key to look up |
Returns: the value that was stored, or none when the key was absent
Reserve
pub func Reserve(&var self, additional: uint)
Makes room for additional more entries on top of those already present.
self is borrowed mutably; the length does not change. Terminates the program when the storage cannot be
had; TryReserve reports that instead.
| Name | Type | Description |
|---|---|---|
additional | uint | how many more elements to make room for |
TryReserve
pub func TryReserve(
&var self,
additional: uint
) throws AllocError
Makes room for additional more entries on top of those already present, or throws why it could not.
self is borrowed mutably; the length does not change. A failure leaves the map exactly as it was.
| Name | Type | Description |
|---|---|---|
additional | uint | how many more elements to make room for |
Returns: nothing, or throws the allocator's refusal
ShrinkToFit
pub func ShrinkToFit(&var self)
Releases the capacity the entry count does not need.
self is borrowed mutably. An empty map gives up its storage entirely. Terminates the program when the
allocator refuses the smaller table.
Clear
pub func Clear(&var self)
Destroys every entry, keeping the storage.
self is borrowed mutably. Runs in time proportional to the capacity.
NextEntry
pub func NextEntry(
&self,
cursor: &var uint
) -> KeyValue<K, V>?
The entry in the next occupied slot at or after cursor, or none at the end.
self is borrowed; cursor is advanced past whatever is reported, so a caller starts it at zero and keeps
calling. The key and value are copies. The order is the table's, which is the seed's and not the insertion's.
Nothing may be inserted or removed while a walk is in progress: either moves entries between slots, and a
cursor is a slot.
| Name | Type | Description |
|---|---|---|
cursor | &var uint | the position to resume from, advanced past what was read |
Returns: the next entry, or none once every slot has been looked at
GetOrInsert
pub func GetOrInsert(&var self, key: K, value: V) -> &var V
The value stored under key, inserting value first when there is none.
self is borrowed mutably and stays borrowed while the reference is in use; key and value are moved in
only when there was no entry, and destroyed otherwise — the map keeps what it already had. A caller that needs
to know whether the map grew compares Length before and after.
This is what an entry API is for, without pretending Rux has closures: a caller that wants to read a value or
put one there gets one operation instead of a lookup followed by an insertion, and does not have to name the
key twice.
Terminates the program when the table cannot grow.
| Name | Type | Description |
|---|---|---|
key | K | the key to look up |
value | V | the value stored when the key is absent |
Returns: the value stored under key, borrowed for writing
Replace
pub func Replace(&var self, key: K, value: V) -> V?
Stores value under key and hands back the value that was there.
self is borrowed mutably; both are moved in. The old value is moved out rather than destroyed, which is the
difference between this and Insert: one hands the previous value back and the other throws it away.
Reports none when there was no entry, having inserted this one.
Terminates the program when the table cannot grow.
| Name | Type | Description |
|---|---|---|
key | K | the key to look up |
value | V | the value to act on |
Returns: the value that was there, or none when the key was absent
RemoveEntry
pub func RemoveEntry(&var self, key: K) -> KeyValue<K, V>?
Takes the entry for key out and hands back both halves of it.
self is borrowed mutably. Both the stored key and the value are moved out, so a key that owns something
leaves intact too — which Remove does not offer, since it destroys the key. Reports none when there is
no such entry.
The stored key is handed back rather than the one that was looked up: the two compare equal, but a key type
carrying anything the equality ignores makes them different values, and the map's is the one that was kept.
| Name | Type | Description |
|---|---|---|
key | K | the key to look up |
Returns: the whole entry, or none when the key was absent
Iterate
pub func Iterate(&self) -> HashMapIterator<K, V>
An iterator over the entries, by copy.
self is borrowed and must outlive the iterator. This is the method a for loop finds. The order is the
table's, which is the seed's and not the insertion's.
Nothing may be inserted or removed while the iterator is alive.
Returns: a cursor over the entries, in an order the seed decides
IntoIterator
pub func IntoIterator(self) -> HashMapIntoIterator<K, V>
Takes the map apart into an iterator over its entries. Consumes the map: it is moved into the iterator, and the storage goes with it, so nothing is copied and nothing is reallocated. Each entry comes out by value, which is how one whose key or value owns something leaves. Whatever is not taken is destroyed when the iterator's life ends.
Returns: a cursor that consumes the map as it goes
Related
HashMapIterator<K, V>
pub struct HashMapIterator<K, V> {
// private fields
}
An iterator over the entries of a map, by copy. Borrows the map, so the map must outlive it and must not be inserted into or removed from while it is alive: either moves entries between slots, and this walks slots.
Type parameters
| Name | Description |
|---|---|
K | the key type |
V | the value type |
Implements Iterator
HashMapIterator<K, V> conforms to Iterator.
Next
pub func Next(&var self) -> KeyValue<K, V>?
The next entry, or none once every slot has been looked at.
self is borrowed mutably and advanced. The key and value are copies.
Returns: the next entry, or none at the end
HashMapIntoIterator<K, V>
pub struct HashMapIntoIterator<K, V> {
// private fields
}
An iterator that takes a map apart, yielding each entry by value. Move-only: it owns the storage the map owned. Whatever is left when its life ends is destroyed with it, which is what makes stopping part-way safe.
Type parameters
| Name | Description |
|---|---|
K | the key type |
V | the value type |
Implements Iterator
HashMapIntoIterator<K, V> conforms to Iterator.
Next
pub func Next(&var self) -> KeyValue<K, V>?
The next entry, moved out, or none once every slot has been looked at.
self is borrowed mutably and advanced. The slot is emptied as the entry leaves, so what remains for the
destructor is exactly what nobody took.
Returns: the next entry, or none at the end
WriteHashMapDebug<K, V>
pub func WriteHashMapDebug<K: Debug, V: Debug>(
writer: &var TextWriter,
value: &HashMap<K, V>,
spec: FormatSpec
) throws FormatError
Writes a borrowed hash map as {key: value, key: value}.
An empty map is {}. Braces rather than brackets, because a map is not a run of elements and reading its
rendering as one would be reading it wrongly.
The order is the table's, which follows from the seed the map was made with and from every insertion and removal
since — so it is not the insertion order, it does not hold between two maps with the same entries, and it changes
between runs of the same program. Nothing here sorts, since that would need an ordering on the key that a hash
map does not have. TreeMap is the container whose rendering is stable.
Every key and value is shown through a borrow of it, so a map of move-only entries is shown without taking one
out. spec is handed to each of them unchanged.
Reports whatever the writer or an entry's implementation reported, stopping at the first refusal.
Type parameters
| Name | Description |
|---|---|
K: Debug | the key type, which must know how to show itself |
V: Debug | the value type, which must know how to show itself |
Parameters
| Name | Type | Description |
|---|---|---|
writer | &var TextWriter | the destination |
value | &HashMap<K, V> | the map to show, borrowed for the call |
spec | FormatSpec | what to show each key and value under |
Returns: success when the whole rendering was accepted