Skip to content

Commit

Permalink
Update update.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
wiiznokes committed Nov 10, 2023
1 parent aaa45b6 commit 78de84a
Showing 1 changed file with 37 additions and 65 deletions.
102 changes: 37 additions & 65 deletions data/src/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,86 +30,58 @@ impl Update {
Self {}
}


fn update2(nodes: &mut Nodes, node_id: &Id, updated: &mut HashSet<Id> ) -> Result<Option<Value>, UpdateError> {

let Some(node) = nodes.get_mut(node_id) else {
return Err(UpdateError::NodeNotFound);
};

if !updated.contains(&node_id) {

if !node.is_valid() {
return Ok(None);
}
pub fn graph(&mut self, nodes: &mut Nodes, root_nodes: &RootNodes) -> Result<(), UpdateError> {
let mut updated: HashSet<Id> = HashSet::new();

let mut input_values = Vec::new();
for id in &node.inputs {
match Self::update2(nodes, id, updated)? {
Some(value) => input_values.push(value),
None => return Ok(None),
}
}

node.update(&input_values)?;
updated.insert(node.id);
for node_id in root_nodes {
Self::update_rec(nodes, node_id, &mut updated)?;
}

return Ok(node.value);
Ok(())
}



pub fn graph(&mut self, nodes: &mut Nodes, root_nodes: &RootNodes) -> Result<(), UpdateError> {
let mut to_update: Vec<Id> = Vec::new();

for node_id in root_nodes {
let Some(node) = nodes.get(node_id) else {
return Err(UpdateError::NodeNotFound);
};
pub fn clear_cache(&mut self) {}

let mut ids = Vec::new();
if node.validate(nodes, &mut ids)? {
to_update.extend(ids);
fn update_rec(
nodes: &mut Nodes,
node_id: &Id,
updated: &mut HashSet<Id>,
) -> Result<Option<Value>, UpdateError> {
if updated.contains(node_id) {
return match nodes.get(node_id) {
Some(node) => Ok(node.value),
None => Err(UpdateError::NodeNotFound),
};
}

let mut updated: HashSet<Id> = HashSet::new();

to_update.reverse();

let input_ids: Vec<Id>;
let mut input_values = Vec::new();
for node_id in to_update {
if !updated.contains(&node_id) {
let Some(node) = nodes.get(&node_id) else {
return Err(UpdateError::NodeNotFound);
};

input_values.clear();
for id in &node.inputs {
match nodes.get(id) {
Some(node) => match node.value {
Some(value) => input_values.push(value),
None => return Err(UpdateError::ValueIsNone),
},
None => return Err(UpdateError::NodeNotFound),
}
}

let Some(node) = nodes.get_mut(&node_id) else {
return Err(UpdateError::NodeNotFound);
};
{
let Some(node) = nodes.get(node_id) else {
return Err(UpdateError::NodeNotFound);
};

node.update(&input_values)?;
if !node.is_valid() {
return Ok(None);
}
input_ids = node.inputs.clone();
}

updated.insert(node.id);
for id in &input_ids {
match Self::update_rec(nodes, id, updated)? {
Some(value) => input_values.push(value),
None => return Ok(None),
}
}

Ok(())
}
let Some(node) = nodes.get_mut(node_id) else {
return Err(UpdateError::NodeNotFound);
};

pub fn clear_cache(&mut self) {}
node.update(&input_values)?;
updated.insert(node.id);

Ok(node.value)
}
}

impl Node {
Expand Down

0 comments on commit 78de84a

Please sign in to comment.