From 1d2e4043eb4a4612392ef4ef154260d08a1428b5 Mon Sep 17 00:00:00 2001 From: Daniel Lemire Date: Fri, 4 Dec 2026 18:44:24 -0500 Subject: [PATCH] roaring64: compare sign-extended planes in BSI Equals Equals compared the planes of the wider BSI past the top of the narrower one against an empty plane. That is only right when the narrower BSI holds no negative values. Two BSIs of different widths holding the same negative value compared unequal, and a 2-plane BSI holding -1 compared equal to a 3-plane BSI holding 3, since their two low planes match. Compare each plane past the top of a BSI against its sign plane instead. --- roaring64/bsi64.go | 33 +++++++++++++++++++------------ roaring64/bsi64_test.go | 44 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 13 deletions(-) diff --git a/roaring64/bsi64.go b/roaring64/bsi64.go index a8eb0f73..8088e371 100644 --- a/roaring64/bsi64.go +++ b/roaring64/bsi64.go @@ -1934,29 +1934,36 @@ func (b *BSI) IncrementAll() { b.Increment(b.GetExistenceBitmap()) } -// Equals - Check for semantic equality of two BSIs. +// Equals - Check for semantic equality of two BSIs: they have the same +// columns, with the same values. Two BSIs of different widths are equal if +// they hold the same values. func (b *BSI) Equals(other *BSI) bool { if !b.eBM.Equals(&other.eBM) { return false } + // Compare the planes sign-extended to the wider of the two widths: past + // its top, a BSI's planes repeat its sign plane. + empty := NewBitmap() for i := 0; i < len(b.bA) || i < len(other.bA); i++ { - if i >= len(b.bA) { - if !other.bA[i].IsEmpty() { - return false - } - } else if i >= len(other.bA) { - if !b.bA[i].IsEmpty() { - return false - } - } else { - if !b.bA[i].Equals(&other.bA[i]) { - return false - } + if !b.signExtendedPlane(i, empty).Equals(other.signExtendedPlane(i, empty)) { + return false } } return true } +// signExtendedPlane returns bit plane i of b. Past the top of b it returns +// the sign plane, and it returns empty if b has no planes. +func (b *BSI) signExtendedPlane(i int, empty *Bitmap) *Bitmap { + if len(b.bA) == 0 { + return empty + } + if i >= len(b.bA) { + i = len(b.bA) - 1 + } + return &b.bA[i] +} + // GetSizeInBytes - the size in bytes of the data structure func (b *BSI) GetSizeInBytes() int { size := b.eBM.GetSizeInBytes() diff --git a/roaring64/bsi64_test.go b/roaring64/bsi64_test.go index 35852c05..2714bea8 100644 --- a/roaring64/bsi64_test.go +++ b/roaring64/bsi64_test.go @@ -689,6 +689,50 @@ func TestIncrementNegative(t *testing.T) { } } +func TestEqualsDifferentWidths(t *testing.T) { + narrow := func(v int64) *BSI { + bsi := NewDefaultBSI() + bsi.SetValue(1, v) + bsi.SetValue(2, 1) + return bsi + } + wide := func(v int64) *BSI { + bsi := NewBSI(1<<40, -(1 << 40)) + bsi.SetValue(1, v) + bsi.SetValue(2, 1) + return bsi + } + for _, v := range []int64{-1, -2, -5, 0, 3, 5} { + n, w := narrow(v), wide(v) + assert.NotEqual(t, n.BitCount(), w.BitCount()) + assert.True(t, n.Equals(w), "%d", v) + assert.True(t, w.Equals(n), "%d", v) + } + + // Different values must not compare equal, even when the low planes of + // the narrower BSI match those of the wider one. + minusOne := narrow(-1) // planes 11 + three := NewDefaultBSI() + three.SetValue(1, 4) // planes 0010 + three.SetValue(1, 3) // planes 0011 + three.SetValue(2, 1) + assert.Greater(t, three.BitCount(), minusOne.BitCount()) + assert.False(t, minusOne.Equals(three)) + assert.False(t, three.Equals(minusOne)) + assert.False(t, wide(-1).Equals(wide(3))) + + // A BSI with no planes holds only zeros. + noPlanes := &BSI{} + noPlanes.eBM.Add(1) + zero := NewDefaultBSI() + zero.SetValue(1, 0) + assert.True(t, noPlanes.Equals(zero)) + assert.True(t, zero.Equals(noPlanes)) + zero.SetValue(1, -1) + assert.False(t, noPlanes.Equals(zero)) + assert.False(t, zero.Equals(noPlanes)) +} + func TestTransposeWithCounts(t *testing.T) { bsi := setup() bsi.SetValue(101, 50)