Skip to content

Commit

Permalink
Minor code cleaning.
Browse files Browse the repository at this point in the history
  • Loading branch information
lemire committed Jul 21, 2018
1 parent fe09428 commit 4bf867c
Show file tree
Hide file tree
Showing 9 changed files with 14 additions and 36 deletions.
10 changes: 0 additions & 10 deletions arraycontainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ func (ac *arrayContainer) iremoveRange(firstOfRange, endx int) container {
// flip the values in the range [firstOfRange,endx)
func (ac *arrayContainer) not(firstOfRange, endx int) container {
if firstOfRange >= endx {
//p("arrayContainer.not(): exiting early with ac.clone()")
return ac.clone()
}
return ac.notClose(firstOfRange, endx-1) // remove everything in [firstOfRange,endx-1]
Expand All @@ -128,18 +127,15 @@ func (ac *arrayContainer) not(firstOfRange, endx int) container {
// flip the values in the range [firstOfRange,lastOfRange]
func (ac *arrayContainer) notClose(firstOfRange, lastOfRange int) container {
if firstOfRange > lastOfRange { // unlike add and remove, not uses an inclusive range [firstOfRange,lastOfRange]
//p("arrayContainer.notClose(): exiting early with ac.clone()")
return ac.clone()
}

// determine the span of array indices to be affected^M
startIndex := binarySearch(ac.content, uint16(firstOfRange))
//p("startIndex=%v", startIndex)
if startIndex < 0 {
startIndex = -startIndex - 1
}
lastIndex := binarySearch(ac.content, uint16(lastOfRange))
//p("lastIndex=%v", lastIndex)
if lastIndex < 0 {
lastIndex = -lastIndex - 2
}
Expand All @@ -148,9 +144,7 @@ func (ac *arrayContainer) notClose(firstOfRange, lastOfRange int) container {
newValuesInRange := spanToBeFlipped - currentValuesInRange
cardinalityChange := newValuesInRange - currentValuesInRange
newCardinality := len(ac.content) + cardinalityChange
//p("new card is %v", newCardinality)
if newCardinality > arrayDefaultMaxSize {
//p("new card over arrayDefaultMaxSize, so returning bitmap")
return ac.toBitmapContainer().not(firstOfRange, lastOfRange+1)
}
answer := newArrayContainer()
Expand Down Expand Up @@ -507,7 +501,6 @@ func (ac *arrayContainer) lazyorArray(value2 *arrayContainer) container {
}

func (ac *arrayContainer) and(a container) container {
//p("ac.and() called")
switch x := a.(type) {
case *arrayContainer:
return ac.andArray(x)
Expand Down Expand Up @@ -726,7 +719,6 @@ func (ac *arrayContainer) inot(firstOfRange, endx int) container {

// flip the values in the range [firstOfRange,lastOfRange]
func (ac *arrayContainer) inotClose(firstOfRange, lastOfRange int) container {
//p("ac.inotClose() starting")
if firstOfRange > lastOfRange { // unlike add and remove, not uses an inclusive range [firstOfRange,lastOfRange]
return ac
}
Expand All @@ -749,7 +741,6 @@ func (ac *arrayContainer) inotClose(firstOfRange, lastOfRange int) container {
if cardinalityChange > 0 {
if newCardinality > len(ac.content) {
if newCardinality > arrayDefaultMaxSize {
//p("ac.inotClose() converting to bitmap and doing inot there")
bcRet := ac.toBitmapContainer()
bcRet.inot(firstOfRange, lastOfRange+1)
*ac = *bcRet.toArrayContainer()
Expand All @@ -770,7 +761,6 @@ func (ac *arrayContainer) inotClose(firstOfRange, lastOfRange int) container {
}
}
ac.content = ac.content[:newCardinality]
//p("bottom of ac.inotClose(): returning ac")
return ac
}

Expand Down
6 changes: 3 additions & 3 deletions benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -581,9 +581,9 @@ func BenchmarkNexts(b *testing.B) {

density := float32(100) / float32(gap)

density_str := fmt.Sprintf("__%f%%", density)
densityStr := fmt.Sprintf("__%f%%", density)

b.Run("next"+density_str, func(b *testing.B) {
b.Run("next"+densityStr, func(b *testing.B) {
for n := 0; n < b.N; n++ {
totnext = 0
iter := bm.Iterator()
Expand All @@ -595,7 +595,7 @@ func BenchmarkNexts(b *testing.B) {
b.StopTimer()
})

b.Run("nextmany"+density_str, func(b *testing.B) {
b.Run("nextmany"+densityStr, func(b *testing.B) {
for n := 0; n < b.N; n++ {
totnextmany = 0
iter := bm.ManyIterator()
Expand Down
14 changes: 2 additions & 12 deletions bitmapcontainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func (bcmi *bitmapContainerManyIterator) nextMany(hs uint32, buf []uint32) int {

for n < len(buf) {
if bitset == 0 {
base += 1
base++
if base >= len(bcmi.ptr.bitmap) {
bcmi.base = base
bcmi.bitset = bitset
Expand Down Expand Up @@ -207,16 +207,13 @@ func bitmapContainerSizeInBytes() int {

func bitmapEquals(a, b []uint64) bool {
if len(a) != len(b) {
//p("bitmaps differ on length. len(a)=%v; len(b)=%v", len(a), len(b))
return false
}
for i, v := range a {
if v != b[i] {
//p("bitmaps differ on element i=%v", i)
return false
}
}
//p("bitmapEquals returning true")
return true
}

Expand All @@ -239,9 +236,7 @@ func (bc *bitmapContainer) fillLeastSignificant16bits(x []uint32, i int, mask ui
func (bc *bitmapContainer) equals(o container) bool {
srb, ok := o.(*bitmapContainer)
if ok {
//p("bitmapContainers.equals: both are bitmapContainers")
if srb.cardinality != bc.cardinality {
//p("bitmapContainers.equals: card differs: %v vs %v", srb.cardinality, bc.cardinality)
return false
}
return bitmapEquals(bc.bitmap, srb.bitmap)
Expand Down Expand Up @@ -331,12 +326,9 @@ func (bc *bitmapContainer) iremoveRange(firstOfRange, lastOfRange int) container
// flip all values in range [firstOfRange,endx)
func (bc *bitmapContainer) inot(firstOfRange, endx int) container {
if endx-firstOfRange == maxCapacity {
//p("endx-firstOfRange == maxCapacity")
flipBitmapRange(bc.bitmap, firstOfRange, endx)
bc.cardinality = maxCapacity - bc.cardinality
//p("bc.cardinality is now %v", bc.cardinality)
} else if endx-firstOfRange > maxCapacity/2 {
//p("endx-firstOfRange > maxCapacity/2")
flipBitmapRange(bc.bitmap, firstOfRange, endx)
bc.computeCardinality()
} else {
Expand Down Expand Up @@ -812,8 +804,6 @@ func (bc *bitmapContainer) andNotRun16(rc *runContainer16) container {
}

func (bc *bitmapContainer) iandNot(a container) container {
//p("bitmapContainer.iandNot() starting")

switch x := a.(type) {
case *arrayContainer:
return bc.iandNotArray(x)
Expand Down Expand Up @@ -957,7 +947,7 @@ func (bc *bitmapContainer) PrevSetBit(i int) int {
if w != 0 {
return b - countLeadingZeros(w)
}
x -= 1
x--
for ; x >= 0; x-- {
if bc.bitmap[x] != 0 {
return (x * 64) + 63 - countLeadingZeros(bc.bitmap[x])
Expand Down
2 changes: 1 addition & 1 deletion bitmapcontainer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestBitmapContainerNumberOfRuns024(t *testing.T) {

func TestBitmapcontainerAndCardinality(t *testing.T) {
Convey("bitmap containers get cardinality in range, miss the last index, issue #183", t, func() {
for r := 0; r <= 65535; r += 1 {
for r := 0; r <= 65535; r++ {
c1 := newRunContainer16Range(0, uint16(r))
c2 := newBitmapContainerwithRange(0, int(r))
So(r+1, ShouldEqual, c1.andCardinality(c2))
Expand Down
6 changes: 3 additions & 3 deletions parallel.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ func toBitmapContainer(c container) container {
func appenderRoutine(bitmapChan chan<- *Bitmap, resultChan <-chan keyedContainer, expectedKeysChan <-chan int) {
expectedKeys := -1
appendedKeys := 0
keys := make([]uint16, 0)
containers := make([]container, 0)
var keys []uint16
var containers []container
for appendedKeys != expectedKeys {
select {
case item := <-resultChan:
Expand Down Expand Up @@ -337,7 +337,7 @@ func ParAnd(parallelism int, bitmaps ...*Bitmap) *Bitmap {
// (if it is set to 0, a default number of workers is chosen)
func ParOr(parallelism int, bitmaps ...*Bitmap) *Bitmap {
var lKey uint16 = MaxUint16
var hKey uint16 = 0
var hKey uint16

bitmapsFiltered := bitmaps[:0]
for _, b := range bitmaps {
Expand Down
2 changes: 1 addition & 1 deletion roaring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1478,7 +1478,7 @@ func TestNextMany(t *testing.T) {
expected := make([]uint32, count)
{
v := uint32(0)
for i, _ := range expected {
for i := range expected {
expected[i] = v
v += gap
}
Expand Down
4 changes: 2 additions & 2 deletions runcontainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -1328,9 +1328,9 @@ func (ri *manyRunIterator16) nextMany(hs uint32, buf []uint32) int {
}
buf[n] = uint32(ri.rc.iv[ri.curIndex].start) | hs
if ri.curIndex != 0 {
ri.curSeq += 1
ri.curSeq++
}
n += 1
n++
// not strictly necessarily due to len(buf)-n min check, but saves some work
continue
}
Expand Down
3 changes: 1 addition & 2 deletions runcontainer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,7 @@ func TestRleRunIterator16(t *testing.T) {
rc := newRunContainer16CopyIv([]interval16{newInterval16Range(4, 9)})
So(rc.cardinality(), ShouldEqual, 6)
it := rc.newManyRunIterator16()

buf := make([]uint32, 0)
var buf []uint32
n := it.nextMany(0, buf)
So(n, ShouldEqual, 0)
}
Expand Down
3 changes: 1 addition & 2 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const (
// MaxUint32 is the largest uint32 value.
MaxUint32 = 4294967295

// One more than the maximum allowed bitmap bit index. For use as an upper
// MaxRange is One more than the maximum allowed bitmap bit index. For use as an upper
// bound for ranges.
MaxRange uint64 = MaxUint32 + 1

Expand Down Expand Up @@ -125,7 +125,6 @@ func flipBitmapRange(bitmap []uint64, start int, end int) {
endword := (end - 1) / 64
bitmap[firstword] ^= ^(^uint64(0) << uint(start%64))
for i := firstword; i < endword; i++ {
//p("flipBitmapRange on i=%v", i)
bitmap[i] = ^bitmap[i]
}
bitmap[endword] ^= ^uint64(0) >> (uint(-end) % 64)
Expand Down

0 comments on commit 4bf867c

Please sign in to comment.