@@ -44,26 +44,30 @@ use std::iter::range_step;
44
44
use std:: thread:: Thread ;
45
45
use arena:: TypedArena ;
46
46
47
- enum Tree < ' a > {
48
- Nil ,
49
- Node ( & ' a Tree < ' a > , & ' a Tree < ' a > , int )
47
+ struct Tree < ' a > {
48
+ l : Option < & ' a Tree < ' a > > ,
49
+ r : Option < & ' a Tree < ' a > > ,
50
+ i : i32
50
51
}
51
52
52
- fn item_check ( t : & Tree ) -> int {
53
+ fn item_check ( t : & Option < & Tree > ) -> i32 {
53
54
match * t {
54
- Tree :: Nil => 0 ,
55
- Tree :: Node ( l, r, i) => i + item_check ( l) - item_check ( r)
55
+ None => 0 ,
56
+ Some ( & Tree { ref l, ref r, i } ) => i + item_check ( l) - item_check ( r)
56
57
}
57
58
}
58
59
59
- fn bottom_up_tree < ' r > ( arena : & ' r TypedArena < Tree < ' r > > , item : int , depth : int )
60
- -> & ' r Tree < ' r > {
60
+ fn bottom_up_tree < ' r > ( arena : & ' r TypedArena < Tree < ' r > > , item : i32 , depth : i32 )
61
+ -> Option < & ' r Tree < ' r > > {
61
62
if depth > 0 {
62
- arena. alloc ( Tree :: Node ( bottom_up_tree ( arena, 2 * item - 1 , depth - 1 ) ,
63
- bottom_up_tree ( arena, 2 * item, depth - 1 ) ,
64
- item) )
63
+ let t: & Tree < ' r > = arena. alloc ( Tree {
64
+ l : bottom_up_tree ( arena, 2 * item - 1 , depth - 1 ) ,
65
+ r : bottom_up_tree ( arena, 2 * item, depth - 1 ) ,
66
+ i : item
67
+ } ) ;
68
+ Some ( t)
65
69
} else {
66
- arena . alloc ( Tree :: Nil )
70
+ None
67
71
}
68
72
}
69
73
@@ -86,22 +90,22 @@ fn main() {
86
90
let tree = bottom_up_tree ( & arena, 0 , depth) ;
87
91
88
92
println ! ( "stretch tree of depth {}\t check: {}" ,
89
- depth, item_check( tree) ) ;
93
+ depth, item_check( & tree) ) ;
90
94
}
91
95
92
96
let long_lived_arena = TypedArena :: new ( ) ;
93
97
let long_lived_tree = bottom_up_tree ( & long_lived_arena, 0 , max_depth) ;
94
98
95
99
let messages = range_step ( min_depth, max_depth + 1 , 2 ) . map ( |depth| {
96
100
use std:: num:: Int ;
97
- let iterations = 2 i . pow ( ( max_depth - depth + min_depth) as uint ) ;
101
+ let iterations = 2 . pow ( ( max_depth - depth + min_depth) as usize ) ;
98
102
Thread :: scoped ( move || {
99
103
let mut chk = 0 ;
100
- for i in range ( 1 , iterations + 1 ) {
104
+ for i in 1 .. iterations + 1 {
101
105
let arena = TypedArena :: new ( ) ;
102
106
let a = bottom_up_tree ( & arena, i, depth) ;
103
107
let b = bottom_up_tree ( & arena, -i, depth) ;
104
- chk += item_check ( a) + item_check ( b) ;
108
+ chk += item_check ( & a) + item_check ( & b) ;
105
109
}
106
110
format ! ( "{}\t trees of depth {}\t check: {}" ,
107
111
iterations * 2 , depth, chk)
@@ -113,5 +117,5 @@ fn main() {
113
117
}
114
118
115
119
println ! ( "long lived tree of depth {}\t check: {}" ,
116
- max_depth, item_check( long_lived_tree) ) ;
120
+ max_depth, item_check( & long_lived_tree) ) ;
117
121
}
0 commit comments