Skip to content

Commit

Permalink
Merge pull request FeatureBaseDB#1839 from tgruben/numbytes
Browse files Browse the repository at this point in the history
added convenience function to efficiently calculate size of a roaring bitmap in bytes
  • Loading branch information
tgruben authored Jan 24, 2019
2 parents b86f0c6 + c2ca00e commit 923e679
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
12 changes: 12 additions & 0 deletions roaring/roaring.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,18 @@ func (b *Bitmap) Count() (n uint64) {
return b.Containers.Count()
}

// Size returns the number of bytes required for the bitmap.
func (b *Bitmap) Size() int {
numbytes := 0
citer, _ := b.Containers.Iterator(0)
for citer.Next() {
_, c := citer.Value()
numbytes += c.size()

}
return numbytes
}

// CountRange returns the number of bits set between [start, end).
func (b *Bitmap) CountRange(start, end uint64) (n uint64) {
if b.Containers.Size() == 0 {
Expand Down
23 changes: 23 additions & 0 deletions roaring/roaring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,29 @@ func TestContainerCount(t *testing.T) {
t.Fatalf("Count != CountRange\n")
}
}
func TestSize(t *testing.T) {
//array
a := roaring.NewFileBitmap(0, 65535, 131072)
if a.Size() != 6 {
t.Fatalf("Size in bytes incorrect \n")
}

//bitmap
b := roaring.NewFileBitmap()
for i := uint64(0); i <= 4096; i++ {
b.DirectAdd(i)
}

if b.Size() != 8192 {
t.Fatalf("Size in bytes incorrect \n")
}
//convert to rle
b.Optimize()
//rle
if b.Size() != 6 {
t.Fatalf("Size in bytes incorrect \n")
}
}

func TestCountRange(t *testing.T) {
tests := []struct {
Expand Down

0 comments on commit 923e679

Please sign in to comment.