diff --git a/roaring/roaring.go b/roaring/roaring.go index 9c45c044b..035f17ac7 100644 --- a/roaring/roaring.go +++ b/roaring/roaring.go @@ -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() (numbytes int) { + + citer, _ := b.Containers.Iterator(0) + for citer.Next() { + _, c := citer.Value() + numbytes += c.size() + + } + return +} + // CountRange returns the number of bits set between [start, end). func (b *Bitmap) CountRange(start, end uint64) (n uint64) { if b.Containers.Size() == 0 { diff --git a/roaring/roaring_test.go b/roaring/roaring_test.go index 5da9fc38c..e258dadbd 100644 --- a/roaring/roaring_test.go +++ b/roaring/roaring_test.go @@ -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<2048;i++{ + b.DirectAdd(i) + } + + if b.Size() != 4096 { + 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 {