-
Notifications
You must be signed in to change notification settings - Fork 1
/
tree.t
45 lines (37 loc) · 1.02 KB
/
tree.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
-- SPDX-FileCopyrightText: 2024 René Hiemstra <[email protected]>
-- SPDX-FileCopyrightText: 2024 Torsten Keßler <[email protected]>
--
-- SPDX-License-Identifier: MIT
local alloc = require("alloc")
local base = require("base")
local io = terralib.includec("stdio.h")
local Allocator = alloc.Allocator
local Tree = terralib.memoize(function(T)
local struct tree
local Vec = alloc.SmartBlock(tree)
struct tree(base.AbstractBase) {
data: T
son: Vec
}
tree:complete()
tree.staticmethods.new = terra(alloc: Allocator, data: T, nsons: uint64)
return tree {data, alloc:allocate(sizeof(tree), nsons)}
end
tree.metamethods.__for = function(iter, body)
local terra go(root: tree): {}
var sz = root.son:size()
for i = 0, sz do
go(root.son(i))
end
var data = root.data
[body(data)]
end
return quote
go(iter)
end
end
return tree
end)
return {
Tree = Tree
}