diff options
-rw-r--r-- | src/library/scala/collection/mutable/HashTable.scala | 21 | ||||
-rw-r--r-- | test/files/run/t2524.scala | 10 |
2 files changed, 28 insertions, 3 deletions
diff --git a/src/library/scala/collection/mutable/HashTable.scala b/src/library/scala/collection/mutable/HashTable.scala index 6726a3dbbc..9d3a35376c 100644 --- a/src/library/scala/collection/mutable/HashTable.scala +++ b/src/library/scala/collection/mutable/HashTable.scala @@ -45,12 +45,27 @@ trait HashTable[A] { /** The initial threshold */ - protected def initialThreshold: Int = newThreshold(initialSize) + protected def initialThreshold: Int = newThreshold(initialCapacity) /** The actual hash table. */ - protected var table: Array[HashEntry[A, Entry]] = - if (initialSize == 0) null else new Array(initialSize) + protected var table: Array[HashEntry[A, Entry]] = new Array(initialCapacity) + + private def initialCapacity = if (initialSize == 0) 1 else powerOfTwo(initialSize) + + /** + * Returns a power of two >= `target`. + */ + private def powerOfTwo(target: Int): Int = { + /* See http://bits.stephan-brumme.com/roundUpToNextPowerOfTwo.html */ + var c = target - 1; + c |= c >>> 1; + c |= c >>> 2; + c |= c >>> 4; + c |= c >>> 8; + c |= c >>> 16; + c + 1; + } /** The number of mappings contained in this hash table. */ diff --git a/test/files/run/t2524.scala b/test/files/run/t2524.scala new file mode 100644 index 0000000000..552663e9e8 --- /dev/null +++ b/test/files/run/t2524.scala @@ -0,0 +1,10 @@ +object Test { + def main(args: Array[String]) { + val m = new collection.mutable.HashMap[String, String] { + override def initialSize = 0 + } + m.toString + m("key") = "value" + assert(m("key") == "value") + } +} |