replaced min code with bmp.iterator

This commit is contained in:
Yuce Tekol 2019-06-11 16:58:32 +03:00
parent b64a3e0c68
commit 13a42d9c07
No known key found for this signature in database
GPG key ID: CB59E46D2FB90573
5 changed files with 3 additions and 111 deletions

View file

@ -789,7 +789,7 @@ func (e *executor) executeMinRowShard(ctx context.Context, index string, c *pql.
}, nil
}
// executeMaxRowShard returns the minimum row ID for a shard.
// executeMaxRowShard returns the maximum row ID for a shard.
func (e *executor) executeMaxRowShard(ctx context.Context, index string, c *pql.Call, shard uint64) (Pair, error) {
var filter *Row
if len(c.Children) == 1 {

View file

@ -164,13 +164,6 @@ func (btc *bTreeContainers) Freeze() Containers {
return nbtc
}
func (btc *bTreeContainers) First() (key uint64, c *Container) {
if btc.tree.Len() == 0 {
return 0, nil
}
return btc.tree.First()
}
func (btc *bTreeContainers) Last() (key uint64, c *Container) {
if btc.tree.Len() == 0 {
return 0, nil

View file

@ -131,13 +131,6 @@ func (sc *sliceContainers) Freeze() Containers {
return other
}
func (sc *sliceContainers) First() (key uint64, c *Container) {
if len(sc.keys) == 0 {
return 0, nil
}
return sc.keys[0], sc.containers[0]
}
func (sc *sliceContainers) Last() (key uint64, c *Container) {
if len(sc.keys) == 0 {
return 0, nil

View file

@ -15,7 +15,6 @@
package roaring
import (
"reflect"
"testing"
)
@ -100,45 +99,3 @@ func testContainersIterator(cs Containers, t *testing.T) {
t.Fatalf("itr should be done, but got true")
}
}
func TestContainers(t *testing.T) {
cs := NewFileBitmap().Containers
first := NewContainerArray([]uint16{1, 2, 3})
last := NewContainerArray([]uint16{1, 2, 3, 4, 5, 6})
cs.Put(3, first)
cs.Put(6, last)
key, container := cs.First()
if key != 3 {
t.Fatalf("cs.First key 3 != %d", key)
}
if !reflect.DeepEqual(first, container) {
t.Fatalf("cs.First container %v != %v", first, container)
}
key, container = cs.Last()
if key != 6 {
t.Fatalf("cs.First key 6 != %d", key)
}
if !reflect.DeepEqual(last, container) {
t.Fatalf("cs.First container %v != %v", last, container)
}
cs = NewFileBitmap().Containers
key, container = cs.First()
if key != 0 {
t.Fatalf("cs.First key 0 != %d", key)
}
if nil != container {
t.Fatalf("cs.First container nil != %v", container)
}
key, container = cs.Last()
if key != 0 {
t.Fatalf("cs.First key 0 != %d", key)
}
if nil != container {
t.Fatalf("cs.First container nil != %v", container)
}
}

View file

@ -103,9 +103,6 @@ type Containers interface {
// are shared (but marked as frozen).
Freeze() Containers
// First returns the lowest key and associated container.
First() (key uint64, c *Container)
// Last returns the highest key and associated container.
Last() (key uint64, c *Container)
@ -385,13 +382,8 @@ func (b *Bitmap) remove(v uint64) bool {
// Min returns the lowest value in the bitmap.
// Second return value is true if containers exist in the bitmap.
func (b *Bitmap) Min() (uint64, bool) {
if b.Containers.Size() == 0 {
return 0, false
}
hb, c := b.Containers.First()
lb, ok := c.min()
return hb<<16 | uint64(lb), ok
v, eof := b.Iterator().Next()
return v, !eof
}
// Max returns the highest value in the bitmap.
@ -1995,17 +1987,6 @@ func (c *Container) runRemove(v uint16) (*Container, bool) {
return c, true
}
// min returns the minimum value in the container.
func (c *Container) min() (uint16, bool) {
if c.isArray() {
return c.arrayMin()
} else if c.isRun() {
return c.runMin()
} else {
return c.bitmapMin()
}
}
// max returns the maximum value in the container.
func (c *Container) max() uint16 {
if c == nil || c.N() == 0 {
@ -2021,34 +2002,11 @@ func (c *Container) max() uint16 {
}
}
// Second result value is true if array is non-empty.
func (c *Container) arrayMin() (uint16, bool) {
array := c.array()
if len(array) == 0 {
return 0, false
}
return array[0], true
}
func (c *Container) arrayMax() uint16 {
array := c.array()
return array[len(array)-1]
}
// Second result value is true if array is non-empty.
func (c *Container) bitmapMin() (uint16, bool) {
bitmap := c.bitmap()
for i := 0; i < len(bitmap); i++ {
// If value is zero then skip.
v := bitmap[i]
if v != 0 {
r := bits.TrailingZeros64(v)
return uint16(r + i*64), true
}
}
return 0, false
}
func (c *Container) bitmapMax() uint16 {
// Search bitmap in reverse order.
bitmap := c.bitmap()
@ -2063,15 +2021,6 @@ func (c *Container) bitmapMax() uint16 {
return 0
}
// Second result value is true if array is non-empty.
func (c *Container) runMin() (uint16, bool) {
runs := c.runs()
if len(runs) == 0 {
return 0, false
}
return runs[0].start, true
}
func (c *Container) runMax() uint16 {
runs := c.runs()
if len(runs) == 0 {