Seperated state from structural tree.

This commit is contained in:
Jacob VanDomelen 2019-05-23 18:40:57 -07:00
parent 673b0dd5eb
commit 5abe93a0dc
3 changed files with 13 additions and 11 deletions

View file

@ -1,11 +1,13 @@
mod tree; mod tree;
mod state; mod state;
use uuid::Uuid;
fn build_tree(h: u32) -> Option<Box<tree::Tree>> { fn build_tree(h: u32) -> Option<Box<tree::Tree>> {
let mut result: Option<Box<tree::Tree>> = None; let mut result: Option<Box<tree::Tree>> = None;
if h != 0 { if h != 0 {
result = Some(tree::concat_trees(state::create(), build_tree(h - 1), build_tree(h - 1))); result = Some(tree::concat_trees(Uuid::new_v4(), build_tree(h - 1), build_tree(h - 1)));
match &result { match &result {
Some(r) => (*r).run_simulation(), Some(r) => (*r).run_simulation(),
_ => () _ => ()
@ -23,7 +25,7 @@ pub fn run_bracket() {
println!("========================================="); println!("=========================================");
println!("Running bracket..."); println!("Running bracket...");
height += 1; height += 1;
tree = *tree::concat_trees(state::create(), Some(Box::new(tree)), build_tree(height)); tree = *tree::concat_trees(Uuid::new_v4(), Some(Box::new(tree)), build_tree(height));
tree.run_simulation(); tree.run_simulation();
} }
} }

View file

@ -11,8 +11,8 @@ impl fmt::Display for State {
} }
} }
pub fn create() -> State { pub fn create(id: &Uuid) -> State {
State { State {
id: Uuid::new_v4() id: id.clone()
} }
} }

View file

@ -1,15 +1,15 @@
use std::fmt; use std::fmt;
use super::state; use uuid::Uuid;
pub struct Tree { pub struct Tree {
state: state::State, id: Uuid,
left: Option<Box<Tree>>, left: Option<Box<Tree>>,
right: Option<Box<Tree>> right: Option<Box<Tree>>
} }
pub fn concat_trees(s: state::State, l: Option<Box<Tree>>, r: Option<Box<Tree>>) -> Box<Tree> { pub fn concat_trees(id: Uuid, l: Option<Box<Tree>>, r: Option<Box<Tree>>) -> Box<Tree> {
Box::new(Tree { Box::new(Tree {
state: s, id: id,
left: l, left: l,
right: r right: r
}) })
@ -24,13 +24,13 @@ impl fmt::Display for Tree {
} }
}; };
write!(f, "({} :{}|{})", self.state, node_str(&self.left), node_str(&self.right)) write!(f, "({} :{}|{})", self.id, node_str(&self.left), node_str(&self.right))
} }
} }
fn fmt_node(t: &Option<Box<Tree>>) -> String { fn fmt_node(t: &Option<Box<Tree>>) -> String {
match t { match t {
Some(n) => format!("{}", (*n).state), Some(n) => format!("{}", (*n).id),
_ => String::from("_") _ => String::from("_")
} }
} }
@ -38,7 +38,7 @@ fn fmt_node(t: &Option<Box<Tree>>) -> String {
impl Tree { impl Tree {
pub fn run_simulation(&self) { pub fn run_simulation(&self) {
println!("================================"); println!("================================");
println!("Running simulation for node: {}", self.state); println!("Running simulation for node: {}", self.id);
println!("With children {} and {}", fmt_node(&self.left), fmt_node(&self.right)); println!("With children {} and {}", fmt_node(&self.left), fmt_node(&self.right));
} }
} }