Skip to content

Commit

Permalink
feature: introduces LenInBytes and capInBytes methods
Browse files Browse the repository at this point in the history
  • Loading branch information
aliszka committed Dec 13, 2024
1 parent e657602 commit 05faef1
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
14 changes: 14 additions & 0 deletions bitmap_opt.go
Original file line number Diff line number Diff line change
Expand Up @@ -666,3 +666,17 @@ func (dst *Bitmap) CompareNumKeys(src *Bitmap) int {
return 0
}
}

func (ra *Bitmap) LenInBytes() int {
if ra == nil {
return 0
}
return len(ra.data) * 2
}

func (ra *Bitmap) capInBytes() int {
if ra == nil {
return 0
}
return cap(ra.data) * 2
}
53 changes: 53 additions & 0 deletions bitmap_opt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -924,6 +924,59 @@ func TestCompareNumKeys(t *testing.T) {
})
}

func TestLenBytes(t *testing.T) {
t.Run("non-nil bitmap", func(t *testing.T) {
bm := NewBitmap()

for _, x := range []int{1, 1 + maxCardinality, 1 + maxCardinality*2} {
bm.Set(uint64(x))

require.Equal(t, len(bm.ToBuffer()), bm.LenInBytes())
}
})

t.Run("empty bitmap", func(t *testing.T) {
bm := NewBitmap()

// real length is greater then 0, though ToBuffer() returns empty slice
require.Less(t, 0, bm.LenInBytes())
})

t.Run("nil bitmap", func(t *testing.T) {
var bm *Bitmap

require.Equal(t, 0, bm.LenInBytes())
})
}

func TestCapBytes(t *testing.T) {
t.Run("non-nil bitmap", func(t *testing.T) {
bm := NewBitmap()

for _, x := range []int{1, 1 + maxCardinality, 1 + maxCardinality*2} {
bm.Set(uint64(x))

// ToBuffer() sets cap to len, real cap is >= than buffer's one
require.LessOrEqual(t, cap(bm.ToBuffer()), bm.capInBytes())
require.LessOrEqual(t, bm.LenInBytes(), bm.capInBytes())
}
})

t.Run("empty bitmap", func(t *testing.T) {
bm := NewBitmap()

// real cap is greater than 0, though ToBuffer() returns empty slice
require.Less(t, 0, bm.capInBytes())
require.LessOrEqual(t, bm.LenInBytes(), bm.capInBytes())
})

t.Run("nil bitmap", func(t *testing.T) {
var bm *Bitmap

require.Equal(t, 0, bm.capInBytes())
})
}

func TestMergeToSuperset(t *testing.T) {
run := func(t *testing.T, bufs [][]uint16) {
containerThreshold := uint64(math.MaxUint16 + 1)
Expand Down

0 comments on commit 05faef1

Please sign in to comment.