diff options
author | Stefan Zeiger <szeiger@novocode.com> | 2011-12-02 12:45:40 +0100 |
---|---|---|
committer | Stefan Zeiger <szeiger@novocode.com> | 2011-12-02 12:45:40 +0100 |
commit | 989c0d0693e27d06d1f70524b66527d1ef12f5a2 (patch) | |
tree | 11147703e4314062ea3565d0decfbf7e2afeae65 /src | |
parent | 947797ea23d711e501605c0cc218fec88e3b97ef (diff) | |
download | scala-989c0d0693e27d06d1f70524b66527d1ef12f5a2.tar.gz scala-989c0d0693e27d06d1f70524b66527d1ef12f5a2.tar.bz2 scala-989c0d0693e27d06d1f70524b66527d1ef12f5a2.zip |
Improve performance of BitSet.size
Using java.lang.Long.bitCount for the size computation is a lot faster
than the previous Scala implementation. Closes SI-2196.
Diffstat (limited to 'src')
-rw-r--r-- | src/library/scala/collection/BitSetLike.scala | 13 |
1 files changed, 1 insertions, 12 deletions
diff --git a/src/library/scala/collection/BitSetLike.scala b/src/library/scala/collection/BitSetLike.scala index 7d9f48f299..e4f9fd436a 100644 --- a/src/library/scala/collection/BitSetLike.scala +++ b/src/library/scala/collection/BitSetLike.scala @@ -65,7 +65,7 @@ trait BitSetLike[+This <: BitSetLike[This] with SortedSet[Int]] extends SortedSe var i = nwords while (i > 0) { i -= 1 - s += popCount(word(i)) + s += java.lang.Long.bitCount(word(i)) } s } @@ -221,15 +221,4 @@ object BitSetLike { else assert(w == 0L) newelems } - - private val pc1: Array[Int] = { - def countBits(x: Int): Int = if (x == 0) 0 else x % 2 + countBits(x >>> 1) - Array.tabulate(256)(countBits _) - } - - private def popCount(w: Long): Int = { - def pc2(w: Int) = if (w == 0) 0 else pc1(w & 0xff) + pc1(w >>> 8) - def pc4(w: Int) = if (w == 0) 0 else pc2(w & 0xffff) + pc2(w >>> 16) - if (w == 0L) 0 else pc4(w.toInt) + pc4((w >>> 32).toInt) - } } |