Remove the previous arena impl.

This commit is contained in:
Bas Wiel, van de 2024-05-09 16:33:03 +02:00
parent 058b036286
commit d33324da1a
1 changed files with 0 additions and 65 deletions

View File

@ -1,65 +0,0 @@
struct Node<T> {
value: T,
children: Vec<NodeId>,
parent: Option<NodeId>,
}
struct Arena<T> {
nodes: Vec<Node<T>>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
struct NodeId(usize);
impl<T> Arena<T> {
fn new() -> Self {
Arena { nodes: Vec::new() }
}
fn new_node(&mut self, value: T) -> NodeId {
let node_id = NodeId(self.nodes.len());
self.nodes.push(Node {
value,
children: Vec::new(),
parent: None,
});
node_id
}
fn insert_node(&mut self, node: Node<T>) -> NodeId {
let node_id = NodeId(self.nodes.len());
self.nodes.push(node);
node_id
}
fn add_child(&mut self, parent_id: NodeId, child_id: NodeId) {
if let Some(node) = self.nodes.get_mut(parent_id.0) {
node.children.push(child_id);
}
if let Some(node) = self.nodes.get_mut(child_id.0) {
node.parent = Some(parent_id);
}
}
fn get_node_mut(&mut self, node_id: NodeId) -> Option<&mut Node<T>> {
self.nodes.get_mut(node_id.0)
}
fn iter_mut(&mut self) -> std::slice::IterMut<Node<T>> {
self.nodes.iter_mut()
}
fn traverse_and_modify(&mut self, start_id: NodeId, modify_fn: &mut dyn FnMut(&mut Node<T>)) {
let mut stack = vec![start_id];
while let Some(node_id) = stack.pop() {
if let Some(node) = self.get_node_mut(node_id) {
modify_fn(node);
for &child_id in &node.children {
stack.push(child_id);
}
}
}
}
}