Skip to content

Commit

Permalink
Use stack allocated iterators in each container type to avoid duplica…
Browse files Browse the repository at this point in the history
…ting iterator logic
  • Loading branch information
kevinconaway committed Mar 30, 2020
1 parent 06686db commit 7c87cea
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 28 deletions.
6 changes: 4 additions & 2 deletions arraycontainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ func (ac *arrayContainer) fillLeastSignificant16bits(x []uint32, i int, mask uin
}

func (ac *arrayContainer) iterate(cb func(x uint16) bool) bool {
for i := 0; i < len(ac.content); i++ {
if !cb(ac.content[i]) {
iterator := shortIterator{ac.content, 0}

for iterator.hasNext() {
if !cb(iterator.next()) {
return false
}
}
Expand Down
9 changes: 5 additions & 4 deletions bitmapcontainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,14 @@ func (bc *bitmapContainer) maximum() uint16 {
}

func (bc *bitmapContainer) iterate(cb func(x uint16) bool) bool {
for i := bc.NextSetBit(0); i >= 0; {
j := i
i = bc.NextSetBit(i + 1)
if !cb(uint16(j)) {
iterator := bitmapContainerShortIterator{bc, bc.NextSetBit(0)}

for iterator.hasNext() {
if !cb(iterator.next()) {
return false
}
}

return true
}

Expand Down
7 changes: 3 additions & 4 deletions roaring.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,10 +421,8 @@ func (rb *Bitmap) String() string {
// The iteration results are undefined if the bitmap is modified (e.g., with Add or Remove).
// There is no guarantee as to what order the values will be iterated
func (rb *Bitmap) Iterate(cb func(x uint32) bool) {
var hs uint32
for i := 0; i < rb.highlowcontainer.size(); i++ {
hs = uint32(rb.highlowcontainer.getKeyAtIndex(i)) << 16

hs := uint32(rb.highlowcontainer.getKeyAtIndex(i)) << 16
c := rb.highlowcontainer.getContainerAtIndex(i)

var shouldContinue bool
Expand All @@ -443,8 +441,9 @@ func (rb *Bitmap) Iterate(cb func(x uint32) bool) {
return cb(uint32(x) | hs)
})
}

if !shouldContinue {
return
break
}
}
}
Expand Down
21 changes: 3 additions & 18 deletions runcontainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -1163,25 +1163,10 @@ func (rc *runContainer16) newRunIterator16() *runIterator16 {
}

func (rc *runContainer16) iterate(cb func(x uint16) bool) bool {
curIndex := int64(0)
curPosInIndex := uint16(0)
iterator := runIterator16{rc, 0, 0}

hasNext := func() bool {
return int64(len(rc.iv)) > curIndex+1 ||
(int64(len(rc.iv)) == curIndex+1 && rc.iv[curIndex].length >= curPosInIndex)
}

for hasNext() {
next := rc.iv[curIndex].start + curPosInIndex

if curPosInIndex == rc.iv[curIndex].length {
curPosInIndex = 0
curIndex++
} else {
curPosInIndex++
}

if !cb(next) {
for iterator.hasNext() {
if !cb(iterator.next()) {
return false
}
}
Expand Down

0 comments on commit 7c87cea

Please sign in to comment.