mirror of
https://github.com/featurebasedb/featurebase.git
synced 2026-09-09 22:51:02 +00:00
micro optimization to bitmap difference
This commit is contained in:
parent
50dab75c82
commit
3f3cc60531
2 changed files with 21 additions and 10 deletions
|
|
@ -422,24 +422,25 @@ func (b *Bitmap) Difference(other *Bitmap) *Bitmap {
|
|||
ki, ci := b.keys, b.containers
|
||||
kj, cj := other.keys, other.containers
|
||||
|
||||
ni, nj := len(ki), len(kj)
|
||||
i, j := 0, 0
|
||||
for {
|
||||
var key uint64
|
||||
var container *container
|
||||
|
||||
ni, nj := len(ki), len(kj)
|
||||
if ni == 0 { // eof(i)
|
||||
if ni == i { // eof(i)
|
||||
break
|
||||
} else if nj == 0 || ki[0] < kj[0] { // eof(j) or i < j
|
||||
key, container = ki[0], ci[0].clone()
|
||||
ki, ci = ki[1:], ci[1:]
|
||||
} else if nj == j || ki[i] < kj[j] { // eof(j) or i < j
|
||||
key, container = ki[i], ci[i].clone()
|
||||
i++
|
||||
output.keys = append(output.keys, key)
|
||||
output.containers = append(output.containers, container)
|
||||
} else if nj > 0 && ki[0] > kj[0] { // i > j
|
||||
kj, cj = kj[1:], cj[1:]
|
||||
} else if nj > j && ki[i] > kj[j] { // i > j
|
||||
j++
|
||||
} else { // i == j
|
||||
key, container = ki[0], difference(ci[0], cj[0])
|
||||
ki, ci = ki[1:], ci[1:]
|
||||
kj, cj = kj[1:], cj[1:]
|
||||
key, container = ki[i], difference(ci[i], cj[j])
|
||||
i++
|
||||
j++
|
||||
output.keys = append(output.keys, key)
|
||||
output.containers = append(output.containers, container)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@ import (
|
|||
"testing"
|
||||
"testing/quick"
|
||||
|
||||
"github.com/pilosa/pilosa"
|
||||
"github.com/pilosa/pilosa/roaring"
|
||||
_ "github.com/pilosa/pilosa/test"
|
||||
)
|
||||
|
|
@ -487,6 +488,15 @@ func TestBitmap_Difference(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestBitmap_Difference2(t *testing.T) {
|
||||
bm0 := roaring.NewBitmap(0, 1, 2, 131072, 262144, pilosa.SliceWidth+5, pilosa.SliceWidth+7)
|
||||
bm1 := roaring.NewBitmap(2, 3, 100000, 262144, 2*pilosa.SliceWidth+1)
|
||||
result := bm0.Difference(bm1)
|
||||
if !reflect.DeepEqual(result.Slice(), []uint64{0, 1, 131072, pilosa.SliceWidth + 5, pilosa.SliceWidth + 7}) {
|
||||
t.Fatalf("unexpected : %v", result.Slice())
|
||||
}
|
||||
}
|
||||
|
||||
func TestBitmap_Difference_Empty(t *testing.T) {
|
||||
bm0 := roaring.NewBitmap(0, 2683177)
|
||||
bm1 := roaring.NewBitmap()
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue