use crate::merkle::{Family, Location, Position};
pub(super) const MAX_PATH_LEN: usize = u64::BITS as usize - 1;
#[derive(Debug)]
pub struct Iterator<F: Family> {
target_loc: Location<F>, node_pos: Position<F>, first_leaf: u64, height: u32, }
impl<F: Family> Iterator<F> {
pub const fn new(
peak_pos: Position<F>,
height: u32,
first_leaf_loc: Location<F>,
target_loc: Location<F>,
) -> Self {
Self {
target_loc,
node_pos: peak_pos,
first_leaf: first_leaf_loc.as_u64(),
height,
}
}
}
impl<F: Family> core::iter::Iterator for Iterator<F> {
type Item = (Position<F>, Position<F>, u32);
fn next(&mut self) -> Option<Self::Item> {
if self.height == 0 {
return None;
}
let parent_pos = self.node_pos;
let parent_height = self.height;
let (left, right) = F::children(parent_pos, parent_height);
let mid = self.first_leaf + (1u64 << (parent_height - 1));
self.height -= 1;
if self.target_loc.as_u64() < mid {
self.node_pos = left;
Some((parent_pos, right, parent_height))
} else {
self.node_pos = right;
self.first_leaf = mid;
Some((parent_pos, left, parent_height))
}
}
}