From 266051dd26299293b115cfa136c7df11f3e271ef Mon Sep 17 00:00:00 2001 From: Yuce Tekol Date: Mon, 17 Sep 2018 16:25:34 +0300 Subject: [PATCH 1/4] Adds DirectAdd function to roaring.Bitmap --- roaring/roaring.go | 7 +++++++ roaring/roaring_test.go | 15 +++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/roaring/roaring.go b/roaring/roaring.go index 41710de89..755e508b7 100644 --- a/roaring/roaring.go +++ b/roaring/roaring.go @@ -167,6 +167,13 @@ func (b *Bitmap) Add(a ...uint64) (changed bool, err error) { return changed, nil } +// DirectAdd adds values to the bitmap by bypassing the op log. +func (b *Bitmap) DirectAdd(values []uint64) { + for _, value := range values { + b.add(value) + } +} + func (b *Bitmap) add(v uint64) bool { cont := b.Containers.GetOrCreate(highbits(v)) return cont.add(lowbits(v)) diff --git a/roaring/roaring_test.go b/roaring/roaring_test.go index d29f1226f..073b96c6f 100644 --- a/roaring/roaring_test.go +++ b/roaring/roaring_test.go @@ -331,6 +331,21 @@ 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() + bm.DirectAdd([]uint64{0, 1, 2, 3, 4, 5, 12, 13, 14, 15, 16, 17}) + bm.DirectAdd([]uint64{1000000, 1000002, 1000003, 1000004, 1000005, 1000006, 1000010, 1000011, 1000012, 1000013, 1000014}) + 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 From 09c24cd3becf4a6351cabbee3749afe60b0809c0 Mon Sep 17 00:00:00 2001 From: Yuce Tekol Date: Mon, 17 Sep 2018 17:10:02 +0300 Subject: [PATCH 2/4] Changed the signature of Bitmap.DirectAdd function --- roaring/roaring.go | 2 +- roaring/roaring_test.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/roaring/roaring.go b/roaring/roaring.go index 755e508b7..8ac2f81a1 100644 --- a/roaring/roaring.go +++ b/roaring/roaring.go @@ -168,7 +168,7 @@ func (b *Bitmap) Add(a ...uint64) (changed bool, err error) { } // DirectAdd adds values to the bitmap by bypassing the op log. -func (b *Bitmap) DirectAdd(values []uint64) { +func (b *Bitmap) DirectAdd(values ...uint64) { for _, value := range values { b.add(value) } diff --git a/roaring/roaring_test.go b/roaring/roaring_test.go index 073b96c6f..a06e882c8 100644 --- a/roaring/roaring_test.go +++ b/roaring/roaring_test.go @@ -334,8 +334,8 @@ 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() - bm.DirectAdd([]uint64{0, 1, 2, 3, 4, 5, 12, 13, 14, 15, 16, 17}) - bm.DirectAdd([]uint64{1000000, 1000002, 1000003, 1000004, 1000005, 1000006, 1000010, 1000011, 1000012, 1000013, 1000014}) + bm.DirectAdd(0, 1, 2, 3, 4, 5, 12, 13, 14, 15, 16, 17) + bm.DirectAdd(1000000, 1000002, 1000003, 1000004, 1000005, 1000006, 1000010, 1000011, 1000012, 1000013, 1000014) if len(bits) != int(bm.Count()) { t.Fatalf("count %d != %d", len(bits), bm.Count()) } From 37fdd73a7f9e1646b600384d38ed5c57d32324c8 Mon Sep 17 00:00:00 2001 From: Yuce Tekol Date: Mon, 17 Sep 2018 17:16:03 +0300 Subject: [PATCH 3/4] DirectAdd adds a single value --- roaring/roaring.go | 8 +++----- roaring/roaring_test.go | 5 +++-- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/roaring/roaring.go b/roaring/roaring.go index 8ac2f81a1..eed7aae39 100644 --- a/roaring/roaring.go +++ b/roaring/roaring.go @@ -167,11 +167,9 @@ func (b *Bitmap) Add(a ...uint64) (changed bool, err error) { return changed, nil } -// DirectAdd adds values to the bitmap by bypassing the op log. -func (b *Bitmap) DirectAdd(values ...uint64) { - for _, value := range values { - b.add(value) - } +// DirectAdd adds a value to the bitmap by bypassing the op log. +func (b *Bitmap) DirectAdd(value uint64) bool { + return b.add(value) } func (b *Bitmap) add(v uint64) bool { diff --git a/roaring/roaring_test.go b/roaring/roaring_test.go index a06e882c8..64cb45e81 100644 --- a/roaring/roaring_test.go +++ b/roaring/roaring_test.go @@ -334,8 +334,9 @@ 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() - bm.DirectAdd(0, 1, 2, 3, 4, 5, 12, 13, 14, 15, 16, 17) - bm.DirectAdd(1000000, 1000002, 1000003, 1000004, 1000005, 1000006, 1000010, 1000011, 1000012, 1000013, 1000014) + 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()) } From e664a0e42f967056d71c9ab113c5952918256b55 Mon Sep 17 00:00:00 2001 From: Yuce Tekol Date: Mon, 17 Sep 2018 20:25:14 +0300 Subject: [PATCH 4/4] rename add -> DirectAdd --- roaring/roaring.go | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/roaring/roaring.go b/roaring/roaring.go index eed7aae39..c3f2cc3db 100644 --- a/roaring/roaring.go +++ b/roaring/roaring.go @@ -168,11 +168,7 @@ func (b *Bitmap) Add(a ...uint64) (changed bool, err error) { } // DirectAdd adds a value to the bitmap by bypassing the op log. -func (b *Bitmap) DirectAdd(value uint64) bool { - return b.add(value) -} - -func (b *Bitmap) add(v uint64) bool { +func (b *Bitmap) DirectAdd(v uint64) bool { cont := b.Containers.GetOrCreate(highbits(v)) return cont.add(lowbits(v)) } @@ -776,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 @@ -2912,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: