8000 Optimized HashTable.nextPositivePowerOfTwo · scala/scala@635b8af · GitHub
[go: up one dir, main page]

Skip to content

Commit 635b8af

Browse files
committed
Optimized HashTable.nextPositivePowerOfTwo
1 parent db659c5 commit 635b8af

File tree

3 files changed

+4
-17
lines changed

3 files changed

+4
-17
lines changed

src/library/scala/collection/mutable/FlatHashTable.scala

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,7 @@ trait FlatHashTable[A] extends FlatHashTable.HashUtils[A] {
4747

4848
@transient protected var seedvalue: Int = tableSizeSeed
4949

50-
import HashTable.powerOfTwo
51-
52-
protected def capacity(expectedSize: Int) = if (expectedSize == 0) 1 else powerOfTwo(expectedSize)
50+
protected def capacity(expectedSize: Int) = HashTable.nextPositivePowerOfTwo(expectedSize)
5351

5452
/** The initial size of the hash table.
5553
*/

src/library/scala/collection/mutable/HashTable.scala

Lines changed: 2 additions & 11 deletions
10000
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ private[collection] object HashTable {
401401

402402
private[collection] final def sizeForThreshold(_loadFactor: Int, thr: Int) = ((thr.toLong * loadFactorDenum) / _loadFactor).toInt
403403

404-
private[collection] final def capacity(expectedSize: Int) = if (expectedSize == 0) 1 else powerOfTwo(expectedSize)
404+
private[collection] final def capacity(expectedSize: Int) = nextPositivePowerOfTwo(expectedSize)
405405

406406
trait HashUtils[KeyType] {
407407
protected final def sizeMapBucketBitSize = 5
@@ -429,16 +429,7 @@ private[collection] object HashTable {
429429
/**
430430
* Returns a power of two >= `target`.
431431
*/
432-
private[collection] def powerOfTwo(target: Int): Int = {
433-
/* See http://bits.stephan-brumme.com/roundUpToNextPowerOfTwo.html */
434-
var c = target - 1
435-
c |= c >>> 1
436-
c |= c >>> 2
437-
c |= c >>> 4
438-
c |= c >>> 8
439-
c |= c >>> 16
440-
c + 1
441-
}
432+
private[collection] def nextPositivePowerOfTwo(target : Int): Int = 1 << -numberOfLeadingZeros(target - 1)
442433

443434
class Contents[A, Entry >: Null <: HashEntry[A, Entry]](
444435
val loadFactor: Int,

src/library/scala/collection/mutable/OpenHashMap.scala

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ object OpenHashMap {
3131
final private class OpenEntry[Key, Value](var key: Key,
3232
var hash: Int,
3333
var value: Option[Value])
34-
35-
private[mutable] def nextPositivePowerOfTwo(i : Int) = 1 << (32 - Integer.numberOfLeadingZeros(i - 1))
3634
}
3735

3836
/** A mutable hash map based on an open hashing scheme. The precise scheme is
@@ -67,7 +65,7 @@ extends AbstractMap[Key, Value]
6765

6866
override def empty: OpenHashMap[Key, Value] = OpenHashMap.empty[Key, Value]
6967

70-
private[this] val actualInitialSize = OpenHashMap.nextPositivePowerOfTwo(initialSize)
68+
private[this] val actualInitialSize = HashTable.nextPositivePowerOfTwo(initialSize)
7169

7270
private var mask = actualInitialSize - 1
7371

0 commit comments

Comments
 (0)
0