|
3 | 3 | package main |
4 | 4 |
|
5 | 5 | import "core:fmt" |
6 | | -import "core:strconv" |
7 | | -import "core:os" |
8 | 6 | import "core:math" |
9 | 7 | import "core:mem" |
| 8 | +import "core:os" |
| 9 | +import "core:strconv" |
10 | 10 |
|
11 | 11 | MIN_DEPTH :: 4 |
12 | 12 | main :: proc() { |
13 | | - n := strconv.parse_int(os.args[1]) or_else 10 |
14 | | - max_depth := math.max(MIN_DEPTH+2,n) |
15 | | - { |
16 | | - stretch_depth := max_depth + 1 |
17 | | - stretch_tree := makeTree(stretch_depth) |
18 | | - defer { delete_tree(stretch_tree) } |
19 | | - fmt.printf("stretch tree of depth %d\t check: %d\n", stretch_depth, check(stretch_tree) ); |
20 | | - } |
21 | | - long_lived_tree := makeTree(max_depth) |
22 | | - defer delete_tree(long_lived_tree) |
23 | | - depth:int= MIN_DEPTH; |
24 | | - for ;depth <= max_depth; depth += 2 { |
25 | | - iterations := 1 << u8(max_depth - depth + MIN_DEPTH) |
26 | | - sum: u64 = 0 |
27 | | - for _ in 0..<iterations { |
28 | | - tree := makeTree(depth) |
29 | | - defer { delete_tree(tree) } |
30 | | - sum += u64(check(tree)) |
31 | | - } |
32 | | - fmt.printf("%d\t trees of depth %d\t check: %d\n", iterations, depth, sum ) |
33 | | - } |
| 13 | + #no_bounds_check n := strconv.parse_int(os.args[1]) or_else 10 |
| 14 | + max_depth := math.max(MIN_DEPTH + 2, n) |
| 15 | + { |
| 16 | + stretch_depth := max_depth + 1 |
| 17 | + stretch_tree := makeTree(stretch_depth) |
| 18 | + defer {delete_tree(stretch_tree)} |
| 19 | + fmt.printf("stretch tree of depth %d\t check: %d\n", stretch_depth, check(stretch_tree)) |
| 20 | + } |
| 21 | + long_lived_tree := makeTree(max_depth) |
| 22 | + defer delete_tree(long_lived_tree) |
| 23 | + depth: int = MIN_DEPTH |
| 24 | + for ; depth <= max_depth; depth += 2 { |
| 25 | + iterations := 1 << u8(max_depth - depth + MIN_DEPTH) |
| 26 | + sum: u64 = 0 |
| 27 | + for _ in 0 ..< iterations { |
| 28 | + tree := makeTree(depth) |
| 29 | + defer {delete_tree(tree)} |
| 30 | + sum += u64(check(tree)) |
| 31 | + } |
| 32 | + fmt.printf("%d\t trees of depth %d\t check: %d\n", iterations, depth, sum) |
| 33 | + } |
34 | 34 |
|
35 | | - fmt.printf("long lived tree of depth %d\t check: %d\n", max_depth, check(long_lived_tree)) |
| 35 | + fmt.printf("long lived tree of depth %d\t check: %d\n", max_depth, check(long_lived_tree)) |
36 | 36 | } |
37 | 37 |
|
38 | 38 | Node :: struct { |
39 | | - left,right:^Node, |
| 39 | + left, right: ^Node, |
40 | 40 | } |
41 | 41 |
|
42 | | -makeTree::proc(depth:int)->^Node { |
43 | | - node := new(Node) |
44 | | - if node == nil { |
45 | | - fmt.println("alloc error") |
46 | | - return node |
47 | | - } |
48 | | - if depth > 0 { |
49 | | - node.left = makeTree(depth-1) |
50 | | - node.right = makeTree(depth-1) |
51 | | - } |
52 | | - return node |
| 42 | +makeTree :: proc(depth: int) -> ^Node { |
| 43 | + node := new(Node) |
| 44 | + if node == nil { |
| 45 | + fmt.println("alloc error") |
| 46 | + return node |
| 47 | + } |
| 48 | + if depth > 0 { |
| 49 | + node.left = makeTree(depth - 1) |
| 50 | + node.right = makeTree(depth - 1) |
| 51 | + } |
| 52 | + return node |
53 | 53 | } |
54 | | -delete_tree::proc(t:^Node){ |
55 | | - if t.left != nil {delete_tree(t.left)} |
56 | | - if t.right != nil {delete_tree(t.right)} |
57 | | - free(t) |
| 54 | +delete_tree :: proc(t: ^Node) { |
| 55 | + if t.left != nil {delete_tree(t.left)} |
| 56 | + if t.right != nil {delete_tree(t.right)} |
| 57 | + free(t) |
58 | 58 | } |
59 | | -check::proc(tree:^Node)->int { |
60 | | - sum : int = 1 |
61 | | - if tree == nil { return 0 } |
62 | | - sum += check(tree.left) |
63 | | - sum += check(tree.right) |
64 | | - return sum |
| 59 | +check :: proc(tree: ^Node) -> int { |
| 60 | + sum: int = 1 |
| 61 | + if tree == nil {return 0} |
| 62 | + sum += check(tree.left) |
| 63 | + sum += check(tree.right) |
| 64 | + return sum |
65 | 65 | } |
0 commit comments