code wiki / (root) / _py_freq_substrate.nx

_py_freq_substrate.nx source

↩ module page · 21 lines · 647 B

1import "syscalls.nx" 2import "sketch_count_sketch.nx" 3 4func main() -> i64 { 5 let cs: *CountSketch = nx_cs_alloc(5, 512, 42) 6 if cs == (0 as *CountSketch) { return 1 } 7 var i: i64 = 0 8 while i < 100000 { 9 let k: i64 = (i * 17) % 10000 10 nx_cs_add(cs, k, 1) 11 i = i + 1 12 } 13 // True top count: each of the 10000 keys gets hit n_events/n_distinct times 14 // = 10. Some may get 9 or 11 due to integer arithmetic. 15 // Query key 0: counted ~10 times. 16 let est: i64 = nx_cs_estimate(cs, 0) 17 // gate: estimate within 50% of expected 10 18 if est < 5 { return 2 } 19 if est > 20 { return 3 } 20 return 0 21}