← Standard library

Dictionary

sealed class Dictionary<K, V> where K is Hashable
import Dictionary from Wax;

A mutable mapping from keys to values. Keys satisfy Hashable: equal keys must have equal hashes. Do not change a stored key's hash or equality behavior while it is in the dictionary. Assignment inserts or replaces; indexed reads throw ItemNotFoundError for absent keys. TryGetValue provides a lookup without that error and distinguishes absence from a stored null. Key and value arrays are snapshots in unspecified table order, not insertion order.

This example returns 1: the key exists even though its value is null.

import Dictionary from Wax;
api fn Main() : int32 {
    var names = new Dictionary<int32, string?>();
    names[7] = null;
    string? name;
    bool found = names.TryGetValue(7, out name);
    return found && name == null ? 1 : 0;
}

Dictionary.wax:45

Inherits from object. Follow these links for inherited members.

Members

constructorconstructor

public constructor ()

Creates an empty dictionary with no allocated entry capacity. The first insertion allocates storage.

Dictionary.wax:60

WithCapacityconstructor

public constructor WithCapacity (int32 capacity)

Creates an empty dictionary and reserves room for the requested number of entries. The reported slot capacity can exceed the request. Reserving capacity does not insert entries.

Dictionary.wax:75

sizegetter

public get size() : int32

The number of live key/value entries. Replacing an existing value leaves this count unchanged.

Dictionary.wax:86

capacitygetter

public get capacity() : int32

The number of allocated hash-table slots, not the number of entries that can be inserted before growth. Some slots are kept free for probing.

Dictionary.wax:94

EnsureCapacitymethod

public fn EnsureCapacity(int32 capacity) : void

Reserves room for at least the requested total entry count without removing entries or shrinking storage. Does not change size. A reservation on a newly empty dictionary allocates a minimum table even for a zero request.

Dictionary.wax:177

EnsureAdditionalCapacitymethod

public fn EnsureAdditionalCapacity(int32 additional) : void

Reserves room for size + additional entries without changing size. Expresses an additional entry count rather than a total capacity.

Dictionary.wax:188

thisindexer-getter

public get this(K key) : V throws ItemNotFoundError

Looks up a key using its hash and Equals. Throws ItemNotFoundError when absent. Use TryGetValue for lookup without a missing-key error.

Dictionary.wax:196

thisindexer-setter

public set this(K key, V value) : void

Inserts a missing key or replaces the value for an equal existing key. Replacement preserves size and the original stored key. May grow or rebuild the table.

Dictionary.wax:219

TryGetValuemethod

public fn TryGetValue(K key, out V? value) : bool

Returns true and writes the stored value when the key exists, including a stored null. Returns false and writes null when absent. The boolean distinguishes missing keys from present null values.

Dictionary.wax:258

GetValuemethod

public fn GetValue(K key) : V?

Returns the stored value, or null when the key is absent. A stored null also returns null; use TryGetValue or Has to distinguish those cases.

Dictionary.wax:285

Hasmethod

public fn Has(K key) : bool

Returns true when an equal key exists, even if its stored value is null. An empty dictionary returns false.

Dictionary.wax:294

Removemethod

public fn Remove(K key) : bool

Removes an equal key and returns true, or returns false if absent. Does not shrink capacity. Removed key/value references can remain retained until the next table rebuild or Clear.

Dictionary.wax:320

Clearmethod

public fn Clear() : void

Removes every entry and releases the dictionary's stored key/value references while retaining the allocated slot capacity. Does not mutate the objects previously referenced by entries.

Dictionary.wax:351

GetValueOrDefaultmethod

public fn GetValueOrDefault(K key, V defaultValue) : V

Returns the stored value when the key exists, otherwise defaultValue. A present null value is returned as null, not replaced by the default.

Dictionary.wax:367

ContainsValuemethod

public fn ContainsValue(V value) : bool where V is Equatable

Scans live entries and tests values with Equals. Returns false when empty. Unlike key lookup, this scans the allocated table rather than using a value hash index.

Dictionary.wax:391

Keysmethod

public fn Keys() : K[]

Returns a new dense array of live keys in unspecified table order. This is a snapshot: later dictionary edits do not change the array. Reference keys still refer to the same objects. With no intervening mutation, its order matches Values.

Dictionary.wax:407

Valuesmethod

public fn Values() : V[]

Returns a new dense array of live values in unspecified table order, including duplicates. Later dictionary edits do not change the array; reference values still refer to the same objects. With no intervening mutation, its order matches Keys.

Dictionary.wax:429