pub(crate) fn add_nodes<'a>(
        &mut self,
        new_kps: &[&'a KeyPackage],
    ) -> Vec<(LeafIndex, &'a Credential)> {
        let num_new_kp = new_kps.len();
        let mut added_members = Vec::with_capacity(num_new_kp);

        if num_new_kp > (2 * self.leaf_count().as_usize()) {
            self.nodes
                .reserve_exact((2 * num_new_kp) - (2 * self.leaf_count().as_usize()));
        }

        // Add new nodes for key packages into existing free leaves.
        // Note that zip makes it so only the first free_leaves().len() nodes are taken.
        let free_leaves = self.free_leaves();
        let free_leaves_len = free_leaves.len();
        for (&new_kp, leaf_index) in new_kps.iter().zip(free_leaves) {
            self.nodes[leaf_index] = Node::new_leaf(Some((new_kp).clone()));
            added_members.push((leaf_index, new_kp.credential()));
        }
        // Add the remaining nodes.
        for &key_package in new_kps.iter().skip(free_leaves_len) {
            added_members.push(self.add_node(key_package));
        }

        // Add the newly added leaves to the unmerged leaves of all non-blank
        // parent nodes in their direct path.
        for (leaf_index, _) in &added_members {
            let dirpath = treemath::leaf_direct_path(leaf_index.to_owned(), self.leaf_count())
                .expect("add_nodes: Error when computing direct path.");
            for d in dirpath.iter() {
                if !self.nodes[d].is_blank() {
                    let node = &mut self.nodes[d];
                    // We can unwrap here, because we just checked that the node
                    // is not blank.
                    let mut parent_node = node.node.take().unwrap();
                    if !parent_node.unmerged_leaves().contains(leaf_index) {
                        parent_node.add_unmerged_leaf(leaf_index.to_owned());
                    }
                    self.nodes[d].node = Some(parent_node);
                }
            }
        }

        self.trim_tree();
        added_members
    }
/// Add nodes for the provided key packages. Returns a vector containing the
/// `LeafIndex` of the leaf each `KeyPackage` was placed into, as well as a
/// reference to the corresponding `KeyPackage`'s `Credential`.
Add nodes for the provided key packages.
Add nodes for the provided key packages .
pub(crate) fn add_nodes<'a>(
        &mut self,
        new_kps: &[&'a KeyPackage],
    ) -> Vec<(LeafIndex, &'a Credential)> {
        let num_new_kp = new_kps.len();
        let mut added_members = Vec::with_capacity(num_new_kp);
        if num_new_kp > (2 * self.leaf_count().as_usize()) {
            self.nodes
                .reserve_exact((2 * num_new_kp) - (2 * self.leaf_count().as_usize()));
        }
        // Add new nodes for key packages into existing free leaves.
        // Note that zip makes it so only the first free_leaves().len() nodes are taken.
        let free_leaves = self.free_leaves();
        let free_leaves_len = free_leaves.len();
        for (&new_kp, leaf_index) in new_kps.iter().zip(free_leaves) {
            self.nodes[leaf_index] = Node::new_leaf(Some((new_kp).clone()));
            added_members.push((leaf_index, new_kp.credential()));
        }
        // Add the remaining nodes.
        for &key_package in new_kps.iter().skip(free_leaves_len) {
            added_members.push(self.add_node(key_package));
        }
        // Add the newly added leaves to the unmerged leaves of all non-blank
        // parent nodes in their direct path.
        for (leaf_index, _) in &added_members {
            let dirpath = treemath::leaf_direct_path(leaf_index.to_owned(), self.leaf_count())
                .expect("add_nodes: Error when computing direct path.");
            for d in dirpath.iter() {
                if !self.nodes[d].is_blank() {
                    let node = &mut self.nodes[d];
                    // We can unwrap here, because we just checked that the node
                    // is not blank.
                    let mut parent_node = node.node.take().unwrap();
                    if !parent_node.unmerged_leaves().contains(leaf_index) {
                        parent_node.add_unmerged_leaf(leaf_index.to_owned());
                    }
                    self.nodes[d].node = Some(parent_node);
                }
            }
        }
        self.trim_tree();
        added_members
    }
pub ( crate ) fn add_nodes < ' a > ( & mut self , new_kps : & [ & ' a KeyPackage ] , ) -> Vec < ( LeafIndex , & ' a Credential ) > { let num_new_kp = new_kps . len ( ) ; let mut added_members = Vec :: with_capacity ( num_new_kp ) ; if num_new_kp > ( 2 * self . leaf_count ( ) . as_usize ( ) ) { self . nodes . reserve_exact ( ( 2 * num_new_kp ) - ( 2 * self . leaf_count ( ) . as_usize ( ) ) ) ; } // Add new nodes for key packages into existing free leaves. // Note that zip makes it so only the first free_leaves().len() nodes are taken. let free_leaves = self . free_leaves ( ) ; let free_leaves_len = free_leaves . len ( ) ; for ( & new_kp , leaf_index ) in new_kps . iter ( ) . zip ( free_leaves ) { self . nodes [ leaf_index ] = Node :: new_leaf ( Some ( ( new_kp ) . clone ( ) ) ) ; added_members . push ( ( leaf_index , new_kp . credential ( ) ) ) ; } // Add the remaining nodes. for & key_package in new_kps . iter ( ) . skip ( free_leaves_len ) { added_members . push ( self . add_node ( key_package ) ) ; } // Add the newly added leaves to the unmerged leaves of all non-blank // parent nodes in their direct path. for ( leaf_index , _ ) in & added_members { let dirpath = treemath :: leaf_direct_path ( leaf_index . to_owned ( ) , self . leaf_count ( ) ) . expect ( " " ) ; for d in dirpath . iter ( ) { if ! self . nodes [ d ] . is_blank ( ) { let node = & mut self . nodes [ d ] ; // We can unwrap here, because we just checked that the node // is not blank. let mut parent_node = node . node . take ( ) . unwrap ( ) ; if ! parent_node . unmerged_leaves ( ) . contains ( leaf_index ) { parent_node . add_unmerged_leaf ( leaf_index . to_owned ( ) ) ; } self . nodes [ d ] . node = Some ( parent_node ) ; } } } self . trim_tree ( ) ; added_members }
Add nodes for the provided key packages.
Add nodes for the provided key packages .
&mut self
new_kps
&[&'a KeyPackage]
&mut self
new_kps
&[&'a KeyPackage]