Skip to content

Commit 426cec5

Browse files
committed
Merge pull request scala#2287 from vigdorchik/ticket/si-7102
SI-7102 Override isEmpty for bitsets with an efficient implementation
2 parents d816bc5 + 1b3a379 commit 426cec5

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/library/scala/collection/BitSetLike.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ trait BitSetLike[+This <: BitSetLike[This] with SortedSet[Int]] extends SortedSe
6969
s
7070
}
7171

72+
override def isEmpty: Boolean = 0 until nwords forall (i => word(i) == 0)
73+
7274
implicit def ordering: Ordering[Int] = Ordering.Int
7375

7476
def rangeImpl(from: Option[Int], until: Option[Int]): This = {

test/files/run/bitsets.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,19 @@ object TestMutable {
3737
Console.println("mi1 = " + ms1.toImmutable)
3838
Console.println("mi2 = " + ms2.toImmutable)
3939
Console.println
40+
41+
val N = 257
42+
val gen = 3
43+
val bs = BitSet((1 until N): _*)
44+
(1 until N).foldLeft(gen) {
45+
case (acc, i) =>
46+
assert(bs.size == N-i, s"Bad size for $bs, expected ${N-i} actual ${bs.size}")
47+
assert(!bs.isEmpty, s"Unexpected isEmpty for $bs")
48+
bs -= acc
49+
acc*gen % N
50+
}
51+
assert(bs.size == 0, s"Expected size == 0 for $bs")
52+
assert(bs.isEmpty, s"Expected isEmpty for $bs")
4053
}
4154

4255
object TestMutable2 {

0 commit comments

Comments
 (0)