From ea9805223fe0365184d0142abb25a4a351b3996f Mon Sep 17 00:00:00 2001 From: amalsaidov Date: Sat, 5 Dec 2026 02:54:58 +0700 Subject: [PATCH] roaring64: fix BSI Add and Increment for negative values Add summed the bit planes as unsigned numbers. The value from other was not sign-extended to the width of b, and a carry out of the top plane became a new sign plane, so any negative operand gave a wrong result (5 + -3 = 10, -1 + -1 = 6, and Increment turned -1 into 4). Add the planes in two's complement instead: sign-extend both operands to one plane wider than the wider of the two, drop the final carry, and trim sign planes that only repeat the one below (never below the original width). Adding a BSI to itself now works too. --- roaring64/bsi64.go | 58 ++++++++++++++++++++++++++-------- roaring64/bsi64_test.go | 69 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+), 13 deletions(-) diff --git a/roaring64/bsi64.go b/roaring64/bsi64.go index 3c69bbe5..a8eb0f73 100644 --- a/roaring64/bsi64.go +++ b/roaring64/bsi64.go @@ -1828,25 +1828,57 @@ func (b *BSI) Clone() *BSI { // Add - In-place sum the contents of another BSI with this BSI, column wise. func (b *BSI) Add(other *BSI) { - + if other == b { + other = b.Clone() + } b.eBM.Or(&other.eBM) - for i := 0; i < len(other.bA); i++ { - b.addDigit(&other.bA[i], i) + planes := make([]*Bitmap, len(other.bA)) + for i := range other.bA { + planes[i] = &other.bA[i] } + b.addPlanes(planes) } -func (b *BSI) addDigit(foundSet *Bitmap, i int) { - - if i >= b.BitCount()+1 || b.BitCount() == 0 { +// addPlanes adds a two's complement value, given as bit planes whose last +// plane is the sign, to every column of b. Both operands are sign-extended to +// one plane more than the wider of the two, so the sum cannot overflow, and +// the carry out of the sign plane is dropped. +func (b *BSI) addPlanes(planes []*Bitmap) { + if len(planes) == 0 { + return + } + minLen := len(b.bA) + width := len(b.bA) + if len(planes) > width { + width = len(planes) + } + width++ + for len(b.bA) < width { b.bA = append(b.bA, Bitmap{}) + if n := len(b.bA); n > 1 { + b.bA[n-1].Or(&b.bA[n-2]) + } } - carry := And(&b.bA[i], foundSet) - b.bA[i].Xor(foundSet) - if !carry.IsEmpty() { - if i+1 >= b.BitCount() { - b.bA = append(b.bA, Bitmap{}) + carry := NewBitmap() + for i := 0; i < width; i++ { + x := &b.bA[i] + y := planes[len(planes)-1] + if i < len(planes) { + y = planes[i] } - b.addDigit(carry, i+1) + xy := Xor(x, y) + nextCarry := And(x, y) + nextCarry.Or(And(carry, xy)) + x.Xor(y) + x.Xor(carry) + carry = nextCarry + } + // Drop sign planes that only repeat the one below them. + if minLen < 2 { + minLen = 2 + } + for n := len(b.bA); n > minLen && b.bA[n-1].Equals(&b.bA[n-2]); n-- { + b.bA = b.bA[:n-1] } } @@ -1893,7 +1925,7 @@ func (b *BSI) Increment(foundSet *Bitmap) { if foundSet == nil { foundSet = &b.eBM } - b.addDigit(foundSet, 0) + b.addPlanes([]*Bitmap{foundSet, NewBitmap()}) b.eBM.Or(foundSet) } diff --git a/roaring64/bsi64_test.go b/roaring64/bsi64_test.go index 7925f2b0..35852c05 100644 --- a/roaring64/bsi64_test.go +++ b/roaring64/bsi64_test.go @@ -576,6 +576,62 @@ func TestAdd(t *testing.T) { } +func TestAddNegativeValues(t *testing.T) { + cases := []struct{ a, b int64 }{ + {5, -3}, {-3, 5}, {1, -1}, {-1, -1}, {10, -20}, {-100, 1}, {0, -2}, {-7, 0}, + } + for _, c := range cases { + bsi := NewDefaultBSI() + bsi.SetValue(1, c.a) + other := NewDefaultBSI() + other.SetValue(1, c.b) + bsi.Add(other) + v, ok := bsi.GetValue(1) + assert.True(t, ok) + assert.Equal(t, c.a+c.b, v, "%d + %d", c.a, c.b) + } +} + +func TestAddMixedSigns(t *testing.T) { + r := rand.New(rand.NewSource(1)) + bsi := NewDefaultBSI() + other := NewDefaultBSI() + want := map[uint64]int64{} + for i := 0; i < 200; i++ { + col := uint64(r.Intn(100)) + v := r.Int63n(1<<20) - 1<<19 + if i%2 == 0 { + bsi.SetValue(col, v) + want[col] = v + } else { + // columns may also be set only in other + other.SetValue(col+50, v) + } + } + for _, col := range other.GetExistenceBitmap().ToArray() { + v, _ := other.GetValue(col) + want[col] += v + } + bsi.Add(other) + assert.Equal(t, uint64(len(want)), bsi.GetCardinality()) + for col, w := range want { + v, ok := bsi.GetValue(col) + assert.True(t, ok) + assert.Equal(t, w, v, "column %d", col) + } +} + +func TestAddToItself(t *testing.T) { + bsi := NewDefaultBSI() + bsi.SetValue(1, -3) + bsi.SetValue(2, 4) + bsi.Add(bsi) + v, _ := bsi.GetValue(1) + assert.Equal(t, int64(-6), v) + v, _ = bsi.GetValue(2) + assert.Equal(t, int64(8), v) +} + func TestBatchValueBig(t *testing.T) { bsi := NewDefaultBSI() @@ -620,6 +676,19 @@ func TestIncrementFromZero(t *testing.T) { assert.Equal(t, int64(10), sum) } +func TestIncrementNegative(t *testing.T) { + bsi := NewDefaultBSI() + bsi.SetValue(1, -1) + bsi.SetValue(2, -2) + bsi.SetValue(3, 3) + bsi.IncrementAll() + for col, want := range map[uint64]int64{1: 0, 2: -1, 3: 4} { + v, ok := bsi.GetValue(col) + assert.True(t, ok) + assert.Equal(t, want, v, "column %d", col) + } +} + func TestTransposeWithCounts(t *testing.T) { bsi := setup() bsi.SetValue(101, 50)