Permalink
Browse files

SI-5513: add inplace set-theoretic operations for mutable bitsets.

  • Loading branch information...
1 parent 5483861 commit 38a1515e8e321a93530a7c963ac3c10bdab0456e @vigdorchik vigdorchik committed Mar 11, 2013
Showing with 77 additions and 1 deletion.
  1. +50 −1 src/library/scala/collection/mutable/BitSet.scala
  2. +5 −0 test/files/run/bitsets.check
  3. +22 −0 test/files/run/bitsets.scala
@@ -58,14 +58,18 @@ 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
val elems1 = new Array[Long](newlen)
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)
}
@@ -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)
@@ -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
}

0 comments on commit 38a1515

Please sign in to comment.