diff --git a/roaring/roaring.go b/roaring/roaring.go index 41710de89..c3f2cc3db 100644 --- a/roaring/roaring.go +++ b/roaring/roaring.go @@ -167,7 +167,8 @@ func (b *Bitmap) Add(a ...uint64) (changed bool, err error) { return changed, nil } -func (b *Bitmap) add(v uint64) bool { +// DirectAdd adds a value to the bitmap by bypassing the op log. +func (b *Bitmap) DirectAdd(v uint64) bool { cont := b.Containers.GetOrCreate(highbits(v)) return cont.add(lowbits(v)) } @@ -771,22 +772,22 @@ func (b *Bitmap) Flip(start, end uint64) *Bitmap { v, eof := itr.Next() //copy over previous bits. for v < start && !eof { - result.add(v) + result.DirectAdd(v) v, eof = itr.Next() } //flip bits in range . for i := start; i <= end; i++ { if eof { - result.add(i) + result.DirectAdd(i) } else if v == i { v, eof = itr.Next() } else { - result.add(i) + result.DirectAdd(i) } } //add remaining. for !eof { - result.add(v) + result.DirectAdd(v) v, eof = itr.Next() } return result @@ -2907,7 +2908,7 @@ type op struct { func (op *op) apply(b *Bitmap) bool { switch op.typ { case opTypeAdd: - return b.add(op.value) + return b.DirectAdd(op.value) case opTypeRemove: return b.remove(op.value) default: diff --git a/roaring/roaring_test.go b/roaring/roaring_test.go index d29f1226f..64cb45e81 100644 --- a/roaring/roaring_test.go +++ b/roaring/roaring_test.go @@ -331,6 +331,22 @@ func TestBitmap_ArrayCountRange(t *testing.T) { } } +func TestBitmap_DirectAdd(t *testing.T) { + bits := []uint64{0, 1, 2, 3, 4, 5, 12, 13, 14, 15, 16, 17, 1000000, 1000002, 1000003, 1000004, 1000005, 1000006, 1000010, 1000011, 1000012, 1000013, 1000014} + bm := roaring.NewBitmap() + for _, b := range []uint64{0, 1, 2, 3, 4, 5, 12, 13, 14, 15, 16, 17, 1000000, 1000002, 1000003, 1000004, 1000005, 1000006, 1000010, 1000011, 1000012, 1000013, 1000014} { + bm.DirectAdd(b) + } + if len(bits) != int(bm.Count()) { + t.Fatalf("count %d != %d", len(bits), bm.Count()) + } + for _, bit := range bits { + if !bm.Contains(bit) { + t.Fatalf("%d should be in the bitmap", bit) + } + } +} + func TestBitmap_RunCountRange(t *testing.T) { bm0 := roaring.NewFileBitmap(0, 1, 2, 3, 4, 5, 12, 13, 14, 15, 16, 17, 1000000, 1000002, 1000003, 1000004, 1000005, 1000006, 1000010, 1000011, 1000012, 1000013, 1000014) bm0.Optimize() // convert to runs