code wiki / (root) / sketch_cms.nx

sketch_cms.nx

buildroot/runtime/sketch_cms.nx

7543 B213 linesdepth 6pulls 11 transitivereach 3 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 · 3 importers

syscalls.nx murmur3.nx sketch_hll.nx sketch_types.nx sketch_cms.nx sketch_cms_test.nx sketch_count_sketch_negative_bench sketch_count_sketch_vs_cms_bias_be

imports: syscalls.nxmurmur3.nxsketch_hll.nxsketch_types.nx

imported by: sketch_cms_test.nxsketch_count_sketch_negative_bench.nxsketch_count_sketch_vs_cms_bias_bench.nx

structs

47struct Cms {

consts

32const NX_CMS_D_MIN: i64 = 1
33const NX_CMS_D_MAX: i64 = 32
34const NX_CMS_W_MIN: i64 = 16
35const NX_CMS_W_MAX: i64 = 16777216 // 16M counters per row
37const NX_CMS_SEED_DERIVE: i64 = 0x9E3779B9
38const NX_CMS_SEED_HI: i64 = 0x9747B28C
39const NX_CMS_SEED_LO: i64 = 0x36185EC0
40const NX_CMS_U32_MAX: i64 = 0xFFFFFFFF
156const NX_E_MILLI: i64 = 2718

functions

58func nx_cms_alloc(d: i64, w: i64, seed_base: i64) -> *Cms {
97func nx_cms_row_index(c: *Cms, hi: i64, lo: i64, i: i64) -> i64 {
105func nx_cms_add(c: *Cms, key: *u8, len: i64, count: i64) -> i64 {
131func nx_cms_estimate(c: *Cms, key: *u8, len: i64) -> i64 {
158func nx_cms_abs_bound(c: *Cms) -> i64 {
called by 1: nx_cms_query
166func nx_cms_conf_ppb(d: i64) -> i64 {
called by 1: nx_cms_query
178func nx_cms_query(c: *Cms, key: *u8, len: i64) -> *ApproxI64 {
188func nx_cms_merge(a: *Cms, b: *Cms) -> *Cms {
called by 1: main calls 1: nx_cms_alloc
211func nx_cms_memory_bytes(c: *Cms) -> i64 {