1
1
use std;
2
+ import std:: arena;
3
+ import std:: arena:: arena;
2
4
3
- enum tree { nil, node( ~ tree , ~ tree , int ) , }
5
+ enum tree { nil, node( & tree , & tree , int ) , }
4
6
5
- fn item_check ( t : ~ tree ) -> int {
7
+ fn item_check ( t : & tree ) -> int {
6
8
alt * t {
7
9
nil { ret 0 ; }
8
10
node ( left, right, item) {
@@ -11,11 +13,13 @@ fn item_check(t: ~tree) -> int {
11
13
}
12
14
}
13
15
14
- fn bottom_up_tree ( item : int , depth : int ) -> ~ tree {
16
+ fn bottom_up_tree ( arena : & a . arena :: arena , item : int , depth : int ) -> & a . tree {
15
17
if depth > 0 {
16
- ret ~node ( bottom_up_tree ( 2 * item - 1 , depth - 1 ) ,
17
- bottom_up_tree ( 2 * item, depth - 1 ) , item) ;
18
- } else { ret ~nil; }
18
+ ret new ( * arena) node ( bottom_up_tree ( arena, 2 * item - 1 , depth - 1 ) ,
19
+ bottom_up_tree ( arena, 2 * item, depth - 1 ) ,
20
+ item) ;
21
+ }
22
+ ret new( * arena) nil;
19
23
}
20
24
21
25
fn main ( args : [ str ] ) {
@@ -28,22 +32,29 @@ fn main(args: [str]) {
28
32
let mut max_depth;
29
33
if min_depth + 2 > n {
30
34
max_depth = min_depth + 2 ;
31
- } else { max_depth = n; }
35
+ } else {
36
+ max_depth = n;
37
+ }
38
+
39
+ let stretch_arena = arena:: arena ( ) ;
32
40
let stretch_depth = max_depth + 1 ;
33
- let stretch_tree = bottom_up_tree ( 0 , stretch_depth) ;
41
+ let stretch_tree = bottom_up_tree ( & stretch_arena, 0 , stretch_depth) ;
42
+
34
43
io:: println ( #fmt ( "stretch tree of depth %d\t check: %d" ,
35
44
stretch_depth,
36
45
item_check ( stretch_tree) ) ) ;
37
- let long_lived_tree = bottom_up_tree ( 0 , max_depth) ;
46
+
47
+ let long_lived_arena = arena:: arena ( ) ;
48
+ let long_lived_tree = bottom_up_tree ( & long_lived_arena, 0 , max_depth) ;
38
49
let mut depth = min_depth;
39
50
while depth <= max_depth {
40
51
let iterations = int:: pow ( 2 , ( max_depth - depth + min_depth) as uint ) ;
41
52
let mut chk = 0 ;
42
53
let mut i = 1 ;
43
54
while i <= iterations {
44
- let mut temp_tree = bottom_up_tree ( i, depth) ;
55
+ let mut temp_tree = bottom_up_tree ( & long_lived_arena , i, depth) ;
45
56
chk += item_check ( temp_tree) ;
46
- temp_tree = bottom_up_tree ( -i, depth) ;
57
+ temp_tree = bottom_up_tree ( & long_lived_arena , -i, depth) ;
47
58
chk += item_check ( temp_tree) ;
48
59
i += 1 ;
49
60
}
0 commit comments