Skip to content

Commit

Permalink
feature: introduces CompareNumKeys to compare number of keys/containe…
Browse files Browse the repository at this point in the history
…rs between bitmaps
  • Loading branch information
aliszka committed Dec 5, 2024
1 parent 08464f0 commit e657602
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
19 changes: 19 additions & 0 deletions bitmap_opt.go
Original file line number Diff line number Diff line change
Expand Up @@ -647,3 +647,22 @@ func andNotSelectedContainers(a, b *Bitmap, ai, an, bi, bn int, containerBuf []u
}
}
}

func (dst *Bitmap) CompareNumKeys(src *Bitmap) int {
if dst == nil && src == nil {
return 0
}
if src == nil {
return 1
}
if dst == nil {
return -1
}
if dstN, srcN := dst.keys.numKeys(), src.keys.numKeys(); dstN > srcN {
return 1
} else if dstN < srcN {
return -1
} else {
return 0
}
}
53 changes: 53 additions & 0 deletions bitmap_opt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,59 @@ func TestIssue_Or_NotMergeContainers(t *testing.T) {
})
}

func TestCompareNumKeys(t *testing.T) {
var bmNil *Bitmap

bm1Key := NewBitmap()
bm1Key.Set(1)

bm2Keys := NewBitmap()
bm2Keys.Set(1)
bm2Keys.Set(1 + uint64(maxCardinality))

bm3Keys := NewBitmap()
bm3Keys.Set(1)
bm3Keys.Set(1 + uint64(maxCardinality))
bm3Keys.Set(1 + uint64(maxCardinality)*2)

t.Run("greater", func(t *testing.T) {
for _, bms := range [][2]*Bitmap{
{bm1Key, bmNil},
{bm2Keys, bmNil},
{bm2Keys, bm1Key},
{bm3Keys, bmNil},
{bm3Keys, bm1Key},
{bm3Keys, bm2Keys},
} {
require.Equal(t, 1, bms[0].CompareNumKeys(bms[1]))
}
})

t.Run("equal", func(t *testing.T) {
for _, bms := range [][2]*Bitmap{
{bmNil, bmNil},
{bm1Key, bm1Key},
{bm2Keys, bm2Keys},
{bm3Keys, bm3Keys},
} {
require.Equal(t, 0, bms[0].CompareNumKeys(bms[1]))
}
})

t.Run("less", func(t *testing.T) {
for _, bms := range [][2]*Bitmap{
{bmNil, bm1Key},
{bmNil, bm2Keys},
{bmNil, bm3Keys},
{bm1Key, bm2Keys},
{bm1Key, bm3Keys},
{bm2Keys, bm3Keys},
} {
require.Equal(t, -1, bms[0].CompareNumKeys(bms[1]))
}
})
}

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

0 comments on commit e657602

Please sign in to comment.