Skip to content

Instantly share code, notes, and snippets.

@matsu-chara
Created February 11, 2017 09:02
Show Gist options
  • Save matsu-chara/20270a684b3f0b2f77f36c2aae57cd6b to your computer and use it in GitHub Desktop.
Save matsu-chara/20270a684b3f0b2f77f36c2aae57cd6b to your computer and use it in GitHub Desktop.
import java.nio.ByteBuffer
import java.security.MessageDigest
def md5(s: String) = {
MessageDigest.getInstance("MD5").digest(s.getBytes)
}
def hashes(e: String): Seq[Long] = {
val h1 = ByteBuffer.wrap(md5(e).splitAt(8)._1).getLong
val h2 = ByteBuffer.wrap(md5(e).splitAt(8)._1).getLong
(1 to NUM_HASH).map { i => h1 + i * h2 }
}
val NUM_HASH = 10
val SIZE = 64
val filter = ArraySeq.fill(SIZE)(false)
def put(e: String): Unit = {
hashes(e).foreach { h =>
filter(h % SIE) = true
}
}
def has(e: String): Boolean = {
hashes(e).foldLeft(true) { case (acc, h) =>
acc && filters(h)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment