new initializer

This commit is contained in:
NikVolf
2019-08-22 20:35:56 +03:00
parent 27337f1bb0
commit 5a479363ff

View File

@@ -76,7 +76,7 @@ impl Tree {
NodeLink::Generated(idx) NodeLink::Generated(idx)
} }
/// Populate tree with plain list of the leaves/nodes. Mostly for test, /// Populate tree with plain list of the leaves/nodes. Mostly for tests,
/// since this `Tree` structure is for partially loaded tree. /// since this `Tree` structure is for partially loaded tree.
pub fn populate(loaded: Vec<MMRNode>) -> Self { pub fn populate(loaded: Vec<MMRNode>) -> Self {
let mut result = Tree::default(); let mut result = Tree::default();
@@ -88,6 +88,27 @@ impl Tree {
result result
} }
pub fn new(
length: u32,
stored: Vec<(u32, MMRNode)>,
generated: Vec<MMRNode>,
) -> Self {
let mut result = Tree::default();
result.stored_count = length;
for (idx, node) in stored.into_iter() {
result.stored.insert(idx, node);
}
result.generated_count = generated.len() as u32;
for (idx, node) in generated.into_iter().enumerate() {
result.generated.insert(idx as u32, node);
}
result
}
/// Append one leaf to the tree. /// Append one leaf to the tree.
pub fn append_leaf(&mut self, root: NodeLink, new_leaf: NodeData) -> AppendTransaction { pub fn append_leaf(&mut self, root: NodeLink, new_leaf: NodeData) -> AppendTransaction {