-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathbinary_tree_gen_test.cw
More file actions
61 lines (47 loc) · 1.2 KB
/
binary_tree_gen_test.cw
File metadata and controls
61 lines (47 loc) · 1.2 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
; Binary Tree Example
module:
import test
import fmt
rec Data:
x u32
y u32
z u32
fun lt2(a ^Data, b ^Data) bool:
if a^.x != a^.y:
return a^.x < a^.y
if b^.x != b^.y:
return b^.x < b^.y
return a^.z < b^.z
import bt2 = "./binary_tree_gen" (Data, lt2)
fun lt(a ^u32, b ^u32) bool:
return a^ < b^
import bt = "./binary_tree_gen" (u32, lt)
global N u32 = 64
ref global! NodePool = {[N]bt\Node:}
global! NodePoolFreeIndex uint = 0
fun alloc(p u32) ^!bt\Node:
let out = @!NodePool[NodePoolFreeIndex]
set NodePoolFreeIndex += 1
set out^.payload = p
return out
fun reverse_bits(bits u32, width u32) u32:
let! x u32 = bits
let! out u32 = 0
for i = 0, width, 1:
set out <<= 1
if x & 1 == 0:
set out |= 1
set x >>= 1
return out
fun DumpNode(payload ^u32) void:
fmt\print#(payload^, "\n")
fun main(argc s32, argv ^^u8) s32:
let! root bt\MaybeNode = bt\Leaf
for i = 0, N, 1:
let node = alloc(reverse_bits(i, 6))
; (fmt\print# "before insert " i "\n")
let x = bt\Insert(root, node)
set root = x
do bt\InorderTraversal(root, DumpNode)
test\Success#()
return 0