code wiki / (root) / nx_sketch_cms.nx

nx_sketch_cms.nx

buildroot/runtime/nx_sketch_cms.nx

7593 B219 linesdepth 6pulls 11 transitivereach 0 importersview sourcekind sketch/demotopic sketch
docsdependenciesstructsconstsfunctions

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

nx_syscalls.nx nx_murmur3.nx nx_sketch_hll.nx nx_sketch_types.nx nx_sketch_cms.nx

imports: nx_syscalls.nxnx_murmur3.nxnx_sketch_hll.nxnx_sketch_types.nx

imported by: nobody (leaf or entry point)

structs

53struct Cms

consts

38const NX_CMS_D_MIN: i64 = 1
39const NX_CMS_D_MAX: i64 = 32
40const NX_CMS_W_MIN: i64 = 16
41const NX_CMS_W_MAX: i64 = 16777216 // 16M counters per row
43const NX_CMS_SEED_DERIVE: i64 = 0x9E3779B9
44const NX_CMS_SEED_HI: i64 = 0x9747B28C
45const NX_CMS_SEED_LO: i64 = 0x36185EC0
46const NX_CMS_U32_MAX: i64 = 0xFFFFFFFF
162const NX_E_MILLI: i64 = 2718

functions

64func nx_cms_alloc(d: i64, w: i64, seed_base: i64) -> *Cms
called by 1: nx_cms_merge calls 1: sys_mmap
103func nx_cms_row_index(c: *Cms, hi: i64, lo: i64, i: i64) -> i64
111func nx_cms_add(c: *Cms, key: *u8, len: i64, count: i64) -> i64
137func nx_cms_estimate(c: *Cms, key: *u8, len: i64) -> i64
164func nx_cms_abs_bound(c: *Cms) -> i64
called by 1: nx_cms_query
172func nx_cms_conf_ppb(d: i64) -> i64
called by 1: nx_cms_query
184func nx_cms_query(c: *Cms, key: *u8, len: i64) -> *ApproxI64
194func nx_cms_merge(a: *Cms, b: *Cms) -> *Cms
calls 1: nx_cms_alloc
217func nx_cms_memory_bytes(c: *Cms) -> i64