Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Faulty implementation of method diff in subclass BitSetN of scala.collection.immutable.BitSet #10238

Merged
merged 1 commit into from
Dec 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 62 additions & 44 deletions src/library/scala/collection/immutable/BitSet.scala
Original file line number Diff line number Diff line change
Expand Up @@ -244,36 +244,45 @@ object BitSet extends SpecificIterableFactory[Int, BitSet] {
anyChanges ||= currentWord != oldWord
i -= 1
}
if (i < 0) {
// all indices >= 0 have had result 0, so the bitset is empty
this.empty
} else {
val minimumNonZeroIndex: Int = i + 1
while (!anyChanges && i >= 0) {
val oldWord = word(i)
currentWord = oldWord & ~bs.word(i)
anyChanges ||= currentWord != oldWord
i -= 1
}
if (anyChanges) {
if (minimumNonZeroIndex == -1) {
this.empty
} else if (minimumNonZeroIndex == 0) {
new BitSet1(currentWord)
} else if (minimumNonZeroIndex == 1) {
new BitSet2(word(0) & ~bs.word(0), currentWord)
i match {
case -1 =>
if (anyChanges) {
if (currentWord == 0) {
this.empty
} else {
new BitSet1(currentWord)
}
} else {
this
}
case 0 =>
val oldFirstWord = word(0)
val firstWord = oldFirstWord & ~bs.word(0)
anyChanges ||= firstWord != oldFirstWord
if (anyChanges) {
new BitSet2(firstWord, currentWord)
} else {
this
}
case _ =>
val minimumNonZeroIndex: Int = i + 1
while (!anyChanges && i >= 0) {
val oldWord = word(i)
currentWord = oldWord & ~bs.word(i)
anyChanges ||= currentWord != oldWord
i -= 1
}
if (anyChanges) {
val newArray = elems.take(minimumNonZeroIndex + 1)
newArray(i + 1) = currentWord
while (i >= 0) {
newArray(i) = word(i) & ~bs.word(i)
i -= 1
}
this.fromBitMaskNoCopy(newArray)
new BitSetN(newArray)
} else {
this
}
} else {
this
}
}
} else {
var i = bsnwords - 1
Expand Down Expand Up @@ -314,36 +323,45 @@ object BitSet extends SpecificIterableFactory[Int, BitSet] {
anyChanges ||= currentWord != oldWord
i -= 1
}
if (i < 0) {
// all indices >= 0 have had result 0, so the bitset is empty
if (currentWord == 0) this.empty else this.fromBitMaskNoCopy(Array(currentWord))
} else {
val minimumNonZeroIndex: Int = i + 1
while (!anyChanges && i >= 0) {
val oldWord = word(i)
currentWord = BitSetOps.computeWordForFilter(pred, isFlipped, oldWord, i)
anyChanges ||= currentWord != oldWord
i -= 1
}
if (anyChanges) {
if (minimumNonZeroIndex == -1) {
this.empty
} else if (minimumNonZeroIndex == 0) {
new BitSet1(currentWord)
} else if (minimumNonZeroIndex == 1) {
new BitSet2(BitSetOps.computeWordForFilter(pred, isFlipped, word(0), 0), currentWord)
i match {
case -1 =>
if (anyChanges) {
if (currentWord == 0) {
this.empty
} else {
new BitSet1(currentWord)
}
} else {
this
}
case 0 =>
val oldFirstWord = word(0)
val firstWord = BitSetOps.computeWordForFilter(pred, isFlipped, oldFirstWord, 0)
anyChanges ||= firstWord != oldFirstWord
if (anyChanges) {
new BitSet2(firstWord, currentWord)
} else {
this
}
case _ =>
val minimumNonZeroIndex: Int = i + 1
while (!anyChanges && i >= 0) {
val oldWord = word(i)
currentWord = BitSetOps.computeWordForFilter(pred, isFlipped, oldWord, i)
anyChanges ||= currentWord != oldWord
i -= 1
}
if (anyChanges) {
val newArray = elems.take(minimumNonZeroIndex + 1)
newArray(i + 1) = currentWord
while (i >= 0) {
newArray(i) = BitSetOps.computeWordForFilter(pred, isFlipped, word(i), i)
i -= 1
}
this.fromBitMaskNoCopy(newArray)
new BitSetN(newArray)
} else {
this
}
} else {
this
}
}
}

Expand Down
13 changes: 13 additions & 0 deletions test/junit/scala/collection/immutable/SetTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,17 @@ class SetTest {
testCorrectness()
testNoHashCodeInvocationsDuringSubsetOf()
}

@Test def pr10238(): Unit = {
assertEquals(BitSet(0), BitSet(0, 128) diff BitSet(128))

val us = scala.collection.immutable.BitSet(39, 41, 44, 46, 256)
val vs = scala.collection.immutable.BitSet(39, 41, 44, 46, 64, 256)
val xs = scala.collection.immutable.BitSet.fromBitMask(us.toBitMask.take(3))
val ys = scala.collection.immutable.BitSet.fromBitMask(vs.toBitMask.take(3))
val diff = ys diff xs
val expected = scala.collection.immutable.BitSet(64)
assertEquals(diff, expected)
}

}