nx_sketch_cms.nx
buildroot/runtime/nx_sketch_cms.nx
about
sketch_cms.nx -- Count-Min Sketch in NishiLang.
Cormode-Muthukrishnan 2005. d-row x w-column counter grid;
add() increments d counters chosen by d hash functions of the
item; estimate() returns the MIN across rows -- overestimate-
only by construction (no false negatives).
Provable bound:
Pr[estimate(x) <= true(x) + eps * totalCount] >= 1 - delta
for d = ceil(ln(1/delta)), w = ceil(e / eps)
Defaults d=5, w=2718 -> eps ~ 0.001, delta ~ 0.007 (~ e^-5).
Memory: 8 * d * w bytes (i64 counters). d=5, w=2718 -> 109 KB.
KIRSCH-MITZENMACHER 2008 double-hashing:
Instead of d independent murmur calls, compute ONE 64-bit hash
(hi, lo) and derive d row indices via h_i(x) = (hi + i * lo) ^
seeds[i] mod w. Gives near-independent rows at single-hash
cost. Mirrors core-sketch/cms.ts.
LOSSLESS-LANGUAGE DISCIPLINE (doc 20):
nx_cms_query returns ApproxI64 with envelope_kind = NX_ENV_ABS
(absolute additive bound = eps * totalCount), NOT rel_stddev.
Substrate's typed envelope discriminator forces callers to
handle the abs-vs-relative distinction explicitly.
dependencies 4 imports · 0 importers
imports: nx_syscalls.nxnx_murmur3.nxnx_sketch_hll.nxnx_sketch_types.nx
imported by: nobody (leaf or entry point)
structs
| 53 | struct Cms |
consts
| 38 | const NX_CMS_D_MIN: i64 = 1 |
| 39 | const NX_CMS_D_MAX: i64 = 32 |
| 40 | const NX_CMS_W_MIN: i64 = 16 |
| 41 | const NX_CMS_W_MAX: i64 = 16777216 // 16M counters per row |
| 43 | const NX_CMS_SEED_DERIVE: i64 = 0x9E3779B9 |
| 44 | const NX_CMS_SEED_HI: i64 = 0x9747B28C |
| 45 | const NX_CMS_SEED_LO: i64 = 0x36185EC0 |
| 46 | const NX_CMS_U32_MAX: i64 = 0xFFFFFFFF |
| 162 | const NX_E_MILLI: i64 = 2718 |
functions
| 64 | func nx_cms_alloc(d: i64, w: i64, seed_base: i64) -> *Cms |
| 103 | func nx_cms_row_index(c: *Cms, hi: i64, lo: i64, i: i64) -> i64 |
| 111 | func nx_cms_add(c: *Cms, key: *u8, len: i64, count: i64) -> i64 |
| 137 | func nx_cms_estimate(c: *Cms, key: *u8, len: i64) -> i64 |
| 164 | func nx_cms_abs_bound(c: *Cms) -> i64 called by 1: nx_cms_query |
| 172 | func nx_cms_conf_ppb(d: i64) -> i64 called by 1: nx_cms_query |
| 184 | func nx_cms_query(c: *Cms, key: *u8, len: i64) -> *ApproxI64 |
| 194 | func nx_cms_merge(a: *Cms, b: *Cms) -> *Cms calls 1: nx_cms_alloc |
| 217 | func nx_cms_memory_bytes(c: *Cms) -> i64 |