sketch_segment_tree.nx
buildroot/runtime/sketch_segment_tree.nx
about
sketch_segment_tree.nx -- segment tree for range sum / min / max queries.
Foundational data structure. Array of N elements, supports:
point_update(i, value) O(log N)
range_sum(lo, hi) O(log N)
range_min(lo, hi) O(log N)
range_max(lo, hi) O(log N)
All three aggregates are maintained per node so callers don't have to
pick a single aggregate at construction. Memory: 3 * 2N i64s
(sum/min/max per node) for N-element backing array.
USE CASES:
- online range-sum queries (stocks-OHLC over time-window)
- dynamic-programming acceleration (e.g., longest increasing subseq)
- inverse-index aggregation
- online statistics over sliding indexed ranges
API uses HALF-OPEN intervals: range_*(lo, hi) covers indices [lo, hi).
LOSSLESS-LANGUAGE DISCIPLINE: all queries EXACT. Production tier.
Initialized to:
sum = 0
min = +MAX (so unwritten slots don't affect min)
max = -MAX (similar)
Caller responsibility: point_update before query for meaningful min/max
over ranges containing unwritten slots.
dependencies 2 imports · 1 importers
imports: syscalls.nxsketch_types.nx
imported by: sketch_segment_tree_test.nx
structs
| 38 | struct SegmentTree { |
consts
| 32 | const NX_ST_MIN_N: i64 = 2 |
| 33 | const NX_ST_MAX_N: i64 = 1000000 |
| 35 | const NX_ST_PLUS_INF: i64 = 0x4000000000000000 |
| 36 | const NX_ST_MINUS_INF: i64 = -0x4000000000000000 |
functions
| 48 | func nx_st_next_pow2(n: i64) -> i64 {
called by 1: nx_st_alloc |
| 54 | func nx_st_alloc(n: i64) -> *SegmentTree { |
| 78 | func nx_st_min2(a: i64, b: i64) -> i64 { |
| 83 | func nx_st_max2(a: i64, b: i64) -> i64 { |
| 92 | func nx_st_update(t: *SegmentTree, idx: i64, value: i64) -> i64 { |
| 117 | func nx_st_range_sum(t: *SegmentTree, lo: i64, hi: i64) -> i64 { |
| 139 | func nx_st_range_min(t: *SegmentTree, lo: i64, hi: i64) -> i64 { |
| 161 | func nx_st_range_max(t: *SegmentTree, lo: i64, hi: i64) -> i64 { |
| 185 | func nx_st_get(t: *SegmentTree, idx: i64) -> i64 {
called by 1: main |
| 193 | func nx_st_query_sum(t: *SegmentTree, lo: i64, hi: i64) -> *ApproxI64 { |
| 200 | func nx_st_memory_bytes(t: *SegmentTree) -> i64 { |
| 204 | func nx_st_n(t: *SegmentTree) -> i64 { return t.n } |