Skip to content

Commit

Permalink
Step down container index if next present container key is larger tha…
Browse files Browse the repository at this point in the history
…n target container key

This means that previous value (if exist) must be always in the previous container
  • Loading branch information
zoltanmeze committed Jul 8, 2024
1 parent 072549f commit ceda2f8
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 ceda2f8

Please sign in to comment.