Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fork-tree insert requires post-order dfs traversal
  • Loading branch information
davxy committed May 26, 2022
commit 0acd8ea9ab4a46b2c42278fe046bf6f829d8b23b
29 changes: 10 additions & 19 deletions utils/fork-tree/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,29 +143,20 @@ where
}
}

let mut children = &mut self.roots;
let mut i = 0;
while i < children.len() {
let child = &children[i];
if child.hash == hash {
return Err(Error::Duplicate)
}
if child.number < number && is_descendent_of(&child.hash, &hash)? {
children = &mut children[i].children;
i = 0;
} else {
i += 1;
}
let (children, is_root) =
match self.find_node_where_mut(&hash, &number, is_descendent_of, &|_| true)? {
Some(parent) => (&mut parent.children, false),
None => (&mut self.roots, true),
};

if children.iter().any(|elem| elem.hash == hash) {
return Err(Error::Duplicate)
}

let is_first = children.is_empty();
children.push(Node { data, hash, number, children: Default::default() });

// Quick way to check if the pushed node is a root
let is_root = children.as_ptr() == self.roots.as_ptr();

if is_first {
// Rebalance is required only if we've extended the branch depth.
if children.len() == 1 {
// Rebalance may be required only if we've extended the branch depth.
self.rebalance();
}

Expand Down