Skip to content

Commit

Permalink
SI-7102 Specialize isEmpty for bitsets
Browse files Browse the repository at this point in the history
Currently bitsets use default isEmpty implementation inherited from
Set, which tests for "size == 0".

Calculating the size of a word in a bitmap requires summing through
all bits set, whereas testing for emptyness needs only one comparison
with zero.

This commit overrides the default implementation with the specialized
one looking for a non-zero word in this bitmap.
  • Loading branch information
vigdorchik committed Mar 21, 2013
1 parent 7adab90 commit 1b3a379
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/library/scala/collection/BitSetLike.scala
Expand Up @@ -69,6 +69,8 @@ trait BitSetLike[+This <: BitSetLike[This] with SortedSet[Int]] extends SortedSe
s
}

override def isEmpty: Boolean = 0 until nwords forall (i => word(i) == 0)

implicit def ordering: Ordering[Int] = Ordering.Int

def rangeImpl(from: Option[Int], until: Option[Int]): This = {
Expand Down
13 changes: 13 additions & 0 deletions test/files/run/bitsets.scala
Expand Up @@ -37,6 +37,19 @@ object TestMutable {
Console.println("mi1 = " + ms1.toImmutable)
Console.println("mi2 = " + ms2.toImmutable)
Console.println

val N = 257
val gen = 3
val bs = BitSet((1 until N): _*)
(1 until N).foldLeft(gen) {
case (acc, i) =>
assert(bs.size == N-i, s"Bad size for $bs, expected ${N-i} actual ${bs.size}")
assert(!bs.isEmpty, s"Unexpected isEmpty for $bs")
bs -= acc
acc*gen % N
}
assert(bs.size == 0, s"Expected size == 0 for $bs")
assert(bs.isEmpty, s"Expected isEmpty for $bs")
}

object TestMutable2 {
Expand Down

0 comments on commit 1b3a379

Please sign in to comment.