From c3a648b3f85004775f3b944b3c21322a256a4e87 Mon Sep 17 00:00:00 2001 From: Travis Date: Fri, 23 Jun 2017 16:55:49 -0500 Subject: [PATCH] remove unused roaring.BufIterator. test the unread() panic case in bufBitmapIterator --- roaring/roaring.go | 66 ++++---------------------------- roaring/roaring_internal_test.go | 59 ++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 58 deletions(-) diff --git a/roaring/roaring.go b/roaring/roaring.go index 87a8bf277..7a294d118 100644 --- a/roaring/roaring.go +++ b/roaring/roaring.go @@ -976,56 +976,6 @@ func (itr *Iterator) peek() uint64 { return uint64(key)<<16 | uint64(itr.j) } -// BufIterator wraps an iterator to provide the ability to unread values. -type BufIterator struct { - buf struct { - v uint64 - eof bool - full bool - } - itr *Iterator -} - -// NewBufIterator returns a buffered iterator that wraps itr. -func NewBufIterator(itr *Iterator) *BufIterator { - return &BufIterator{itr: itr} -} - -// Seek moves to the first pair equal to or greater than pseek/bseek. -func (itr *BufIterator) Seek(v uint64) { - itr.buf.full = false - itr.itr.Seek(v) -} - -// Next returns the next pair in the bitmap. -// If a value has been buffered then it is returned and the buffer is cleared. -func (itr *BufIterator) Next() (v uint64, eof bool) { - if itr.buf.full { - itr.buf.full = false - return itr.buf.v, itr.buf.eof - } - - // Read value onto buffer in case of unread. - itr.buf.v, itr.buf.eof = itr.itr.Next() - return itr.buf.v, itr.buf.eof -} - -// Peek reads the next value but leaves it on the buffer. -func (itr *BufIterator) Peek() (v uint64, eof bool) { - v, eof = itr.Next() - itr.Unread() - return -} - -// Unread pushes previous pair on to the buffer. -// Panics if the buffer is already full. -func (itr *BufIterator) Unread() { - if itr.buf.full { - panic("roaring.BufIterator: buffer full") - } - itr.buf.full = true -} - // The maximum size of array containers. const ArrayMaxSize = 4096 @@ -2377,7 +2327,7 @@ func (c *container) bitmapZeroRange(i, j uint64) { func unionArrayBitmap(a, b *container) *container { output := &container{} - itr := newBufIterator(newBitmapIterator(b.bitmap)) + itr := newBufBitmapIterator(newBitmapIterator(b.bitmap)) for i := 0; ; { vb, eof := itr.next() if i >= len(a.array) && eof { @@ -2549,7 +2499,7 @@ func differenceRunBitmap(a, b *container) *container { if a.n == 0 || b.n == 0 { return a.clone() } - itr := newBufIterator(newBitmapIterator(b.bitmap)) + itr := newBufBitmapIterator(newBitmapIterator(b.bitmap)) return differenceRunIterator(a, itr) } @@ -2673,7 +2623,7 @@ func differenceRunRun(a, b *container) *container { func differenceArrayBitmap(a, b *container) *container { output := &container{} - itr := newBufIterator(newBitmapIterator(b.bitmap)) + itr := newBufBitmapIterator(newBitmapIterator(b.bitmap)) for i := 0; i < len(a.array); { va := a.array[i] vb, eof := itr.next() @@ -2698,7 +2648,7 @@ func differenceArrayBitmap(a, b *container) *container { func differenceBitmapArray(a, b *container) *container { output := &container{} - itr := newBufIterator(newBitmapIterator(a.bitmap)) + itr := newBufBitmapIterator(newBitmapIterator(a.bitmap)) i := 0 va, eof := itr.next() for { @@ -2728,8 +2678,8 @@ func differenceBitmapArray(a, b *container) *container { func differenceBitmapBitmap(a, b *container) *container { output := &container{} - itr0 := newBufIterator(newBitmapIterator(a.bitmap)) - itr1 := newBufIterator(newBitmapIterator(b.bitmap)) + itr0 := newBufBitmapIterator(newBitmapIterator(a.bitmap)) + itr1 := newBufBitmapIterator(newBitmapIterator(b.bitmap)) v0, eof0 := itr0.next() v1, eof1 := itr1.next() for { @@ -3028,7 +2978,7 @@ type containerIterator interface { next() (uint32, bool) } -// bitmapIterator represents an iterator over container array values. +// arrayIterator represents an iterator over container array values. type arrayIterator struct { array []uint32 i int @@ -3102,7 +3052,7 @@ type bufBitmapIterator struct { } // newBufBitmapIterator returns a buffered iterator that wraps a bitmapIterator. -func newBufIterator(itr *bitmapIterator) *bufBitmapIterator { +func newBufBitmapIterator(itr *bitmapIterator) *bufBitmapIterator { return &bufBitmapIterator{itr: itr} } diff --git a/roaring/roaring_internal_test.go b/roaring/roaring_internal_test.go index 3cb9742c5..75a686443 100644 --- a/roaring/roaring_internal_test.go +++ b/roaring/roaring_internal_test.go @@ -2159,3 +2159,62 @@ func TestBitmap_BitmapWriteToWithEmpty(t *testing.T) { t.Fatalf("Counts do not match after a marshal %d %d", bm0.Count(), bm1.Count()) } } + +func Test_BufBitmapIterator_Next(t *testing.T) { + b := NewBitmap() + for i := uint64(0); i < 4097; i++ { + b.Add(i) + } + if !b.containers[0].isBitmap() { + t.Fatalf("wrong container type") + } + + bin := []uint32{} + + itr := newBufBitmapIterator(newBitmapIterator(b.containers[0].bitmap)) + x := uint32(0) + + for i := 0; i < 10; i++ { + x, _ = itr.next() + bin = append(bin, x) + } + exp := []uint32{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} + if !reflect.DeepEqual(bin, exp) { + t.Fatalf("BufBitmapIterator expected (%v) but got (%v)", exp, bin) + } + + // ensure that unread points next back one such that the last value is repeated + itr.unread() + x, _ = itr.next() + bin = append(bin, x) + exp = append(exp, uint32(9)) + if !reflect.DeepEqual(bin, exp) { + t.Fatalf("BufBitmapIterator expected (%v) but got (%v)", exp, bin) + } +} + +func Test_BufBitmapIterator_UnreadPanic(t *testing.T) { + + defer func() { + if r := recover(); r == nil { + t.Errorf("BufBitmapIterator unread did not panic") + } + }() + + b := NewBitmap() + for i := uint64(0); i < 4097; i++ { + b.Add(i) + } + if !b.containers[0].isBitmap() { + t.Fatalf("wrong container type") + } + + itr := newBufBitmapIterator(newBitmapIterator(b.containers[0].bitmap)) + for i := 0; i < 10; i++ { + itr.next() + } + + // ensure that unreading back-to-back panics + itr.unread() + itr.unread() +}