Guest User

Untitled

a guest
Feb 19th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. val SEED = 23
  2.  
  3. def hash(seed: Int, value: Boolean): Int = firstTerm(seed) + (if (value) 1 else 0)
  4. def hash(seed: Int, value: Char): Int = firstTerm(seed) + value.asInstanceOf[Int]
  5. def hash(seed: Int, value: Int): Int = firstTerm(seed) + value
  6. def hash(seed: Int, value: Long): Int = firstTerm(seed) + (value ^ (value >>> 32) ).asInstanceOf[Int]
  7. def hash(seed: Int, value: Float): Int = hash(seed, JFloat.floatToIntBits(value))
  8. def hash(seed: Int, value: Double): Int = hash(seed, JDouble.doubleToLongBits(value))
  9. def hash(seed: Int, anyRef: AnyRef): Int = {
  10. var result = seed
  11. if (anyRef == null) result = hash(result, 0)
  12. else if (!isArray(anyRef)) result = hash(result, anyRef.hashCode())
  13. else { // is an array
  14. for (id <- 0 until JArray.getLength(anyRef)) {
  15. val item = JArray.get(anyRef, id)
  16. result = hash(result, item)
  17. }
  18. }
  19. result
  20. }
  21. private def firstTerm(seed: Int): Int = PRIME * seed
  22. private def isArray(anyRef: AnyRef): Boolean = anyRef.getClass.isArray
  23. private val PRIME = 37
Add Comment
Please, Sign In to add comment