diff --git a/roaring64/bsi64.go b/roaring64/bsi64.go index 000e20d3..fab8015b 100644 --- a/roaring64/bsi64.go +++ b/roaring64/bsi64.go @@ -1976,29 +1976,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 c07d33e4..7f53d3ef 100644 --- a/roaring64/bsi64_test.go +++ b/roaring64/bsi64_test.go @@ -773,6 +773,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 TestNewBSIBounds(t *testing.T) { // NewBSI(100, 0) is 7 bits plus a sign: -128..127, wider than [0, 100]. bsi := NewBSI(100, 0)