Permalink
Browse files

Fix `Hashing`.

Move `Hashing` to `scala.util.hashing`.
Adde `Hashing.Default` to `Hashing` companion object.
Change `TrieMap` to autoswitch from `Hashing.Default` to `TrieMap.MangledHashing`.
  • Loading branch information...
axel22 committed Jun 1, 2012
1 parent bb30e08 commit ddaf6c5235acbf808ffd5ded710f513f4df3882e
@@ -14,7 +14,7 @@ package concurrent
import java.util.concurrent.atomic._
import collection.immutable.{ ListMap => ImmutableListMap }
import collection.parallel.mutable.ParTrieMap
import math.Hashing
import util.hashing.Hashing
import generic._
import annotation.tailrec
import annotation.switch
@@ -637,7 +637,7 @@ extends scala.collection.concurrent.Map[K, V]
with CustomParallelizable[(K, V), ParTrieMap[K, V]]
with Serializable
{
private var hashingobj = hashf
private var hashingobj = if (hashf.isInstanceOf[Hashing.Default[_]]) new TrieMap.MangledHashing[K] else hashf
private var equalityobj = ef
private var rootupdater = rtupd
def hashing = hashingobj
@@ -824,14 +824,8 @@ extends scala.collection.concurrent.Map[K, V]
if (!RDCSS_ROOT(r, r.gcasRead(this), INode.newRootNode[K, V])) clear()
}
@inline private def mangle(hc: Int): Int = {
var hcode = hc * 0x9e3775cd
hcode = java.lang.Integer.reverseBytes(hcode)
hcode * 0x9e3775cd
}
@inline
def computeHash(k: K) = mangle(hashingobj.hashCode(k))
def computeHash(k: K) = hashingobj.hashCode(k)
final def lookup(k: K): V = {
val hc = computeHash(k)
@@ -919,7 +913,15 @@ object TrieMap extends MutableMapFactory[TrieMap] {
implicit def canBuildFrom[K, V]: CanBuildFrom[Coll, (K, V), TrieMap[K, V]] = new MapCanBuildFrom[K, V]
def empty[K, V]: TrieMap[K, V] = new TrieMap[K, V]
class MangledHashing[K] extends Hashing[K] {
def hashCode(k: K) = {
var hcode = k.## * 0x9e3775cd
hcode = java.lang.Integer.reverseBytes(hcode)
hcode * 0x9e3775cd
}
}
}
@@ -1,3 +1,11 @@
/* __ *\
** ________ ___ / / ___ Scala API **
** / __/ __// _ | / / / _ | (c) 2003-2012, LAMP/EPFL **
** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
** /____/\___/_/ |_/____/_/ | | **
** |/ **
\* */
package scala.util
import java.lang.Integer.{ rotateLeft => rotl }
@@ -6,7 +6,7 @@
** |/ **
\* */
package scala.math
package scala.util.hashing
/** `Hashing` is a trait whose instances each represent a strategy for hashing
* instances of a type.
@@ -29,10 +29,12 @@ trait Hashing[T] extends Serializable {
object Hashing {
implicit def default[T] = new Hashing[T] {
final class Default[T] extends Hashing[T] {
def hashCode(x: T) = x.##
}
implicit def default[T] = new Default[T]
def fromFunction[T](f: T => Int) = new Hashing[T] {
def hashCode(x: T) = f(x)
}
@@ -1,7 +1,7 @@
import math.Hashing
import util.hashing.Hashing

0 comments on commit ddaf6c5

Please sign in to comment.