Skip to content

Commit

Permalink
Merge pull request #443 from RoaringBitmap/issue440
Browse files Browse the repository at this point in the history
more fixes regarding issue 440
  • Loading branch information
lemire authored Aug 5, 2024
2 parents add9cb0 + d541e61 commit de7eded
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
2 changes: 0 additions & 2 deletions arraycontainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -1227,12 +1227,10 @@ func (ac *arrayContainer) numberOfRuns() (nr int) {
// convert to run or array *if needed*
func (ac *arrayContainer) toEfficientContainer() container {
numRuns := ac.numberOfRuns()

sizeAsRunContainer := runContainer16SerializedSizeInBytes(numRuns)
sizeAsBitmapContainer := bitmapContainerSizeInBytes()
card := ac.getCardinality()
sizeAsArrayContainer := arrayContainerSizeInBytes(card)

if sizeAsRunContainer < minOfInt(sizeAsBitmapContainer, sizeAsArrayContainer) {
return newRunContainer16FromArray(ac)
}
Expand Down
24 changes: 24 additions & 0 deletions roaring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,30 @@ func TestIssue440_3(t *testing.T) {
require.Equal(t, b1, b2)
}

func TestIssue440_4(t *testing.T) {
a := NewBitmap()
a.AddMany([]uint32{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13})
a.RunOptimize()
b1, err := a.MarshalBinary()
require.NoError(t, err)
a.RunOptimize()
b2, err := a.MarshalBinary()
require.NoError(t, err)
require.Equal(t, b1, b2)
}

func TestIssue440_5(t *testing.T) {
a := NewBitmap()
a.AddMany([]uint32{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14})
a.RunOptimize()
b1, err := a.MarshalBinary()
require.NoError(t, err)
a.RunOptimize()
b2, err := a.MarshalBinary()
require.NoError(t, err)
require.Equal(t, b1, b2)
}

func checkValidity(t *testing.T, rb *Bitmap) {
t.Helper()

Expand Down
11 changes: 4 additions & 7 deletions runcontainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ import (
"errors"
"fmt"
"sort"
"unsafe"
)

// runContainer16 does run-length encoding of sets of
Expand Down Expand Up @@ -1013,12 +1012,10 @@ func newRunContainer16TakeOwnership(iv []interval16) *runContainer16 {
}

const (
baseRc16Size = int(unsafe.Sizeof(runContainer16{}))
perIntervalRc16Size = int(unsafe.Sizeof(interval16{}))
baseRc16Size = 2
perIntervalRc16Size = 4
)

const baseDiskRc16Size = int(unsafe.Sizeof(uint16(0)))

// see also runContainer16SerializedSizeInBytes(numRuns int) int

// getSizeInBytes returns the number of bytes of memory
Expand All @@ -1030,7 +1027,7 @@ func (rc *runContainer16) getSizeInBytes() int {
// runContainer16SerializedSizeInBytes returns the number of bytes of disk
// required to hold numRuns in a runContainer16.
func runContainer16SerializedSizeInBytes(numRuns int) int {
return perIntervalRc16Size*numRuns + baseDiskRc16Size
return perIntervalRc16Size*numRuns + baseRc16Size
}

// Add adds a single value k to the set.
Expand Down Expand Up @@ -2521,7 +2518,7 @@ func (rc *runContainer16) toEfficientContainer() container {
sizeAsBitmapContainer := bitmapContainerSizeInBytes()
card := rc.getCardinality()
sizeAsArrayContainer := arrayContainerSizeInBytes(card)
if sizeAsRunContainer > minOfInt(sizeAsBitmapContainer, sizeAsArrayContainer) {
if sizeAsRunContainer < minOfInt(sizeAsBitmapContainer, sizeAsArrayContainer) {
return rc
}
if card <= arrayDefaultMaxSize {
Expand Down

0 comments on commit de7eded

Please sign in to comment.