Tree

Enum Tree 

Source
pub enum Tree<T: Value> {
    Leaf(Leaf<T>),
    PackedLeaf(PackedLeaf<T>),
    Node {
        hash: RwLock<Hash256>,
        left: Arc<Self>,
        right: Arc<Self>,
    },
    Zero(usize),
}

Variants§

§

Leaf(Leaf<T>)

§

PackedLeaf(PackedLeaf<T>)

§

Node

Fields

§left: Arc<Self>
§right: Arc<Self>
§

Zero(usize)

Implementations§

Source§

impl<T: Value> Tree<T>

Source

pub fn empty(depth: usize) -> Arc<Self>

Source

pub fn node(left: Arc<Self>, right: Arc<Self>, hash: Hash256) -> Arc<Self>

Source

pub fn zero(depth: usize) -> Arc<Self>

Source

pub fn leaf(value: T) -> Arc<Self>

Source

pub fn leaf_with_hash(value: T, hash: Hash256) -> Arc<Self>

Source

pub fn node_unboxed(left: Arc<Self>, right: Arc<Self>) -> Self

Source

pub fn zero_unboxed(depth: usize) -> Self

Source

pub fn leaf_unboxed(value: T) -> Self

Source

pub fn get_recursive( &self, index: usize, depth: usize, packing_depth: usize, ) -> Option<&T>

Source

pub fn with_updated_leaf( &self, index: usize, new_value: T, depth: usize, ) -> Result<Arc<Self>, Error>

Create a new tree where the indexth leaf is set to new_value.

NOTE: callers are responsible for bounds-checking index before calling this function.

Source

pub fn with_updated_leaves<U: UpdateMap<T>>( &self, updates: &U, prefix: usize, depth: usize, hashes: Option<&BTreeMap<(usize, usize), Hash256>>, ) -> Result<Arc<Self>, Error>

Source

pub fn compute_len(&self) -> usize

Compute the number of elements stored in this subtree.

This method should be avoided if possible. Prefer to read the length cached in a List or similar.

Source§

impl<T: Value> Tree<T>

Source

pub fn rebase_on<'a>( orig: &'a Arc<Self>, base: &'a Arc<Self>, lengths: Option<(Length, Length)>, full_depth: usize, ) -> Result<RebaseAction<'a, Self>, Error>

Source

pub fn intra_rebase( orig: &Arc<Self>, known_subtrees: &mut HashMap<(usize, Hash256), Arc<Self>>, current_depth: usize, ) -> Result<IntraRebaseAction<Self>, Error>

Exploit structural sharing between identical parts of the tree.

This method traverses a fully-hashed tree and replaces identical subtrees with clones of the first equal subtree. The result is a tree that shares memory for common subtrees, and thus uses less memory overall.

You MUST pass a fully-hashed tree to this function, or an Error::IntraRebaseZeroHash error will be returned.

Arguments are:

  • orig: The tree to rebase.
  • known_subtrees: map from (depth, tree_hash_root) to Arc<Node>. This should be empty for the top-level call. The recursive calls fill it in. It can be discarded after the method returns.
  • current_depth: The depth of the tree orig. This will be decremented as we recurse down the tree towards the leaves.

Presently leaves are left untouched by this procedure, so it will only produce savings in trees with equal internal nodes (i.e. equal subtrees with at least two leaves/packed leaves under them).

The input tree must be fully-hashed, and the result will also remain fully-hashed.

Source§

impl<T: Value + Send + Sync> Tree<T>

Source

pub fn tree_hash(&self) -> Hash256

Trait Implementations§

Source§

impl<T: Value> Clone for Tree<T>

Source§

fn clone(&self) -> Self

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<T: Debug + Value> Debug for Tree<T>

Source§

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

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

impl<T: Value> Hash for Tree<T>
where Leaf<T>: Hash, PackedLeaf<T>: Hash, Arc<Self>: Hash, usize: Hash,

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T: Value + MemorySize> MemorySize for Tree<T>

Source§

fn self_pointer(&self) -> usize

The memory address of this item.
Source§

fn subtrees(&self) -> Vec<&dyn MemorySize>

Subtrees (Arcs) for this type’s fields that consume memory.
Source§

fn intrinsic_size(&self) -> usize

Memory consumed by this type’s non-recursive fields.
Source§

impl<T> PartialEq for Tree<T>
where T: Value,

Source§

fn eq(&self, other: &Self) -> 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.

Auto Trait Implementations§

§

impl<T> !Freeze for Tree<T>

§

impl<T> !RefUnwindSafe for Tree<T>

§

impl<T> Send for Tree<T>
where T: Sync + Send,

§

impl<T> Sync for Tree<T>
where T: Sync + Send,

§

impl<T> Unpin for Tree<T>
where T: Unpin,

§

impl<T> !UnwindSafe for Tree<T>

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<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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, 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.