This commit is contained in:
krahets
2024-03-25 22:43:12 +08:00
parent 22017aa8e5
commit 87af663929
70 changed files with 7428 additions and 32 deletions

View File

@ -554,6 +554,46 @@ index = hash(key) % capacity
}
```
=== "Kotlin"
```kotlin title="simple_hash.kt"
/* 加法哈希 */
fun addHash(key: String): Int {
var hash = 0L
for (c in key.toCharArray()) {
hash = (hash + c.code) % MODULUS
}
return hash.toInt()
}
/* 乘法哈希 */
fun mulHash(key: String): Int {
var hash = 0L
for (c in key.toCharArray()) {
hash = (31 * hash + c.code) % MODULUS
}
return hash.toInt()
}
/* 异或哈希 */
fun xorHash(key: String): Int {
var hash = 0
for (c in key.toCharArray()) {
hash = hash xor c.code
}
return hash and MODULUS
}
/* 旋转哈希 */
fun rotHash(key: String): Int {
var hash = 0L
for (c in key.toCharArray()) {
hash = ((hash shl 4) xor (hash shr 28) xor c.code.toLong()) % MODULUS
}
return hash.toInt()
}
```
=== "Zig"
```zig title="simple_hash.zig"
@ -868,6 +908,12 @@ $$
// C 未提供内置 hash code 函数
```
=== "Kotlin"
```kotlin title="built_in_hash.kt"
```
=== "Zig"
```zig title="built_in_hash.zig"