Skip to content

Commit

Permalink
Merge pull request #438 from zoltanmeze/fix-previous-value
Browse files Browse the repository at this point in the history
Fix previousValue not returning correct results in some cases
  • Loading branch information
lemire authored Jul 8, 2024
2 parents 072549f + ceda2f8 commit e4aecda
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
5 changes: 3 additions & 2 deletions roaring.go
Original file line number Diff line number Diff line change
Expand Up @@ -1935,13 +1935,14 @@ func (rb *Bitmap) PreviousValue(target uint32) int64 {
}

if rb.highlowcontainer.getKeyAtIndex(containerIndex) > originalKey {
return -1
// target absent, key of first container after target too high
containerIndex--
}

for containerIndex != -1 && prevValue == -1 {
containerKey := rb.highlowcontainer.getKeyAtIndex(containerIndex)
container := rb.highlowcontainer.getContainer(containerKey)
// if containerKey > orginalKey then we are past the container which mapped to the orignal key
// if containerKey > originalKey then we are past the container which mapped to the original key
// in that case we can just return the minimum from that container
var responseBit int
if containerKey < originalKey {
Expand Down
11 changes: 11 additions & 0 deletions roaring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2973,6 +2973,17 @@ func TestNextAndPreviousValue(t *testing.T) {
}
})

t.Run("Absent target container", func(t *testing.T) {
bmp := BitmapOf(2, 3, 131072, MaxUint32)

assert.Equal(t, int64(3), bmp.PreviousValue(65536))
assert.Equal(t, int64(131072), bmp.PreviousValue(MaxUint32>>1))
assert.Equal(t, int64(131072), bmp.PreviousValue(MaxUint32-131071))

bmp = BitmapOf(131072)
assert.Equal(t, int64(-1), bmp.PreviousValue(65536))
})

t.Run("skipping with ranges", func(t *testing.T) {
bmp := New()
intervalEnd := 512
Expand Down

0 comments on commit e4aecda

Please sign in to comment.