@@ -67,11 +67,7 @@ impl MyTree {
67
67
Self { tree }
68
68
}
69
69
70
- fn execute_rec (
71
- & mut self ,
72
- inputs : & [ f32 ; INPUTS_COUNT ] ,
73
- node_idx : NodeIdx < Dyn < InstructionNode > > ,
74
- ) -> f32 {
70
+ fn execute_rec ( & mut self , inputs : & [ f32 ] , node_idx : NodeIdx < Dyn < InstructionNode > > ) -> f32 {
75
71
let node = self . tree . node ( & node_idx) ;
76
72
77
73
let children_ids = node. children ( ) . map ( |child| child. idx ( ) ) . collect :: < Vec < _ > > ( ) ;
@@ -96,7 +92,7 @@ impl MyTree {
96
92
}
97
93
98
94
fn execute < ' a > (
99
- inputs : & [ f32 ; INPUTS_COUNT ] ,
95
+ inputs : & [ f32 ] ,
100
96
mut node : NodeMut < ' a , Dyn < InstructionNode > > ,
101
97
) -> ( NodeMut < ' a , Dyn < InstructionNode > > , f32 ) {
102
98
let num_children = node. num_children ( ) ;
@@ -120,59 +116,43 @@ fn execute<'a>(
120
116
( node, new_value)
121
117
}
122
118
123
- fn impl_over_children_idx ( ) {
119
+ fn test_implementation ( method : & str , f : impl FnOnce ( & [ f32 ] , & mut MyTree ) ) {
124
120
let inputs = [ 10.0 , 20.0 ] ;
125
121
126
122
let mut tree = MyTree :: example ( ) ;
127
- let node_idx = tree. tree . root ( ) . idx ( ) ;
128
123
129
- println ! ( "\n \n # IMPL OVER CHILDREN INDICES" ) ;
124
+ println ! ( "\n \n # {}" , method ) ;
130
125
println ! ( "\n inputs = {:?}\n " , & inputs) ;
131
126
println ! ( "Before execute:\n {}\n " , & tree. tree) ;
132
- tree. execute_rec ( & inputs, node_idx) ;
133
- println ! ( "After execute:\n {}\n " , & tree. tree) ;
134
- }
135
-
136
- fn impl_over_children_mut ( ) {
137
- let inputs = [ 10.0 , 20.0 ] ;
138
-
139
- let mut tree = MyTree :: example ( ) ;
140
-
141
- println ! ( "\n \n # IMPL WITH INTO_CHILD_MUT & INTO_PARENT_MUT" ) ;
142
- println ! ( "\n inputs = {:?}\n " , & inputs) ;
143
- println ! ( "Before execute:\n {}\n " , & tree. tree) ;
144
- execute ( & inputs, tree. tree . root_mut ( ) ) ;
145
- println ! ( "After execute:\n {}\n " , & tree. tree) ;
146
- }
147
-
148
- fn impl_recursive_set ( ) {
149
- let inputs = [ 10.0 , 20.0 ] ;
150
-
151
- let mut tree = MyTree :: example ( ) ;
152
-
153
- println ! ( "\n \n # IMPL WITH RECURSIVE_SET" ) ;
154
- println ! ( "\n inputs = {:?}\n " , & inputs) ;
155
- println ! ( "Before execute:\n {}\n " , & tree. tree) ;
156
-
157
- tree. tree
158
- . root_mut ( )
159
- . recursive_set ( |node_data, children_data| {
160
- let instruction = node_data. instruction ;
161
- let children_sum: f32 = children_data. iter ( ) . map ( |x| x. value ) . sum ( ) ;
162
- let value = match node_data. instruction {
163
- Instruction :: Input ( i) => inputs[ i] ,
164
- Instruction :: Add => children_sum,
165
- Instruction :: AddI { val } => val + children_sum,
166
- } ;
167
-
168
- InstructionNode { instruction, value }
169
- } ) ;
170
-
127
+ f ( & inputs, & mut tree) ;
171
128
println ! ( "After execute:\n {}\n " , & tree. tree) ;
172
129
}
173
130
174
131
fn main ( ) {
175
- impl_over_children_idx ( ) ;
176
- impl_over_children_mut ( ) ;
177
- impl_recursive_set ( ) ;
132
+ test_implementation ( "IMPL OVER CHILDREN INDICES" , |inputs, tree| {
133
+ tree. execute_rec ( inputs, tree. tree . root ( ) . idx ( ) ) ;
134
+ } ) ;
135
+
136
+ test_implementation (
137
+ "IMPL WITH INTO_CHILD_MUT & INTO_PARENT_MUT" ,
138
+ |inputs, tree| {
139
+ execute ( & inputs, tree. tree . root_mut ( ) ) ;
140
+ } ,
141
+ ) ;
142
+
143
+ test_implementation ( "IMPL recursive_set" , |inputs, tree| {
144
+ tree. tree
145
+ . root_mut ( )
146
+ . recursive_set ( |node_data, children_data| {
147
+ let instruction = node_data. instruction ;
148
+ let children_sum: f32 = children_data. iter ( ) . map ( |x| x. value ) . sum ( ) ;
149
+ let value = match node_data. instruction {
150
+ Instruction :: Input ( i) => inputs[ i] ,
151
+ Instruction :: Add => children_sum,
152
+ Instruction :: AddI { val } => val + children_sum,
153
+ } ;
154
+
155
+ InstructionNode { instruction, value }
156
+ } ) ;
157
+ } ) ;
178
158
}
0 commit comments