From ab047eb986b19f60540af945ad2ba0cba6094e72 Mon Sep 17 00:00:00 2001 From: bloeys Date: Mon, 9 Sep 2024 07:55:06 +0400 Subject: [PATCH] Fix NSet.SetBits in GetDifference --- nset.go | 6 ++++++ nset_test.go | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/nset.go b/nset.go index 03fa76b..99927d9 100644 --- a/nset.go +++ b/nset.go @@ -257,7 +257,13 @@ func (n *NSet[T]) GetDifference(otherSet *NSet[T]) *NSet[T] { } if b1.StorageUnitCount > b2.StorageUnitCount { + copy(newB.Data[b2.StorageUnitCount:], b1.Data[b2.StorageUnitCount:]) + for j := uint32(b2.StorageUnitCount); j < newB.StorageUnitCount; j++ { + + storage := newB.Data[j] + outSet.SetBits += uint64(bits.OnesCount64(uint64(storage))) + } } } diff --git a/nset_test.go b/nset_test.go index 89a3fe2..1103e3f 100755 --- a/nset_test.go +++ b/nset_test.go @@ -126,7 +126,7 @@ func TestNSet(t *testing.T) { nDiff1.AddMany(1, 2, 3, 4, 5, math.MaxUint32) nDiff3 = nDiff1.GetDifference(nDiff2) - AllTrue(t, nDiff3.SetBits == 2, nDiff3.ContainsAll(2, 5, math.MaxUint32), !nDiff3.ContainsAny(1, 3, 4)) + AllTrue(t, nDiff3.SetBits == 3, nDiff3.ContainsAll(2, 5, math.MaxUint32), !nDiff3.ContainsAny(1, 3, 4)) } func TestNSetFullRange(t *testing.T) {