v0.4.0

HashMap<K, V> structv0.1.0

Source
A hash table from keys to values.
pub struct HashMap<K, V> {
    // private fields
}

Move-only: it owns its entries and storage and prohibits copying.

Type parameters

NameDescription
Kthe key type
Vthe 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.

NameTypeDescription
allocatorAllocatorwhere the storage comes from
hashfunc(K, uint64, uint64) -> uint64how a key is hashed, taking the key and the two seed words
equalsfunc(K, K) -> boolwhether two keys are the same

Returns: an empty collection that has taken no storage yet

Source

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.

NameTypeDescription
allocatorAllocatorwhere the storage comes from
seedTableSeedthe two words that key the hash, which decide the iteration order
hashfunc(K, uint64, uint64) -> uint64how a key is hashed, taking the key and the two seed words
equalsfunc(K, K) -> boolwhether two keys are the same

Returns: an empty map keyed with that seed

Source

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.

NameTypeDescription
allocatorAllocatorwhere the storage comes from
capacityuinthow many elements to make room for
hashfunc(K, uint64, uint64) -> uint64how a key is hashed, taking the key and the two seed words
equalsfunc(K, K) -> boolwhether two keys are the same

Returns: an empty map with room for that many entries already taken

Source

Methods

Length

pub func Length(&self) -> uint

How many entries the map holds.

Returns: how many entries it holds

Source

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

Source

IsEmpty

pub func IsEmpty(&self) -> bool

Whether the map holds no entries.

Returns: whether it holds nothing

Source

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.

NameTypeDescription
keyKthe key to look up

Returns: whether an entry with that key is present

Source

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.

NameTypeDescription
keyKthe key to look up

Returns: a copy of the value, or none when the key is absent

Source

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.

NameTypeDescription
keyKthe key to look up

Returns: a reference to the value, or none when the key is absent

Source

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.

NameTypeDescription
keyKthe key to look up

Returns: a writable reference to the value, or none when the key is absent

Source

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.

NameTypeDescription
keyKthe key to look up
valueVthe value to act on

Source

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.

NameTypeDescription
keyKthe key to look up
valueVthe value to act on

Returns: nothing, or throws the allocator's refusal

Source

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.

NameTypeDescription
keyKthe key to look up

Returns: the value that was stored, or none when the key was absent

Source

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.

NameTypeDescription
additionaluinthow many more elements to make room for

Source

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.

NameTypeDescription
additionaluinthow many more elements to make room for

Returns: nothing, or throws the allocator's refusal

Source

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.

Source

Clear

pub func Clear(&var self)

Destroys every entry, keeping the storage. self is borrowed mutably. Runs in time proportional to the capacity.

Source

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.

NameTypeDescription
cursor&var uintthe position to resume from, advanced past what was read

Returns: the next entry, or none once every slot has been looked at

Source

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.

NameTypeDescription
keyKthe key to look up
valueVthe value stored when the key is absent

Returns: the value stored under key, borrowed for writing

Source

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.

NameTypeDescription
keyKthe key to look up
valueVthe value to act on

Returns: the value that was there, or none when the key was absent

Source

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.

NameTypeDescription
keyKthe key to look up

Returns: the whole entry, or none when the key was absent

Source

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

Source

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

Source

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.

Source

Type parameters

NameDescription
Kthe key type
Vthe 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

Source

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.

Source

Type parameters

NameDescription
Kthe key type
Vthe 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

Source

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.

Source

Type parameters

NameDescription
K: Debugthe key type, which must know how to show itself
V: Debugthe value type, which must know how to show itself

Parameters

NameTypeDescription
writer&var TextWriterthe destination
value&HashMap<K, V>the map to show, borrowed for the call
specFormatSpecwhat to show each key and value under

Returns: success when the whole rendering was accepted