Skip to content

Commit

Permalink
implement MurmurHash closes #76
Browse files Browse the repository at this point in the history
  • Loading branch information
mogproject committed Jun 2, 2015
1 parent 18d64e4 commit 22da5e5
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ package com.github.mogproject.redismock.util
class EnhancedByte(x: Byte) {

/** Convert to unsigned integer */
def toUnsignedInt: Int = (x.toInt + 256) % 256
def toUnsignedInt: Int = x & 0xff

def toUnsignedLong: Long = toUnsignedInt.toLong

/** Count the number of 1-bit */
def popCount: Int = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.github.mogproject.redismock.util

import com.github.mogproject.redismock.util.Implicits._


object MurmurHash {

private val defaultSeed = 0xadc83b19L

def murmurHash64A(data: Seq[Byte], seed: Long = defaultSeed): Long = {
val m = 0xc6a4a7935bd1e995L
val r = 47

val f: Long => Long = m.*
val g: Long => Long = x => x ^ (x >>> r)

val h = data.grouped(8).foldLeft(seed ^ f(data.length)) { case (y, xs) =>
val k = xs.foldRight(0L)((b, x) => (x << 8) + b.toUnsignedLong)
val j: Long => Long = if (xs.length == 8) f compose g compose f else identity
f(y ^ j(k))
}
(g compose f compose g)(h)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.github.mogproject.redismock.util

import org.scalatest.{BeforeAndAfterAll, BeforeAndAfterEach, Matchers, FunSpec}


class MurmurHashSpec extends FunSpec with Matchers with BeforeAndAfterEach with BeforeAndAfterAll {
describe("HashUtil#murmurHash64A") {
it("should return 64 bit value") {
MurmurHash.murmurHash64A("A".getBytes) shouldBe 0xfc089b66b14af040L
MurmurHash.murmurHash64A("AB".getBytes) shouldBe 0x24fb508dc42efb7fL
MurmurHash.murmurHash64A("B".getBytes) shouldBe 0x130dd9603dcd32a4L
MurmurHash.murmurHash64A("BA".getBytes) shouldBe 0x25cbc673105b365dL

MurmurHash.murmurHash64A("".getBytes) shouldBe 0xd8dfea6585bc9732L
MurmurHash.murmurHash64A("ABCDEFGHabcdefg".getBytes) shouldBe 0x3dfb8e09231eeb32L
MurmurHash.murmurHash64A("ABCDEFGHabcdefgh".getBytes) shouldBe 0x40055074d92b389fL
MurmurHash.murmurHash64A("ABCDEFGHabcdefghi".getBytes) shouldBe 0xd298ffe6aa77babeL

MurmurHash.murmurHash64A(Seq.fill[Byte](100)(0)) shouldBe 0xc9450ffd5a7bf9ccL
MurmurHash.murmurHash64A(Seq.fill[Byte](100)(-1)) shouldBe 0x78be0c4f11cdc6d5L
}
}
}

0 comments on commit 22da5e5

Please sign in to comment.