Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions roaring64/bsi64.go
Original file line number Diff line number Diff line change
Expand Up @@ -1809,11 +1809,16 @@ func (b *BSI) Retain(retain *Bitmap) (dropped uint64) {
return
}

// NewBSIRetainSet - Construct a new BSI from a clone of existing BSI, retain only values contained in foundSet
// NewBSIRetainSet - Construct a new BSI from a clone of existing BSI, retain only values contained in foundSet.
// The new BSI has the same width, MaxValue, MinValue and run optimization setting as b.
func (b *BSI) NewBSIRetainSet(foundSet *Bitmap) *BSI {

newBSI := NewDefaultBSI()
newBSI.bA = make([]Bitmap, b.BitCount()+1)
newBSI := &BSI{
bA: make([]Bitmap, len(b.bA)),
MaxValue: b.MaxValue,
MinValue: b.MinValue,
runOptimized: b.runOptimized,
}
var wg sync.WaitGroup
wg.Add(1)
go func() {
Expand Down
35 changes: 35 additions & 0 deletions roaring64/bsi64_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,41 @@ func BenchmarkBSIClone(b *testing.B) {
}
}

func TestCloneKeepsSettings(t *testing.T) {
bsi := NewBSI(1000, -1000)
bsi.SetValue(1, -5)
bsi.SetValue(2, 7)
bsi.RunOptimize()
for name, copied := range map[string]*BSI{
"Clone": bsi.Clone(),
"NewBSIRetainSet": bsi.NewBSIRetainSet(BitmapOf(1)),
} {
assert.Equal(t, int64(1000), copied.MaxValue, name)
assert.Equal(t, int64(-1000), copied.MinValue, name)
assert.True(t, copied.HasRunCompression(), name)
assert.Equal(t, bsi.BitCount(), copied.BitCount(), name)
v, ok := copied.GetValue(1)
assert.True(t, ok, name)
assert.Equal(t, int64(-5), v, name)
}

// A copy of a fixed-width BSI stays fixed-width: SetValue does not widen
// it, just as it does not widen the original.
clone := bsi.Clone()
bsi.SetValue(3, 1<<40)
clone.SetValue(3, 1<<40)
assert.Equal(t, bsi.BitCount(), clone.BitCount())

// A copy of an auto-sized BSI stays auto-sized.
auto := NewDefaultBSI()
auto.SetValue(1, 3)
autoClone := auto.Clone()
assert.False(t, autoClone.HasRunCompression())
autoClone.SetValue(2, 1<<40)
v, _ := autoClone.GetValue(2)
assert.Equal(t, int64(1<<40), v)
}

func benchmarkBSIAddSetup(n int, bits uint, signed bool, seed int64) *BSI {
r := rand.New(rand.NewSource(seed))
bsi := NewDefaultBSI()
Expand Down
Loading