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
| Name | Linux | Windows | Meaning |
|---|---|---|---|
SDICT_TABLE_TYPE_ID | -716025276 | -716025276 | |
DICT_TABLE_TYPE_ID | -716025275 | -716025275 | |
IDICT_TABLE_TYPE_ID | -716025274 | -716025274 |
Classes
Cursor over the live entries of an sdict; see dict_iter_new.
d str
idx int
cap intAn 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_valueFunctions
Reads 8 bytes at addr as a str (pointer). Raw memory access.
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.
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).
Reads a sign-extended 4-byte integer at addr.
Writes a 4-byte integer at addr.
The next power of two ≥ n (minimum 1); table capacities are always powers of two so the index is a mask, not a modulo.
A new str → int dict with a fixed capacity, rounded up to a power of two. The buffer outlives the caller's scope.
Total number of slots.
Number of occupied entries.
Inserts or updates key → value. Returns 1, or 0 when the table is full.
The value for key, or -1 if absent.
1 if the key is present.
Removes a key (tombstone). Returns 1 if it was present.
Doubles the capacity of an int dict, copying the live entries into a new buffer; returns the new handle.
Like dict_set but doubles the table before it passes half full; returns the (possibly new) handle, which must be reassigned.
A new str → str dict with a fixed capacity.
Total number of slots.
Number of occupied entries.
Inserts or updates key → value. Returns 1, or 0 when the table is full. The dict holds a counted reference to the value.
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.
Like sdict_set_grow with a ref value.
The value for key, or "" if absent (use sdict_has when "" can be a real value).
1 if the key is present.
Removes a key (tombstone). Returns 1 if it was present.
Doubles the capacity of an sdict; returns the new handle.
Like sdict_set but doubles the table before it passes half full; returns the (possibly new) handle, which must be reassigned.
All live entries as two parallel arrays (keys, values) in the same order.
Resets an existing iterator to the beginning of d, without allocating.
An iterator positioned before the first entry of an sdict.
Puts an existing iterator on the last slot of d, for a backward scan with dict_iter_has_previous / dict_iter_previous.
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.
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.
1 if another live entry follows; advances past empty slots and tombstones without consuming the entry. Call before every dict_iter_next.
The next (key, value) pair; undefined without a preceding dict_iter_has_next that returned 1.
Deletes the pair most recently returned by dict_iter_next; safe during iteration.
A new dict whose values are int, long or str, each tagged with its type.
Total number of slots.
Number of occupied entries.
Insertion engine shared by the typed setters: stores an 8-byte carrier and its type tag.
Inserts or updates an int value. Returns 1, or 0 when full.
Inserts or updates a long value.
Inserts or updates a str value.
The type tag of the value for key: 1 int, 2 long, 3 str, 0 absent.
1 if the key is present, whatever the value type.
The value if it was stored as int, otherwise -1 (a type mismatch counts as absent).
The value if it was stored as long, otherwise -1.
The value if it was stored as str, otherwise "".
Removes a key of any value type. Returns 1 if it was present.
A new dict whose values are boxes (see box_int and friends), so any type, including other dicts, can be stored.
Inserts or updates a boxed value. Returns 1, or 0 when full.
The box for key, or "" if absent. Check adict_has first: the sentinel is not a box.
1 if the key is present.
Removes a key (tombstone); the box itself is not freed.
Number of occupied entries.
The type id stored in a box: 1 int, 2 long, 3 str, 4 dict. Only valid on a real box.
A new box holding an int.
The int in a box created with box_int (no type check).
A new box holding a long.
The long in a box created with box_long.
A new box holding a str (the pointer, not a copy: the string must stay valid).
The str in a box created with box_str.
A new box holding the handle of another dict of any flavour, for nesting.
The dict handle in a box created with box_dict.
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.
Key of the occupied slot idx (see sdict_next_slot).
Value of the occupied slot idx (see sdict_next_slot).
A new Sdict with room for capacity entries.
d.set(key, value): inserts or updates, growing the table before it passes half full.d.get(key): the value, or "" if absent.d.has(key): 1 if the key is present.d.remove(key): removes the key; 1 if it was present.d.count(): number of entries.d.reset(): moves the cursor before the first entry.d.next(): moves the cursor to the next entry; 1 if there is one, 0 at the end.d.key(): key of the current entry.d.value(): value of the current entry.A new idict (int keys, str values) with at least capacity slots, rounded up to a power of two; the capacity is fixed.
The total number of slots.
The number of keys present.
Inserts or updates key -> value; returns 1, or 0 if the table is full.
The value for key, or "" if absent (use idict_has when "" can be a real value).
1 if key is present, 0 otherwise.
Removes key, leaving a tombstone; returns 1 if it was present.
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.
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.
- inline def dict_slot_addr(d: str, idx: int) -> str
- inline def sdict_slot_addr(d: str, idx: int) -> str
- def vdict_int_to_carrier(x: int) -> str
- def vdict_carrier_to_int(carrier: str) -> int
- inline def vdict_int_as_long(x: long) -> long
- inline def vdict_slot_addr(d: str, idx: int) -> str
- def idict_slot_of(key: int, mask: int) -> int
- def idict_find(d: str, key: int) -> str
- def idict_free_slot(d: str, key: int) -> str
- def dict_probe_get(d: str, key: str, key_hash: int) -> str
- def dict_probe_set(d: str, key: str, key_hash: int) -> str
- def sdict_probe_get(d: str, key: str, key_hash: int) -> str
- def sdict_probe_set(d: str, key: str, key_hash: int) -> str