From e8b9a384791b4207d3cc63558abd6d37af1a03be Mon Sep 17 00:00:00 2001 From: Karl Gaissmaier Date: Fri, 13 Dec 2024 23:19:38 +0100 Subject: [PATCH] simplify the code in NextSet --- bitset.go | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/bitset.go b/bitset.go index 8a9844e..243cf2e 100644 --- a/bitset.go +++ b/bitset.go @@ -501,23 +501,25 @@ func (b *BitSet) NextSet(i uint) (uint, bool) { if x >= len(b.set) { return 0, false } - w := b.set[x] - w = w >> wordsIndex(i) - if w != 0 { - return i + uint(bits.TrailingZeros64(w)), true + + // process first (partial) word + word := b.set[x] >> wordsIndex(i) + if word != 0 { + return i + uint(bits.TrailingZeros64(word)), true } + + // process the following full words until next bit is set x++ - // bounds check elimination in the loop - if x < 0 { + if x >= len(b.set) { return 0, false } - for x < len(b.set) { - if b.set[x] != 0 { - return uint(x)*wordSize + uint(bits.TrailingZeros64(b.set[x])), true - } - x++ + for idx, word := range b.set[x:] { + if word != 0 { + return uint((x+idx)<