implemented iterator that yields cloned blocks from a page

This commit is contained in:
2026-03-27 06:49:10 -04:00
parent a26dfbd290
commit 0fe4b4132d

View File

@@ -1,7 +1,7 @@
use std::collections::HashMap; use std::collections::HashMap;
#[derive(Debug)] #[derive(Debug, Clone)]
pub struct NoteBlock { pub struct NoteBlock {
// IDs are int64 as this is the datatype of rowids in sqlite // IDs are int64 as this is the datatype of rowids in sqlite
id: i128, id: i128,
@@ -87,22 +87,41 @@ impl NotePage {
} }
} }
struct NotePageIterator { struct NotePageIterator<'a> {
page: &NotePage, page: &'a NotePage,
node_stack: Vec<&BlockTreeNode> node_stack: Vec<&'a BlockTreeNode>
} }
impl NotePageIterator { impl<'a> NotePageIterator<'a> {
fn new(page: 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; type Item = NoteBlock;
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
// 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) = &current_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) = &current_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(&current_node.block_id).unwrap().clone())
} }
} }