From ddaf6c5235acbf808ffd5ded710f513f4df3882e Mon Sep 17 00:00:00 2001 From: Aleksandar Prokopec Date: Fri, 1 Jun 2012 21:06:36 +0200 Subject: 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`. --- .../scala/collection/concurrent/TrieMap.scala | 22 ++++++------ src/library/scala/math/Hashing.scala | 40 --------------------- src/library/scala/util/MurmurHash3.scala | 8 +++++ src/library/scala/util/hashing/Hashing.scala | 42 ++++++++++++++++++++++ 4 files changed, 62 insertions(+), 50 deletions(-) delete mode 100644 src/library/scala/math/Hashing.scala create mode 100644 src/library/scala/util/hashing/Hashing.scala (limited to 'src') diff --git a/src/library/scala/collection/concurrent/TrieMap.scala b/src/library/scala/collection/concurrent/TrieMap.scala index 7c786619fb..f944d20bdb 100644 --- a/src/library/scala/collection/concurrent/TrieMap.scala +++ b/src/library/scala/collection/concurrent/TrieMap.scala @@ -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 + } + } + } diff --git a/src/library/scala/math/Hashing.scala b/src/library/scala/math/Hashing.scala deleted file mode 100644 index 0d0fecf442..0000000000 --- a/src/library/scala/math/Hashing.scala +++ /dev/null @@ -1,40 +0,0 @@ -/* __ *\ -** ________ ___ / / ___ Scala API ** -** / __/ __// _ | / / / _ | (c) 2003-2011, LAMP/EPFL ** -** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** -** /____/\___/_/ |_/____/_/ | | ** -** |/ ** -\* */ - -package scala.math - -/** `Hashing` is a trait whose instances each represent a strategy for hashing - * instances of a type. - * - * `Hashing`'s companion object defines a default hashing strategy for all - * objects - it calls their `##` method. - * - * Note: when using a custom `Hashing`, make sure to use it with the `Equiv` - * such that if any two objects are equal, then their hash codes must be equal. - * - * @since 2.10 - */ -@annotation.implicitNotFound(msg = "No implicit Hashing defined for ${T}.") -trait Hashing[T] extends Serializable { - - def hashCode(x: T): Int - -} - - -object Hashing { - - implicit def default[T] = new Hashing[T] { - def hashCode(x: T) = x.## - } - - def fromFunction[T](f: T => Int) = new Hashing[T] { - def hashCode(x: T) = f(x) - } - -} diff --git a/src/library/scala/util/MurmurHash3.scala b/src/library/scala/util/MurmurHash3.scala index fbb67116dd..9a7f64b4ee 100644 --- a/src/library/scala/util/MurmurHash3.scala +++ b/src/library/scala/util/MurmurHash3.scala @@ -1,3 +1,11 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003-2012, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + package scala.util import java.lang.Integer.{ rotateLeft => rotl } diff --git a/src/library/scala/util/hashing/Hashing.scala b/src/library/scala/util/hashing/Hashing.scala new file mode 100644 index 0000000000..f27a825125 --- /dev/null +++ b/src/library/scala/util/hashing/Hashing.scala @@ -0,0 +1,42 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003-2011, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +package scala.util.hashing + +/** `Hashing` is a trait whose instances each represent a strategy for hashing + * instances of a type. + * + * `Hashing`'s companion object defines a default hashing strategy for all + * objects - it calls their `##` method. + * + * Note: when using a custom `Hashing`, make sure to use it with the `Equiv` + * such that if any two objects are equal, then their hash codes must be equal. + * + * @since 2.10 + */ +@annotation.implicitNotFound(msg = "No implicit Hashing defined for ${T}.") +trait Hashing[T] extends Serializable { + + def hashCode(x: T): Int + +} + + +object Hashing { + + 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) + } + +} -- cgit v1.2.3