Merge pull request #1646 from yuce/roaring-fast-add

Adds DirectAdd function to roaring.Bitmap
This commit is contained in:
Yuce Tekol 2018-09-17 22:04:30 +03:00 committed by GitHub
commit 74780528d7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 6 deletions

View file

@ -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:

View file

@ -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