@@ -44,26 +44,30 @@ use std::iter::range_step;
4444use std:: thread:: Thread ;
4545use arena:: TypedArena ;
4646
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
5051}
5152
52- fn item_check ( t : & Tree ) -> int {
53+ fn item_check ( t : & Option < & Tree > ) -> i32 {
5354 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)
5657 }
5758}
5859
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 > > {
6162 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)
6569 } else {
66- arena . alloc ( Tree :: Nil )
70+ None
6771 }
6872}
6973
@@ -86,22 +90,22 @@ fn main() {
8690 let tree = bottom_up_tree ( & arena, 0 , depth) ;
8791
8892 println ! ( "stretch tree of depth {}\t check: {}" ,
89- depth, item_check( tree) ) ;
93+ depth, item_check( & tree) ) ;
9094 }
9195
9296 let long_lived_arena = TypedArena :: new ( ) ;
9397 let long_lived_tree = bottom_up_tree ( & long_lived_arena, 0 , max_depth) ;
9498
9599 let messages = range_step ( min_depth, max_depth + 1 , 2 ) . map ( |depth| {
96100 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 ) ;
98102 Thread :: scoped ( move || {
99103 let mut chk = 0 ;
100- for i in range ( 1 , iterations + 1 ) {
104+ for i in 1 .. iterations + 1 {
101105 let arena = TypedArena :: new ( ) ;
102106 let a = bottom_up_tree ( & arena, i, depth) ;
103107 let b = bottom_up_tree ( & arena, -i, depth) ;
104- chk += item_check ( a) + item_check ( b) ;
108+ chk += item_check ( & a) + item_check ( & b) ;
105109 }
106110 format ! ( "{}\t trees of depth {}\t check: {}" ,
107111 iterations * 2 , depth, chk)
@@ -113,5 +117,5 @@ fn main() {
113117 }
114118
115119 println ! ( "long lived tree of depth {}\t check: {}" ,
116- max_depth, item_check( long_lived_tree) ) ;
120+ max_depth, item_check( & long_lived_tree) ) ;
117121}
0 commit comments