Refine the CollapsedTree API
This commit is contained in:
parent
759631f7e6
commit
b711c8762e
|
@ -25,10 +25,12 @@ func calculateNumNodes(size uint64) int {
|
||||||
return bits.OnesCount64(size)
|
return bits.OnesCount64(size)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: phase out this function
|
||||||
func EmptyCollapsedTree() *CollapsedTree {
|
func EmptyCollapsedTree() *CollapsedTree {
|
||||||
return &CollapsedTree{nodes: []Hash{}, size: 0}
|
return &CollapsedTree{nodes: []Hash{}, size: 0}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: phase out this function
|
||||||
func NewCollapsedTree(nodes []Hash, size uint64) (*CollapsedTree, error) {
|
func NewCollapsedTree(nodes []Hash, size uint64) (*CollapsedTree, error) {
|
||||||
tree := new(CollapsedTree)
|
tree := new(CollapsedTree)
|
||||||
if err := tree.Init(nodes, size); err != nil {
|
if err := tree.Init(nodes, size); err != nil {
|
||||||
|
@ -37,23 +39,24 @@ func NewCollapsedTree(nodes []Hash, size uint64) (*CollapsedTree, error) {
|
||||||
return tree, nil
|
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 {
|
func (tree CollapsedTree) Equal(other CollapsedTree) bool {
|
||||||
return tree.size == other.size && slices.Equal(tree.nodes, other.nodes)
|
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) {
|
func (tree *CollapsedTree) Add(hash Hash) {
|
||||||
tree.nodes = append(tree.nodes, hash)
|
tree.nodes = append(tree.nodes, hash)
|
||||||
tree.size++
|
tree.size++
|
||||||
tree.collapse()
|
tree.collapse()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tree *CollapsedTree) Append(other *CollapsedTree) error {
|
func (tree *CollapsedTree) Append(other CollapsedTree) error {
|
||||||
if tree.size > 0 {
|
if tree.size > 0 {
|
||||||
maxSize := uint64(1) << bits.TrailingZeros64(tree.size)
|
maxSize := uint64(1) << bits.TrailingZeros64(tree.size)
|
||||||
if other.size > maxSize {
|
if other.size > maxSize {
|
||||||
|
|
Loading…
Reference in New Issue