Added dynamic simulation.
This commit is contained in:
parent
b5f39f294a
commit
673b0dd5eb
3 changed files with 46 additions and 10 deletions
|
@ -1,12 +1,15 @@
|
||||||
mod tree;
|
mod tree;
|
||||||
|
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(Uuid::new_v4(), build_tree(h - 1), build_tree(h - 1)));
|
result = Some(tree::concat_trees(state::create(), build_tree(h - 1), build_tree(h - 1)));
|
||||||
|
match &result {
|
||||||
|
Some(r) => (*r).run_simulation(),
|
||||||
|
_ => ()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
result
|
result
|
||||||
|
@ -19,8 +22,8 @@ pub fn run_bracket() {
|
||||||
loop {
|
loop {
|
||||||
println!("=========================================");
|
println!("=========================================");
|
||||||
println!("Running bracket...");
|
println!("Running bracket...");
|
||||||
println!("{}", tree);
|
|
||||||
height += 1;
|
height += 1;
|
||||||
tree = *tree::concat_trees(Uuid::new_v4(), Some(Box::new(tree)), build_tree(height));
|
tree = *tree::concat_trees(state::create(), Some(Box::new(tree)), build_tree(height));
|
||||||
|
tree.run_simulation();
|
||||||
}
|
}
|
||||||
}
|
}
|
18
gemla/src/bracket/state.rs
Normal file
18
gemla/src/bracket/state.rs
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
use uuid::Uuid;
|
||||||
|
use std::fmt;
|
||||||
|
|
||||||
|
pub struct State {
|
||||||
|
id: Uuid
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for State {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
write!(f, "{}", self.id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn create() -> State {
|
||||||
|
State {
|
||||||
|
id: Uuid::new_v4()
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,15 +1,15 @@
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use uuid::Uuid;
|
use super::state;
|
||||||
|
|
||||||
pub struct Tree {
|
pub struct Tree {
|
||||||
id: Uuid,
|
state: state::State,
|
||||||
left: Option<Box<Tree>>,
|
left: Option<Box<Tree>>,
|
||||||
right: Option<Box<Tree>>
|
right: Option<Box<Tree>>
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn concat_trees(v: Uuid, l: Option<Box<Tree>>, r: Option<Box<Tree>>) -> Box<Tree> {
|
pub fn concat_trees(s: state::State, l: Option<Box<Tree>>, r: Option<Box<Tree>>) -> Box<Tree> {
|
||||||
Box::new(Tree {
|
Box::new(Tree {
|
||||||
id: v,
|
state: s,
|
||||||
left: l,
|
left: l,
|
||||||
right: r
|
right: r
|
||||||
})
|
})
|
||||||
|
@ -24,6 +24,21 @@ impl fmt::Display for Tree {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
write!(f, "({} :{}|{})", self.id, node_str(&self.left), node_str(&self.right))
|
write!(f, "({} :{}|{})", self.state, node_str(&self.left), node_str(&self.right))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn fmt_node(t: &Option<Box<Tree>>) -> String {
|
||||||
|
match t {
|
||||||
|
Some(n) => format!("{}", (*n).state),
|
||||||
|
_ => String::from("_")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Tree {
|
||||||
|
pub fn run_simulation(&self) {
|
||||||
|
println!("================================");
|
||||||
|
println!("Running simulation for node: {}", self.state);
|
||||||
|
println!("With children {} and {}", fmt_node(&self.left), fmt_node(&self.right));
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Add table
Reference in a new issue