diff --git a/src/formula/mod.rs b/src/formula/mod.rs deleted file mode 100644 index b0d4024..0000000 --- a/src/formula/mod.rs +++ /dev/null @@ -1,18 +0,0 @@ -use crate::formula::node::Node; - -pub mod node; - -pub struct Formula { - root: node::Node, -} - -impl Formula { - pub fn new(optional_root: Option) -> Self { - match optional_root { - Some(root) => Formula { root }, - None => Formula { - root: node::Node::empty(), - }, - } - } -} diff --git a/src/formula/node/graph_builder.rs b/src/formula/node/graph_builder.rs deleted file mode 100644 index 7b6ac61..0000000 --- a/src/formula/node/graph_builder.rs +++ /dev/null @@ -1,24 +0,0 @@ -use crate::formula::node::Node; - -pub struct GraphBuilder(Node); - -impl GraphBuilder { - pub fn from_hash_maps(mut self) -> GraphBuilder { - self - } - /// Path format: /index0/index1/index2/.../indexn - /// For example: /0/1/0/0/1 - /// The reason for unreadable format is because graph builder, as I ended up realising, doesn't need to be used. - pub fn add_at_path(mut self, path: String, func: fn(Vec) -> f64) -> GraphBuilder { - let mut node_lookup = self.0; - for i in path.split("/").map(str::parse::) { - if let Ok(index) = i { - node_lookup = node_lookup.children[index]; - } - } - self - } - pub fn finish(self) -> Node { - self.0 - } -} diff --git a/src/formula/node/graph_modifier.rs b/src/formula/node/graph_modifier.rs deleted file mode 100644 index 4e779d7..0000000 --- a/src/formula/node/graph_modifier.rs +++ /dev/null @@ -1 +0,0 @@ -struct GraphModifier; diff --git a/src/lib.rs b/src/lib.rs index 746f792..274142d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1 +1 @@ -mod formula; +mod node; diff --git a/src/node/graph_modifier.rs b/src/node/graph_modifier.rs new file mode 100644 index 0000000..df429e4 --- /dev/null +++ b/src/node/graph_modifier.rs @@ -0,0 +1,12 @@ +use crate::node::Node; + +pub struct GraphModifier<'a> { + root: &'a Node, +} + +impl<'a> GraphModifier<'a> { + pub fn new(root: &Node) -> GraphModifier { + GraphModifier { root } + } + +} diff --git a/src/formula/node/mod.rs b/src/node/mod.rs similarity index 59% rename from src/formula/node/mod.rs rename to src/node/mod.rs index 32a39bc..6aaa697 100644 --- a/src/formula/node/mod.rs +++ b/src/node/mod.rs @@ -1,6 +1,5 @@ -use crate::formula::node::graph_builder::GraphBuilder; +use crate::node::graph_modifier::GraphModifier; -mod graph_builder; mod graph_modifier; pub struct Node { @@ -9,15 +8,18 @@ pub struct Node { } impl Node { + pub fn new(children: Option>, function: Option) -> f64>) -> Self { + Self { + children: children.unwrap_or(vec![]), + function: function.unwrap_or(|_| 0f64), + } + } pub fn empty() -> Self { - Node { + Self { children: vec![], function: |_| 0f64, } } - pub fn build() -> GraphBuilder { - GraphBuilder - } pub fn get_value(&self) -> f64 { if self.children.len() == 0 { return (self.function)(vec![0f64]); @@ -29,4 +31,7 @@ impl Node { return (self.function)(inputs); } + pub fn modify_tree(&self) -> GraphModifier { + GraphModifier::new(self) + } }