Skip to content

Commit 1c8ee97

Browse files
committed
mutable_traversal examples updated, recursive_set is added
1 parent e430ed4 commit 1c8ee97

File tree

1 file changed

+31
-51
lines changed

1 file changed

+31
-51
lines changed

examples/mutable_traversal.rs

Lines changed: 31 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,7 @@ impl MyTree {
6767
Self { tree }
6868
}
6969

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 {
7571
let node = self.tree.node(&node_idx);
7672

7773
let children_ids = node.children().map(|child| child.idx()).collect::<Vec<_>>();
@@ -96,7 +92,7 @@ impl MyTree {
9692
}
9793

9894
fn execute<'a>(
99-
inputs: &[f32; INPUTS_COUNT],
95+
inputs: &[f32],
10096
mut node: NodeMut<'a, Dyn<InstructionNode>>,
10197
) -> (NodeMut<'a, Dyn<InstructionNode>>, f32) {
10298
let num_children = node.num_children();
@@ -120,59 +116,43 @@ fn execute<'a>(
120116
(node, new_value)
121117
}
122118

123-
fn impl_over_children_idx() {
119+
fn test_implementation(method: &str, f: impl FnOnce(&[f32], &mut MyTree)) {
124120
let inputs = [10.0, 20.0];
125121

126122
let mut tree = MyTree::example();
127-
let node_idx = tree.tree.root().idx();
128123

129-
println!("\n\n# IMPL OVER CHILDREN INDICES");
124+
println!("\n\n# {}", method);
130125
println!("\ninputs = {:?}\n", &inputs);
131126
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!("\ninputs = {:?}\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!("\ninputs = {:?}\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);
171128
println!("After execute:\n{}\n", &tree.tree);
172129
}
173130

174131
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+
});
178158
}

0 commit comments

Comments
 (0)