pub struct Forest(/* private fields */);Expand description
A compact representation of trees in a forest. Used in the Merkle forest (MMR).
Each active bit of the stored number represents a disjoint tree with number of leaves equal to the bit position.
The forest value has the following interpretations:
- its value is the number of leaves in the forest
- the version number (MMR is append only so the number of leaves always increases)
- bit count corresponds to the number of trees (trees) in the forest
- each true bit position determines the depth of a tree in the forest
Examples:
Forest(0)is a forest with no trees.Forest(0b01)is a forest with a single leaf/node (the smallest tree possible).Forest(0b10)is a forest with a single binary tree with 2 leaves (3 nodes).Forest(0b11)is a forest with two trees: one with 1 leaf (1 node), and one with 2 leaves (3 nodes).Forest(0b1010)is a forest with two trees: one with 8 leaves (15 nodes), one with 2 leaves (3 nodes).Forest(0b1000)is a forest with one tree, which has 8 leaves (15 nodes).
Implementations§
Source§impl Forest
impl Forest
Sourcepub const fn with_height(height: usize) -> Forest
pub const fn with_height(height: usize) -> Forest
Creates a forest with a given height.
This is equivalent to Forest::new(1 << height).
§Panics
This will panic if height is greater than usize::BITS - 1.
Sourcepub fn append_leaf(&mut self)
pub fn append_leaf(&mut self)
Adds exactly one more leaf to the capacity of this forest.
Some smaller trees might be merged together.
Sourcepub fn num_leaves(self) -> usize
pub fn num_leaves(self) -> usize
Returns a count of leaves in the entire underlying forest (MMR).
Sourcepub const fn num_nodes(self) -> usize
pub const fn num_nodes(self) -> usize
Return the total number of nodes of a given forest.
§Panics
This will panic if the forest has size greater than usize::MAX / 2 + 1.
Sourcepub const fn num_trees(self) -> usize
pub const fn num_trees(self) -> usize
Return the total number of trees of a given forest (the number of active bits).
Sourcepub fn largest_tree_height_unchecked(self) -> usize
pub fn largest_tree_height_unchecked(self) -> usize
Returns the height (bit position) of the largest tree in the forest.
§Panics
This will panic if the forest is empty.
Sourcepub fn largest_tree_height(self) -> Option<usize>
pub fn largest_tree_height(self) -> Option<usize>
Returns the height (bit position) of the largest tree in the forest.
If the forest cannot be empty, use largest_tree_height_unchecked for performance.
Sourcepub fn largest_tree_unchecked(self) -> Forest
pub fn largest_tree_unchecked(self) -> Forest
Returns a forest with only the largest tree present.
§Panics
This will panic if the forest is empty.
Sourcepub fn largest_tree(self) -> Forest
pub fn largest_tree(self) -> Forest
Returns a forest with only the largest tree present.
If forest cannot be empty, use largest_tree for better performance.
Sourcepub fn smallest_tree_height_unchecked(self) -> usize
pub fn smallest_tree_height_unchecked(self) -> usize
Returns the height (bit position) of the smallest tree in the forest.
§Panics
This will panic if the forest is empty.
Sourcepub fn smallest_tree_height(self) -> Option<usize>
pub fn smallest_tree_height(self) -> Option<usize>
Returns the height (bit position) of the smallest tree in the forest.
If the forest cannot be empty, use smallest_tree_height_unchecked for better
performance.
Sourcepub fn smallest_tree_unchecked(self) -> Forest
pub fn smallest_tree_unchecked(self) -> Forest
Returns a forest with only the smallest tree present.
§Panics
This will panic if the forest is empty.
Sourcepub fn smallest_tree(self) -> Forest
pub fn smallest_tree(self) -> Forest
Returns a forest with only the smallest tree present.
If forest cannot be empty, use smallest_tree for performance.
Sourcepub fn trees_larger_than(self, tree_idx: u32) -> Forest
pub fn trees_larger_than(self, tree_idx: u32) -> Forest
Keeps only trees larger than the reference tree.
For example, if we start with the bit pattern 0b0101_0110, and keep only the trees larger
than tree index 1, that targets this bit:
Forest(0b0101_0110).trees_larger_than(1)
^
Becomes: 0b0101_0100
^And keeps only trees after that bit, meaning that the tree at tree_idx is also removed,
resulting in 0b0101_0100.
let range = Forest::new(0b0101_0110);
assert_eq!(range.trees_larger_than(1), Forest::new(0b0101_0100));Sourcepub fn all_smaller_trees_unchecked(self) -> Forest
pub fn all_smaller_trees_unchecked(self) -> Forest
Creates a new forest with all possible trees smaller than the smallest tree in this forest.
This forest must have exactly one tree.
§Panics
With debug assertions enabled, this function panics if this forest does not have exactly one tree.
For a non-panicking version of this function, see Forest::all_smaller_trees().
Sourcepub fn all_smaller_trees(self) -> Option<Forest>
pub fn all_smaller_trees(self) -> Option<Forest>
Creates a new forest with all possible trees smaller than the smallest tree in this
forest, or returns None if this forest has more or less than one tree.
If the forest cannot have more or less than one tree, use
Forest::all_smaller_trees_unchecked() for performance.
Sourcepub fn next_larger_tree(self) -> Forest
pub fn next_larger_tree(self) -> Forest
Returns a forest with exactly one tree, one size (depth) larger than the current one.
Sourcepub fn has_single_leaf_tree(self) -> bool
pub fn has_single_leaf_tree(self) -> bool
Returns true if the forest contains a single-node tree.
Sourcepub fn with_single_leaf(self) -> Forest
pub fn with_single_leaf(self) -> Forest
Add a single-node tree if not already present in the forest.
Sourcepub fn without_single_leaf(self) -> Forest
pub fn without_single_leaf(self) -> Forest
Remove the single-node tree if present in the forest.
Sourcepub fn without_trees(self, other: Forest) -> Forest
pub fn without_trees(self, other: Forest) -> Forest
Returns a new forest that does not have the trees that other has.
Sourcepub fn tree_index(&self, leaf_idx: usize) -> usize
pub fn tree_index(&self, leaf_idx: usize) -> usize
Returns index of the forest tree for a specified leaf index.
Sourcepub fn root_in_order_index(&self) -> InOrderIndex
pub fn root_in_order_index(&self) -> InOrderIndex
Returns the smallest tree’s root element as an InOrderIndex.
This function takes the smallest tree in this forest, “pretends” that it is a subtree of a fully balanced binary tree, and returns the the in-order index of that balanced tree’s root node.
Sourcepub fn rightmost_in_order_index(&self) -> InOrderIndex
pub fn rightmost_in_order_index(&self) -> InOrderIndex
Returns the in-order index of the rightmost element (the smallest tree).
Sourcepub fn leaf_to_corresponding_tree(self, leaf_idx: usize) -> Option<u32>
pub fn leaf_to_corresponding_tree(self, leaf_idx: usize) -> Option<u32>
Given a leaf index in the current forest, return the tree number responsible for the leaf.
Note:
The result is a tree position p, it has the following interpretations:
p+1is the depth of the tree.- Because the root element is not part of the proof,
pis the length of the authentication path. 2^pis equal to the number of leaves in this particular tree.- And
2^(p+1)-1corresponds to the size of the tree.
For example, given a forest with 6 leaves whose forest is 0b110:
__ tree 2 __
/ \
____ ____ _ tree 1 _
/ \ / \ / \
0 1 2 3 4 5Leaf indices 0..=3 are in the tree at index 2 and leaf indices 4..=5 are in the tree at
index 1.
Trait Implementations§
Source§impl BitXorAssign for Forest
impl BitXorAssign for Forest
Source§fn bitxor_assign(&mut self, rhs: Forest)
fn bitxor_assign(&mut self, rhs: Forest)
^= operation. Read moreSource§impl Deserializable for Forest
impl Deserializable for Forest
Source§fn read_from<R>(source: &mut R) -> Result<Forest, DeserializationError>where
R: ByteReader,
fn read_from<R>(source: &mut R) -> Result<Forest, DeserializationError>where
R: ByteReader,
source, attempts to deserialize these bytes
into Self, and returns the result. Read moreSource§fn read_from_bytes(bytes: &[u8]) -> Result<Self, DeserializationError>
fn read_from_bytes(bytes: &[u8]) -> Result<Self, DeserializationError>
Source§impl From<BaseElement> for Forest
impl From<BaseElement> for Forest
Source§fn from(value: BaseElement) -> Forest
fn from(value: BaseElement) -> Forest
Source§impl From<Forest> for BaseElement
impl From<Forest> for BaseElement
Source§fn from(value: Forest) -> BaseElement
fn from(value: Forest) -> BaseElement
Source§impl Ord for Forest
impl Ord for Forest
Source§impl PartialOrd for Forest
impl PartialOrd for Forest
Source§impl Serializable for Forest
impl Serializable for Forest
Source§fn write_into<W>(&self, target: &mut W)where
W: ByteWriter,
fn write_into<W>(&self, target: &mut W)where
W: ByteWriter,
self into bytes and writes these bytes into the target.Source§fn get_size_hint(&self) -> usize
fn get_size_hint(&self) -> usize
impl Copy for Forest
impl Eq for Forest
impl StructuralPartialEq for Forest
Auto Trait Implementations§
impl Freeze for Forest
impl RefUnwindSafe for Forest
impl Send for Forest
impl Sync for Forest
impl Unpin for Forest
impl UnwindSafe for Forest
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<D> OwoColorize for D
impl<D> OwoColorize for D
Source§fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
Source§fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
Source§fn black(&self) -> FgColorDisplay<'_, Black, Self>
fn black(&self) -> FgColorDisplay<'_, Black, Self>
Source§fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
Source§fn red(&self) -> FgColorDisplay<'_, Red, Self>
fn red(&self) -> FgColorDisplay<'_, Red, Self>
Source§fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
Source§fn green(&self) -> FgColorDisplay<'_, Green, Self>
fn green(&self) -> FgColorDisplay<'_, Green, Self>
Source§fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
Source§fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
Source§fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
Source§fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
Source§fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
Source§fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
Source§fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
Source§fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
Source§fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
Source§fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
Source§fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
Source§fn white(&self) -> FgColorDisplay<'_, White, Self>
fn white(&self) -> FgColorDisplay<'_, White, Self>
Source§fn on_white(&self) -> BgColorDisplay<'_, White, Self>
fn on_white(&self) -> BgColorDisplay<'_, White, Self>
Source§fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
Source§fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
Source§fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
Source§fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
Source§fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
Source§fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
Source§fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
Source§fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
Source§fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
Source§fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
Source§fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
Source§fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
Source§fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
Source§fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
Source§fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
Source§fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
Source§fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
Source§fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
Source§fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
Source§fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
Source§fn bold(&self) -> BoldDisplay<'_, Self>
fn bold(&self) -> BoldDisplay<'_, Self>
Source§fn dimmed(&self) -> DimDisplay<'_, Self>
fn dimmed(&self) -> DimDisplay<'_, Self>
Source§fn italic(&self) -> ItalicDisplay<'_, Self>
fn italic(&self) -> ItalicDisplay<'_, Self>
Source§fn underline(&self) -> UnderlineDisplay<'_, Self>
fn underline(&self) -> UnderlineDisplay<'_, Self>
Source§fn blink(&self) -> BlinkDisplay<'_, Self>
fn blink(&self) -> BlinkDisplay<'_, Self>
Source§fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
Source§fn reversed(&self) -> ReversedDisplay<'_, Self>
fn reversed(&self) -> ReversedDisplay<'_, Self>
Source§fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
Source§fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::fg or
a color-specific method, such as OwoColorize::green, Read moreSource§fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::bg or
a color-specific method, such as OwoColorize::on_yellow, Read more