2024-04-06 02:30:11 +08:00
|
|
|
/**
|
|
|
|
* File: simple_hash.kt
|
|
|
|
* Created Time: 2024-01-25
|
|
|
|
* Author: curtishd (1023632660@qq.com)
|
|
|
|
*/
|
|
|
|
|
|
|
|
package chapter_hashing
|
|
|
|
|
|
|
|
/* 加法雜湊 */
|
|
|
|
fun addHash(key: String): Int {
|
|
|
|
var hash = 0L
|
2024-04-14 20:46:20 +08:00
|
|
|
val MODULUS = 1000000007
|
2024-04-06 02:30:11 +08:00
|
|
|
for (c in key.toCharArray()) {
|
|
|
|
hash = (hash + c.code) % MODULUS
|
|
|
|
}
|
|
|
|
return hash.toInt()
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 乘法雜湊 */
|
|
|
|
fun mulHash(key: String): Int {
|
|
|
|
var hash = 0L
|
2024-04-14 20:46:20 +08:00
|
|
|
val MODULUS = 1000000007
|
2024-04-06 02:30:11 +08:00
|
|
|
for (c in key.toCharArray()) {
|
|
|
|
hash = (31 * hash + c.code) % MODULUS
|
|
|
|
}
|
|
|
|
return hash.toInt()
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 互斥或雜湊 */
|
|
|
|
fun xorHash(key: String): Int {
|
|
|
|
var hash = 0
|
2024-04-14 20:46:20 +08:00
|
|
|
val MODULUS = 1000000007
|
2024-04-06 02:30:11 +08:00
|
|
|
for (c in key.toCharArray()) {
|
|
|
|
hash = hash xor c.code
|
|
|
|
}
|
|
|
|
return hash and MODULUS
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 旋轉雜湊 */
|
|
|
|
fun rotHash(key: String): Int {
|
|
|
|
var hash = 0L
|
2024-04-14 20:46:20 +08:00
|
|
|
val MODULUS = 1000000007
|
2024-04-06 02:30:11 +08:00
|
|
|
for (c in key.toCharArray()) {
|
|
|
|
hash = ((hash shl 4) xor (hash shr 28) xor c.code.toLong()) % MODULUS
|
|
|
|
}
|
|
|
|
return hash.toInt()
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Driver Code */
|
|
|
|
fun main() {
|
|
|
|
val key = "Hello 演算法"
|
|
|
|
|
2024-04-11 20:18:19 +08:00
|
|
|
var hash = addHash(key)
|
2024-04-06 02:30:11 +08:00
|
|
|
println("加法雜湊值為 $hash")
|
|
|
|
|
|
|
|
hash = mulHash(key)
|
|
|
|
println("乘法雜湊值為 $hash")
|
|
|
|
|
|
|
|
hash = xorHash(key)
|
|
|
|
println("互斥或雜湊值為 $hash")
|
|
|
|
|
|
|
|
hash = rotHash(key)
|
|
|
|
println("旋轉雜湊值為 $hash")
|
|
|
|
}
|