Skip to content

Commit e306912

Browse files
committed
Improved handling of empty BitSet
1 parent 3fb11f2 commit e306912

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

src/main/scala/scala/collection/decorators/BitSetDecorator.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ class BitSetDecorator[+C <: BitSet with BitSetOps[C]](protected val bs: C) {
1515
*/
1616
def <<(shiftBy: Int): C = {
1717

18-
val shiftedBits = if (shiftBy > 0) shiftLeft(shiftBy)
18+
val shiftedBits = if (bs.nwords == 0 || bs.nwords == 1 && bs.word(0) == 0) Array.emptyLongArray
19+
else if (shiftBy > 0) shiftLeft(shiftBy)
1920
else if (shiftBy == 0) bs.toBitMask
2021
else shiftRight(-shiftBy)
2122

@@ -30,7 +31,8 @@ class BitSetDecorator[+C <: BitSet with BitSetOps[C]](protected val bs: C) {
3031
*/
3132
def >>(shiftBy: Int): C = {
3233

33-
val shiftedBits = if (shiftBy > 0) shiftRight(shiftBy)
34+
val shiftedBits = if (bs.nwords == 0 || bs.nwords == 1 && bs.word(0) == 0) Array.emptyLongArray
35+
else if (shiftBy > 0) shiftRight(shiftBy)
3436
else if (shiftBy == 0) bs.toBitMask
3537
else shiftLeft(-shiftBy)
3638

src/test/scala/scala/collection/decorators/BitSetDecoratorTest.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import scala.collection.BitSet
66

77
class BitSetDecoratorTest {
88

9-
import Assert.assertEquals
9+
import Assert.{assertEquals, assertSame}
1010
import BitSet.empty
1111

1212
@Test
1313
def shiftEmptyLeft(): Unit = {
1414
for (shiftBy <- 0 to 128) {
15-
assertEquals(empty, empty << shiftBy)
15+
assertSame(empty, empty << shiftBy)
1616
}
1717
}
1818

@@ -39,15 +39,15 @@ class BitSetDecoratorTest {
3939
@Test
4040
def shiftEmptyRight(): Unit = {
4141
for (shiftBy <- 0 to 128) {
42-
assertEquals(empty, empty >> shiftBy)
42+
assertSame(empty, empty >> shiftBy)
4343
}
4444
}
4545

4646
@Test
4747
def shiftLowestBitRight(): Unit = {
4848
assertEquals(BitSet(0), BitSet(0) >> 0)
4949
for (shiftBy <- 1 to 128) {
50-
assertEquals(empty, BitSet(0) >> shiftBy)
50+
assertSame(empty, BitSet(0) >> shiftBy)
5151
}
5252
}
5353

0 commit comments

Comments
 (0)