Skip to content

Commit

Permalink
Merge pull request #297 from seiflotfy/better-bitmap
Browse files Browse the repository at this point in the history
optimise bitmaps
  • Loading branch information
lemire authored Jan 25, 2021
2 parents 6ec715d + fd57ecc commit 62f3e85
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
19 changes: 11 additions & 8 deletions bitmapcontainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ type bitmapContainerShortIterator struct {

func (bcsi *bitmapContainerShortIterator) next() uint16 {
j := bcsi.i
bcsi.i = bcsi.ptr.NextSetBit(bcsi.i + 1)
bcsi.i = bcsi.ptr.NextSetBit(uint(bcsi.i) + 1)
return uint16(j)
}
func (bcsi *bitmapContainerShortIterator) hasNext() bool {
Expand All @@ -126,7 +126,7 @@ func (bcsi *bitmapContainerShortIterator) peekNext() uint16 {

func (bcsi *bitmapContainerShortIterator) advanceIfNeeded(minval uint16) {
if bcsi.hasNext() && bcsi.peekNext() < minval {
bcsi.i = bcsi.ptr.NextSetBit(int(minval))
bcsi.i = bcsi.ptr.NextSetBit(uint(minval))
}
}

Expand Down Expand Up @@ -1007,20 +1007,23 @@ func (bc *bitmapContainer) fillArray(container []uint16) {
}
}

func (bc *bitmapContainer) NextSetBit(i int) int {
x := i / 64
if x >= len(bc.bitmap) {
func (bc *bitmapContainer) NextSetBit(i uint) int {
var (
x = i / 64
length = uint(len(bc.bitmap))
)
if x >= length {
return -1
}
w := bc.bitmap[x]
w = w >> uint(i%64)
if w != 0 {
return i + countTrailingZeros(w)
return int(i) + countTrailingZeros(w)
}
x++
for ; x < len(bc.bitmap); x++ {
for ; x < length; x++ {
if bc.bitmap[x] != 0 {
return (x * 64) + countTrailingZeros(bc.bitmap[x])
return int(x*64) + countTrailingZeros(bc.bitmap[x])
}
}
return -1
Expand Down
5 changes: 3 additions & 2 deletions bitmapcontainer_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package roaring

import (
"github.com/stretchr/testify/assert"
"math/rand"
"testing"

"github.com/stretchr/testify/assert"
)

// bitmapContainer's numberOfRuns() function should be correct against the runContainer equivalent
Expand Down Expand Up @@ -169,7 +170,7 @@ func TestBitmapNextSet(t *testing.T) {

m := 0

for n := 0; m < testSize; n, m = bc.NextSetBit(n+1), m+1 {
for n := 0; m < testSize; n, m = bc.NextSetBit(uint(n)+1), m+1 {
assert.Equal(t, m, n)
}

Expand Down
2 changes: 1 addition & 1 deletion roaring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1744,7 +1744,7 @@ func validate(bc *bitmapContainer, ac *arrayContainer) bool {
// Checking that the two containers contain the same values
counter := 0

for i := bc.NextSetBit(0); i >= 0; i = bc.NextSetBit(i + 1) {
for i := bc.NextSetBit(0); i >= 0; i = bc.NextSetBit(uint(i) + 1) {
counter++
if !ac.contains(uint16(i)) {
log.Println("content differs")
Expand Down

0 comments on commit 62f3e85

Please sign in to comment.