Skip to content

Commit

Permalink
Refactor Forest::mark_dirty for clarity (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
alice-i-cecile committed Jun 9, 2022
1 parent fbc09ab commit f644639
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/forest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ impl NodeData {
is_dirty: true,
}
}

/// Marks a node and all of its parents (recursively) as dirty
///
/// This clears any cached data and signals that the data must be recomputed.
#[inline]
fn mark_dirty(&mut self) {
self.main_size_layout_cache = None;
self.other_layout_cache = None;
self.is_dirty = true;
}
}

pub(crate) struct Forest {
Expand Down Expand Up @@ -169,18 +179,17 @@ impl Forest {
}

pub fn mark_dirty(&mut self, node: NodeId) {
fn mark_dirty_impl(nodes: &mut Vec<NodeData>, parents: &[ParentsVec<NodeId>], node_id: NodeId) {
let node = &mut nodes[node_id];
node.main_size_layout_cache = None;
node.other_layout_cache = None;
node.is_dirty = true;
// Performs a recursive depth-first search up the tree until the root node is reached
// WARNING: this will stack-overflow if the tree contains a cycle
fn mark_dirty_recursive(nodes: &mut Vec<NodeData>, parents: &[ParentsVec<NodeId>], node_id: NodeId) {
nodes[node_id].mark_dirty();

for parent in &parents[node_id] {
mark_dirty_impl(nodes, parents, *parent);
mark_dirty_recursive(nodes, parents, *parent);
}
}

mark_dirty_impl(&mut self.nodes, &self.parents, node);
mark_dirty_recursive(&mut self.nodes, &self.parents, node);
}

pub fn compute_layout(&mut self, node: NodeId, size: Size<Number>) {
Expand Down

0 comments on commit f644639

Please sign in to comment.