Skip to content

Commit

Permalink
[Go] Fix (*BitSet).equals (#3455)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcking authored Jan 2, 2022
1 parent 4620acc commit 89f7760
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
12 changes: 9 additions & 3 deletions runtime/Go/antlr/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,18 @@ func (b *BitSet) equals(other interface{}) bool {
return true
}

if len(b.data) != len(otherBitSet.data) {
// We only compare set bits, so we cannot rely on the two slices having the same size. Its
// possible for two BitSets to have different slice lengths but the same set bits. So we only
// compare the relavent words and ignore the trailing zeros.
bLen := b.minLen()
otherLen := otherBitSet.minLen()

if bLen != otherLen {
return false
}

for k := range b.data {
if b.data[k] != otherBitSet.data[k] {
for i := 0; i < bLen; i++ {
if b.data[i] != otherBitSet.data[i] {
return false
}
}
Expand Down
6 changes: 6 additions & 0 deletions runtime/Go/antlr/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,10 @@ func TestBitSet(t *testing.T) {
bs3.add(63)
bs1.or(bs3)
testBitSet(t, bs1, "{63, 64}", 2, []int{63, 64}, 63, 2)
bs1.clear(64)
bs4 := NewBitSet()
bs4.or(bs1)
if got, want := bs4.equals(bs1), true; got != want {
t.Errorf("%+v.equals(%+v) = %v, want %v", bs4, bs1, got, want)
}
}

0 comments on commit 89f7760

Please sign in to comment.