Skip to content

Commit

Permalink
Fixes SI-5804.
Browse files Browse the repository at this point in the history
The hash table initialSize method is now within
the the hashset and hashmap classes, and not in the companion.
Overriding this method now yields hashmaps and hashsets
of the proper initial capacity.

Review by @phaller.
  • Loading branch information
Aleksandar Prokopec committed May 18, 2012
1 parent 34fa132 commit f51dbd5
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 15 deletions.
8 changes: 4 additions & 4 deletions src/library/scala/collection/mutable/FlatHashTable.scala
Expand Up @@ -50,6 +50,10 @@ trait FlatHashTable[A] extends FlatHashTable.HashUtils[A] {

protected def capacity(expectedSize: Int) = if (expectedSize == 0) 1 else powerOfTwo(expectedSize)

/** The initial size of the hash table.
*/
def initialSize: Int = 32

private def initialCapacity = capacity(initialSize)

protected def randomSeed = seedGenerator.get.nextInt()
Expand Down Expand Up @@ -361,10 +365,6 @@ private[collection] object FlatHashTable {
def defaultLoadFactor: Int = 450
final def loadFactorDenum = 1000

/** The initial size of the hash table.
*/
def initialSize: Int = 32

def sizeForThreshold(size: Int, _loadFactor: Int) = math.max(32, (size.toLong * loadFactorDenum / _loadFactor).toInt)

def newThreshold(_loadFactor: Int, size: Int) = {
Expand Down
20 changes: 9 additions & 11 deletions src/library/scala/collection/mutable/HashTable.scala
Expand Up @@ -56,7 +56,15 @@ trait HashTable[A, Entry >: Null <: HashEntry[A, Entry]] extends HashTable.HashU

protected def tableSizeSeed = Integer.bitCount(table.length - 1)

protected def initialSize: Int = HashTable.initialSize
/** The initial size of the hash table.
*/
protected def initialSize: Int = 16

/** The initial threshold.
*/
private def initialThreshold(_loadFactor: Int): Int = newThreshold(_loadFactor, initialCapacity)

private def initialCapacity = capacity(initialSize)

private def lastPopulatedIndex = {
var idx = table.length - 1
Expand Down Expand Up @@ -354,16 +362,6 @@ private[collection] object HashTable {
private[collection] final def defaultLoadFactor: Int = 750 // corresponds to 75%
private[collection] final def loadFactorDenum = 1000;

/** The initial size of the hash table.
*/
private[collection] final def initialSize: Int = 16

/** The initial threshold.
*/
private[collection] final def initialThreshold(_loadFactor: Int): Int = newThreshold(_loadFactor, initialCapacity)

private[collection] final def initialCapacity = capacity(initialSize)

private[collection] final def newThreshold(_loadFactor: Int, size: Int) = ((size.toLong * _loadFactor) / loadFactorDenum).toInt

private[collection] final def sizeForThreshold(_loadFactor: Int, thr: Int) = ((thr.toLong * loadFactorDenum) / _loadFactor).toInt
Expand Down
4 changes: 4 additions & 0 deletions test/files/run/t5804.check
@@ -0,0 +1,4 @@
128
16
128
32
32 changes: 32 additions & 0 deletions test/files/run/t5804.scala
@@ -0,0 +1,32 @@


import collection.mutable._


object Test {

def main(args: Array[String]) {
class CustomHashMap extends HashMap[Int, Int] {
override def initialSize = 65

println(table.length)
}

new CustomHashMap
new HashMap {
println(table.length)
}

class CustomHashSet extends HashSet[Int] {
override def initialSize = 96

println(table.length)
}

new CustomHashSet
new HashSet {
println(table.length)
}
}

}

0 comments on commit f51dbd5

Please sign in to comment.