From b70693edce4f04af271c6baf19e4c2ad24fb6c62 Mon Sep 17 00:00:00 2001 From: Jack Case Date: Fri, 27 Mar 2026 06:57:03 -0400 Subject: [PATCH] implement iterator for blocktreenode this makes more sense as a useful iterator because block ids are copy. This can be used to implement mutable/immutable reference iteration for NotePage. --- src/blocktree.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/blocktree.rs b/src/blocktree.rs index eba32bf..6e42380 100644 --- a/src/blocktree.rs +++ b/src/blocktree.rs @@ -38,6 +38,42 @@ impl BlockTreeNode { } } +struct BlockTreeIterator<'a> { + node_stack: Vec<&'a BlockTreeNode> +} + +impl<'a> BlockTreeIterator<'a> { + fn new(root_node: &'a BlockTreeNode) -> Self { + Self { + node_stack: vec![root_node] + } + } +} + +impl<'a> Iterator for BlockTreeIterator<'a> { + type Item = i128; + fn next(&mut self) -> Option { + + // stack is empty, we are done otherwise continue with current_node + let Some(current_node) = self.node_stack.pop() else { + return None; + }; + + // if the current node has a next sibling, stack it. + if let Some(next_sibling_node) = ¤t_node.next_sibling_node { + self.node_stack.push(next_sibling_node); + } + + // if the current node has a first child, stack it. + if let Some(first_child_node) = ¤t_node.first_child_node { + self.node_stack.push(first_child_node); + } + + // yield a clone of the block pointed to by current_node + Some(current_node.block_id) + } +} + #[derive(Debug)] pub struct NotePage { title: String,