Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 45 additions & 13 deletions roaring64/bsi64.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
}

Expand Down Expand Up @@ -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)
}

Expand Down
69 changes: 69 additions & 0 deletions roaring64/bsi64_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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)
Expand Down
Loading