Skip to content

Commit 5d0d532

Browse files
authored
Merge pull request scala/scala#10238 from francesco-kriegel/patch-1
Faulty implementation of method `diff` in subclass `BitSetN` of `scala.collection.immutable.BitSet`
2 parents f3ef126 + fe70961 commit 5d0d532

File tree

1 file changed

+62
-44
lines changed

1 file changed

+62
-44
lines changed

library/src/scala/collection/immutable/BitSet.scala

Lines changed: 62 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -244,36 +244,45 @@ object BitSet extends SpecificIterableFactory[Int, BitSet] {
244244
anyChanges ||= currentWord != oldWord
245245
i -= 1
246246
}
247-
if (i < 0) {
248-
// all indices >= 0 have had result 0, so the bitset is empty
249-
this.empty
250-
} else {
251-
val minimumNonZeroIndex: Int = i + 1
252-
while (!anyChanges && i >= 0) {
253-
val oldWord = word(i)
254-
currentWord = oldWord & ~bs.word(i)
255-
anyChanges ||= currentWord != oldWord
256-
i -= 1
257-
}
258-
if (anyChanges) {
259-
if (minimumNonZeroIndex == -1) {
260-
this.empty
261-
} else if (minimumNonZeroIndex == 0) {
262-
new BitSet1(currentWord)
263-
} else if (minimumNonZeroIndex == 1) {
264-
new BitSet2(word(0) & ~bs.word(0), currentWord)
247+
i match {
248+
case -1 =>
249+
if (anyChanges) {
250+
if (currentWord == 0) {
251+
this.empty
252+
} else {
253+
new BitSet1(currentWord)
254+
}
265255
} else {
256+
this
257+
}
258+
case 0 =>
259+
val oldFirstWord = word(0)
260+
val firstWord = oldFirstWord & ~bs.word(0)
261+
anyChanges ||= firstWord != oldFirstWord
262+
if (anyChanges) {
263+
new BitSet2(firstWord, currentWord)
264+
} else {
265+
this
266+
}
267+
case _ =>
268+
val minimumNonZeroIndex: Int = i + 1
269+
while (!anyChanges && i >= 0) {
270+
val oldWord = word(i)
271+
currentWord = oldWord & ~bs.word(i)
272+
anyChanges ||= currentWord != oldWord
273+
i -= 1
274+
}
275+
if (anyChanges) {
266276
val newArray = elems.take(minimumNonZeroIndex + 1)
267277
newArray(i + 1) = currentWord
268278
while (i >= 0) {
269279
newArray(i) = word(i) & ~bs.word(i)
270280
i -= 1
271281
}
272-
this.fromBitMaskNoCopy(newArray)
282+
new BitSetN(newArray)
283+
} else {
284+
this
273285
}
274-
} else {
275-
this
276-
}
277286
}
278287
} else {
279288
var i = bsnwords - 1
@@ -314,36 +323,45 @@ object BitSet extends SpecificIterableFactory[Int, BitSet] {
314323
anyChanges ||= currentWord != oldWord
315324
i -= 1
316325
}
317-
if (i < 0) {
318-
// all indices >= 0 have had result 0, so the bitset is empty
319-
if (currentWord == 0) this.empty else this.fromBitMaskNoCopy(Array(currentWord))
320-
} else {
321-
val minimumNonZeroIndex: Int = i + 1
322-
while (!anyChanges && i >= 0) {
323-
val oldWord = word(i)
324-
currentWord = BitSetOps.computeWordForFilter(pred, isFlipped, oldWord, i)
325-
anyChanges ||= currentWord != oldWord
326-
i -= 1
327-
}
328-
if (anyChanges) {
329-
if (minimumNonZeroIndex == -1) {
330-
this.empty
331-
} else if (minimumNonZeroIndex == 0) {
332-
new BitSet1(currentWord)
333-
} else if (minimumNonZeroIndex == 1) {
334-
new BitSet2(BitSetOps.computeWordForFilter(pred, isFlipped, word(0), 0), currentWord)
326+
i match {
327+
case -1 =>
328+
if (anyChanges) {
329+
if (currentWord == 0) {
330+
this.empty
331+
} else {
332+
new BitSet1(currentWord)
333+
}
335334
} else {
335+
this
336+
}
337+
case 0 =>
338+
val oldFirstWord = word(0)
339+
val firstWord = BitSetOps.computeWordForFilter(pred, isFlipped, oldFirstWord, 0)
340+
anyChanges ||= firstWord != oldFirstWord
341+
if (anyChanges) {
342+
new BitSet2(firstWord, currentWord)
343+
} else {
344+
this
345+
}
346+
case _ =>
347+
val minimumNonZeroIndex: Int = i + 1
348+
while (!anyChanges && i >= 0) {
349+
val oldWord = word(i)
350+
currentWord = BitSetOps.computeWordForFilter(pred, isFlipped, oldWord, i)
351+
anyChanges ||= currentWord != oldWord
352+
i -= 1
353+
}
354+
if (anyChanges) {
336355
val newArray = elems.take(minimumNonZeroIndex + 1)
337356
newArray(i + 1) = currentWord
338357
while (i >= 0) {
339358
newArray(i) = BitSetOps.computeWordForFilter(pred, isFlipped, word(i), i)
340359
i -= 1
341360
}
342-
this.fromBitMaskNoCopy(newArray)
361+
new BitSetN(newArray)
362+
} else {
363+
this
343364
}
344-
} else {
345-
this
346-
}
347365
}
348366
}
349367

0 commit comments

Comments
 (0)