Dictionary
sealed class Dictionary<K, V> where K is Hashableimport 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;
}
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.
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.
sizegetter
public get size() : int32The number of live key/value entries. Replacing an existing value leaves this count unchanged.
capacitygetter
public get capacity() : int32The number of allocated hash-table slots, not the number of entries that can be inserted before growth. Some slots are kept free for probing.
EnsureCapacitymethod
public fn EnsureCapacity(int32 capacity) : voidReserves 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.
EnsureAdditionalCapacitymethod
public fn EnsureAdditionalCapacity(int32 additional) : voidReserves room for size + additional entries without changing size. Expresses an
additional entry count rather than a total capacity.
thisindexer-getter
public get this(K key) : V throws ItemNotFoundErrorLooks up a key using its hash and Equals. Throws ItemNotFoundError when absent. Use
TryGetValue for lookup without a missing-key error.
thisindexer-setter
public set this(K key, V value) : voidInserts 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.
TryGetValuemethod
public fn TryGetValue(K key, out V? value) : boolReturns 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.
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.
Hasmethod
public fn Has(K key) : boolReturns true when an equal key exists, even if its stored value is null. An empty dictionary returns false.
Removemethod
public fn Remove(K key) : boolRemoves 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.
Clearmethod
public fn Clear() : voidRemoves 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.
GetValueOrDefaultmethod
public fn GetValueOrDefault(K key, V defaultValue) : VReturns the stored value when the key exists, otherwise defaultValue. A present null value
is returned as null, not replaced by the default.
ContainsValuemethod
public fn ContainsValue(V value) : bool where V is EquatableScans 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.
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.
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.