summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/library/scala/collection/BitSetLike.scala16
-rw-r--r--src/library/scala/collection/mutable/BitSet.scala5
-rw-r--r--test/files/run/bitsets.check4
-rw-r--r--test/files/run/bitsets.scala14
4 files changed, 33 insertions, 6 deletions
diff --git a/src/library/scala/collection/BitSetLike.scala b/src/library/scala/collection/BitSetLike.scala
index 034ed2b24a..9c4e710558 100644
--- a/src/library/scala/collection/BitSetLike.scala
+++ b/src/library/scala/collection/BitSetLike.scala
@@ -106,8 +106,8 @@ trait BitSetLike[+This <: BitSetLike[This] with SortedSet[Int]] extends SortedSe
private var current = start
private val end = nwords * WordLength
def hasNext: Boolean = {
- while (current < end && !self.contains(current)) current += 1
- current < end
+ while (current != end && !self.contains(current)) current += 1
+ current != end
}
def next(): Int =
if (hasNext) { val r = current; current += 1; r }
@@ -117,7 +117,10 @@ trait BitSetLike[+This <: BitSetLike[This] with SortedSet[Int]] extends SortedSe
override def foreach[B](f: Int => B) {
for (i <- 0 until nwords) {
val w = word(i)
- for (j <- i * WordLength until (i + 1) * WordLength) {
+ /* NOTE: `until` instead of `to` will not work here because
+ the maximum value of `(i + 1) * WordLength` could be
+ `Int.MaxValue + 1` (i.e. `Int.MinValue`). */
+ for (j <- i * WordLength to (i + 1) * WordLength - 1) {
if ((w & (1L << j)) != 0L) f(j)
}
}
@@ -197,11 +200,15 @@ trait BitSetLike[+This <: BitSetLike[This] with SortedSet[Int]] extends SortedSe
override def addString(sb: StringBuilder, start: String, sep: String, end: String) = {
sb append start
var pre = ""
- for (i <- 0 until nwords * WordLength)
+ val max = nwords * WordLength
+ var i = 0
+ while(i != max) {
if (contains(i)) {
sb append pre append i
pre = sep
}
+ i += 1
+ }
sb append end
}
@@ -212,6 +219,7 @@ trait BitSetLike[+This <: BitSetLike[This] with SortedSet[Int]] extends SortedSe
object BitSetLike {
private[collection] val LogWL = 6
private val WordLength = 64
+ private[collection] val MaxSize = (Int.MaxValue >> LogWL) + 1
private[collection] def updateArray(elems: Array[Long], idx: Int, w: Long): Array[Long] = {
var len = elems.length
diff --git a/src/library/scala/collection/mutable/BitSet.scala b/src/library/scala/collection/mutable/BitSet.scala
index 397f8099eb..29c341590d 100644
--- a/src/library/scala/collection/mutable/BitSet.scala
+++ b/src/library/scala/collection/mutable/BitSet.scala
@@ -12,7 +12,7 @@ package scala.collection
package mutable
import generic._
-import BitSetLike.{LogWL, updateArray}
+import BitSetLike.{LogWL, MaxSize, updateArray}
/** A class for mutable bitsets.
*
@@ -63,9 +63,10 @@ class BitSet(protected var elems: Array[Long]) extends AbstractSet[Int]
}
private def ensureCapacity(idx: Int) {
+ require(idx < MaxSize)
if (idx >= nwords) {
var newlen = nwords
- while (idx >= newlen) newlen = newlen * 2
+ while (idx >= newlen) newlen = (newlen * 2) min MaxSize
val elems1 = new Array[Long](newlen)
Array.copy(elems, 0, elems1, 0, nwords)
elems = elems1
diff --git a/test/files/run/bitsets.check b/test/files/run/bitsets.check
index 41c2ccdcb8..9bbc769b72 100644
--- a/test/files/run/bitsets.check
+++ b/test/files/run/bitsets.check
@@ -42,6 +42,10 @@ b2:BitSet(5)
b3:BitSet(5, 7)
b4:BitSet(7)
b0:BitSet(5, 6, 7)
+bMax:BitSet(2147483647)
+2147483647
+bLarge:BitSet(2000000001)
+false
is0 = BitSet()
is1 = BitSet()
is2 = BitSet(2)
diff --git a/test/files/run/bitsets.scala b/test/files/run/bitsets.scala
index d55f9e4e83..c88782cab7 100644
--- a/test/files/run/bitsets.scala
+++ b/test/files/run/bitsets.scala
@@ -115,6 +115,19 @@ object TestMutable3 {
println(s"b0:$b0")
}
+object TestMutable4 {
+ import scala.collection.mutable.BitSet
+
+ val bMax = BitSet(Int.MaxValue)
+ println(s"bMax:$bMax")
+ bMax.foreach(println)
+
+ val bLarge = BitSet(2000000001)
+ println(s"bLarge:$bLarge")
+
+ println(bMax == bLarge)
+}
+
object TestImmutable {
import scala.collection.immutable.BitSet
@@ -190,6 +203,7 @@ object Test extends App {
TestMutable
TestMutable2
TestMutable3
+ TestMutable4
TestImmutable
TestImmutable2
}