From 8946a3cfb89030b4108c1043a8a0c674bef80800 Mon Sep 17 00:00:00 2001 From: Jacob VanDomelen Date: Sun, 26 May 2019 22:24:12 -0700 Subject: [PATCH] Genericized Tree struct implementation. --- gemla/src/bracket/mod.rs | 9 ++++---- gemla/src/bracket/tree.rs | 44 --------------------------------------- gemla/src/main.rs | 1 + gemla/src/tree/mod.rs | 43 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 49 insertions(+), 48 deletions(-) delete mode 100644 gemla/src/bracket/tree.rs create mode 100644 gemla/src/tree/mod.rs diff --git a/gemla/src/bracket/mod.rs b/gemla/src/bracket/mod.rs index ce3709b..c452a8c 100644 --- a/gemla/src/bracket/mod.rs +++ b/gemla/src/bracket/mod.rs @@ -1,6 +1,7 @@ -mod tree; mod state; +use super::tree; + use uuid::Uuid; // pub struct Bracket { @@ -9,8 +10,8 @@ use uuid::Uuid; // } /// Constructs a tree with a given height while simultaneously running a simulation on each node. -fn build_tree(h: u32) -> Option> { - let mut result: Option> = None; +fn build_tree(h: u32) -> Option>> { + let mut result: Option>> = None; // Recursively building a tree and running the simulation after wards to ensure a bottom-up // execution order. @@ -30,7 +31,7 @@ fn build_tree(h: u32) -> Option> { /// TODO: Explain reasoning for bracket system against genetic algorithm. pub fn run_bracket() { let mut height = 1; - let mut tree: tree::Tree = *build_tree(height).expect("Error getting result from build_tree."); + let mut tree: tree::Tree = *build_tree(height).expect("Error getting result from build_tree."); // Building tree one node at a time, appending to the top. loop { diff --git a/gemla/src/bracket/tree.rs b/gemla/src/bracket/tree.rs deleted file mode 100644 index 7a728d8..0000000 --- a/gemla/src/bracket/tree.rs +++ /dev/null @@ -1,44 +0,0 @@ -use std::fmt; -use uuid::Uuid; - -pub struct Tree { - id: Uuid, - left: Option>, - right: Option> -} - -pub fn combine_trees(id: Uuid, l: Option>, r: Option>) -> Tree { - Tree { - id: id, - left: l, - right: r - } -} - -impl fmt::Display for Tree { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - let node_str = |t: &Option>| -> String { - match t { - Some(n) => format!("{}", *n), - _ => String::from("_") - } - }; - - write!(f, "({} :{}|{})", self.id, node_str(&self.left), node_str(&self.right)) - } -} - -fn fmt_node(t: &Option>) -> String { - match t { - Some(n) => format!("{}", (*n).id), - _ => String::from("_") - } -} - -impl Tree { - pub fn run_simulation(&self) { - println!("================================"); - println!("Running simulation for node: {}", self.id); - println!("With children {} and {}", fmt_node(&self.left), fmt_node(&self.right)); - } -} \ No newline at end of file diff --git a/gemla/src/main.rs b/gemla/src/main.rs index 8dcabf9..094d22a 100644 --- a/gemla/src/main.rs +++ b/gemla/src/main.rs @@ -1,4 +1,5 @@ mod bracket; +mod tree; mod constants; #[macro_use] diff --git a/gemla/src/tree/mod.rs b/gemla/src/tree/mod.rs new file mode 100644 index 0000000..03faf9d --- /dev/null +++ b/gemla/src/tree/mod.rs @@ -0,0 +1,43 @@ +use std::fmt; + +pub struct Tree { + val: T, + left: Option>>, + right: Option>> +} + +pub fn combine_trees(v: T, l: Option>>, r: Option>>) -> Tree { + Tree { + val: v, + left: l, + right: r + } +} + +impl fmt::Display for Tree { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let node_str = |t: &Option>>| -> String { + match t { + Some(n) => format!("{}", *n), + _ => String::from("_") + } + }; + + write!(f, "({} :{}|{})", self.val, node_str(&self.left), node_str(&self.right)) + } +} + +fn fmt_node(t: &Option>>) -> String { + match t { + Some(n) => format!("{}", (*n).val), + _ => String::from("_") + } +} + +impl Tree { + pub fn run_simulation(&self) { + println!("================================"); + println!("Running simulation for node: {}", self.val); + println!("With children {} and {}", fmt_node(&self.left), fmt_node(&self.right)); + } +} \ No newline at end of file