summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/library/scala/collection/mutable/BitSet.scala51
-rw-r--r--test/files/run/bitsets.check5
-rw-r--r--test/files/run/bitsets.scala22
3 files changed, 77 insertions, 1 deletions
diff --git a/src/library/scala/collection/mutable/BitSet.scala b/src/library/scala/collection/mutable/BitSet.scala
index 2a535a799c..397f8099eb 100644
--- a/src/library/scala/collection/mutable/BitSet.scala
+++ b/src/library/scala/collection/mutable/BitSet.scala
@@ -58,6 +58,11 @@ class BitSet(protected var elems: Array[Long]) extends AbstractSet[Int]
if (idx < nwords) elems(idx) else 0L
private def updateWord(idx: Int, w: Long) {
+ ensureCapacity(idx)
+ elems(idx) = w
+ }
+
+ private def ensureCapacity(idx: Int) {
if (idx >= nwords) {
var newlen = nwords
while (idx >= newlen) newlen = newlen * 2
@@ -65,7 +70,6 @@ class BitSet(protected var elems: Array[Long]) extends AbstractSet[Int]
Array.copy(elems, 0, elems1, 0, nwords)
elems = elems1
}
- elems(idx) = w
}
protected def fromBitMaskNoCopy(words: Array[Long]): BitSet = new BitSet(words)
@@ -92,6 +96,51 @@ class BitSet(protected var elems: Array[Long]) extends AbstractSet[Int]
def += (elem: Int): this.type = { add(elem); this }
def -= (elem: Int): this.type = { remove(elem); this }
+ /** Updates this bitset to the union with another bitset by performing a bitwise "or".
+ *
+ * @param other the bitset to form the union with.
+ * @return the bitset itself.
+ */
+ def |= (other: BitSet): this.type = {
+ ensureCapacity(other.nwords)
+ for (i <- 0 until other.nwords)
+ elems(i) = elems(i) | other.word(i)
+ this
+ }
+ /** Updates this bitset to the intersection with another bitset by performing a bitwise "and".
+ *
+ * @param other the bitset to form the intersection with.
+ * @return the bitset itself.
+ */
+ def &= (other: BitSet): this.type = {
+ ensureCapacity(other.nwords)
+ for (i <- 0 until other.nwords)
+ elems(i) = elems(i) & other.word(i)
+ this
+ }
+ /** Updates this bitset to the symmetric difference with another bitset by performing a bitwise "xor".
+ *
+ * @param other the bitset to form the symmetric difference with.
+ * @return the bitset itself.
+ */
+ def ^= (other: BitSet): this.type = {
+ ensureCapacity(other.nwords)
+ for (i <- 0 until other.nwords)
+ elems(i) = elems(i) ^ other.word(i)
+ this
+ }
+ /** Updates this bitset to the difference with another bitset by performing a bitwise "and-not".
+ *
+ * @param other the bitset to form the difference with.
+ * @return the bitset itself.
+ */
+ def &~= (other: BitSet): this.type = {
+ ensureCapacity(other.nwords)
+ for (i <- 0 until other.nwords)
+ elems(i) = elems(i) & ~other.word(i)
+ this
+ }
+
override def clear() {
elems = new Array[Long](elems.length)
}
diff --git a/test/files/run/bitsets.check b/test/files/run/bitsets.check
index 3f01d2a400..41c2ccdcb8 100644
--- a/test/files/run/bitsets.check
+++ b/test/files/run/bitsets.check
@@ -37,6 +37,11 @@ m2_r1 = true
m2_r2 = true
m2_r3 = true
+b1:BitSet(5, 6, 7)
+b2:BitSet(5)
+b3:BitSet(5, 7)
+b4:BitSet(7)
+b0:BitSet(5, 6, 7)
is0 = BitSet()
is1 = BitSet()
is2 = BitSet(2)
diff --git a/test/files/run/bitsets.scala b/test/files/run/bitsets.scala
index bdeb1fd811..0ea43fcb95 100644
--- a/test/files/run/bitsets.scala
+++ b/test/files/run/bitsets.scala
@@ -81,6 +81,27 @@ object TestMutable2 {
println
}
+object TestMutable3 {
+ import scala.collection.mutable.BitSet
+
+ val b0 = BitSet(5, 6)
+ val b1 = BitSet(7)
+ val b2 = BitSet(1, 5)
+ val b3 = BitSet(6, 7)
+ val b4 = BitSet(6, 7)
+
+ b1 |= b0
+ println(s"b1:$b1")
+ b2 &= b0
+ println(s"b2:$b2")
+ b3 ^= b0
+ println(s"b3:$b3")
+ b4 &~= b0
+ println(s"b4:$b4")
+ b0 ^= b0 |= b1
+ println(s"b0:$b0")
+}
+
object TestImmutable {
import scala.collection.immutable.BitSet
@@ -155,6 +176,7 @@ object TestImmutable2 {
object Test extends App {
TestMutable
TestMutable2
+ TestMutable3
TestImmutable
TestImmutable2
}