From b711c8762efa585629e45826d1c9857a0ef2aaf3 Mon Sep 17 00:00:00 2001 From: Andrew Ayer Date: Wed, 12 Jun 2024 11:21:58 -0400 Subject: [PATCH] Refine the CollapsedTree API --- merkletree/collapsed_tree.go | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/merkletree/collapsed_tree.go b/merkletree/collapsed_tree.go index 09eadb7..ce60c96 100644 --- a/merkletree/collapsed_tree.go +++ b/merkletree/collapsed_tree.go @@ -25,10 +25,12 @@ func calculateNumNodes(size uint64) int { return bits.OnesCount64(size) } +// TODO: phase out this function func EmptyCollapsedTree() *CollapsedTree { return &CollapsedTree{nodes: []Hash{}, size: 0} } +// TODO: phase out this function func NewCollapsedTree(nodes []Hash, size uint64) (*CollapsedTree, error) { tree := new(CollapsedTree) if err := tree.Init(nodes, size); err != nil { @@ -37,23 +39,24 @@ func NewCollapsedTree(nodes []Hash, size uint64) (*CollapsedTree, error) { return tree, nil } -func CloneCollapsedTree(source *CollapsedTree) *CollapsedTree { - nodes := make([]Hash, len(source.nodes)) - copy(nodes, source.nodes) - return &CollapsedTree{nodes: nodes, size: source.size} -} - func (tree CollapsedTree) Equal(other CollapsedTree) bool { return tree.size == other.size && slices.Equal(tree.nodes, other.nodes) } +func (tree CollapsedTree) Clone() CollapsedTree { + return CollapsedTree{ + nodes: slices.Clone(tree.nodes), + size: tree.size, + } +} + func (tree *CollapsedTree) Add(hash Hash) { tree.nodes = append(tree.nodes, hash) tree.size++ tree.collapse() } -func (tree *CollapsedTree) Append(other *CollapsedTree) error { +func (tree *CollapsedTree) Append(other CollapsedTree) error { if tree.size > 0 { maxSize := uint64(1) << bits.TrailingZeros64(tree.size) if other.size > maxSize {