diff --git a/roaring/containers.go b/roaring/containers.go index 08c8d25eb..133a30cf3 100644 --- a/roaring/containers.go +++ b/roaring/containers.go @@ -124,6 +124,14 @@ func (sc *SliceContainers) Size() int { } +func (sc *SliceContainers) Count() uint64 { + n := uint64(0) + for i := range sc.containers { + n += uint64(sc.containers[i].n) + } + return n +} + func (sc *SliceContainers) seek(key uint64) (int, bool) { i := search64(sc.keys, key) found := true @@ -153,6 +161,7 @@ func (si *SliceIterator) Next() bool { si.key = si.e.keys[si.i] si.value = si.e.containers[si.i] si.i++ + return true } diff --git a/roaring/roaring.go b/roaring/roaring.go index fafce4d59..8d132e7e0 100644 --- a/roaring/roaring.go +++ b/roaring/roaring.go @@ -93,6 +93,7 @@ type Containers interface { // return the first container at or after key. found will be true if a // container is found at key. Iterator(key uint64) (citer ContainerIterator, found bool) + Count() uint64 } type ContainerIterator interface { @@ -218,12 +219,7 @@ func (b *Bitmap) Max() uint64 { // Count returns the number of bits set in the bitmap. func (b *Bitmap) Count() (n uint64) { - citer, _ := b.Containers.Iterator(0) - for citer.Next() { - _, c := citer.Value() - n += uint64(c.n) - } - return n + return b.Containers.Count() } // CountRange returns the number of bits set between [start, end).