Advertisement
Guest User

Untitled

a guest
Aug 21st, 2021
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 2.62 KB | None | 0 0
  1. use indextree::{Arena, NodeId};
  2.  
  3. trait CalcNode {
  4.     fn update(&mut self, input: Vec<&dyn CalcNode>);
  5.     fn output(&self) -> i64;
  6. }
  7.  
  8. struct NodeModel {
  9.     output: i64,
  10.     internal: i64,
  11. }
  12.  
  13. impl CalcNode for NodeModel {
  14.     //input can't be cloned/copied in final version
  15.     fn update(&mut self, input: Vec<&dyn CalcNode>) {
  16.         //example for n possible update funciton
  17.         let chid_input: i64 = input.iter().map(|&f| f.output()).sum();
  18.         self.output = self.internal * chid_input;
  19.         self.internal += 1
  20.     }
  21.  
  22.     fn output(&self) -> i64 {
  23.         // output is not copy-able in final version
  24.         self.output
  25.     }
  26. }
  27.  
  28. impl NodeModel {
  29.     fn new(internal: i64) -> Self {
  30.         Self {
  31.             output: internal,
  32.             internal,
  33.         }
  34.     }
  35. }
  36.  
  37. fn main() {
  38.     let (mut arena_, root) = build_tree();
  39.     let arena = &mut arena_;
  40.  
  41.     //update 3 times as test
  42.     let node_stack: Vec<NodeId> = root.descendants(arena).collect(); //collect all nodes depth first
  43.  
  44.     for _ in 0..3 {
  45.         //update in reverse order deepest node first, ROOT LAST.
  46.         for &n in node_stack.iter().rev() {
  47.             // collect updated children when available
  48.             let children: Vec<&dyn CalcNode> = {
  49.                 n.children(arena)
  50.                     .map(|f| arena.get(f).unwrap().get().as_ref())
  51.                     .collect()
  52.             };
  53.  
  54.             //get the node to update
  55.             let node = { arena.get_mut(n).unwrap().get_mut().as_mut() }; //<- can't borrow arena here to get the node
  56.  
  57.             // update the node
  58.             node.update(children);
  59.         }
  60.     }
  61. }
  62.  
  63. fn build_tree() -> (Arena<Box<dyn CalcNode>>, NodeId) {
  64.     // Create a new arena
  65.     let mut arena: Arena<Box<dyn CalcNode>> = Arena::new();
  66.  
  67.     // Add some new nodes to the arena
  68.     let a = arena.new_node(Box::new(NodeModel::new(1)));
  69.     let b = arena.new_node(Box::new(NodeModel::new(2)));
  70.     let c = arena.new_node(Box::new(NodeModel::new(3)));
  71.     let d = arena.new_node(Box::new(NodeModel::new(4)));
  72.     let e = arena.new_node(Box::new(NodeModel::new(5)));
  73.     let f = arena.new_node(Box::new(NodeModel::new(6)));
  74.     let g = arena.new_node(Box::new(NodeModel::new(7)));
  75.     let h = arena.new_node(Box::new(NodeModel::new(8)));
  76.  
  77.     // Build tree
  78.     //           a
  79.     //        b     c
  80.     //       d e    f
  81.     //             g h
  82.     a.append(b, &mut arena);
  83.     a.append(c, &mut arena);
  84.     b.append(d, &mut arena);
  85.     b.append(e, &mut arena);
  86.     c.append(f, &mut arena);
  87.     f.append(g, &mut arena);
  88.     f.append(h, &mut arena);
  89.  
  90.     return (arena, a);
  91. }
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement