mirror of
https://github.com/bloeys/nset.git
synced 2025-12-29 06:28:19 +00:00
Copy function
This commit is contained in:
19
nset.go
19
nset.go
@ -149,6 +149,25 @@ func (n *NSet[T]) String() string {
|
|||||||
return b.String()
|
return b.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (n *NSet[T]) Copy() *NSet[T] {
|
||||||
|
|
||||||
|
newSet := NewNSet[T]()
|
||||||
|
for i := 0; i < len(n.Buckets); i++ {
|
||||||
|
|
||||||
|
b := &n.Buckets[i]
|
||||||
|
newB := &newSet.Buckets[i]
|
||||||
|
|
||||||
|
newB.StorageUnitCount = b.StorageUnitCount
|
||||||
|
newB.Data = make([]StorageType, len(b.Data))
|
||||||
|
|
||||||
|
copy(newB.Data, b.Data)
|
||||||
|
}
|
||||||
|
|
||||||
|
newSet.StorageUnitCount = n.StorageUnitCount
|
||||||
|
return newSet
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func NewNSet[T IntsIf]() *NSet[T] {
|
func NewNSet[T IntsIf]() *NSet[T] {
|
||||||
|
|
||||||
n := &NSet[T]{
|
n := &NSet[T]{
|
||||||
|
|||||||
@ -34,8 +34,11 @@ func TestNSet(t *testing.T) {
|
|||||||
IsEq(t, nset.BucketCount-1, n.GetBucketIndex(math.MaxUint32))
|
IsEq(t, nset.BucketCount-1, n.GetBucketIndex(math.MaxUint32))
|
||||||
IsEq(t, math.MaxUint32/64/nset.BucketCount, n.GetStorageUnitIndex(math.MaxUint32))
|
IsEq(t, math.MaxUint32/64/nset.BucketCount, n.GetStorageUnitIndex(math.MaxUint32))
|
||||||
|
|
||||||
|
nCopy := n.Copy()
|
||||||
n.Remove(1)
|
n.Remove(1)
|
||||||
AllTrue(t, n.Contains(0), n.Contains(63), !n.Contains(1))
|
|
||||||
|
AllTrue(t, n.Contains(0), n.Contains(63), !n.Contains(1), nCopy.ContainsAll(0, 1, 63, math.MaxUint32))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNSetFullRange(t *testing.T) {
|
func TestNSetFullRange(t *testing.T) {
|
||||||
|
|||||||
Reference in New Issue
Block a user