make get node parent mut optionally return a parent

This commit is contained in:
Evan Almloff 2022-11-24 22:16:27 -06:00
parent 21edea62be
commit e08a2186a6
2 changed files with 20 additions and 3 deletions

View file

@ -97,7 +97,7 @@ impl<S: State> RealDom<S> {
} }
fn resolve_height(&mut self, node_id: RealNodeId) { fn resolve_height(&mut self, node_id: RealNodeId) {
if let Some((node, parent)) = self.tree.node_parent_mut(node_id) { if let Some((node, Some(parent))) = self.tree.node_parent_mut(node_id) {
let height = parent.node_data.height; let height = parent.node_data.height;
node.node_data.height = height + 1; node.node_data.height = height + 1;
unsafe { unsafe {

View file

@ -94,13 +94,13 @@ pub trait TreeView<T>: Sized {
fn parent_mut(&mut self, id: NodeId) -> Option<&mut T>; fn parent_mut(&mut self, id: NodeId) -> Option<&mut T>;
fn node_parent_mut(&mut self, id: NodeId) -> Option<(&mut T, &mut T)> { fn node_parent_mut(&mut self, id: NodeId) -> Option<(&mut T, Option<&mut T>)> {
let mut_ptr: *mut Self = self; let mut_ptr: *mut Self = self;
unsafe { unsafe {
// Safety: No node has itself as a parent. // Safety: No node has itself as a parent.
(*mut_ptr) (*mut_ptr)
.get_mut(id) .get_mut(id)
.and_then(|parent| (*mut_ptr).parent_mut(id).map(|children| (parent, children))) .map(|node| (node, (*mut_ptr).parent_mut(id).map(|parent| parent)))
} }
} }
@ -819,3 +819,20 @@ fn traverse_breadth_first() {
node_count += 1; node_count += 1;
}); });
} }
trait UpwardPass<T> {
fn upward_pass(&mut self, node: &mut T, parent: Option<&mut T>) -> bool;
fn resolve_pass(&mut self, tree: &mut impl TreeView<T>, starting_nodes: &[NodeId]) {
let mut stack = Vec::new();
for node in starting_nodes {
stack.push(*node);
}
while let Some(node_id) = stack.pop() {
let (node, parent) = tree.node_parent_mut(node_id).unwrap();
if self.upward_pass(node, parent) {
stack.push(tree.parent_id(node_id).unwrap());
}
}
}
}