fn append(&self, new_leaves: &[HashValue]) -> Result<(HashValue, Vec<Node>)> {
        // Deal with the case where new_leaves is empty
        if new_leaves.is_empty() {
            if self.num_leaves == 0 {
                return Ok((*ACCUMULATOR_PLACEHOLDER_HASH, Vec::new()));
            } else {
                let root_hash = self.get_hash(Position::root_from_leaf_count(self.num_leaves))?;
                return Ok((root_hash, Vec::new()));
            }
        }

        let num_new_leaves = new_leaves.len();
        let last_new_leaf_count = self.num_leaves + num_new_leaves as LeafCount;
        let root_level = Position::root_level_from_leaf_count(last_new_leaf_count);
        let mut to_freeze = Vec::with_capacity(Self::max_to_freeze(num_new_leaves, root_level));

        // Iterate over the new leaves, adding them to to_freeze and then adding any frozen parents
        // when right children are encountered.  This has the effect of creating frozen nodes in
        // perfect post-order, which can be used as a strictly increasing append only index for
        // the underlying storage.
        //
        // We will track newly created left siblings while iterating so we can pair them with their
        // right sibling, if and when it becomes frozen.  If the frozen left sibling is not created
        // in this iteration, it must already exist in storage.
        let mut left_siblings: Vec<(_, _)> = Vec::new();
        for (leaf_offset, leaf) in new_leaves.iter().enumerate() {
            let leaf_pos = Position::from_leaf_index(self.num_leaves + leaf_offset as LeafCount);
            let mut hash = *leaf;
            to_freeze.push((leaf_pos, hash));
            let mut pos = leaf_pos;
            while pos.is_right_child() {
                let sibling = pos.sibling();
                hash = match left_siblings.pop() {
                    Some((x, left_hash)) => {
                        assert_eq!(x, sibling);
                        Self::hash_internal_node(left_hash, hash)
                    }
                    None => Self::hash_internal_node(self.reader.get(sibling)?, hash),
                };
                pos = pos.parent();
                to_freeze.push((pos, hash));
            }
            // The node remaining must be a left child, possibly the root of a complete binary tree.
            left_siblings.push((pos, hash));
        }

        // Now reconstruct the final root hash by walking up to root level and adding
        // placeholder hash nodes as needed on the right, and left siblings that have either
        // been newly created or read from storage.
        let (mut pos, mut hash) = left_siblings.pop().expect("Must have at least one node");
        for _ in pos.level()..root_level as u32 {
            hash = if pos.is_left_child() {
                Self::hash_internal_node(hash, *ACCUMULATOR_PLACEHOLDER_HASH)
            } else {
                let sibling = pos.sibling();
                match left_siblings.pop() {
                    Some((x, left_hash)) => {
                        assert_eq!(x, sibling);
                        Self::hash_internal_node(left_hash, hash)
                    }
                    None => Self::hash_internal_node(self.reader.get(sibling)?, hash),
                }
            };
            pos = pos.parent();
        }
        assert!(left_siblings.is_empty());

        Ok((hash, to_freeze))
    }
/// implementation for pub interface `MerkleAccumulator::append`
implementation for pub interface `MerkleAccumulator::append`
implementation for pub interface ` MerkleAccumulator :: append `
fn append(&self, new_leaves: &[HashValue]) -> Result<(HashValue, Vec<Node>)> {
        // Deal with the case where new_leaves is empty
        if new_leaves.is_empty() {
            if self.num_leaves == 0 {
                return Ok((*ACCUMULATOR_PLACEHOLDER_HASH, Vec::new()));
            } else {
                let root_hash = self.get_hash(Position::root_from_leaf_count(self.num_leaves))?;
                return Ok((root_hash, Vec::new()));
            }
        }
        let num_new_leaves = new_leaves.len();
        let last_new_leaf_count = self.num_leaves + num_new_leaves as LeafCount;
        let root_level = Position::root_level_from_leaf_count(last_new_leaf_count);
        let mut to_freeze = Vec::with_capacity(Self::max_to_freeze(num_new_leaves, root_level));
        // Iterate over the new leaves, adding them to to_freeze and then adding any frozen parents
        // when right children are encountered.  This has the effect of creating frozen nodes in
        // perfect post-order, which can be used as a strictly increasing append only index for
        // the underlying storage.
        //
        // We will track newly created left siblings while iterating so we can pair them with their
        // right sibling, if and when it becomes frozen.  If the frozen left sibling is not created
        // in this iteration, it must already exist in storage.
        let mut left_siblings: Vec<(_, _)> = Vec::new();
        for (leaf_offset, leaf) in new_leaves.iter().enumerate() {
            let leaf_pos = Position::from_leaf_index(self.num_leaves + leaf_offset as LeafCount);
            let mut hash = *leaf;
            to_freeze.push((leaf_pos, hash));
            let mut pos = leaf_pos;
            while pos.is_right_child() {
                let sibling = pos.sibling();
                hash = match left_siblings.pop() {
                    Some((x, left_hash)) => {
                        assert_eq!(x, sibling);
                        Self::hash_internal_node(left_hash, hash)
                    }
                    None => Self::hash_internal_node(self.reader.get(sibling)?, hash),
                };
                pos = pos.parent();
                to_freeze.push((pos, hash));
            }
            // The node remaining must be a left child, possibly the root of a complete binary tree.
            left_siblings.push((pos, hash));
        }
        // Now reconstruct the final root hash by walking up to root level and adding
        // placeholder hash nodes as needed on the right, and left siblings that have either
        // been newly created or read from storage.
        let (mut pos, mut hash) = left_siblings.pop().expect("Must have at least one node");
        for _ in pos.level()..root_level as u32 {
            hash = if pos.is_left_child() {
                Self::hash_internal_node(hash, *ACCUMULATOR_PLACEHOLDER_HASH)
            } else {
                let sibling = pos.sibling();
                match left_siblings.pop() {
                    Some((x, left_hash)) => {
                        assert_eq!(x, sibling);
                        Self::hash_internal_node(left_hash, hash)
                    }
                    None => Self::hash_internal_node(self.reader.get(sibling)?, hash),
                }
            };
            pos = pos.parent();
        }
        assert!(left_siblings.is_empty());
        Ok((hash, to_freeze))
    }
fn append ( & self , new_leaves : & [ HashValue ] ) -> Result < ( HashValue , Vec < Node > ) > { // Deal with the case where new_leaves is empty if new_leaves . is_empty ( ) { if self . num_leaves == 0 { return Ok ( ( * ACCUMULATOR_PLACEHOLDER_HASH , Vec :: new ( ) ) ) ; } else { let root_hash = self . get_hash ( Position :: root_from_leaf_count ( self . num_leaves ) ) ? ; return Ok ( ( root_hash , Vec :: new ( ) ) ) ; } } let num_new_leaves = new_leaves . len ( ) ; let last_new_leaf_count = self . num_leaves + num_new_leaves as LeafCount ; let root_level = Position :: root_level_from_leaf_count ( last_new_leaf_count ) ; let mut to_freeze = Vec :: with_capacity ( Self :: max_to_freeze ( num_new_leaves , root_level ) ) ; // Iterate over the new leaves, adding them to to_freeze and then adding any frozen parents // when right children are encountered.  This has the effect of creating frozen nodes in // perfect post-order, which can be used as a strictly increasing append only index for // the underlying storage. // // We will track newly created left siblings while iterating so we can pair them with their // right sibling, if and when it becomes frozen.  If the frozen left sibling is not created // in this iteration, it must already exist in storage. let mut left_siblings : Vec < ( _ , _ ) > = Vec :: new ( ) ; for ( leaf_offset , leaf ) in new_leaves . iter ( ) . enumerate ( ) { let leaf_pos = Position :: from_leaf_index ( self . num_leaves + leaf_offset as LeafCount ) ; let mut hash = * leaf ; to_freeze . push ( ( leaf_pos , hash ) ) ; let mut pos = leaf_pos ; while pos . is_right_child ( ) { let sibling = pos . sibling ( ) ; hash = match left_siblings . pop ( ) { Some ( ( x , left_hash ) ) => { assert_eq ! ( x sibling ) ; Self :: hash_internal_node ( left_hash , hash ) } None => Self :: hash_internal_node ( self . reader . get ( sibling ) ? , hash ) , } ; pos = pos . parent ( ) ; to_freeze . push ( ( pos , hash ) ) ; } // The node remaining must be a left child, possibly the root of a complete binary tree. left_siblings . push ( ( pos , hash ) ) ; } // Now reconstruct the final root hash by walking up to root level and adding // placeholder hash nodes as needed on the right, and left siblings that have either // been newly created or read from storage. let ( mut pos , mut hash ) = left_siblings . pop ( ) . expect ( " " ) ; for _ in pos . level ( ) .. root_level as u32 { hash = if pos . is_left_child ( ) { Self :: hash_internal_node ( hash , * ACCUMULATOR_PLACEHOLDER_HASH ) } else { let sibling = pos . sibling ( ) ; match left_siblings . pop ( ) { Some ( ( x , left_hash ) ) => { assert_eq ! ( x sibling ) ; Self :: hash_internal_node ( left_hash , hash ) } None => Self :: hash_internal_node ( self . reader . get ( sibling ) ? , hash ) , } } ; pos = pos . parent ( ) ; } assert ! ( left_siblings is_empty ( ) ) ; Ok ( ( hash , to_freeze ) ) }
implementation for pub interface `MerkleAccumulator::append`
implementation for pub interface ` MerkleAccumulator :: append `
&self
new_leaves
&[HashValue]
&self
new_leaves
&[HashValue]