@@ -8,7 +8,7 @@ class BitSetDecorator[+C <: BitSet with BitSetOps[C]](protected val bs: C) {
8
8
import BitSetOps ._
9
9
10
10
/**
11
- * Bitwise left shift of this BitSet by given the shift distance.
11
+ * Bitwise left shift of this BitSet by the given shift distance.
12
12
* The shift distance may be negative, in which case this method performs a right shift.
13
13
* @param shiftBy shift distance, in bits
14
14
* @return a new BitSet whose value is a bitwise shift left of this BitSet by given shift distance (`shiftBy`)
@@ -44,8 +44,13 @@ class BitSetDecorator[+C <: BitSet with BitSetOps[C]](protected val bs: C) {
44
44
val bitOffset = shiftBy & WordMask
45
45
val wordOffset = shiftBy >>> LogWL
46
46
47
+ var significantWordCount = bs.nwords
48
+ while (significantWordCount > 0 && bs.word(significantWordCount - 1 ) == 0 ) {
49
+ significantWordCount -= 1
50
+ }
51
+
47
52
if (bitOffset == 0 ) {
48
- val newSize = bs.nwords + wordOffset
53
+ val newSize = significantWordCount + wordOffset
49
54
require(newSize <= MaxSize )
50
55
val newBits = Array .ofDim[Long ](newSize)
51
56
var i = wordOffset
@@ -56,14 +61,14 @@ class BitSetDecorator[+C <: BitSet with BitSetOps[C]](protected val bs: C) {
56
61
newBits
57
62
} else {
58
63
val revBitOffset = WordLength - bitOffset
59
- val extraBits = bs.word(bs.nwords - 1 ) >>> revBitOffset
64
+ val extraBits = bs.word(significantWordCount - 1 ) >>> revBitOffset
60
65
val extraWordCount = if (extraBits == 0 ) 0 else 1
61
- val newSize = bs.nwords + wordOffset + extraWordCount
66
+ val newSize = significantWordCount + wordOffset + extraWordCount
62
67
require(newSize <= MaxSize )
63
68
val newBits = Array .ofDim[Long ](newSize)
64
69
var previous = 0L
65
70
var i = 0
66
- while (i < bs.nwords ) {
71
+ while (i < significantWordCount ) {
67
72
val current = bs.word(i)
68
73
newBits(i + wordOffset) = (previous >>> revBitOffset) | (current << bitOffset)
69
74
previous = current
0 commit comments