Gigagraph
This commit is contained in:
parent
3491eb28d5
commit
d07c660e2a
6 changed files with 24 additions and 50 deletions
|
|
@ -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<node::Node>) -> Self {
|
||||
match optional_root {
|
||||
Some(root) => Formula { root },
|
||||
None => Formula {
|
||||
root: node::Node::empty(),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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>) -> f64) -> GraphBuilder {
|
||||
let mut node_lookup = self.0;
|
||||
for i in path.split("/").map(str::parse::<i64>) {
|
||||
if let Ok(index) = i {
|
||||
node_lookup = node_lookup.children[index];
|
||||
}
|
||||
}
|
||||
self
|
||||
}
|
||||
pub fn finish(self) -> Node {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
|
@ -1 +0,0 @@
|
|||
struct GraphModifier;
|
||||
|
|
@ -1 +1 @@
|
|||
mod formula;
|
||||
mod node;
|
||||
|
|
|
|||
12
src/node/graph_modifier.rs
Normal file
12
src/node/graph_modifier.rs
Normal file
|
|
@ -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 }
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -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<Vec<Node>>, function: Option<fn(Vec<f64>) -> 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)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue