diff --git a/src/blocktree.rs b/src/blocktree.rs index daa7b25..eba32bf 100644 --- a/src/blocktree.rs +++ b/src/blocktree.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct NoteBlock { // IDs are int64 as this is the datatype of rowids in sqlite id: i128, @@ -87,22 +87,41 @@ impl NotePage { } } -struct NotePageIterator { - page: &NotePage, - node_stack: Vec<&BlockTreeNode> +struct NotePageIterator<'a> { + page: &'a NotePage, + node_stack: Vec<&'a BlockTreeNode> } -impl NotePageIterator { - fn new(page: +impl<'a> NotePageIterator<'a> { + fn new(page: &'a NotePage) -> Self { + Self { + page: page, + node_stack: vec![&page.block_tree_root] + } + } } -impl Iterator for NotePageIterator { +impl<'a> Iterator for NotePageIterator<'a> { type Item = NoteBlock; fn next(&mut self) -> Option { - // no existing iteration state, initialize with root node - if let None = self.iter_state { + // 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(self.page.block_table.get(¤t_node.block_id).unwrap().clone()) } }