diff --git a/roaring/internal_test.go b/roaring/internal_test.go deleted file mode 100644 index 107851489..000000000 --- a/roaring/internal_test.go +++ /dev/null @@ -1,57 +0,0 @@ -// Copyright 2017 Pilosa Corp. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package roaring - -import ( - "reflect" - "testing" -) - -// Ensure iterator returns values from a bitmap. -func TestBitmapIterator(t *testing.T) { - for i, tt := range []struct { - bitmap []uint64 - values []uint16 - }{ - // Empty - { - bitmap: []uint64{6}, // 0110 - values: []uint16{1, 2}, - }, - - // Single uint64 bitmap - { - bitmap: []uint64{6}, // 0110 - values: []uint16{1, 2}, - }, - - // Multi uint64 bitmap - { - bitmap: []uint64{1 << 63, 1, 0, 1, 3 << 62}, - values: []uint16{63, 64, 192, 318, 319}, - }, - } { - itr := newBitmapIterator(tt.bitmap) - - var a []uint16 - for v, eof := itr.next(); !eof; v, eof = itr.next() { - a = append(a, v) - } - - if !reflect.DeepEqual(a, tt.values) { - t.Errorf("%d. unexpected values: exp=%+v, got=%+v", i, a, tt.values) - } - } -} diff --git a/roaring/roaring.go b/roaring/roaring.go index 3c2276715..3f8dcd0d8 100644 --- a/roaring/roaring.go +++ b/roaring/roaring.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -// package roaring implements roaring bitmaps with support for incremental changes. +// Package roaring implements roaring bitmaps with support for incremental changes. package roaring import ( @@ -50,11 +50,15 @@ const ( // bitmapN is the number of values in a container.bitmap. bitmapN = (1 << 16) / 64 - // manual allocation size tuned to our average client data - manualAlloc = 524288 - ContainerArray = byte(1) + //ContainerArray indicates a container of bit position values + ContainerArray = byte(1) + + //ContainerBitmap indicates a container of bits packed in a uint64 array block ContainerBitmap = byte(2) - ContainerRun = byte(3) + + //ContainerRun indicates a container of run encoded bits + ContainerRun = byte(3) + maxContainerVal = 0xffff ) @@ -128,7 +132,7 @@ func (b *Bitmap) add(v uint64) bool { // If index is negative then there's not an exact match // and a container needs to be added. if i < 0 { - b.insertAt(hb, newContainer(), int(-i-1)) + b.insertAt(hb, newContainer(), -i-1) i = -i - 1 } return b.containers[i].add(lowbits(v)) @@ -181,7 +185,7 @@ func (b *Bitmap) Max() uint64 { hb := b.keys[len(b.keys)-1] lb := b.containers[len(b.containers)-1].max() - return uint64(hb)<<16 | uint64(lb) + return hb<<16 | uint64(lb) } // Count returns the number of bits set in the bitmap. @@ -216,7 +220,7 @@ func (b *Bitmap) CountRange(start, end uint64) (n uint64) { } else { // Count first partial container and advance i so we don't recount it n += uint64(b.containers[i].countRange(int(lowbits(start)), maxContainerVal+1)) - i += 1 + i++ } // Count last container. @@ -364,10 +368,8 @@ func (b *Bitmap) Intersect(other *Bitmap) *Bitmap { if ni == 0 && nj == 0 { // eof(i,j) break } else if ni == 0 || (nj != 0 && ki[0] > kj[0]) { // eof(i) or i > j - key, container = kj[0], cj[0].clone() kj, cj = kj[1:], cj[1:] } 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 { // i == j key, container = ki[0], intersect(ci[0], cj[0]) @@ -566,7 +568,7 @@ func (b *Bitmap) WriteTo(w io.Writer) (n int64, err error) { byte8 := make([]byte, 8) // Build header before writing individual container blocks. - // Metadata for each container is 8+2+2+4 = sizeof(key) + sizeof(container_type)+sizeof(cardinality) + sizeof(file offset) + // Metadata for each container is 8+2+2+4 = sizeof(key) + sizeof(containerType)+sizeof(cardinality) + sizeof(file offset) // Cookie header section. ew := &errWriter{ w: w, @@ -586,8 +588,8 @@ func (b *Bitmap) WriteTo(w io.Writer) (n int64, err error) { //count := c.count() //assert(c.count() == c.n, "cannot write container count, mismatch: count=%d, n=%d", count, c.n) if c.n > 0 { - ew.WriteUint64(byte8, uint64(key)) - ew.WriteUint16(byte2, uint16(c.container_type)) + ew.WriteUint64(byte8, key) + ew.WriteUint16(byte2, uint16(c.containerType)) ew.WriteUint16(byte2, uint16(c.n-1)) } } @@ -598,7 +600,7 @@ func (b *Bitmap) WriteTo(w io.Writer) (n int64, err error) { for _, c := range b.containers { if c.n > 0 { - ew.WriteUint32(byte4, uint32(offset)) + ew.WriteUint32(byte4, offset) offset += uint32(c.size()) } } @@ -662,14 +664,14 @@ func (b *Bitmap) UnmarshalBinary(data []byte) error { if i >= len(b.keys) { b.keys = append(b.keys, binary.LittleEndian.Uint64(buf[0:8])) b.containers = append(b.containers, &container{ - container_type: byte(binary.LittleEndian.Uint16(buf[8:10])), - n: int(binary.LittleEndian.Uint16(buf[10:12])) + 1, - mapped: true, + containerType: byte(binary.LittleEndian.Uint16(buf[8:10])), + n: int(binary.LittleEndian.Uint16(buf[10:12])) + 1, + mapped: true, }) } else { b.keys[i] = binary.LittleEndian.Uint64(buf[0:8]) c := b.containers[i] - c.container_type = byte(binary.LittleEndian.Uint16(buf[8:10])) + c.containerType = byte(binary.LittleEndian.Uint16(buf[8:10])) c.n = int(binary.LittleEndian.Uint16(buf[10:12])) + 1 c.mapped = true @@ -687,7 +689,7 @@ func (b *Bitmap) UnmarshalBinary(data []byte) error { // Map byte slice directly to the container data. c := b.containers[i] - switch c.container_type { + switch c.containerType { case ContainerRun: c.array = nil c.bitmap = nil @@ -716,18 +718,18 @@ func (b *Bitmap) UnmarshalBinary(data []byte) error { } // Unmarshal the op and apply it. - var op op - if err := op.UnmarshalBinary(buf); err != nil { + var opr op + if err := opr.UnmarshalBinary(buf); err != nil { // FIXME(benbjohnson): return error with position so file can be trimmed. return err } - op.apply(b) + opr.apply(b) // Increase the op count. b.opN++ // Move the buffer forward. - buf = buf[op.size():] + buf = buf[opr.size():] } return nil @@ -793,7 +795,7 @@ func (b *Bitmap) Check() error { return a } -//Perform a logical negate of the bits in the range [start,end]. +// Flip performs a logical negate of the bits in the range [start,end]. func (b *Bitmap) Flip(start, end uint64) *Bitmap { result := NewBitmap() itr := b.Iterator() @@ -834,7 +836,7 @@ type Iterator struct { } // eof returns true if the iterator is at the end of the bitmap. -func (itr *Iterator) eof() bool { return int(itr.i) >= len(itr.bitmap.containers) } +func (itr *Iterator) eof() bool { return itr.i >= len(itr.bitmap.containers) } // Seek moves to the first value equal to or greater than `seek`. func (itr *Iterator) Seek(seek uint64) { @@ -849,7 +851,7 @@ func (itr *Iterator) Seek(seek uint64) { // Move to the correct value index inside the container. lb := lowbits(seek) - if int(itr.i) >= len(itr.bitmap.containers) { + if itr.i >= len(itr.bitmap.containers) { panic(fmt.Sprintf("data Corruption %d %d %d", itr.i, len(itr.bitmap.containers), seek)) } c := itr.bitmap.containers[itr.i] @@ -859,7 +861,7 @@ func (itr *Iterator) Seek(seek uint64) { if itr.j < 0 { itr.j = -itr.j - 1 } - if int(itr.j) < len(c.array) { + if itr.j < len(c.array) { itr.j-- return } @@ -902,7 +904,7 @@ func (itr *Iterator) Next() (v uint64, eof bool) { c := itr.bitmap.containers[itr.i] if c.isArray() { - if itr.j >= int(c.n-1) { + if itr.j >= c.n-1 { // Reached end of array, move to the next container. itr.i, itr.j = itr.i+1, -1 continue @@ -951,7 +953,7 @@ func (itr *Iterator) Next() (v uint64, eof bool) { itr.j++ // Find first non-zero bit in current bitmap, if possible. - hb := int(itr.j >> 6) + hb := itr.j >> 6 if hb >= len(c.bitmap) { itr.i, itr.j = itr.i+1, -1 @@ -959,14 +961,14 @@ func (itr *Iterator) Next() (v uint64, eof bool) { } lb := c.bitmap[hb] >> (uint(itr.j) % 64) if lb != 0 { - itr.j = int(itr.j) + trailingZeroN(lb) + itr.j = itr.j + trailingZeroN(lb) return itr.peek(), false } // Otherwise iterate through remaining bitmaps to find next bit. for hb++; hb < len(c.bitmap); hb++ { if c.bitmap[hb] != 0 { - itr.j = int(hb<<6) + trailingZeroN(c.bitmap[hb]) + itr.j = hb<<6 + trailingZeroN(c.bitmap[hb]) return itr.peek(), false } } @@ -981,18 +983,18 @@ func (itr *Iterator) peek() uint64 { key := itr.bitmap.keys[itr.i] c := itr.bitmap.containers[itr.i] if c.isArray() { - return uint64(key)<<16 | uint64(c.array[itr.j]) + return key<<16 | uint64(c.array[itr.j]) } if c.isRun() { - return uint64(key)<<16 | uint64(c.runs[itr.j].start+uint16(itr.k)) + return key<<16 | uint64(c.runs[itr.j].start+uint16(itr.k)) } - return uint64(key)<<16 | uint64(itr.j) + return key<<16 | uint64(itr.j) } -// The maximum size of array containers. +// ArrayMaxSize represents the maximum size of array containers. const ArrayMaxSize = 4096 -// The maximum size of run length encoded containers. +// RunMaxSize represents the maximum size of run length encoded containers. const RunMaxSize = 2048 // container represents a container for uint32 integers. @@ -1002,12 +1004,12 @@ const RunMaxSize = 2048 // an array or RLE container is used, depending on the contents. For containers // with more than 4,096 values, the values are encoded into bitmaps. type container struct { - container_type byte // array, bitmap, or run - n int // number of integers in container - array []uint16 // used for array containers - bitmap []uint64 // used for bitmap containers - runs []interval16 // used for RLE containers - mapped bool // mapped directly to a byte slice when true + mapped bool // mapped directly to a byte slice when true + containerType byte // array, bitmap, or run + n int // number of integers in container + array []uint16 // used for array containers + bitmap []uint64 // used for bitmap containers + runs []interval16 // used for RLE containers } type interval16 struct { @@ -1022,22 +1024,22 @@ func (iv interval16) runlen() int { // newContainer returns a new instance of container. func newContainer() *container { - return &container{container_type: ContainerArray} + return &container{containerType: ContainerArray} } // isArray returns true if the container is an array container. func (c *container) isArray() bool { - return c.container_type == ContainerArray + return c.containerType == ContainerArray } // isBitmap returns true if the container is a bitmap container. func (c *container) isBitmap() bool { - return c.container_type == ContainerBitmap + return c.containerType == ContainerBitmap } // isRun returns true if the container is a run-length-encoded container. func (c *container) isRun() bool { - return c.container_type == ContainerRun + return c.containerType == ContainerRun } // unmap creates copies of the containers data in the heap. @@ -1049,7 +1051,7 @@ func (c *container) unmap() { return } - switch c.container_type { + switch c.containerType { case ContainerArray: tmp := make([]uint16, len(c.array)) copy(tmp, c.array) @@ -1115,7 +1117,7 @@ func (c *container) bitmapCountRange(start, end int) int { } // Count partial ending word. - if int(j) < len(c.bitmap) { + if j < len(c.bitmap) { off := 64 - (uint(end) % 64) n += popcount(c.bitmap[j] << off) } @@ -1135,7 +1137,7 @@ func (c *container) runCountRange(start, end int) (n int) { } // iv is superset of range if int(iv.start) < start && int(iv.last) > end { - return int(end - start) + return end - start } // iv is subset of range if int(iv.start) >= start && int(iv.last) < end { @@ -1227,7 +1229,7 @@ func (c *container) runAdd(v uint16) bool { c.unmap() if iv.last < v { if iv.last == v-1 { - c.runs[i].last += 1 + c.runs[i].last++ } else { c.runs = append(c.runs, interval16{start: v, last: v}) } @@ -1239,10 +1241,10 @@ func (c *container) runAdd(v uint16) bool { return true } // just before an interval - c.runs[i].start -= 1 + c.runs[i].start-- } else if i > 0 && v-1 == c.runs[i-1].last { // just after an interval - c.runs[i-1].last += 1 + c.runs[i-1].last++ } else { // alone newIv := interval16{start: v, last: v} @@ -1276,7 +1278,7 @@ func (c *container) arrayCountRuns() (r int) { prev := -2 for _, v := range c.array { if prev+1 != int(v) { - r += 1 + r++ } prev = int(v) } @@ -1415,9 +1417,9 @@ func (c *container) runRemove(v uint16) bool { if v == c.runs[i].last && v == c.runs[i].start { c.runs = append(c.runs[:i], c.runs[i+1:]...) } else if v == c.runs[i].last { - c.runs[i].last -= 1 + c.runs[i].last-- } else if v == c.runs[i].start { - c.runs[i].start += 1 + c.runs[i].start++ } else if v > c.runs[i].start { last := c.runs[i].last c.runs[i].last = v - 1 @@ -1454,10 +1456,13 @@ func (c *container) bitmapMax() uint16 { } // Find the highest set bit. - for j := uint16(63); j >= 0; j-- { + for j := uint16(63); ; j-- { if v&(1<= na && j >= nb { @@ -2172,7 +2177,7 @@ func unionArrayRun(a, b *container) *container { if b.n == maxContainerVal { return b.clone() } - output := &container{container_type: ContainerRun} + output := &container{containerType: ContainerRun} na, nb := len(a.array), len(b.runs) var vb interval16 var va uint16 @@ -2209,18 +2214,18 @@ func (c *container) runAppendInterval(v interval16) int { if len(c.runs) == 0 { c.runs = append(c.runs, v) return int(v.last-v.start) + 1 - } else { - last := c.runs[len(c.runs)-1] - if last.last == maxContainerVal { //protect against overflow - return 0 - } - if last.last+1 >= v.start && v.last > last.last { - c.runs[len(c.runs)-1].last = v.last - return int(v.last - last.last) - } else if last.last+1 < v.start { - c.runs = append(c.runs, v) - return int(v.last-v.start) + 1 - } + } + + last := c.runs[len(c.runs)-1] + if last.last == maxContainerVal { //protect against overflow + return 0 + } + if last.last+1 >= v.start && v.last > last.last { + c.runs[len(c.runs)-1].last = v.last + return int(v.last - last.last) + } else if last.last+1 < v.start { + c.runs = append(c.runs, v) + return int(v.last-v.start) + 1 } return 0 } @@ -2234,8 +2239,8 @@ func unionRunRun(a, b *container) *container { } na, nb := len(a.runs), len(b.runs) output := &container{ - runs: make([]interval16, 0, na+nb), - container_type: ContainerRun, + runs: make([]interval16, 0, na+nb), + containerType: ContainerRun, } var va, vb interval16 for i, j := 0, 0; i < na || j < nb; { @@ -2354,8 +2359,8 @@ func unionArrayBitmap(a, b *container) *container { func unionBitmapBitmap(a, b *container) *container { output := &container{ - bitmap: make([]uint64, bitmapN), - container_type: ContainerBitmap, + bitmap: make([]uint64, bitmapN), + containerType: ContainerBitmap, } for i := 0; i < bitmapN; i++ { @@ -2397,7 +2402,7 @@ func difference(a, b *container) *container { // differenceArrayArray computes the difference bween two arrays. func differenceArrayArray(a, b *container) *container { - output := &container{container_type: ContainerArray} + output := &container{containerType: ContainerArray} na, nb := len(a.array), len(b.array) for i, j := 0, 0; i < na; { va := a.array[i] @@ -2428,14 +2433,14 @@ func differenceArrayRun(a, b *container) *container { return a.clone() } - output := &container{array: make([]uint16, 0, a.n), container_type: ContainerArray} + output := &container{array: make([]uint16, 0, a.n), containerType: ContainerArray} // cardinality upper bound: card(A) i := 0 // array index j := 0 // run index // handle overlap - for i < int(a.n) { + for i < a.n { // keep all array elements before beginning of runs if a.array[i] < b.runs[j].start { @@ -2464,7 +2469,7 @@ func differenceArrayRun(a, b *container) *container { output.array = append(output.array, a.array[i:]...) // TODO: consider handling container.n mutations in one place // like we do with container.add(). - output.n += int(len(a.array[i:])) + output.n += len(a.array[i:]) } return output } @@ -2488,7 +2493,7 @@ func differenceRunArray(a, b *container) *container { if a.n == 0 || b.n == 0 { return a.clone() } - output := &container{runs: make([]interval16, 0, len(a.runs)), container_type: ContainerRun} + output := &container{runs: make([]interval16, 0, len(a.runs)), containerType: ContainerRun} bidx := 0 vb := b.array[bidx] @@ -2536,7 +2541,7 @@ func differenceRunBitmap(a, b *container) *container { if len(a.runs) > 0 && a.runs[0].start == 0 && a.runs[0].last == 65535 { return b.flipBitmap() } - output := &container{container_type: ContainerRun} + output := &container{containerType: ContainerRun} output.n = a.n if len(a.runs) == 0 { return output @@ -2579,61 +2584,6 @@ func differenceRunBitmap(a, b *container) *container { } } - if output.n < ArrayMaxSize && int(len(output.runs)) > output.n/2 { - output.runToArray() - } else if len(output.runs) > RunMaxSize { - output.runToBitmap() - } - return output -} - -func differenceRunIterator(a *container, itr containerIterator) *container { - - output := &container{runs: make([]interval16, 0, a.n), container_type: ContainerRun} - - vb, eof := itr.next() - j := 0 - vr := a.runs[j] - working := !eof - for working { - switch { - case vb < vr.start: //before - case vb > vr.last: //after - if vr.start <= vr.last { - output.n += output.runAppendInterval(vr) - } - j++ - if j < len(a.runs) { - vr = a.runs[j] - } else { - working = false - } - case vb == vr.start: //begining of run - vr.start++ - case vb == a.runs[j].last: //end of run - vr.last-- - if vr.last >= vr.start { - output.n += output.runAppendInterval(vr) - } - j++ - if j < len(a.runs) { - vr = a.runs[j] - } else { - working = false - } - case vb > vr.start: //inside run - output.n += output.runAppendInterval(interval16{start: vr.start, last: vb - 1}) - vr.start = vb + 1 - - } - vb, eof = itr.next() - if eof { - working = false - } - } - if vr.start <= vr.last { - output.n += output.runAppendInterval(vr) - } if output.n < ArrayMaxSize && len(output.runs) > output.n/2 { output.runToArray() } else if len(output.runs) > RunMaxSize { @@ -2657,7 +2607,7 @@ func differenceRunRun(a, b *container) *container { alen := len(a.runs) blen := len(b.runs) - output := &container{runs: make([]interval16, 0, alen+blen), container_type: ContainerRun} // TODO allocate max then truncate? or something else + output := &container{runs: make([]interval16, 0, alen+blen), containerType: ContainerRun} // TODO allocate max then truncate? or something else // cardinality upper bound: sum of number of runs // each B-run could split an A-run in two, up to len(b.runs) times @@ -2665,7 +2615,7 @@ func differenceRunRun(a, b *container) *container { switch { case alast < bstart: // current A-run entirely preceeds current B-run: keep full A-run, advance to next A-run - output.runs = append(output.runs, interval16{start: uint16(astart), last: uint16(alast)}) + output.runs = append(output.runs, interval16{start: astart, last: alast}) apos++ if apos < alen { astart = a.runs[apos].start @@ -2681,7 +2631,7 @@ func differenceRunRun(a, b *container) *container { default: // overlap if astart < bstart { - output.runs = append(output.runs, interval16{start: uint16(astart), last: uint16(bstart - 1)}) + output.runs = append(output.runs, interval16{start: astart, last: bstart - 1}) } if alast > blast { astart = blast + 1 @@ -2695,7 +2645,7 @@ func differenceRunRun(a, b *container) *container { } } if apos < alen { - output.runs = append(output.runs, interval16{start: uint16(astart), last: uint16(alast)}) + output.runs = append(output.runs, interval16{start: astart, last: alast}) apos++ if apos < alen { output.runs = append(output.runs, a.runs[apos:]...) @@ -2707,7 +2657,7 @@ func differenceRunRun(a, b *container) *container { } func differenceArrayBitmap(a, b *container) *container { - output := &container{container_type: ContainerArray} + output := &container{containerType: ContainerArray} for _, va := range a.array { bmidx := va / 64 bidx := va % 64 @@ -2738,7 +2688,7 @@ func differenceBitmapArray(a, b *container) *container { } func differenceBitmapBitmap(a, b *container) *container { - output := &container{bitmap: make([]uint64, bitmapN), container_type: ContainerBitmap} + output := &container{bitmap: make([]uint64, bitmapN), containerType: ContainerBitmap} for i := range a.bitmap { v := a.bitmap[i] & (^b.bitmap[i]) @@ -2781,7 +2731,7 @@ func xor(a, b *container) *container { } func xorArrayArray(a, b *container) *container { - output := &container{container_type: ContainerArray} + output := &container{containerType: ContainerArray} na, nb := len(a.array), len(b.array) for i, j := 0, 0; i < na || j < nb; { if i < na && j >= nb { @@ -2828,8 +2778,8 @@ func xorArrayBitmap(a, b *container) *container { func xorBitmapBitmap(a, b *container) *container { output := &container{ - bitmap: make([]uint64, bitmapN), - container_type: ContainerBitmap, + bitmap: make([]uint64, bitmapN), + containerType: ContainerBitmap, } for i := 0; i < bitmapN; i++ { v := a.bitmap[i] ^ b.bitmap[i] @@ -2867,7 +2817,6 @@ func (op *op) apply(b *Bitmap) bool { default: panic(fmt.Sprintf("invalid op type: %d", op.typ)) } - return false } // WriteTo writes op to the w. @@ -2911,7 +2860,7 @@ func (op *op) UnmarshalBinary(data []byte) error { // size returns the encoded size of the op, in bytes. func (*op) size() int { return 1 + 8 + 4 } -func highbits(v uint64) uint64 { return uint64(v >> 16) } +func highbits(v uint64) uint64 { return v >> 16 } func lowbits(v uint64) uint16 { return uint16(v & 0xFFFF) } // search32 returns the index of value in a. If value is not found, it works the @@ -3014,7 +2963,7 @@ func trailingZeroN(v uint64) int { if y := v << 2; y != 0 { n, v = n-2, y } - return int(n - int64(uint64(v<<1)>>63)) + return int(n - int64(v<<1>>63)) } // bit population count, taken from @@ -3029,110 +2978,6 @@ func popcount(x uint64) (n uint64) { return x >> 56 } -// Returns eof as true if there are no values left in the iterator. -type containerIterator interface { - next() (uint16, bool) -} - -// arrayIterator represents an iterator over container array values. -type arrayIterator struct { - array []uint16 - i int -} - -func newArrayIterator(array []uint16) *arrayIterator { - return &arrayIterator{ - array: array, - i: -1, - } -} - -// next returns the next value in the array. -func (itr *arrayIterator) next() (v uint16, eof bool) { - - itr.i++ - if itr.i >= len(itr.array) { - return 0, true - } - return itr.array[itr.i], false -} - -// bitmapIterator represents an iterator over container bitmap values. -type bitmapIterator struct { - bitmap []uint64 - i int -} - -func newBitmapIterator(bitmap []uint64) *bitmapIterator { - return &bitmapIterator{ - bitmap: bitmap, - i: -1, - } -} - -// next returns the next value in the bitmap. -// Returns eof as true if there are no values left in the iterator. -func (itr *bitmapIterator) next() (v uint16, eof bool) { - if itr.i+1 >= int(len(itr.bitmap)*64) { - return 0, true - } - itr.i++ - - // Find first non-zero bit in current bitmap, if possible. - hb := int(itr.i >> 6) - lb := itr.bitmap[hb] >> (uint(itr.i) % 64) - if lb != 0 { - itr.i = int(itr.i) + trailingZeroN(lb) - return uint16(itr.i), false - } - - // Otherwise iterate through remaining bitmaps to find next bit. - for hb++; hb < len(itr.bitmap); hb++ { - if itr.bitmap[hb] != 0 { - itr.i = int(hb<<6) + trailingZeroN(itr.bitmap[hb]) - return uint16(itr.i), false - } - } - - return 0, true -} - -// bufBitmapIterator wraps an iterator to provide the ability to unread values. -type bufBitmapIterator struct { - buf struct { - v uint16 - eof bool - full bool - } - itr *bitmapIterator -} - -// newBufBitmapIterator returns a buffered iterator that wraps a bitmapIterator. -func newBufBitmapIterator(itr *bitmapIterator) *bufBitmapIterator { - return &bufBitmapIterator{itr: itr} -} - -// 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 *bufBitmapIterator) next() (v uint16, 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 -} - -// unread pushes previous pair on to the buffer. Panics if the buffer is already full. -func (itr *bufBitmapIterator) unread() { - if itr.buf.full { - panic("roaring.bufBitmapIterator: buffer full") - } - itr.buf.full = true -} - // ErrorList represents a list of errors. type ErrorList []error @@ -3168,29 +3013,22 @@ func (a *ErrorList) AppendWithPrefix(err error, prefix string) { } } -// assert panics with a formatted message if condition is false. -func assert(condition bool, format string, a ...interface{}) { - if !condition { - panic(fmt.Sprintf(format, a...)) - } -} - // xorArrayRun computes the exclusive or of an array and a run container. func xorArrayRun(a, b *container) *container { - output := &container{container_type: ContainerRun} + output := &container{containerType: ContainerRun} na, nb := len(a.array), len(b.runs) var vb interval16 var va uint16 - last_i, last_j := -1, -1 + lastI, lastJ := -1, -1 for i, j := 0, 0; i < na || j < nb; { - if i < na && i != last_i { + if i < na && i != lastI { va = a.array[i] } - if j < nb && j != last_j { + if j < nb && j != lastJ { vb = b.runs[j] } - last_i = i - last_j = j + lastI = i + lastJ = j if i < na && (j >= nb || va < vb.start) { //before output.n += output.runAppendInterval(interval16{start: va, last: va}) @@ -3240,91 +3078,91 @@ func xorArrayRun(a, b *container) *container { } // xorCompare computes first exclusive run between two runs. -func xorCompare(x *xorstm) (r1 interval16, has_data bool) { - has_data = false - if !x.va_valid || !x.vb_valid { - if x.vb_valid { - x.vb_valid = false +func xorCompare(x *xorstm) (r1 interval16, hasData bool) { + hasData = false + if !x.vaValid || !x.vbValid { + if x.vbValid { + x.vbValid = false r1 = x.vb - has_data = true + hasData = true return } - if x.va_valid { - x.va_valid = false + if x.vaValid { + x.vaValid = false r1 = x.va - has_data = true + hasData = true return } return } if x.va.last < x.vb.start { //va before - x.va_valid = false + x.vaValid = false r1 = x.va - has_data = true + hasData = true } else if x.vb.last < x.va.start { //vb before - x.vb_valid = false + x.vbValid = false r1 = x.vb - has_data = true + hasData = true } else if x.va.start == x.vb.start && x.va.last == x.vb.last { // Equal - x.va_valid = false - x.vb_valid = false + x.vaValid = false + x.vbValid = false } else if x.va.start <= x.vb.start && x.va.last >= x.vb.last { //vb inside - x.vb_valid = false + x.vbValid = false if x.va.start != x.vb.start { r1 = interval16{start: x.va.start, last: x.vb.start - 1} - has_data = true + hasData = true } if x.vb.last == maxContainerVal { // Check for overflow - x.va_valid = false + x.vaValid = false } else { x.va.start = x.vb.last + 1 if x.va.start > x.va.last { - x.va_valid = false + x.vaValid = false } } } else if x.vb.start <= x.va.start && x.vb.last >= x.va.last { //va inside - x.va_valid = false + x.vaValid = false if x.vb.start != x.va.start { r1 = interval16{start: x.vb.start, last: x.va.start - 1} - has_data = true + hasData = true } if x.va.last == maxContainerVal { //check for overflow - x.vb_valid = false + x.vbValid = false } else { x.vb.start = x.va.last + 1 if x.vb.start > x.vb.last { - x.vb_valid = false + x.vbValid = false } } } else if x.va.start < x.vb.start && x.va.last <= x.vb.last { //va first overlap - x.va_valid = false + x.vaValid = false r1 = interval16{start: x.va.start, last: x.vb.start - 1} - has_data = true + hasData = true if x.va.last == maxContainerVal { // check for overflow - x.vb_valid = false + x.vbValid = false } else { x.vb.start = x.va.last + 1 if x.vb.start > x.vb.last { - x.vb_valid = false + x.vbValid = false } } } else if x.vb.start < x.va.start && x.vb.last <= x.va.last { //vb first overlap - x.vb_valid = false + x.vbValid = false r1 = interval16{start: x.vb.start, last: x.va.start - 1} - has_data = true + hasData = true if x.vb.last == maxContainerVal { // check for overflow - x.va_valid = false + x.vaValid = false } else { x.va.start = x.vb.last + 1 if x.va.start > x.va.last { - x.va_valid = false + x.vaValid = false } } } @@ -3333,8 +3171,8 @@ func xorCompare(x *xorstm) (r1 interval16, has_data bool) { //stm is state machine used to "xor" iterate over runs. type xorstm struct { - va_valid, vb_valid bool - va, vb interval16 + vaValid, vbValid bool + va, vb interval16 } // xorRunRun computes the exclusive or of two run containers. @@ -3348,36 +3186,36 @@ func xorRunRun(a, b *container) *container { } output := &container{} - last_i, last_j := -1, -1 + lastI, lastJ := -1, -1 state := &xorstm{} for i, j := 0, 0; i < na || j < nb; { - if i < na && last_i != i { + if i < na && lastI != i { state.va = a.runs[i] - state.va_valid = true + state.vaValid = true } - if j < nb && last_j != j { + if j < nb && lastJ != j { state.vb = b.runs[j] - state.vb_valid = true + state.vbValid = true } - last_i, last_j = i, j + lastI, lastJ = i, j r1, ok := xorCompare(state) if ok { output.n += output.runAppendInterval(r1) } - if !state.va_valid { + if !state.vaValid { i++ } - if !state.vb_valid { + if !state.vbValid { j++ } } - if output.n < ArrayMaxSize && int(len(output.runs)) > output.n/2 { + if output.n < ArrayMaxSize && len(output.runs) > output.n/2 { output.runToArray() } else if len(output.runs) > RunMaxSize { output.runToBitmap() @@ -3392,7 +3230,7 @@ func xorBitmapRun(a, b *container) *container { output.bitmapXorRange(uint64(b.runs[j].start), uint64(b.runs[j].last)+1) } - if output.n < ArrayMaxSize && int(len(output.runs)) > output.n/2 { + if output.n < ArrayMaxSize && len(output.runs) > output.n/2 { output.runToArray() } else if len(output.runs) > RunMaxSize { output.runToBitmap() diff --git a/roaring/roaring_internal_test.go b/roaring/roaring_internal_test.go index f5c12aa83..4b2813908 100644 --- a/roaring/roaring_internal_test.go +++ b/roaring/roaring_internal_test.go @@ -27,11 +27,11 @@ func (iv interval16) String() string { } func (c *container) String() string { - return fmt.Sprintf("<%s container n=%d, array[%d], runs[%d], bitmap[%d]> type:%d", c.info().Type, c.n, len(c.array), len(c.runs), len(c.bitmap), c.container_type) + return fmt.Sprintf("<%s container n=%d, array[%d], runs[%d], bitmap[%d]> type:%d", c.info().Type, c.n, len(c.array), len(c.runs), len(c.bitmap), c.containerType) } func TestRunAppendInterval(t *testing.T) { - a := container{container_type: ContainerRun} + a := container{containerType: ContainerRun} tests := []struct { base []interval16 app interval16 @@ -80,7 +80,7 @@ func TestInterval16RunLen(t *testing.T) { } func TestContainerRunAdd(t *testing.T) { - c := container{runs: make([]interval16, 0), container_type: ContainerRun} + c := container{runs: make([]interval16, 0), containerType: ContainerRun} tests := []struct { op uint16 exp []interval16 @@ -111,7 +111,7 @@ func TestContainerRunAdd(t *testing.T) { } func TestContainerRunAdd2(t *testing.T) { - c := container{runs: make([]interval16, 0), container_type: ContainerRun} + c := container{runs: make([]interval16, 0), containerType: ContainerRun} ret := c.add(0) if !ret { t.Fatalf("result of adding new bit should be true: %v", c.runs) @@ -126,7 +126,7 @@ func TestContainerRunAdd2(t *testing.T) { } func TestRunCountRange(t *testing.T) { - c := container{runs: make([]interval16, 0), container_type: ContainerRun} + c := container{runs: make([]interval16, 0), containerType: ContainerRun} cnt := c.runCountRange(2, 9) if cnt != 0 { t.Fatalf("should get 0 from empty container, but got: %v", cnt) @@ -179,7 +179,7 @@ func TestRunCountRange(t *testing.T) { } func TestRunContains(t *testing.T) { - c := container{runs: make([]interval16, 0), container_type: ContainerRun} + c := container{runs: make([]interval16, 0), containerType: ContainerRun} if c.runContains(5) { t.Fatalf("empty run container should not contain 5") } @@ -201,7 +201,7 @@ func TestRunContains(t *testing.T) { } func TestBitmapCountRange(t *testing.T) { - c := container{container_type: ContainerBitmap} + c := container{containerType: ContainerBitmap} tests := []struct { start int end int @@ -227,11 +227,11 @@ func TestBitmapCountRange(t *testing.T) { func TestIntersectionCountArrayBitmap3(t *testing.T) { a, b := &container{}, &container{} - a.container_type = ContainerBitmap + a.containerType = ContainerBitmap a.bitmap = getFullBitmap() a.n = maxContainerVal + 1 - b.container_type = ContainerBitmap + b.containerType = ContainerBitmap b.bitmap = getFullBitmap() b.n = maxContainerVal + 1 res := intersectBitmapBitmap(a, b) @@ -288,9 +288,9 @@ func TestIntersectionCountArrayBitmap2(t *testing.T) { for i, test := range tests { a.array = test.array - a.container_type = ContainerArray + a.containerType = ContainerArray b.bitmap = test.bitmap - b.container_type = ContainerBitmap + b.containerType = ContainerBitmap ret := intersectionCountArrayBitmap(a, b) if ret != test.exp { t.Fatalf("test #%v intersectCountArrayBitmap fail received: %v exp: %v", i, ret, test.exp) @@ -299,7 +299,7 @@ func TestIntersectionCountArrayBitmap2(t *testing.T) { } func TestRunRemove(t *testing.T) { - c := container{runs: []interval16{{start: 2, last: 10}, {start: 12, last: 13}, {start: 15, last: 16}}, container_type: ContainerRun} + c := container{runs: []interval16{{start: 2, last: 10}, {start: 12, last: 13}, {start: 15, last: 16}}, containerType: ContainerRun} tests := []struct { op uint16 exp []interval16 @@ -333,7 +333,7 @@ func TestRunRemove(t *testing.T) { } func TestRunMax(t *testing.T) { - c := container{runs: []interval16{{start: 2, last: 10}, {start: 12, last: 13}, {start: 15, last: 16}}, container_type: ContainerRun} + c := container{runs: []interval16{{start: 2, last: 10}, {start: 12, last: 13}, {start: 15, last: 16}}, containerType: ContainerRun} max := c.max() if max != 16 { t.Fatalf("max for %v should be 16", c.runs) @@ -347,8 +347,8 @@ func TestRunMax(t *testing.T) { } func TestIntersectionCountArrayRun(t *testing.T) { - a := &container{container_type: ContainerArray, array: []uint16{1, 5, 10, 11, 12}} - b := &container{container_type: ContainerRun, runs: []interval16{{start: 2, last: 10}, {start: 12, last: 13}, {start: 15, last: 16}}} + a := &container{containerType: ContainerArray, array: []uint16{1, 5, 10, 11, 12}} + b := &container{containerType: ContainerRun, runs: []interval16{{start: 2, last: 10}, {start: 12, last: 13}, {start: 15, last: 16}}} ret := intersectionCountArrayRun(a, b) if ret != 3 { @@ -357,16 +357,16 @@ func TestIntersectionCountArrayRun(t *testing.T) { } func TestIntersectionCountBitmapRun(t *testing.T) { - a := &container{container_type: ContainerBitmap, bitmap: []uint64{0x8000000000000000}} - b := &container{container_type: ContainerRun, runs: []interval16{{start: 63, last: 64}}} + a := &container{containerType: ContainerBitmap, bitmap: []uint64{0x8000000000000000}} + b := &container{containerType: ContainerRun, runs: []interval16{{start: 63, last: 64}}} ret := intersectionCountBitmapRun(a, b) if ret != 1 { t.Fatalf("count of %v with %v should be 1, but got %v", a.bitmap, b.runs, ret) } - a = &container{container_type: ContainerBitmap, bitmap: []uint64{0xF0000001, 0xFF00000000000000, 0xFF000000000000F0, 0x0F0000}} - b = &container{container_type: ContainerRun, runs: []interval16{{start: 29, last: 31}, {start: 125, last: 134}, {start: 191, last: 197}, {start: 200, last: 300}}} + a = &container{containerType: ContainerBitmap, bitmap: []uint64{0xF0000001, 0xFF00000000000000, 0xFF000000000000F0, 0x0F0000}} + b = &container{containerType: ContainerRun, runs: []interval16{{start: 29, last: 31}, {start: 125, last: 134}, {start: 191, last: 197}, {start: 200, last: 300}}} ret = intersectionCountBitmapRun(a, b) if ret != 14 { @@ -414,8 +414,8 @@ func TestIntersectionCountRunRun(t *testing.T) { bruns: []interval16{{start: 9, last: 9}, {start: 11, last: 17}}, exp: 6}, } for i, test := range tests { - a.container_type = ContainerRun - b.container_type = ContainerRun + a.containerType = ContainerRun + b.containerType = ContainerRun a.runs = test.aruns b.runs = test.bruns ret := intersectionCountRunRun(a, b) @@ -456,8 +456,8 @@ func TestIntersectArrayRun(t *testing.T) { } for i, test := range tests { - a.container_type = ContainerArray - b.container_type = ContainerRun + a.containerType = ContainerArray + b.containerType = ContainerRun a.array = test.array b.runs = test.runs ret := intersectArrayRun(a, b) @@ -514,8 +514,8 @@ func TestIntersectRunRun(t *testing.T) { }, } for i, test := range tests { - a.container_type = ContainerRun - b.container_type = ContainerRun + a.containerType = ContainerRun + b.containerType = ContainerRun a.runs = test.aruns b.runs = test.bruns ret := intersectRunRun(a, b) @@ -579,8 +579,8 @@ func TestIntersectBitmapRunBitmap(t *testing.T) { for i, v := range test.exp { exp[i] = v } - a.container_type = ContainerBitmap - b.container_type = ContainerRun + a.containerType = ContainerBitmap + b.containerType = ContainerRun ret := intersectBitmapRun(a, b) if ret.isArray() { ret.arrayToBitmap() @@ -640,8 +640,8 @@ func TestIntersectBitmapRunArray(t *testing.T) { a.bitmap[i] = v } b.runs = test.runs - a.container_type = ContainerBitmap - b.container_type = ContainerRun + a.containerType = ContainerBitmap + b.containerType = ContainerRun ret := intersectBitmapRun(a, b) if !reflect.DeepEqual(ret.array, test.exp) { t.Fatalf("test #%v expected %v, but got %v", i, test.exp, ret.array) @@ -658,19 +658,19 @@ func TestUnionMixed(t *testing.T) { // array container a := &container{} a.array = []uint16{1, 4, 5, 7, 10, 11, 12} - a.container_type = ContainerArray + a.containerType = ContainerArray a.n = 7 // bitmap container b := &container{bitmap: make([]uint64, bitmapN)} b.bitmap[0] = uint64(0x3) b.n = 2 - b.container_type = ContainerBitmap + b.containerType = ContainerBitmap // run container r := &container{} r.runs = []interval16{{start: 5, last: 10}} - r.container_type = ContainerRun + r.containerType = ContainerRun r.n = 6 t.Run("various container Unions", func(t *testing.T) { @@ -711,10 +711,10 @@ func TestIntersectMixed(t *testing.T) { a.runs = []interval16{{start: 5, last: 10}} a.n = 6 - a.container_type = ContainerRun + a.containerType = ContainerRun b.array = []uint16{1, 4, 5, 7, 10, 11, 12} b.n = 7 - b.container_type = ContainerArray + b.containerType = ContainerArray res := intersect(a, b) if !reflect.DeepEqual(res.array, []uint16{5, 7, 10}) { t.Fatalf("test #1 expected %v, but got %v", []uint16{5, 7, 10}, res.array) @@ -730,7 +730,7 @@ func TestIntersectMixed(t *testing.T) { } c.bitmap = []uint64{0x60} c.n = 2 - c.container_type = ContainerBitmap + c.containerType = ContainerBitmap res = intersect(c, a) if !reflect.DeepEqual(res.array, []uint16{5, 6}) { @@ -760,15 +760,15 @@ func TestDifferenceMixed(t *testing.T) { a.runs = []interval16{{start: 5, last: 10}} a.n = a.runCountRange(0, 100) - a.container_type = ContainerRun + a.containerType = ContainerRun b.array = []uint16{0, 2, 4, 6, 8, 10, 12} b.n = len(b.array) - b.container_type = ContainerArray + b.containerType = ContainerArray d.array = []uint16{1, 3, 5, 7, 9, 11, 12} d.n = len(d.array) - d.container_type = ContainerArray + d.containerType = ContainerArray res := difference(a, b) @@ -788,7 +788,7 @@ func TestDifferenceMixed(t *testing.T) { c.bitmap = []uint64{0x64} c.n = c.countRange(0, 100) - c.container_type = ContainerBitmap + c.containerType = ContainerBitmap res = difference(c, a) if !reflect.DeepEqual(res.bitmap, []uint64{0x4}) { t.Fatalf("test #4 expected %v, but got %v", []uint16{4}, res.bitmap) @@ -883,8 +883,8 @@ func TestUnionRunRun(t *testing.T) { for i, test := range tests { a.runs = test.aruns b.runs = test.bruns - a.container_type = ContainerRun - b.container_type = ContainerRun + a.containerType = ContainerRun + b.containerType = ContainerRun ret := unionRunRun(a, b) if !reflect.DeepEqual(ret.runs, test.exp) { t.Fatalf("test #%v expected %v, but got %v", i, test.exp, ret.runs) @@ -925,8 +925,8 @@ func TestUnionArrayRun(t *testing.T) { for i, test := range tests { a.array = test.array b.runs = test.runs - a.container_type = ContainerArray - b.container_type = ContainerRun + a.containerType = ContainerArray + b.containerType = ContainerRun ret := unionArrayRun(a, b) if !reflect.DeepEqual(ret.array, test.exp) { t.Fatalf("test #%v expected %v, but got %v", i, test.exp, ret.array) @@ -935,7 +935,7 @@ func TestUnionArrayRun(t *testing.T) { } func TestBitmapSetRange(t *testing.T) { - c := &container{container_type: ContainerBitmap, bitmap: make([]uint64, bitmapN)} + c := &container{containerType: ContainerBitmap, bitmap: make([]uint64, bitmapN)} tests := []struct { bitmap []uint64 start uint64 @@ -975,7 +975,7 @@ func TestBitmapSetRange(t *testing.T) { } func TestArrayToBitmap(t *testing.T) { - a := &container{container_type: ContainerArray} + a := &container{containerType: ContainerArray} tests := []struct { array []uint16 exp []uint64 @@ -1006,7 +1006,7 @@ func TestArrayToBitmap(t *testing.T) { } func TestBitmapToArray(t *testing.T) { - a := &container{container_type: ContainerBitmap} + a := &container{containerType: ContainerBitmap} tests := []struct { bitmap []uint64 exp []uint16 @@ -1037,7 +1037,7 @@ func TestBitmapToArray(t *testing.T) { } func TestRunToBitmap(t *testing.T) { - a := &container{container_type: ContainerRun} + a := &container{containerType: ContainerRun} tests := []struct { runs []interval16 exp []uint64 @@ -1091,7 +1091,7 @@ func getFullBitmap() []uint64 { } func TestBitmapToRun(t *testing.T) { - a := &container{container_type: ContainerBitmap} + a := &container{containerType: ContainerBitmap} tests := []struct { bitmap []uint64 exp []interval16 @@ -1169,7 +1169,7 @@ func TestBitmapToRun(t *testing.T) { } func TestArrayToRun(t *testing.T) { - a := &container{container_type: ContainerArray} + a := &container{containerType: ContainerArray} tests := []struct { array []uint16 exp []interval16 @@ -1203,7 +1203,7 @@ func TestArrayToRun(t *testing.T) { } func TestRunToArray(t *testing.T) { - a := &container{container_type: ContainerRun} + a := &container{containerType: ContainerRun} tests := []struct { runs []interval16 exp []uint16 @@ -1237,7 +1237,7 @@ func TestRunToArray(t *testing.T) { } func TestBitmapZeroRange(t *testing.T) { - c := &container{container_type: ContainerBitmap, bitmap: make([]uint64, bitmapN)} + c := &container{containerType: ContainerBitmap, bitmap: make([]uint64, bitmapN)} tests := []struct { bitmap []uint64 start uint64 @@ -1273,7 +1273,7 @@ func TestBitmapZeroRange(t *testing.T) { if test.expN != c.n { t.Fatalf("test #%v expected n to be %v, but got %v", i, test.expN, c.n) } - for i, _ := range test.bitmap { + for i := range test.bitmap { c.bitmap[i] = 0 } } @@ -1281,8 +1281,8 @@ func TestBitmapZeroRange(t *testing.T) { } func TestUnionBitmapRun(t *testing.T) { - a := &container{container_type: ContainerBitmap, bitmap: make([]uint64, bitmapN)} - b := &container{container_type: ContainerRun} + a := &container{containerType: ContainerBitmap, bitmap: make([]uint64, bitmapN)} + b := &container{containerType: ContainerRun} tests := []struct { bitmap []uint64 runs []interval16 @@ -1313,14 +1313,14 @@ func TestUnionBitmapRun(t *testing.T) { if ret.n != test.expN { t.Fatalf("test #%v expected n to be %v, but got %v", i, test.expN, ret.n) } - for i, _ := range test.bitmap { + for i := range test.bitmap { a.bitmap[i] = 0 } } } func TestBitmapCountRuns(t *testing.T) { - c := &container{container_type: ContainerBitmap, bitmap: make([]uint64, bitmapN)} + c := &container{containerType: ContainerBitmap, bitmap: make([]uint64, bitmapN)} tests := []struct { bitmap []uint64 exp int @@ -1353,7 +1353,7 @@ func TestBitmapCountRuns(t *testing.T) { t.Fatalf("test #%v expected %v but got %v", i, test.exp, ret) } - for j, _ := range test.bitmap { + for j := range test.bitmap { c.bitmap[j] = 0 } } @@ -1370,7 +1370,7 @@ func TestBitmapCountRuns(t *testing.T) { } func TestArrayCountRuns(t *testing.T) { - c := &container{container_type: ContainerArray} + c := &container{containerType: ContainerArray} tests := []struct { array []uint16 exp int @@ -1411,8 +1411,8 @@ func TestArrayCountRuns(t *testing.T) { } func TestDifferenceArrayRun(t *testing.T) { - a := &container{container_type: ContainerArray} - b := &container{container_type: ContainerRun} + a := &container{containerType: ContainerArray} + b := &container{containerType: ContainerRun} tests := []struct { array []uint16 runs []interval16 @@ -1437,8 +1437,8 @@ func TestDifferenceArrayRun(t *testing.T) { } func TestDifferenceRunArray(t *testing.T) { - a := &container{container_type: ContainerRun} - b := &container{container_type: ContainerArray} + a := &container{containerType: ContainerRun} + b := &container{containerType: ContainerArray} tests := []struct { runs []interval16 array []uint16 @@ -1508,8 +1508,8 @@ func MakeLastBitSet() []uint64 { } func TestDifferenceRunBitmap(t *testing.T) { - a := &container{container_type: ContainerRun} - b := &container{container_type: ContainerBitmap, bitmap: make([]uint64, bitmapN)} + a := &container{containerType: ContainerRun} + b := &container{containerType: ContainerBitmap, bitmap: make([]uint64, bitmapN)} tests := []struct { runs []interval16 bitmap []uint64 @@ -1571,8 +1571,8 @@ func TestDifferenceRunBitmap(t *testing.T) { } func TestDifferenceBitmapRun(t *testing.T) { - a := &container{container_type: ContainerBitmap, bitmap: make([]uint64, bitmapN)} - b := &container{container_type: ContainerRun} + a := &container{containerType: ContainerBitmap, bitmap: make([]uint64, bitmapN)} + b := &container{containerType: ContainerRun} tests := []struct { bitmap []uint64 runs []interval16 @@ -1599,8 +1599,8 @@ func TestDifferenceBitmapRun(t *testing.T) { } func TestDifferenceBitmapArray(t *testing.T) { - b := &container{container_type: ContainerBitmap, bitmap: make([]uint64, bitmapN)} - a := &container{container_type: ContainerArray} + b := &container{containerType: ContainerBitmap, bitmap: make([]uint64, bitmapN)} + a := &container{containerType: ContainerArray} tests := []struct { bitmap []uint64 array []uint16 @@ -1649,8 +1649,8 @@ func TestDifferenceBitmapArray(t *testing.T) { } func TestDifferenceBitmapBitmap(t *testing.T) { - a := &container{bitmap: make([]uint64, bitmapN), container_type: ContainerBitmap} - b := &container{bitmap: make([]uint64, bitmapN), container_type: ContainerBitmap} + a := &container{bitmap: make([]uint64, bitmapN), containerType: ContainerBitmap} + b := &container{bitmap: make([]uint64, bitmapN), containerType: ContainerBitmap} tests := []struct { abitmap []uint64 bbitmap []uint64 @@ -1679,8 +1679,8 @@ func TestDifferenceBitmapBitmap(t *testing.T) { } func TestDifferenceRunRun(t *testing.T) { - a := &container{container_type: ContainerRun} - b := &container{container_type: ContainerRun} + a := &container{containerType: ContainerRun} + b := &container{containerType: ContainerRun} tests := []struct { aruns []interval16 bruns []interval16 @@ -1713,7 +1713,7 @@ func TestDifferenceRunRun(t *testing.T) { } func TestWriteReadArray(t *testing.T) { - ca := &container{array: []uint16{1, 10, 100, 1000}, n: 4, container_type: ContainerArray} + ca := &container{array: []uint16{1, 10, 100, 1000}, n: 4, containerType: ContainerArray} ba := &Bitmap{keys: []uint64{0}, containers: []*container{ca}} ba2 := &Bitmap{} var buf bytes.Buffer @@ -1732,7 +1732,7 @@ func TestWriteReadArray(t *testing.T) { func TestWriteReadBitmap(t *testing.T) { // create bitmap containing > 4096 bits - cb := &container{bitmap: make([]uint64, bitmapN), n: 129 * 32, container_type: ContainerBitmap} + cb := &container{bitmap: make([]uint64, bitmapN), n: 129 * 32, containerType: ContainerBitmap} for i := 0; i < 129; i++ { cb.bitmap[i] = 0x5555555555555555 } @@ -1754,7 +1754,7 @@ func TestWriteReadBitmap(t *testing.T) { func TestWriteReadFullBitmap(t *testing.T) { // create bitmap containing > 4096 bits - cb := &container{bitmap: make([]uint64, bitmapN), n: 65536, container_type: ContainerBitmap} + cb := &container{bitmap: make([]uint64, bitmapN), n: 65536, containerType: ContainerBitmap} for i := 0; i < bitmapN; i++ { cb.bitmap[i] = 0xffffffffffffffff } @@ -1782,7 +1782,7 @@ func TestWriteReadFullBitmap(t *testing.T) { } func TestWriteReadRun(t *testing.T) { - cr := &container{runs: []interval16{{start: 3, last: 13}, {start: 100, last: 109}}, n: 21, container_type: ContainerRun} + cr := &container{runs: []interval16{{start: 3, last: 13}, {start: 100, last: 109}}, n: 21, containerType: ContainerRun} br := &Bitmap{keys: []uint64{0}, containers: []*container{cr}} br2 := &Bitmap{} var buf bytes.Buffer @@ -1806,21 +1806,21 @@ func TestXorArrayRun(t *testing.T) { exp *container }{ { - a: &container{array: []uint16{1, 5, 10, 11, 12}, container_type: ContainerArray}, - b: &container{runs: []interval16{{start: 2, last: 10}, {start: 12, last: 13}, {start: 15, last: 16}}, container_type: ContainerRun}, - exp: &container{array: []uint16{1, 2, 3, 4, 6, 7, 8, 9, 11, 13, 15, 16}, container_type: ContainerArray, n: 12}, + a: &container{array: []uint16{1, 5, 10, 11, 12}, containerType: ContainerArray}, + b: &container{runs: []interval16{{start: 2, last: 10}, {start: 12, last: 13}, {start: 15, last: 16}}, containerType: ContainerRun}, + exp: &container{array: []uint16{1, 2, 3, 4, 6, 7, 8, 9, 11, 13, 15, 16}, containerType: ContainerArray, n: 12}, }, { - a: &container{array: []uint16{1, 5, 10, 11, 12, 13, 14}, container_type: ContainerArray}, - b: &container{runs: []interval16{{start: 2, last: 10}, {start: 12, last: 13}, {start: 15, last: 16}}, container_type: ContainerRun}, - exp: &container{array: []uint16{1, 2, 3, 4, 6, 7, 8, 9, 11, 14, 15, 16}, container_type: ContainerArray, n: 12}, + a: &container{array: []uint16{1, 5, 10, 11, 12, 13, 14}, containerType: ContainerArray}, + b: &container{runs: []interval16{{start: 2, last: 10}, {start: 12, last: 13}, {start: 15, last: 16}}, containerType: ContainerRun}, + exp: &container{array: []uint16{1, 2, 3, 4, 6, 7, 8, 9, 11, 14, 15, 16}, containerType: ContainerArray, n: 12}, }, { - a: &container{array: []uint16{65535}, container_type: ContainerArray}, - b: &container{runs: []interval16{{start: 65534, last: 65535}}, container_type: ContainerRun}, - exp: &container{array: []uint16{65534}, container_type: ContainerArray, n: 1}, + a: &container{array: []uint16{65535}, containerType: ContainerArray}, + b: &container{runs: []interval16{{start: 65534, last: 65535}}, containerType: ContainerRun}, + exp: &container{array: []uint16{65534}, containerType: ContainerArray, n: 1}, }, { - a: &container{array: []uint16{65535}, container_type: ContainerArray}, - b: &container{runs: []interval16{{start: 65535, last: 65535}}, container_type: ContainerRun}, - exp: &container{array: []uint16{}, container_type: ContainerArray, n: 0}, + a: &container{array: []uint16{65535}, containerType: ContainerArray}, + b: &container{runs: []interval16{{start: 65535, last: 65535}}, containerType: ContainerRun}, + exp: &container{array: []uint16{}, containerType: ContainerArray, n: 0}, }, } @@ -1841,8 +1841,8 @@ func TestXorArrayRun(t *testing.T) { //special case that didn't fit the xorrunrun table testing below. func TestXorRunRun1(t *testing.T) { - a := &container{container_type: ContainerRun} - b := &container{container_type: ContainerRun} + a := &container{containerType: ContainerRun} + b := &container{containerType: ContainerRun} a.runs = []interval16{{start: 4, last: 10}} b.runs = []interval16{{start: 5, last: 10}} ret := xorRunRun(a, b) @@ -1856,8 +1856,8 @@ func TestXorRunRun1(t *testing.T) { } func TestXorRunRun(t *testing.T) { - a := &container{container_type: ContainerRun} - b := &container{container_type: ContainerRun} + a := &container{containerType: ContainerRun} + b := &container{containerType: ContainerRun} tests := []struct { aruns []interval16 bruns []interval16 @@ -1954,7 +1954,7 @@ func TestXorRunRun(t *testing.T) { } func TestBitmapFlip(t *testing.T) { - c := &container{bitmap: make([]uint64, bitmapN), container_type: ContainerBitmap} + c := &container{bitmap: make([]uint64, bitmapN), containerType: ContainerBitmap} ttable := []struct { original uint64 @@ -1986,7 +1986,7 @@ func TestBitmapFlip(t *testing.T) { } func TestBitmapXorRange(t *testing.T) { - c := &container{bitmap: make([]uint64, bitmapN), container_type: ContainerBitmap} + c := &container{bitmap: make([]uint64, bitmapN), containerType: ContainerBitmap} tests := []struct { bitmap []uint64 start uint64 @@ -2054,8 +2054,8 @@ func TestBitmapXorRange(t *testing.T) { } func TestXorBitmapRun(t *testing.T) { - a := &container{container_type: ContainerBitmap} - b := &container{container_type: ContainerRun} + a := &container{containerType: ContainerBitmap} + b := &container{containerType: ContainerRun} tests := []struct { bitmap []uint64 runs []interval16 @@ -2390,65 +2390,6 @@ func TestBitmap_BitmapWriteToWithEmpty(t *testing.T) { } } -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 := []uint16{} - - itr := newBufBitmapIterator(newBitmapIterator(b.containers[0].bitmap)) - x := uint16(0) - - for i := 0; i < 10; i++ { - x, _ = itr.next() - bin = append(bin, x) - } - exp := []uint16{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, uint16(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() -} - func TestSearc64(t *testing.T) { tests := []struct { a []uint64 @@ -2528,9 +2469,9 @@ func TestSearc64(t *testing.T) { } func TestIntersectArrayBitmap(t *testing.T) { - a, b := &container{container_type: ContainerArray}, &container{ - container_type: ContainerBitmap, - bitmap: make([]uint64, bitmapN), + a, b := &container{containerType: ContainerArray}, &container{ + containerType: ContainerBitmap, + bitmap: make([]uint64, bitmapN), } tests := []struct { array []uint16 @@ -2576,11 +2517,11 @@ func TestIntersectArrayBitmap(t *testing.T) { for i, test := range tests { a.array = test.array - a.container_type = ContainerArray + a.containerType = ContainerArray for i, bmval := range test.bitmap { b.bitmap[i] = bmval } - b.container_type = ContainerBitmap + b.containerType = ContainerBitmap ret := intersectArrayBitmap(a, b).array if len(ret) == 0 && len(test.exp) == 0 { continue