Skip to content

Commit e90b4c8

Browse files
authored
Merge pull request #205 from whisk/fix/popcnt-empty
fixed UnionCardinality panic on empty bitsets
2 parents b1e974c + 3d78640 commit e90b4c8

2 files changed

Lines changed: 32 additions & 4 deletions

File tree

bitset_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,6 +1302,14 @@ func TestUnion(t *testing.T) {
13021302
}
13031303
}
13041304

1305+
func TestEmptyUnionCardinality(t *testing.T) {
1306+
a := New(0)
1307+
b := New(0)
1308+
if a.UnionCardinality(b) != 0 {
1309+
t.Error("UnionCardinality should be zero")
1310+
}
1311+
}
1312+
13051313
func TestInPlaceUnion(t *testing.T) {
13061314
a := New(100)
13071315
b := New(200)
@@ -1353,6 +1361,14 @@ func TestIntersection(t *testing.T) {
13531361
}
13541362
}
13551363

1364+
func TestEmptyIntersectionCardinality(t *testing.T) {
1365+
a := New(0)
1366+
b := New(0)
1367+
if a.IntersectionCardinality(b) != 0 {
1368+
t.Error("IntersectionCardinality should be zero")
1369+
}
1370+
}
1371+
13561372
func TestInplaceIntersection(t *testing.T) {
13571373
a := New(100)
13581374
b := New(200)
@@ -1408,6 +1424,14 @@ func TestDifference(t *testing.T) {
14081424
}
14091425
}
14101426

1427+
func TestEmptyDifferenceCardinality(t *testing.T) {
1428+
a := New(0)
1429+
b := New(0)
1430+
if a.DifferenceCardinality(b) != 0 {
1431+
t.Error("DifferenceCardinality should be zero")
1432+
}
1433+
}
1434+
14111435
func TestInPlaceDifference(t *testing.T) {
14121436
a := New(100)
14131437
b := New(200)
@@ -1463,6 +1487,14 @@ func TestSymmetricDifference(t *testing.T) {
14631487
}
14641488
}
14651489

1490+
func TestEmptySymmetricDifferenceCardinality(t *testing.T) {
1491+
a := New(0)
1492+
b := New(0)
1493+
if a.SymmetricDifferenceCardinality(b) != 0 {
1494+
t.Error("SymmetricDifferenceCardinality should be zero")
1495+
}
1496+
}
1497+
14661498
func TestInPlaceSymmetricDifference(t *testing.T) {
14671499
a := New(100)
14681500
b := New(200)

popcnt.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,27 @@ func popcntSlice(s []uint64) (cnt uint64) {
1010
}
1111

1212
func popcntMaskSlice(s, m []uint64) (cnt uint64) {
13-
_ = m[len(s)-1] // BCE
1413
for i := range s {
1514
cnt += uint64(bits.OnesCount64(s[i] &^ m[i]))
1615
}
1716
return
1817
}
1918

2019
func popcntAndSlice(s, m []uint64) (cnt uint64) {
21-
_ = m[len(s)-1] // BCE
2220
for i := range s {
2321
cnt += uint64(bits.OnesCount64(s[i] & m[i]))
2422
}
2523
return
2624
}
2725

2826
func popcntOrSlice(s, m []uint64) (cnt uint64) {
29-
_ = m[len(s)-1] // BCE
3027
for i := range s {
3128
cnt += uint64(bits.OnesCount64(s[i] | m[i]))
3229
}
3330
return
3431
}
3532

3633
func popcntXorSlice(s, m []uint64) (cnt uint64) {
37-
_ = m[len(s)-1] // BCE
3834
for i := range s {
3935
cnt += uint64(bits.OnesCount64(s[i] ^ m[i]))
4036
}

0 commit comments

Comments
 (0)