mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-05 12:14:20 +00:00
772183705e
* Add Russian docs site baseline * Add Russian localized codebase * Polish Russian code wording * Update ru code translation. * Update code translation and chapter covers. * Fix pythontutor extraction. * Add README and landing page. * placeholder of profiles * Use figures of English version * Remove chapter paperbook
56 lines
1.1 KiB
Go
56 lines
1.1 KiB
Go
// File: simple_hash.go
|
|
// Created Time: 2023-06-23
|
|
// Author: Reanon (793584285@qq.com)
|
|
|
|
package chapter_hashing
|
|
|
|
import "fmt"
|
|
|
|
/* Аддитивное хеширование */
|
|
func addHash(key string) int {
|
|
var hash int64
|
|
var modulus int64
|
|
|
|
modulus = 1000000007
|
|
for _, b := range []byte(key) {
|
|
hash = (hash + int64(b)) % modulus
|
|
}
|
|
return int(hash)
|
|
}
|
|
|
|
/* Мультипликативное хеширование */
|
|
func mulHash(key string) int {
|
|
var hash int64
|
|
var modulus int64
|
|
|
|
modulus = 1000000007
|
|
for _, b := range []byte(key) {
|
|
hash = (31*hash + int64(b)) % modulus
|
|
}
|
|
return int(hash)
|
|
}
|
|
|
|
/* XOR-хеширование */
|
|
func xorHash(key string) int {
|
|
hash := 0
|
|
modulus := 1000000007
|
|
for _, b := range []byte(key) {
|
|
fmt.Println(int(b))
|
|
hash ^= int(b)
|
|
hash = (31*hash + int(b)) % modulus
|
|
}
|
|
return hash & modulus
|
|
}
|
|
|
|
/* Хеширование с циклическим сдвигом */
|
|
func rotHash(key string) int {
|
|
var hash int64
|
|
var modulus int64
|
|
|
|
modulus = 1000000007
|
|
for _, b := range []byte(key) {
|
|
hash = ((hash << 4) ^ (hash >> 28) ^ int64(b)) % modulus
|
|
}
|
|
return int(hash)
|
|
}
|