libstd/dict

Hash tables keyed by str (word-at-a-time hash cached per slot, linear probing, tombstones), in four flavours: dict_* (int values), sdict_* (str values), vdict_* (tagged int/long/str values) and adict_* (boxed values of any type, including nested dicts); idict_* has int keys and str values, and is what a literal like {200: "OK"} builds. The handle is a str. See Lists & dicts.

#include <libstd/dict>

Includes libstd/string. Same functions and constant names on Linux and Windows: the system-specific part is in wrapper/linux/dict and wrapper/windows/dict (see Linux and Windows).

Example

#include <libstd/stdio>
#include <libstd/dict>

ages = dict_new(8)
dict_set(ages, "anna", 30)
if dict_has(ages, "anna"):
    print(dict_get(ages, "anna"), "\n")

names = sdict_new(8)
sdict_set(names, "it", "ciao")
it = dict_iter_new(names)
while dict_iter_has_next(it):
    k, v = dict_iter_next(it)
    print(k, " -> ", v, "\n")

Constants

NameLinuxWindowsMeaning
SDICT_TABLE_TYPE_ID-716025276-716025276
DICT_TABLE_TYPE_ID-716025275-716025275
IDICT_TABLE_TYPE_ID-716025274-716025274

Classes

class
DictIterator

Cursor over the live entries of an sdict; see dict_iter_new.

    d str
    idx int
    cap int
class
Sdict

An sdict inside an object, with methods (d.set, d.get, d.has, d.remove, d.count) and a cursor of its own (d.reset, d.next, d.key, d.value). A growing table only changes the field table, on which the sdict_* functions still work. Create it with Sdict_new, which allocates the table (new Sdict() alone has the methods but no table). See Lists & dicts.

    table str
    iter_idx int
    iter_key str
    iter_value str
    set &Sdict_set
    get &Sdict_get
    has &Sdict_has
    remove &Sdict_remove
    count &Sdict_count
    reset &Sdict_reset
    next &Sdict_next
    key &Sdict_key
    value &Sdict_value

Functions

def dict_peek64(addr: str) -> str

Reads 8 bytes at addr as a str (pointer). Raw memory access.

def dict_poke64(addr: str, val: str) -> int

Writes 8 bytes at addr. The value becomes immortal (the as long inside pins it), since nothing can follow a pointer in raw memory. Always returns 0.

def dict_poke64_raw(addr: str, val: str) -> int

Like dict_poke64 without the pin: the caller keeps the value alive by counting the reference itself (rt_rc_retain/rt_rc_release, as sdict_set does).

def dict_peek32(addr: str) -> int

Reads a sign-extended 4-byte integer at addr.

def dict_poke32(addr: str, val: int) -> int

Writes a 4-byte integer at addr.

def dict_next_pow2(n: int) -> int

The next power of two ≥ n (minimum 1); table capacities are always powers of two so the index is a mask, not a modulo.

def dict_new(capacity: int) -> str

A new str → int dict with a fixed capacity, rounded up to a power of two. The buffer outlives the caller's scope.

def dict_capacity(d: str) -> int

Total number of slots.

def dict_count(d: str) -> int

Number of occupied entries.

def dict_set(d: str, key: str, value: int) -> int

Inserts or updates key → value. Returns 1, or 0 when the table is full.

def dict_get(d: str, key: str) -> int

The value for key, or -1 if absent.

def dict_has(d: str, key: str) -> int

1 if the key is present.

def dict_del(d: str, key: str) -> int

Removes a key (tombstone). Returns 1 if it was present.

def idict_grow(d: str) -> str

Doubles the capacity of an int dict, copying the live entries into a new buffer; returns the new handle.

def idict_set_grow(d: str, key: str, value: int) -> str

Like dict_set but doubles the table before it passes half full; returns the (possibly new) handle, which must be reassigned.

def sdict_new(capacity: int) -> str

A new str → str dict with a fixed capacity.

def sdict_capacity(d: str) -> int

Total number of slots.

def sdict_count(d: str) -> int

Number of occupied entries.

def sdict_set(d: str, key: str, value: str) -> int

Inserts or updates key → value. Returns 1, or 0 when the table is full. The dict holds a counted reference to the value.

def sdict_set_ref(d: str, key: str, value: ref) -> int

Like sdict_set with an object or an array as the value (a ref, counted like a str); read it back with sdict_get(d, key) as ClassName.

def sdict_set_grow_ref(d: str, key: str, value: ref) -> str

Like sdict_set_grow with a ref value.

def sdict_get(d: str, key: str) -> str

The value for key, or "" if absent (use sdict_has when "" can be a real value).

def sdict_has(d: str, key: str) -> int

1 if the key is present.

def sdict_del(d: str, key: str) -> int

Removes a key (tombstone). Returns 1 if it was present.

def sdict_grow(d: str) -> str

Doubles the capacity of an sdict; returns the new handle.

def sdict_set_grow(d: str, key: str, value: str) -> str

Like sdict_set but doubles the table before it passes half full; returns the (possibly new) handle, which must be reassigned.

def sdict_iter(d: str) -> (str[], str[])

All live entries as two parallel arrays (keys, values) in the same order.

def dict_iter_set(it: DictIterator, d: str) -> None

Resets an existing iterator to the beginning of d, without allocating.

def dict_iter_new(d: str) -> DictIterator

An iterator positioned before the first entry of an sdict.

def dict_iter_set_end(it: DictIterator, d: str) -> None

Puts an existing iterator on the last slot of d, for a backward scan with dict_iter_has_previous / dict_iter_previous.

def dict_iter_has_previous(it: DictIterator) -> int

1 if another live entry precedes, going backward; moves back past empty slots and tombstones without consuming the entry. Call before every dict_iter_previous, on an iterator placed with dict_iter_set_end.

def dict_iter_previous(it: DictIterator) -> (str, str)

The previous (key, value) pair, moving backward; undefined without a preceding dict_iter_has_previous that returned 1. Slots are in hash order, not insertion order, in both directions.

def dict_iter_has_next(it: DictIterator) -> int

1 if another live entry follows; advances past empty slots and tombstones without consuming the entry. Call before every dict_iter_next.

def dict_iter_next(it: DictIterator) -> (str, str)

The next (key, value) pair; undefined without a preceding dict_iter_has_next that returned 1.

def dict_iter_del_current(it: DictIterator) -> None

Deletes the pair most recently returned by dict_iter_next; safe during iteration.

def vdict_new(capacity: int) -> str

A new dict whose values are int, long or str, each tagged with its type.

def vdict_capacity(d: str) -> int

Total number of slots.

def vdict_count(d: str) -> int

Number of occupied entries.

def vdict_set_raw(d: str, key: str, value: str, tag: int) -> int

Insertion engine shared by the typed setters: stores an 8-byte carrier and its type tag.

def vdict_set_int(d: str, key: str, value: int) -> int

Inserts or updates an int value. Returns 1, or 0 when full.

def vdict_set_long(d: str, key: str, value: long) -> int

Inserts or updates a long value.

def vdict_set_str(d: str, key: str, value: str) -> int

Inserts or updates a str value.

def vdict_type_of(d: str, key: str) -> int

The type tag of the value for key: 1 int, 2 long, 3 str, 0 absent.

def vdict_has(d: str, key: str) -> int

1 if the key is present, whatever the value type.

def vdict_get_int(d: str, key: str) -> int

The value if it was stored as int, otherwise -1 (a type mismatch counts as absent).

def vdict_get_long(d: str, key: str) -> long

The value if it was stored as long, otherwise -1.

def vdict_get_str(d: str, key: str) -> str

The value if it was stored as str, otherwise "".

def vdict_del(d: str, key: str) -> int

Removes a key of any value type. Returns 1 if it was present.

def adict_new(capacity: int) -> str

A new dict whose values are boxes (see box_int and friends), so any type, including other dicts, can be stored.

def adict_set(d: str, key: str, boxed_value: str) -> int

Inserts or updates a boxed value. Returns 1, or 0 when full.

def adict_get(d: str, key: str) -> str

The box for key, or "" if absent. Check adict_has first: the sentinel is not a box.

def adict_has(d: str, key: str) -> int

1 if the key is present.

def adict_del(d: str, key: str) -> int

Removes a key (tombstone); the box itself is not freed.

def adict_count(d: str) -> int

Number of occupied entries.

def adict_type_id(boxed: str) -> int

The type id stored in a box: 1 int, 2 long, 3 str, 4 dict. Only valid on a real box.

def box_int(x: int) -> str

A new box holding an int.

def unbox_int(b: str) -> int

The int in a box created with box_int (no type check).

def box_long(x: long) -> str

A new box holding a long.

def unbox_long(b: str) -> long

The long in a box created with box_long.

def box_str(s: str) -> str

A new box holding a str (the pointer, not a copy: the string must stay valid).

def unbox_str(b: str) -> str

The str in a box created with box_str.

def box_dict(d: str) -> str

A new box holding the handle of another dict of any flavour, for nesting.

def unbox_dict(b: str) -> str

The dict handle in a box created with box_dict.

def sdict_next_slot(d: str, idx: int) -> int

Index of the first occupied slot at or after idx, or -1: i = sdict_next_slot(d, 0), then i = sdict_next_slot(d, i + 1). Walks a table without an iterator, also in nested loops over the same dict.

def sdict_key_at(d: str, idx: int) -> str

Key of the occupied slot idx (see sdict_next_slot).

def sdict_value_at(d: str, idx: int) -> str

Value of the occupied slot idx (see sdict_next_slot).

def Sdict_new(capacity: int) -> Sdict

A new Sdict with room for capacity entries.

def Sdict_set(self: Sdict, key: str, value: str) -> None
d.set(key, value): inserts or updates, growing the table before it passes half full.
def Sdict_get(self: Sdict, key: str) -> str
d.get(key): the value, or "" if absent.
def Sdict_has(self: Sdict, key: str) -> int
d.has(key): 1 if the key is present.
def Sdict_remove(self: Sdict, key: str) -> int
d.remove(key): removes the key; 1 if it was present.
def Sdict_count(self: Sdict) -> int
d.count(): number of entries.
def Sdict_reset(self: Sdict) -> None
d.reset(): moves the cursor before the first entry.
def Sdict_next(self: Sdict) -> int
d.next(): moves the cursor to the next entry; 1 if there is one, 0 at the end.
def Sdict_key(self: Sdict) -> str
d.key(): key of the current entry.
def Sdict_value(self: Sdict) -> str
d.value(): value of the current entry.
def idict_new(capacity: int) -> str

A new idict (int keys, str values) with at least capacity slots, rounded up to a power of two; the capacity is fixed.

def idict_capacity(d: str) -> int

The total number of slots.

def idict_count(d: str) -> int

The number of keys present.

def idict_set(d: str, key: int, value: str) -> int

Inserts or updates key -> value; returns 1, or 0 if the table is full.

def idict_get(d: str, key: int) -> str

The value for key, or "" if absent (use idict_has when "" can be a real value).

def idict_has(d: str, key: int) -> int

1 if key is present, 0 otherwise.

def idict_del(d: str, key: int) -> int

Removes key, leaving a tombstone; returns 1 if it was present.

def dict_hash(key: str) -> int

Hash of a string (rt_str_hash: eight bytes at a time, length in O(1) for a sealed string), as a positive 31-bit int.

def sdict_find(d: str, key: str) -> str

Address of the slot holding key, or 0: hash and probe in one runtime call (rt_sdict_find). sdict_get, sdict_has and Sdict use it.

Internal helpers

Used by the functions above; not meant to be called directly.