sketch_misra_gries.nx
buildroot/runtime/sketch_misra_gries.nx
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
imports: syscalls.nxsketch_types.nx
imported by: sketch_mg_ss_bracket_bench.nxsketch_misra_gries_test.nx
structs
| 32 | struct MgCounter { |
| 37 | struct MisraGries { |
consts
| 29 | const NX_MG_K_MIN: i64 = 2 |
| 30 | const NX_MG_K_MAX: i64 = 100000 |
functions
| 46 | func nx_mg_alloc(k: i64) -> *MisraGries { |
| 67 | func nx_mg_counter_at(mg: *MisraGries, i: i64) -> *MgCounter { |
| 73 | func nx_mg_find_key(mg: *MisraGries, key: i64) -> i64 { |
| 83 | func nx_mg_find_empty(mg: *MisraGries) -> i64 { |
| 106 | func if_le(a: i64, b: i64) -> i64 {
called by 1: nx_mg_add |
| 111 | func nx_mg_min_count(mg: *MisraGries) -> i64 { |
| 124 | func nx_mg_add(mg: *MisraGries, key: i64, count: i64) -> i64 { |
| 184 | func nx_mg_estimate(mg: *MisraGries, key: i64) -> i64 { |
| 193 | func nx_mg_max_undercount(mg: *MisraGries) -> i64 { |
| 198 | func nx_mg_upper_bound(mg: *MisraGries, key: i64) -> i64 { |
| 202 | func nx_mg_query(mg: *MisraGries, key: i64) -> *ApproxI64 { |
| 212 | func nx_mg_n_tracked(mg: *MisraGries) -> i64 { |
| 223 | func nx_mg_memory_bytes(mg: *MisraGries) -> i64 { |