Hash_Table module

table_add

procedure · 1 overloadSource

Adds an entry, even when key is already present, and returns a pointer to the stored value.

Because duplicates are allowed, a later table_find returns whichever entry the probe reaches first; use table_set when a key must stay unique. Grows the table when needed, which invalidates earlier pointers. The returned pointer is good until the table next grows.

ExampleOpen in playground ↗
table: Table(int, string);
slot := table_add(*table, 1, "one");
slot.* = "uno";
found, value := table_find(*table, 1);
assert(found && value == "uno");
deinit(*table);
table_add :: (
    table: *Table,
    key: table.Key_Type,
    value: table.Value_Type
) -> *table.Value_Type

Adds an entry, even when key is already present, and returns a pointer to the stored value.

Because duplicates are allowed, a later table_find returns whichever entry the probe reaches first; use table_set when a key must stay unique. Grows the table when needed, which invalidates earlier pointers. The returned pointer is good until the table next grows.

Parameters

table: *Table
key: table.Key_Type
value: table.Value_Type

Returns

*table.Value_Type
ExampleOpen in playground ↗
table: Table(int, string);
slot := table_add(*table, 1, "one");
slot.* = "uno";
found, value := table_find(*table, 1);
assert(found && value == "uno");
deinit(*table);

See also