Landin library reference discuss source

core/tree/tree.ldn

1import core/iter
2import core/mem
3import core/vec
4
5identifier: type = distinct usize
6--- Opaque target-sized ordinal identifying a node in a particular tree. It
7--- does not retain or identify the tree itself.
8public node_id: type = identifier
9
10--- Wrap an ordinal as a node identifier without validating membership in a
11--- tree.
12public id: (ordinal: usize) -> (value: node_id) =
13    value = identifier(ordinal)
14end id
15
16--- Return the ordinal carried by a node identifier.
17public ordinal: (value: node_id) -> (number: usize) =
18    number = usize(value)
19end ordinal
20--- The requested child interval is not wholly within already published nodes.
21public invalid_children: atom
22--- Another node identifier cannot be represented in target `usize`.
23public too_many_nodes: atom
24--- The branch's leaf total cannot be represented in target `usize`.
25public too_many_leaves: atom
26
27node_value: type = struct
28    name: utf8
29    leaves: usize
30    --  Sum of every node's leaf total through this node. Each node has a
31    --  target-sized identifier; two words hold every possible cumulative sum.
32    through_low: usize
33    through_high: usize
34    kind: variant
35        leaf |
36        branch: (first: node_id, count: usize)
37    end kind
38end node_value
39--- Opaque named leaf or branch with a precomputed leaf total. The name is
40--- borrowed UTF-8.
41public node: type = node_value
42
43tree_value: type = struct
44    nodes: vec.list(node_value)
45end tree_value
46--- Append-only store of named nodes. Branches reference contiguous ranges of
47--- already published nodes; allocation is supplied explicitly.
48public tree: type = tree_value
49
50--- Create an empty tree without allocating.
51public new: () -> (result: tree) =
52    result = (nodes: vec.new(item: node_value))
53end new
54
55--- Return the number of published nodes.
56public length: (value: tree) -> (count: usize) =
57    count = vec.length(value.nodes)
58end length
59
60next_id: (count: usize) -> (id: node_id) ! too_many_nodes =
61    maximum: usize = 0 -% 1
62    fail too_many_nodes when count == maximum
63    id = identifier(count)
64end next_id
65
66--- Append a named leaf and return its identifier. Retains the name's backing;
67--- allocation or identifier exhaustion leaves existing nodes unchanged.
68public add_leaf: (provider: type is mem.allocator, inout value: tree,
69                  memory: provider, escaping into value name: utf8)
70                 -> (id: node_id) ! mem.out_of_memory | too_many_nodes =
71    id = try next_id(length(value))
72    mut previous_low: usize = 0
73    mut previous_high: usize = 0
74    if length(value) > 0 then
75        last: node_value = vec.get(value.nodes, length(value) - 1) else (problem)
76            _ = problem
77            --  A nonempty list always has its last node.
78            fail too_many_nodes
79        end
80        previous_low = last.through_low
81        previous_high = last.through_high
82    end if
83    next_low: usize = previous_low +% 1
84    if next_low == 0 then inc previous_high end if
85    added: node_value = (name: name, leaves: 1, through_low: next_low,
86                         through_high: previous_high,
87                         kind: leaf)
88    try vec.push(value.nodes, memory, added)
89end add_leaf
90
91--- Append a branch over a contiguous interval of existing nodes. Retains the
92--- name, validates children and leaf totals before publication, and permits
93--- an empty interval.
94---
95--- Edges name only previously published nodes. Their leaf totals are fixed,
96--- so their cumulative totals give bounded-stack queries at any depth.
97--- Shared children count once for each incoming path, as recursive counting
98--- would. An empty child interval has total zero.
99public add_branch: (provider: type is mem.allocator, inout value: tree,
100                    memory: provider, escaping into value name: utf8,
101                    first: node_id, count: usize)
102                   -> (id: node_id)
103                   ! mem.out_of_memory | too_many_nodes
104                   | invalid_children | too_many_leaves =
105    id = try next_id(length(value))
106    begins: usize = usize(first)
107    amount: usize = count
108    available: usize = length(value)
109    fail invalid_children when begins > available
110    fail invalid_children when amount > available - begins
111    mut before_low: usize = 0
112    mut before_high: usize = 0
113    if begins > 0 then
114        predecessor: node_value = vec.get(value.nodes, begins - 1) else (problem)
115            _ = problem
116            fail invalid_children
117        end
118        before_low = predecessor.through_low
119        before_high = predecessor.through_high
120    end if
121    mut through_low: usize = 0
122    mut through_high: usize = 0
123    if available > 0 then
124        last: node_value = vec.get(value.nodes, available - 1) else (problem)
125            _ = problem
126            fail invalid_children
127        end
128        through_low = last.through_low
129        through_high = last.through_high
130    end if
131    mut total: usize = 0
132    if amount > 0 then
133        end_node: node_value = vec.get(value.nodes, begins + amount - 1)
134            else (problem)
135            _ = problem
136            fail invalid_children
137        end
138        total = end_node.through_low -% before_low
139        mut high: usize = end_node.through_high - before_high
140        if end_node.through_low < before_low then dec high end if
141        fail too_many_leaves when high <> 0
142    end if
143    next_low: usize = through_low +% total
144    if next_low < through_low then inc through_high end if
145    added: node_value =
146        (name: name, leaves: total, through_low: next_low,
147         through_high: through_high,
148         kind: branch(first: first, count: count))
149    try vec.push(value.nodes, memory, added)
150end add_branch
151
152--- Copy the node selected by an identifier. Reports `mem.not_found` when
153--- the identifier is outside the tree's stored node range; the name remains
154--- borrowed from its original backing.
155public get: (value: tree, id: node_id)
156            -> (result: node from held value) ! mem.not_found =
157    result = vec.get(value.nodes, usize(id)) else (problem)
158        _ = problem
159        fail mem.not_found
160    end
161end get
162
163--- Lend the UTF-8 name retained in a node.
164public name: (value: node) -> (text: utf8 from value) =
165    text = value.name
166end name
167
168--- A node with no children of its own.
169public leaf_node: atom
170--- A node over a range of earlier nodes, its children.
171public branch_node: atom
172--- What a node is: `leaf_node` or `branch_node`.
173public node_kind: type = leaf_node | branch_node
174
175--- Whether `value` is a leaf or a branch.
176public kind: (value: node) -> (result: node_kind) =
177    result = match value.kind
178        leaf: leaf_node
179        branch(first, count): begin
180            _ = first
181            _ = count
182            branch_node
183        end
184    end match
185end kind
186
187--- The leaves `value` counts, precomputed, in constant time: one for a
188--- leaf, and a branch's children's total, shared children counting once
189--- per incoming path and an empty branch counting zero.
190public leaves: (value: node) -> (count: usize) =
191    count = value.leaves
192end leaves
193
194--- Return a branch's children: the `count` nodes from `first` on. A leaf
195--- has none, and returns the empty range, `first` and `count` both zero.
196public children: (value: node) -> (first: node_id, count: usize) =
197    match value.kind
198        leaf: begin
199            first = identifier(0)
200            count = 0
201        end
202        branch(start, many): begin
203            first = start
204            count = many
205        end
206    end match
207end children
208
209--  The tree's conformance to core/iter's `iterable` (D1957): `for id in t`
210--  walks every published node's identifier in the order the nodes were
211--  added, which puts each branch after its children.
212walk_first: (s: ptr tree_value) -> (c: usize) =
213    _ = s
214    c = 0
215end walk_first
216
217walk_at_end: (s: ptr tree_value, c: usize) -> (yes: bool) =
218    yes = c >= vec.length(s.val.nodes)
219end walk_at_end
220
221walk_item: (s: ptr tree_value, c: usize) -> (v: node_id) =
222    _ = s
223    v = identifier(c)
224end walk_item
225
226walk_next: (s: ptr tree_value, c: usize) -> (c2: usize) =
227    _ = s
228    c2 = c + 1
229end walk_next
230
231tree_value is iter.iterable (cur: usize, item_type: node_id,
232                             first: walk_first, at_end: walk_at_end,
233                             item: walk_item, next: walk_next)
234
235--- Release node storage through the original allocator and reset the tree.
236--- Name backing remains owned by its caller.
237public release: (provider: type is mem.allocator, inout value: tree,
238                 memory: provider) -> none =
239    vec.release(value.nodes, memory)
240end release