From e553b05586428962bf7058d1044519d87ca72d74 Mon Sep 17 00:00:00 2001 From: Daniel Lemire Date: Mon, 20 Aug 2018 14:00:26 -0400 Subject: [PATCH] Minor optimizations. --- bitset.go | 48 +++++------------------------------------------- 1 file changed, 5 insertions(+), 43 deletions(-) diff --git a/bitset.go b/bitset.go index f7f0eef..6d457e8 100644 --- a/bitset.go +++ b/bitset.go @@ -263,27 +263,26 @@ func (b *BitSet) NextSetMany(i uint, buffer []uint) (uint, []uint) { if x >= len(b.set) || capacity == 0 { return 0, myanswer[:0] } - word := b.set[x] skip := i & (wordSize - 1) - word = (word >> skip) << skip + word := b.set[x] >> skip myanswer = myanswer[:capacity] size := int(0) for word != 0 { r := trailingZeroes64(word) - t := word & ((^word) + 1) - myanswer[size] = r + uint(x<<6) + myanswer[size] = r + i size++ if size == capacity { goto End } word = word ^ t } - for idx, word := range b.set[x+1:] { + x++ + for idx, word := range b.set[x:] { for word != 0 { r := trailingZeroes64(word) t := word & ((^word) + 1) - myanswer[size] = r + uint(x+idx+1)*64 + myanswer[size] = r + (uint(x+idx) << 6) size++ if size == capacity { goto End @@ -299,43 +298,6 @@ End: } } -func (b *BitSet) NextSetManyoldold(i uint, buffer []uint) (uint, []uint) { - myanswer := buffer[:0] - x := int(i >> log2WordSize) - if x >= len(b.set) { - return 0, myanswer - } - w := b.set[x] - skip := i & (wordSize - 1) - w = (w >> skip) << skip - base := uint(x << 6) - capacity := cap(buffer) - for len(myanswer) < capacity { - for w != 0 { - r := trailingZeroes64(w) - - t := w & ((^w) + 1) - myanswer = append(myanswer, r+base) - if len(myanswer) == capacity { - goto End - } - w = w ^ t - } - x += 1 - if x == len(b.set) { - break - } - base += 64 - w = b.set[x] - } -End: - if len(myanswer) > 0 { - return myanswer[len(myanswer)-1], myanswer - } else { - return 0, myanswer - } -} - // NextClear returns the next clear bit from the specified index, // including possibly the current index // along with an error code (true = valid, false = no bit found i.e. all bits are set)