fix for intersect and updated tests

This commit is contained in:
Todd Gruben 2016-09-22 16:47:39 -05:00
parent 57810f8ab6
commit 415507ffe3
2 changed files with 66 additions and 10 deletions

View file

@ -275,15 +275,36 @@ func (b *Bitmap) container(key uint64) *container {
}
return b.containers[i]
}
func insertU64(original []uint64, position int, value uint64) []uint64 {
l := len(original)
target := original
if cap(original) == l {
target = make([]uint64, l+1, l+524288)
copy(target, original[:position])
} else {
target = append(target, 0)
}
copy(target[position+1:], original[position:])
target[position] = value
return target
}
func insertContainer(original []*container, position int, value *container) []*container {
l := len(original)
target := original
if cap(original) == l {
target = make([]*container, l+1, l+524288)
copy(target, original[:position])
} else {
target = append(target, nil)
}
copy(target[position+1:], original[position:])
target[position] = value
return target
}
func (b *Bitmap) insertAt(key uint64, c *container, i int) {
b.keys = append(b.keys, 0)
copy(b.keys[i+1:], b.keys[i:])
b.keys[i] = key
b.containers = append(b.containers, nil)
copy(b.containers[i+1:], b.containers[i:])
b.containers[i] = c
b.keys = insertU64(b.keys, i, key)
b.containers = insertContainer(b.containers, i, c)
}
// IntersectionCount returns the number of intersections between b and other.
@ -309,7 +330,6 @@ func (b *Bitmap) Intersect(other *Bitmap) *Bitmap {
ki, ci := b.keys, b.containers
kj, cj := other.keys, other.containers
for {
var key uint64
var container *container
@ -327,10 +347,10 @@ func (b *Bitmap) Intersect(other *Bitmap) *Bitmap {
key, container = ki[0], intersect(ci[0], cj[0])
ki, ci = ki[1:], ci[1:]
kj, cj = kj[1:], cj[1:]
output.keys = append(output.keys, key)
output.containers = append(output.containers, container)
}
output.keys = append(output.keys, key)
output.containers = append(output.containers, container)
}
return output

View file

@ -80,6 +80,42 @@ func TestBitmap_Max(t *testing.T) {
}
}
func TestBitmap_Intersection(t *testing.T) {
bm0 := roaring.NewBitmap(0, 2683177)
bm1 := roaring.NewBitmap()
for i := uint64(628); i < 2683301; i++ {
bm1.Add(i)
}
result := bm0.Intersect(bm1)
if n := result.Count(); n != 1 {
t.Fatalf("unexpected n: %d", n)
}
}
func TestBitmap_Difference(t *testing.T) {
bm0 := roaring.NewBitmap(0, 2683177)
bm1 := roaring.NewBitmap()
for i := uint64(628); i < 2683301; i++ {
bm1.Add(i)
}
result := bm0.Difference(bm1)
//expect to have just 0
if n := result.Count(); n != 1 {
t.Fatalf("unexpected n: %d", n)
}
}
func TestBitmap_Union(t *testing.T) {
bm0 := roaring.NewBitmap(0, 1000001, 1000002, 1000003)
bm1 := roaring.NewBitmap(0, 50000, 1000001, 1000002)
result := bm0.Union(bm1)
if n := result.Count(); n != 5 {
t.Fatalf("unexpected n: %d", n)
}
}
// Ensure bitmap can return the number of intersecting bits in two bitmaps.
func TestBitmap_IntersectionCount_ArrayArray(t *testing.T) {
bm0 := roaring.NewBitmap(0, 1000001, 1000002, 1000003)