Created
February 11, 2017 09:02
-
-
Save matsu-chara/20270a684b3f0b2f77f36c2aae57cd6b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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