In Scala 2.10, MurmurHash for some reason is deprecated, saying I should use MurmurHash3 now. But the API is different, and there is no useful scaladocs for MurmurHash3 -> fail.
For instance, current code:
trait Foo {
type Bar
def id: Int
def path: Bar
override def hashCode = {
import util.MurmurHash._
var h = startHash(2)
val c = startMagicA
val k = startMagicB
h = extendHash(h, id, c, k)
h = extendHash(h, path.##, nextMagicA(c), nextMagicB(k))
finalizeHash(h)
}
}
How would I do this using MurmurHash3 instead? This needs to be a fast operation, preferably without allocations, so I do not want to construct a Product, Seq, Array[Byte] or whathever MurmurHash3 seems to be offering me.
The MurmurHash3 algorithm was changed, confusingly, from an algorithm that mixed in its own salt, essentially (
candk), to one that just does more bit-mixing. The basic operation is nowmix, which you should fold over all your values, after which you shouldfinalizeHash(theIntargument for length is for convenience also, to help with distinguishing collections of different length). If you want to replace your lastmixbymixLast, it’s a little faster and removes redundancy withfinalizeHash. If it takes you too long to detect what the last mix is, justmix.Typically for a collection you’ll want to mix in an extra value to indicate what type of collection it is.
So minimally you’d have
and “typically” you’d
Note that the length field is NOT there to give a high-quality hash that mixes in that number. All mixing of important hash values should be done with
mix.