mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-24 10:06:28 +08:00
feat: add Swift codes for hash_algorithm article (#576)
This commit is contained in:
parent
9b15072a85
commit
87076132e7
4 changed files with 124 additions and 1 deletions
|
@ -29,6 +29,8 @@ let package = Package(
|
|||
.executable(name: "array_hash_map", targets: ["array_hash_map"]),
|
||||
.executable(name: "hash_map_chaining", targets: ["hash_map_chaining"]),
|
||||
.executable(name: "hash_map_open_addressing", targets: ["hash_map_open_addressing"]),
|
||||
.executable(name: "simple_hash", targets: ["simple_hash"]),
|
||||
.executable(name: "built_in_hash", targets: ["built_in_hash"]),
|
||||
// chapter_tree
|
||||
.executable(name: "binary_tree", targets: ["binary_tree"]),
|
||||
.executable(name: "binary_tree_bfs", targets: ["binary_tree_bfs"]),
|
||||
|
@ -95,6 +97,8 @@ let package = Package(
|
|||
.executableTarget(name: "array_hash_map", dependencies: ["utils"], path: "chapter_hashing", sources: ["array_hash_map.swift"]),
|
||||
.executableTarget(name: "hash_map_chaining", dependencies: ["utils"], path: "chapter_hashing", sources: ["hash_map_chaining.swift"]),
|
||||
.executableTarget(name: "hash_map_open_addressing", dependencies: ["utils"], path: "chapter_hashing", sources: ["hash_map_open_addressing.swift"]),
|
||||
.executableTarget(name: "simple_hash", path: "chapter_hashing", sources: ["simple_hash.swift"]),
|
||||
.executableTarget(name: "built_in_hash", dependencies: ["utils"], path: "chapter_hashing", sources: ["built_in_hash.swift"]),
|
||||
// chapter_tree
|
||||
.executableTarget(name: "binary_tree", dependencies: ["utils"], path: "chapter_tree", sources: ["binary_tree.swift"]),
|
||||
.executableTarget(name: "binary_tree_bfs", dependencies: ["utils"], path: "chapter_tree", sources: ["binary_tree_bfs.swift"]),
|
||||
|
|
37
codes/swift/chapter_hashing/built_in_hash.swift
Normal file
37
codes/swift/chapter_hashing/built_in_hash.swift
Normal file
|
@ -0,0 +1,37 @@
|
|||
/**
|
||||
* File: built_in_hash.swift
|
||||
* Created Time: 2023-07-01
|
||||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
import utils
|
||||
|
||||
@main
|
||||
enum BuiltInHash {
|
||||
/* Driver Code */
|
||||
static func main() {
|
||||
let num = 3
|
||||
let hashNum = num.hashValue
|
||||
print("整数 \(num) 的哈希值为 \(hashNum)")
|
||||
|
||||
let bol = true
|
||||
let hashBol = bol.hashValue
|
||||
print("布尔量 \(bol) 的哈希值为 \(hashBol)")
|
||||
|
||||
let dec = 3.14159
|
||||
let hashDec = dec.hashValue
|
||||
print("小数 \(dec) 的哈希值为 \(hashDec)")
|
||||
|
||||
let str = "Hello 算法"
|
||||
let hashStr = str.hashValue
|
||||
print("字符串 \(str) 的哈希值为 \(hashStr)")
|
||||
|
||||
let arr = [AnyHashable(12836), AnyHashable("小哈")]
|
||||
let hashTup = arr.hashValue
|
||||
print("数组 \(arr) 的哈希值为 \(hashTup)")
|
||||
|
||||
let obj = ListNode(x: 0)
|
||||
let hashObj = obj.hashValue
|
||||
print("节点对象 \(obj) 的哈希值为 \(hashObj)")
|
||||
}
|
||||
}
|
73
codes/swift/chapter_hashing/simple_hash.swift
Normal file
73
codes/swift/chapter_hashing/simple_hash.swift
Normal file
|
@ -0,0 +1,73 @@
|
|||
/**
|
||||
* File: simple_hash.swift
|
||||
* Created Time: 2023-07-01
|
||||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
/* 加法哈希 */
|
||||
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
|
||||
}
|
||||
|
||||
/* 异或哈希 */
|
||||
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 {
|
||||
/* Driver Code */
|
||||
static func main() {
|
||||
let key = "Hello 算法"
|
||||
|
||||
var hash = addHash(key: key)
|
||||
print("加法哈希值为 \(hash)")
|
||||
|
||||
hash = mulHash(key: key)
|
||||
print("乘法哈希值为 \(hash)")
|
||||
|
||||
hash = xorHash(key: key)
|
||||
print("异或哈希值为 \(hash)")
|
||||
|
||||
hash = rotHash(key: key)
|
||||
print("旋转哈希值为 \(hash)")
|
||||
}
|
||||
}
|
|
@ -4,7 +4,7 @@
|
|||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
public class ListNode {
|
||||
public class ListNode: Hashable {
|
||||
public var val: Int // 节点值
|
||||
public var next: ListNode? // 后继节点引用
|
||||
|
||||
|
@ -12,6 +12,15 @@ public class ListNode {
|
|||
val = x
|
||||
}
|
||||
|
||||
public static func == (lhs: ListNode, rhs: ListNode) -> Bool {
|
||||
lhs.val == rhs.val && lhs.next.map { ObjectIdentifier($0) } == rhs.next.map { ObjectIdentifier($0) }
|
||||
}
|
||||
|
||||
public func hash(into hasher: inout Hasher) {
|
||||
hasher.combine(val)
|
||||
hasher.combine(next.map { ObjectIdentifier($0) })
|
||||
}
|
||||
|
||||
public static func arrToLinkedList(arr: [Int]) -> ListNode? {
|
||||
let dum = ListNode(x: 0)
|
||||
var head: ListNode? = dum
|
||||
|
|
Loading…
Reference in a new issue