Files
Yudong Jin 2f561d3d51 feat(ko): add the Korean online edition (#2006)
* feat(ko): add the complete Korean online edition

* Use UTF-8 for Python CI output on Windows

* Remove print-book promotion from the Korean site

* fix(ko): preserve emphasis before Korean particles
2026-10-29 03:46:43 +08:00

74 lines
1.5 KiB
Swift

/**
* File: simple_hash.swift
* Created Time: 2023-07-01
* Author: nuomi1 ([email protected])
*/
/* 덧셈 해시 */
func addHash(key: String) -> Int {
var hash = 0
let MODULUS = 1_000_000_007
for c in key {
for scalar in c.unicodeScalars {
hash = (hash + Int(scalar.value)) % MODULUS
}
}
return hash
}
/* 곱셈 해시 */
func mulHash(key: String) -> Int {
var hash = 0
let MODULUS = 1_000_000_007
for c in key {
for scalar in c.unicodeScalars {
hash = (31 * hash + Int(scalar.value)) % MODULUS
}
}
return hash
}
/* XOR 해시 */
func xorHash(key: String) -> Int {
var hash = 0
let MODULUS = 1_000_000_007
for c in key {
for scalar in c.unicodeScalars {
hash ^= Int(scalar.value)
}
}
return hash & MODULUS
}
/* 회전 해시 */
func rotHash(key: String) -> Int {
var hash = 0
let MODULUS = 1_000_000_007
for c in key {
for scalar in c.unicodeScalars {
hash = ((hash << 4) ^ (hash >> 28) ^ Int(scalar.value)) % MODULUS
}
}
return hash
}
@main
enum SimpleHash {
/* 실행 코드 */
static func main() {
let key = "Hello 算法"
var hash = addHash(key: key)
print("덧셈 해시값 = \(hash)")
hash = mulHash(key: key)
print("곱셈 해시값 = \(hash)")
hash = xorHash(key: key)
print("XOR 해시값 = \(hash)")
hash = rotHash(key: key)
print("회전 해시값 = \(hash)")
}
}