code wiki / _hdl_build / nx_viz_partition.nx
nx_viz_partition.nx source
↩ module page · 68 lines · 2423 B
1// nx_viz_partition.nx -- the PARTITION layer of the sovereign Nishi viz library (the d3-hierarchy partition /
2// icicle / sunburst core, bits-up). Given a tree (parent[] + per-node value[]), assigns every node a normalized
3// span [x0,x1] within [0,total] (proportional to subtree value, partitioned among siblings) + its depth. Render
4// as rectangles (icicle) or annular arcs (sunburst). Pure integer. Closes layout-partition. license_tier: ORIGINAL
5import "nx_syscalls.nx"
6const K_MAGIC_32768: i64 = 32768
7
8// parent[i] = parent index (-1 = root). value[i] = node's OWN weight (leaves carry it; internals can be 0).
9// span_out = 2 ints/node (x0,x1). depth_out = depth/node. returns total (root subtree value).
10func vp_partition(n: i64, parent: *i64, value: *i64, span_out: *i64, depth_out: *i64) -> i64 {
11 var i: i64 = 0
12 while i < n {
13 var d: i64 = 0; var p: i64 = parent[i]
14 while p >= 0 { d = d + 1; p = parent[p] }
15 depth_out[i] = d
16 i = i + 1
17 }
18 var maxd: i64 = 0
19 i = 0
20 while i < n { if depth_out[i] > maxd { maxd = depth_out[i] } i = i + 1 }
21
22 let sval: *i64 = sys_mmap(K_MAGIC_32768) as *i64
23 i = 0
24 while i < n { sval[i] = value[i]; i = i + 1 }
25 var dd: i64 = maxd
26 while dd > 0 {
27 i = 0
28 while i < n {
29 if depth_out[i] == dd { sval[parent[i]] = sval[parent[i]] + sval[i] }
30 i = i + 1
31 }
32 dd = dd - 1
33 }
34
35 i = 0
36 while i < n {
37 if parent[i] < 0 { span_out[i * 2] = 0; span_out[i * 2 + 1] = sval[i] }
38 i = i + 1
39 }
40 dd = 0
41 while dd <= maxd {
42 i = 0
43 while i < n {
44 if depth_out[i] == dd {
45 var cursor: i64 = span_out[i * 2]
46 let wtot: i64 = span_out[i * 2 + 1] - span_out[i * 2]
47 var c: i64 = 0
48 while c < n {
49 if parent[c] == i {
50 var w: i64 = 0
51 if sval[i] > 0 { w = (wtot * sval[c]) / sval[i] }
52 span_out[c * 2] = cursor
53 span_out[c * 2 + 1] = cursor + w
54 cursor = cursor + w
55 }
56 c = c + 1
57 }
58 }
59 i = i + 1
60 }
61 dd = dd + 1
62 }
63
64 i = 0
65 while i < n { if parent[i] < 0 { return sval[i] } i = i + 1 }
66 return 0
67}
68func main() -> i64 { return 0 }