Add a clear() method to bforest::{Set,Map}.

This is a lot more efficient that removing entries one by one.
This commit is contained in:
Jakob Stoklund Olesen
2017-11-20 11:08:25 -08:00
parent d3778e56bb
commit 3389eaef29
3 changed files with 42 additions and 0 deletions

View File

@@ -52,6 +52,20 @@ impl<F: Forest> NodePool<F> {
self.nodes[node] = NodeData::Free { next: self.freelist };
self.freelist = Some(node);
}
/// Free the entire tree rooted at `node`.
pub fn free_tree(&mut self, node: Node) {
if let NodeData::Inner { size, tree, .. } = self[node] {
// Note that we have to capture `tree` by value to avoid borrow checker trouble.
for i in 0..usize::from(size + 1) {
// Recursively free sub-trees. This recursion can never be deeper than `MAX_PATH`,
// and since most trees have less than a handful of nodes, it is worthwhile to
// avoid the heap allocation for an iterative tree traversal.
self.free_tree(tree[i]);
}
}
self.free_node(node);
}
}
#[cfg(test)]