Skip to content

Commit

Permalink
bitsets: cleanup
Browse files Browse the repository at this point in the history
Signed-off-by: Vicent Marti <vmg@strn.cat>
  • Loading branch information
vmg committed Nov 25, 2022
1 parent f7229f8 commit 80d9020
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 20 deletions.
17 changes: 10 additions & 7 deletions go/vt/vtgate/semantics/bitset/bitset.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,20 +118,19 @@ func (bs Bitset) Set(offset int) Bitset {
return toBitset(words)
}

func (bs Bitset) Single() (offset int) {
var found bool
func (bs Bitset) SingleBit() int {
offset := -1
for i := 0; i < len(bs); i++ {
t := bs[i]
if t == 0 {
continue
}
if found || bits.OnesCount8(t) != 1 {
if offset >= 0 || bits.OnesCount8(t) != 1 {
return -1
}
offset = i*bitsetWidth + bits.TrailingZeros8(t)
found = true
}
return
return offset
}

func (bs Bitset) IsContainedBy(b2 Bitset) bool {
Expand Down Expand Up @@ -168,8 +167,12 @@ func (bs Bitset) ForEach(yield func(int)) {
}

func Build(bits ...int) Bitset {
var max int
for _, b := range bits {
if len(bits) == 0 {
return ""
}

max := bits[0]
for _, b := range bits[1:] {
if b > max {
max = b
}
Expand Down
13 changes: 1 addition & 12 deletions go/vt/vtgate/semantics/bitset/bitset_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package bitset

import (
"encoding/binary"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -11,10 +10,8 @@ func TestSingletons(t *testing.T) {
for i := 0; i < 40; i++ {
bs := Single(i)

t.Logf("%d: %#v", i, []byte(bs))

require.Equal(t, 1, bs.Popcount())
require.Equal(t, i, bs.Single())
require.Equal(t, i, bs.SingleBit())

var called bool
bs.ForEach(func(offset int) {
Expand All @@ -25,11 +22,3 @@ func TestSingletons(t *testing.T) {
require.True(t, called)
}
}

func Test1(t *testing.T) {
var bytes []byte
for i := 8; i < 16; i++ {
bytes = binary.BigEndian.AppendUint16(bytes, 1<<i)
}
t.Logf("%#v", string(bytes))
}
2 changes: 1 addition & 1 deletion go/vt/vtgate/semantics/tabletset.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (ts TableSet) IsEmpty() bool {

// TableOffset returns the offset in the Tables array from TableSet
func (ts TableSet) TableOffset() int {
return bitset.Bitset(ts).Single()
return bitset.Bitset(ts).SingleBit()
}

// ForEachTable calls the given callback with the indices for all tables in this TableSet
Expand Down

0 comments on commit 80d9020

Please sign in to comment.