nx_hash.nx
buildroot/runtime/nx_hash.nx
about
nx_hash.nx -- open-addressing hash table (linear probing, fnv-1a).
FOUNDATION primitive. Any code that needs O(1) lookup uses this:
* Symbol table during link
* String interning in lex
* Deduplication in opt
* Visited-set in graph algorithms
Today every NishiLang module that needs lookup either does
linear scan or builds an ad-hoc table. Centralising removes
~hundred lines of repeated code across the runtime.
Algorithm: open-addressing with linear probing. Power-of-two
capacity for fast modulo (mask = cap - 1). Hash via FNV-1a
(Fowler-Noll-Vo, public domain, well-distributed).
Load factor: caller-controlled; resize is a TODO (v0.0.1 expects
caller to size the table appropriately).
API:
nx_hash_new(cap_pow2) -- construct
nx_hash_put(t, key, val) -- insert / overwrite
nx_hash_get(t, key) -- lookup; -1 = not found
nx_hash_has(t, key) -- 1 = present
nx_hash_count(t) -- number of entries
Keys are i64 (use the FNV-1a hash); values are i64.
String interning users compute the FNV-1a of their string and
use that as the i64 key.
dependencies 1 imports · 6 importers
imports: syscalls.nx
imported by: nx_frame_hash.nxnx_hash_kat.nxnx_intern.nxnx_set.nxnx_stl.nxnx_verify_ingest.nx
call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown
structs
| 76 | struct NxHash |
consts
| 38 | const NX_MAGIC_1490: i64 = 1490 |
| 42 | const NX_HASH_FNV_OFFSET: i64 = 0xCBF29CE484222325 |
| 43 | const NX_HASH_FNV_PRIME: i64 = 0x100000001B3 |
| 73 | const NX_HASH_EMPTY: i64 = 0 |
| 74 | const NX_HASH_TOMBSTONE: i64 = -1 |
| 84 | const NX_HASH_BYTES: i64 = 40 |
functions
| 45 | func nx_hash_fnv1a_bytes(buf: *u8, len: i64) -> i64 |
| 57 | func nx_hash_fnv1a_i64(x: i64) -> i64 |
| 86 | func nx_hash_new(cap_pow2: i64) -> *NxHash |
| 103 | func nx_hash_probe(t: *NxHash, key: i64) -> i64 |
| 118 | func nx_hash_put(t: *NxHash, key: i64, val: i64) -> i64 |
| 128 | func nx_hash_get(t: *NxHash, key: i64) -> i64 |
| 136 | func nx_hash_has(t: *NxHash, key: i64) -> i64 |
| 143 | func nx_hash_count(t: *NxHash) -> i64 { return t.n } |
| 147 | func main() -> i64 |