Forest

Struct Forest 

Source
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

Source

pub const fn empty() -> Forest

Creates an empty forest (no trees).

Source

pub const fn new(num_leaves: usize) -> Forest

Creates a forest with num_leaves leaves.

Source

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.

Source

pub fn is_empty(self) -> bool

Returns true if there are no trees in the forest.

Source

pub fn append_leaf(&mut self)

Adds exactly one more leaf to the capacity of this forest.

Some smaller trees might be merged together.

Source

pub fn num_leaves(self) -> usize

Returns a count of leaves in the entire underlying forest (MMR).

Source

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.

Source

pub const fn num_trees(self) -> usize

Return the total number of trees of a given forest (the number of active bits).

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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));
Source

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().

Source

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.

Source

pub fn next_larger_tree(self) -> Forest

Returns a forest with exactly one tree, one size (depth) larger than the current one.

Source

pub fn has_single_leaf_tree(self) -> bool

Returns true if the forest contains a single-node tree.

Source

pub fn with_single_leaf(self) -> Forest

Add a single-node tree if not already present in the forest.

Source

pub fn without_single_leaf(self) -> Forest

Remove the single-node tree if present in the forest.

Source

pub fn without_trees(self, other: Forest) -> Forest

Returns a new forest that does not have the trees that other has.

Source

pub fn tree_index(&self, leaf_idx: usize) -> usize

Returns index of the forest tree for a specified leaf index.

Source

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.

Source

pub fn rightmost_in_order_index(&self) -> InOrderIndex

Returns the in-order index of the rightmost element (the smallest tree).

Source

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+1 is the depth of the tree.
  • Because the root element is not part of the proof, p is the length of the authentication path.
  • 2^p is equal to the number of leaves in this particular tree.
  • And 2^(p+1)-1 corresponds 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            5

Leaf 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 Binary for Forest

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl BitAnd for Forest

Source§

type Output = Forest

The resulting type after applying the & operator.
Source§

fn bitand(self, rhs: Forest) -> <Forest as BitAnd>::Output

Performs the & operation. Read more
Source§

impl BitOr for Forest

Source§

type Output = Forest

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: Forest) -> <Forest as BitOr>::Output

Performs the | operation. Read more
Source§

impl BitXor for Forest

Source§

type Output = Forest

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, rhs: Forest) -> <Forest as BitXor>::Output

Performs the ^ operation. Read more
Source§

impl BitXorAssign for Forest

Source§

fn bitxor_assign(&mut self, rhs: Forest)

Performs the ^= operation. Read more
Source§

impl Clone for Forest

Source§

fn clone(&self) -> Forest

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Forest

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Default for Forest

Source§

fn default() -> Forest

Returns the “default value” for a type. Read more
Source§

impl Deserializable for Forest

Source§

fn read_from<R>(source: &mut R) -> Result<Forest, DeserializationError>
where R: ByteReader,

Reads a sequence of bytes from the provided source, attempts to deserialize these bytes into Self, and returns the result. Read more
Source§

fn read_from_bytes(bytes: &[u8]) -> Result<Self, DeserializationError>

Attempts to deserialize the provided bytes into Self and returns the result. Read more
Source§

impl Display for Forest

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl From<BaseElement> for Forest

Source§

fn from(value: BaseElement) -> Forest

Converts to this type from the input type.
Source§

impl From<Forest> for BaseElement

Source§

fn from(value: Forest) -> BaseElement

Converts to this type from the input type.
Source§

impl Ord for Forest

Source§

fn cmp(&self, other: &Forest) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Forest

Source§

fn eq(&self, other: &Forest) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Forest

Source§

fn partial_cmp(&self, other: &Forest) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Serializable for Forest

Source§

fn write_into<W>(&self, target: &mut W)
where W: ByteWriter,

Serializes self into bytes and writes these bytes into the target.
Source§

fn to_bytes(&self) -> Vec<u8>

Serializes self into a vector of bytes.
Source§

fn get_size_hint(&self) -> usize

Returns an estimate of how many bytes are needed to represent self. Read more
Source§

impl Copy for Forest

Source§

impl Eq for Forest

Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<D> OwoColorize for D

Source§

fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>
where C: Color,

Set the foreground color generically Read more
Source§

fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>
where C: Color,

Set the background color generically. Read more
Source§

fn black(&self) -> FgColorDisplay<'_, Black, Self>

Change the foreground color to black
Source§

fn on_black(&self) -> BgColorDisplay<'_, Black, Self>

Change the background color to black
Source§

fn red(&self) -> FgColorDisplay<'_, Red, Self>

Change the foreground color to red
Source§

fn on_red(&self) -> BgColorDisplay<'_, Red, Self>

Change the background color to red
Source§

fn green(&self) -> FgColorDisplay<'_, Green, Self>

Change the foreground color to green
Source§

fn on_green(&self) -> BgColorDisplay<'_, Green, Self>

Change the background color to green
Source§

fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>

Change the foreground color to yellow
Source§

fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>

Change the background color to yellow
Source§

fn blue(&self) -> FgColorDisplay<'_, Blue, Self>

Change the foreground color to blue
Source§

fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>

Change the background color to blue
Source§

fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>

Change the foreground color to magenta
Source§

fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>

Change the background color to magenta
Source§

fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>

Change the foreground color to purple
Source§

fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>

Change the background color to purple
Source§

fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>

Change the foreground color to cyan
Source§

fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>

Change the background color to cyan
Source§

fn white(&self) -> FgColorDisplay<'_, White, Self>

Change the foreground color to white
Source§

fn on_white(&self) -> BgColorDisplay<'_, White, Self>

Change the background color to white
Source§

fn default_color(&self) -> FgColorDisplay<'_, Default, Self>

Change the foreground color to the terminal default
Source§

fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>

Change the background color to the terminal default
Source§

fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>

Change the foreground color to bright black
Source§

fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>

Change the background color to bright black
Source§

fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>

Change the foreground color to bright red
Source§

fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>

Change the background color to bright red
Source§

fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>

Change the foreground color to bright green
Source§

fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>

Change the background color to bright green
Source§

fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>

Change the foreground color to bright yellow
Source§

fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>

Change the background color to bright yellow
Source§

fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>

Change the foreground color to bright blue
Source§

fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>

Change the background color to bright blue
Source§

fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>

Change the foreground color to bright magenta
Source§

fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>

Change the background color to bright magenta
Source§

fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>

Change the foreground color to bright purple
Source§

fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>

Change the background color to bright purple
Source§

fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>

Change the foreground color to bright cyan
Source§

fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>

Change the background color to bright cyan
Source§

fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>

Change the foreground color to bright white
Source§

fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>

Change the background color to bright white
Source§

fn bold(&self) -> BoldDisplay<'_, Self>

Make the text bold
Source§

fn dimmed(&self) -> DimDisplay<'_, Self>

Make the text dim
Source§

fn italic(&self) -> ItalicDisplay<'_, Self>

Make the text italicized
Source§

fn underline(&self) -> UnderlineDisplay<'_, Self>

Make the text underlined
Make the text blink
Make the text blink (but fast!)
Source§

fn reversed(&self) -> ReversedDisplay<'_, Self>

Swap the foreground and background colors
Source§

fn hidden(&self) -> HiddenDisplay<'_, Self>

Hide the text
Source§

fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>

Cross out the text
Source§

fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>
where Color: DynColor,

Set the foreground color at runtime. Only use if you do not know which color will be used at compile-time. If the color is constant, use either OwoColorize::fg or a color-specific method, such as OwoColorize::green, Read more
Source§

fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>
where Color: DynColor,

Set the background color at runtime. Only use if you do not know what color to use at compile-time. If the color is constant, use either OwoColorize::bg or a color-specific method, such as OwoColorize::on_yellow, Read more
Source§

fn fg_rgb<const R: u8, const G: u8, const B: u8>( &self, ) -> FgColorDisplay<'_, CustomColor<R, G, B>, Self>

Set the foreground color to a specific RGB value.
Source§

fn bg_rgb<const R: u8, const G: u8, const B: u8>( &self, ) -> BgColorDisplay<'_, CustomColor<R, G, B>, Self>

Set the background color to a specific RGB value.
Source§

fn truecolor(&self, r: u8, g: u8, b: u8) -> FgDynColorDisplay<'_, Rgb, Self>

Sets the foreground color to an RGB value.
Source§

fn on_truecolor(&self, r: u8, g: u8, b: u8) -> BgDynColorDisplay<'_, Rgb, Self>

Sets the background color to an RGB value.
Source§

fn style(&self, style: Style) -> Styled<&Self>

Apply a runtime-determined style
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more