From 13a42d9c0783fdf25e79b832611124d938c8cb23 Mon Sep 17 00:00:00 2001 From: Yuce Tekol Date: Tue, 11 Jun 2019 16:58:32 +0300 Subject: [PATCH] replaced min code with bmp.iterator --- executor.go | 2 +- roaring/containers_btree.go | 7 ----- roaring/containers_slice.go | 7 ----- roaring/containers_test.go | 43 ----------------------------- roaring/roaring.go | 55 ++----------------------------------- 5 files changed, 3 insertions(+), 111 deletions(-) diff --git a/executor.go b/executor.go index 7d474b6a4..7eb13dd79 100644 --- a/executor.go +++ b/executor.go @@ -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 { diff --git a/roaring/containers_btree.go b/roaring/containers_btree.go index 2b1dd55a7..c2dbc01f5 100644 --- a/roaring/containers_btree.go +++ b/roaring/containers_btree.go @@ -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 diff --git a/roaring/containers_slice.go b/roaring/containers_slice.go index 7a5722cf4..cbff4f179 100644 --- a/roaring/containers_slice.go +++ b/roaring/containers_slice.go @@ -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 diff --git a/roaring/containers_test.go b/roaring/containers_test.go index 9d5e1610d..1869d31af 100644 --- a/roaring/containers_test.go +++ b/roaring/containers_test.go @@ -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) - } -} diff --git a/roaring/roaring.go b/roaring/roaring.go index 89dca0f17..7b7b5d144 100644 --- a/roaring/roaring.go +++ b/roaring/roaring.go @@ -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 {