kaspad/domain/consensus/model/externalapi/blocklevelparents.go
one230six 63280ed437 refactor: use slices.Contains to simplify code
Signed-off-by: one230six <723682061@qq.com>
2025-06-12 11:23:19 +08:00

60 lines
1.5 KiB
Go

package externalapi
import "slices"
// BlockLevelParents represent the parents within a single super-block level
// See https://github.com/kaspanet/research/issues/3 for further details
type BlockLevelParents []*DomainHash
// Equal returns true if this BlockLevelParents is equal to `other`
func (sl BlockLevelParents) Equal(other BlockLevelParents) bool {
if len(sl) != len(other) {
return false
}
for _, thisHash := range sl {
found := slices.ContainsFunc(other, thisHash.Equal)
if !found {
return false
}
}
return true
}
// Clone creates a clone of this BlockLevelParents
func (sl BlockLevelParents) Clone() BlockLevelParents {
return CloneHashes(sl)
}
// Contains returns true if this BlockLevelParents contains the given blockHash
func (sl BlockLevelParents) Contains(blockHash *DomainHash) bool {
for _, blockLevelParent := range sl {
if blockLevelParent.Equal(blockHash) {
return true
}
}
return false
}
// ParentsEqual returns true if all the BlockLevelParents in `a` and `b` are
// equal pairwise
func ParentsEqual(a, b []BlockLevelParents) bool {
if len(a) != len(b) {
return false
}
for i, blockLevelParents := range a {
if !blockLevelParents.Equal(b[i]) {
return false
}
}
return true
}
// CloneParents creates a clone of the given BlockLevelParents slice
func CloneParents(parents []BlockLevelParents) []BlockLevelParents {
clone := make([]BlockLevelParents, len(parents))
for i, blockLevelParents := range parents {
clone[i] = blockLevelParents.Clone()
}
return clone
}