Skip to content

Instantly share code, notes, and snippets.

@jboner
Created July 22, 2008 13:29
Show Gist options
  • Save jboner/939 to your computer and use it in GitHub Desktop.
Save jboner/939 to your computer and use it in GitHub Desktop.
Scala HashCode builder
val SEED = 23
def hash(seed: Int, value: Boolean): Int = firstTerm(seed) + (if (value) 1 else 0)
def hash(seed: Int, value: Char): Int = firstTerm(seed) + value.asInstanceOf[Int]
def hash(seed: Int, value: Int): Int = firstTerm(seed) + value
def hash(seed: Int, value: Long): Int = firstTerm(seed) + (value ^ (value >>> 32) ).asInstanceOf[Int]
def hash(seed: Int, value: Float): Int = hash(seed, JFloat.floatToIntBits(value))
def hash(seed: Int, value: Double): Int = hash(seed, JDouble.doubleToLongBits(value))
def hash(seed: Int, anyRef: AnyRef): Int = {
var result = seed
if (anyRef == null) result = hash(result, 0)
else if (!isArray(anyRef)) result = hash(result, anyRef.hashCode())
else { // is an array
for (id <- 0 until JArray.getLength(anyRef)) {
val item = JArray.get(anyRef, id)
result = hash(result, item)
}
}
result
}
private def firstTerm(seed: Int): Int = PRIME * seed
private def isArray(anyRef: AnyRef): Boolean = anyRef.getClass.isArray
private val PRIME = 37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment