code wiki / (root) / sketch_misra_gries.nx

sketch_misra_gries.nx

buildroot/runtime/sketch_misra_gries.nx

7388 B225 linesdepth 4pulls 4 transitivereach 2 importersview sourcekind sketch/demotopic sketch
docsdependenciesstructsconstsfunctions

about

sketch_misra_gries.nx -- Misra-Gries frequent items (1982). Bounded set of (key, count) pairs of capacity (k-1). On arrival of (key, w): - if key tracked: count[key] += w - else if free slot: install (key, w) - else: decrement EVERY counter by w (with floor 0); counters that reach 0 are freed for future use GUARANTEE (different from SpaceSaving): true_count - N/k <= estimate <= true_count Misra-Gries UNDERESTIMATES; SpaceSaving OVERESTIMATES. Both bound the error by N/k. Symmetric error semantics let callers pick: - SpaceSaving for upper-bound queries ("at most X") - Misra-Gries for lower-bound queries ("at least X") CAPABILITY STOMP vs DataSketches: DS ships only the "frequent items" variant (SpaceSaving-like). We ship BOTH algorithmic approaches (SpaceSaving + Misra-Gries) so callers pick by error-bound direction. LOSSLESS-LANGUAGE DISCIPLINE: nx_mg_query returns ApproxI64 with NX_ENV_ABS; param_a = N/k (the maximum possible UNDERESTIMATE); conf_ppb = 1e9 (deterministic).

dependencies 2 imports · 2 importers

syscalls.nx sketch_types.nx sketch_misra_gries.nx sketch_mg_ss_bracket_bench.nx sketch_misra_gries_test.nx

imports: syscalls.nxsketch_types.nx

imported by: sketch_mg_ss_bracket_bench.nxsketch_misra_gries_test.nx

structs

32struct MgCounter {
37struct MisraGries {

consts

29const NX_MG_K_MIN: i64 = 2
30const NX_MG_K_MAX: i64 = 100000

functions

46func nx_mg_alloc(k: i64) -> *MisraGries {
called by 2: mainmain
67func nx_mg_counter_at(mg: *MisraGries, i: i64) -> *MgCounter {
73func nx_mg_find_key(mg: *MisraGries, key: i64) -> i64 {
83func nx_mg_find_empty(mg: *MisraGries) -> i64 {
called by 1: nx_mg_add calls 1: nx_mg_counter_at
106func if_le(a: i64, b: i64) -> i64 {
called by 1: nx_mg_add
111func nx_mg_min_count(mg: *MisraGries) -> i64 {
called by 1: nx_mg_add calls 1: nx_mg_counter_at
124func nx_mg_add(mg: *MisraGries, key: i64, count: i64) -> i64 {
184func nx_mg_estimate(mg: *MisraGries, key: i64) -> i64 {
193func nx_mg_max_undercount(mg: *MisraGries) -> i64 {
198func nx_mg_upper_bound(mg: *MisraGries, key: i64) -> i64 {
202func nx_mg_query(mg: *MisraGries, key: i64) -> *ApproxI64 {
212func nx_mg_n_tracked(mg: *MisraGries) -> i64 {
called by 1: main calls 1: nx_mg_counter_at
223func nx_mg_memory_bytes(mg: *MisraGries) -> i64 {