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.
This commit is contained in:
@@ -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<Self::Item> {
|
||||||
|
|
||||||
|
// 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)]
|
#[derive(Debug)]
|
||||||
pub struct NotePage {
|
pub struct NotePage {
|
||||||
title: String,
|
title: String,
|
||||||
|
|||||||
Reference in New Issue
Block a user