Skip to content

Commit 92eca16

Browse files
authored
Merge pull request krahets#266 from nuomi1/feature/hash_map-Swift
feat: add Swift codes for hash_map article
2 parents a01841a + f0c54ab commit 92eca16

File tree

5 files changed

+278
-1
lines changed

5 files changed

+278
-1
lines changed

codes/swift/Package.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ let package = Package(
2020
.executable(name: "linkedlist_queue", targets: ["linkedlist_queue"]),
2121
.executable(name: "array_queue", targets: ["array_queue"]),
2222
.executable(name: "deque", targets: ["deque"]),
23+
.executable(name: "hash_map", targets: ["hash_map"]),
24+
.executable(name: "array_hash_map", targets: ["array_hash_map"]),
2325
],
2426
targets: [
2527
.target(name: "utils", path: "utils"),
@@ -38,5 +40,7 @@ let package = Package(
3840
.executableTarget(name: "linkedlist_queue", dependencies: ["utils"], path: "chapter_stack_and_queue", sources: ["linkedlist_queue.swift"]),
3941
.executableTarget(name: "array_queue", path: "chapter_stack_and_queue", sources: ["array_queue.swift"]),
4042
.executableTarget(name: "deque", path: "chapter_stack_and_queue", sources: ["deque.swift"]),
43+
.executableTarget(name: "hash_map", dependencies: ["utils"], path: "chapter_hashing", sources: ["hash_map.swift"]),
44+
.executableTarget(name: "array_hash_map", path: "chapter_hashing", sources: ["array_hash_map.swift"]),
4145
]
4246
)
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/**
2+
* File: array_hash_map.swift
3+
* Created Time: 2023-01-16
4+
* Author: nuomi1 (nuomi1@qq.com)
5+
*/
6+
7+
/* 键值对 int->String */
8+
class Entry {
9+
var key: Int
10+
var val: String
11+
12+
init(key: Int, val: String) {
13+
self.key = key
14+
self.val = val
15+
}
16+
}
17+
18+
/* 基于数组简易实现的哈希表 */
19+
class ArrayHashMap {
20+
private var bucket: [Entry?] = []
21+
22+
init() {
23+
// 初始化一个长度为 100 的桶(数组)
24+
for _ in 0 ..< 100 {
25+
bucket.append(nil)
26+
}
27+
}
28+
29+
/* 哈希函数 */
30+
private func hashFunc(key: Int) -> Int {
31+
let index = key % 100
32+
return index
33+
}
34+
35+
/* 查询操作 */
36+
func get(key: Int) -> String? {
37+
let index = hashFunc(key: key)
38+
let pair = bucket[index]
39+
return pair?.val
40+
}
41+
42+
/* 添加操作 */
43+
func put(key: Int, val: String) {
44+
let pair = Entry(key: key, val: val)
45+
let index = hashFunc(key: key)
46+
bucket[index] = pair
47+
}
48+
49+
/* 删除操作 */
50+
func remove(key: Int) {
51+
let index = hashFunc(key: key)
52+
// 置为 nil ,代表删除
53+
bucket[index] = nil
54+
}
55+
56+
/* 获取所有键值对 */
57+
func entrySet() -> [Entry] {
58+
var entrySet: [Entry] = []
59+
for pair in bucket {
60+
if let pair = pair {
61+
entrySet.append(pair)
62+
}
63+
}
64+
return entrySet
65+
}
66+
67+
/* 获取所有键 */
68+
func keySet() -> [Int] {
69+
var keySet: [Int] = []
70+
for pair in bucket {
71+
if let pair = pair {
72+
keySet.append(pair.key)
73+
}
74+
}
75+
return keySet
76+
}
77+
78+
/* 获取所有值 */
79+
func valueSet() -> [String] {
80+
var valueSet: [String] = []
81+
for pair in bucket {
82+
if let pair = pair {
83+
valueSet.append(pair.val)
84+
}
85+
}
86+
return valueSet
87+
}
88+
89+
/* 打印哈希表 */
90+
func print() {
91+
for entry in entrySet() {
92+
Swift.print("\(entry.key) -> \(entry.val)")
93+
}
94+
}
95+
}
96+
97+
@main
98+
enum _ArrayHashMap {
99+
/* Driver Code */
100+
static func main() {
101+
/* 初始化哈希表 */
102+
let map = ArrayHashMap()
103+
104+
/* 添加操作 */
105+
// 在哈希表中添加键值对 (key, value)
106+
map.put(key: 12836, val: "小哈")
107+
map.put(key: 15937, val: "小啰")
108+
map.put(key: 16750, val: "小算")
109+
map.put(key: 13276, val: "小法")
110+
map.put(key: 10583, val: "小鸭")
111+
print("\n添加完成后,哈希表为\nKey -> Value")
112+
map.print()
113+
114+
/* 查询操作 */
115+
// 向哈希表输入键 key ,得到值 value
116+
let name = map.get(key: 15937)!
117+
print("\n输入学号 15937 ,查询到姓名 \(name)")
118+
119+
/* 删除操作 */
120+
// 在哈希表中删除键值对 (key, value)
121+
map.remove(key: 10583)
122+
print("\n删除 10583 后,哈希表为\nKey -> Value")
123+
map.print()
124+
125+
/* 遍历哈希表 */
126+
print("\n遍历键值对 Key->Value")
127+
for entry in map.entrySet() {
128+
print("\(entry.key) -> \(entry.val)")
129+
}
130+
print("\n单独遍历键 Key")
131+
for key in map.keySet() {
132+
print(key)
133+
}
134+
print("\n单独遍历值 Value")
135+
for val in map.valueSet() {
136+
print(val)
137+
}
138+
}
139+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* File: hash_map.swift
3+
* Created Time: 2023-01-16
4+
* Author: nuomi1 (nuomi1@qq.com)
5+
*/
6+
7+
import utils
8+
9+
@main
10+
enum HashMap {
11+
/* Driver Code */
12+
static func main() {
13+
/* 初始化哈希表 */
14+
var map: [Int: String] = [:]
15+
16+
/* 添加操作 */
17+
// 在哈希表中添加键值对 (key, value)
18+
map[12836] = "小哈"
19+
map[15937] = "小啰"
20+
map[16750] = "小算"
21+
map[13276] = "小法"
22+
map[10583] = "小鸭"
23+
print("\n添加完成后,哈希表为\nKey -> Value")
24+
PrintUtil.printHashMap(map: map)
25+
26+
/* 查询操作 */
27+
// 向哈希表输入键 key ,得到值 value
28+
let name = map[15937]!
29+
print("\n输入学号 15937 ,查询到姓名 \(name)")
30+
31+
/* 删除操作 */
32+
// 在哈希表中删除键值对 (key, value)
33+
map.removeValue(forKey: 10583)
34+
print("\n删除 10583 后,哈希表为\nKey -> Value")
35+
PrintUtil.printHashMap(map: map)
36+
37+
/* 遍历哈希表 */
38+
print("\n遍历键值对 Key->Value")
39+
for (key, value) in map {
40+
print("\(key) -> \(value)")
41+
}
42+
print("\n单独遍历键 Key")
43+
for key in map.keys {
44+
print(key)
45+
}
46+
print("\n单独遍历值 Value")
47+
for value in map.values {
48+
print(value)
49+
}
50+
}
51+
}

codes/swift/utils/PrintUtil.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,10 @@ public enum PrintUtil {
6868
showTrunks(p: p?.prev)
6969
print(p!.str, terminator: "")
7070
}
71+
72+
public static func printHashMap<K, V>(map: [K: V]) {
73+
for (key, value) in map {
74+
print("\(key) -> \(value)")
75+
}
76+
}
7177
}

docs/chapter_hashing/hash_map.md

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,24 @@ comments: true
210210
=== "Swift"
211211

212212
```swift title="hash_map.swift"
213+
/* 初始化哈希表 */
214+
var map: [Int: String] = [:]
215+
216+
/* 添加操作 */
217+
// 在哈希表中添加键值对 (key, value)
218+
map[12836] = "小哈"
219+
map[15937] = "小啰"
220+
map[16750] = "小算"
221+
map[13276] = "小法"
222+
map[10583] = "小鸭"
213223

224+
/* 查询操作 */
225+
// 向哈希表输入键 key ,得到值 value
226+
let name = map[15937]!
227+
228+
/* 删除操作 */
229+
// 在哈希表中删除键值对 (key, value)
230+
map.removeValue(forKey: 10583)
214231
```
215232

216233
遍历哈希表有三种方式,即 **遍历键值对、遍历键、遍历值**
@@ -348,7 +365,19 @@ comments: true
348365
=== "Swift"
349366

350367
```swift title="hash_map.swift"
351-
368+
/* 遍历哈希表 */
369+
// 遍历键值对 Key->Value
370+
for (key, value) in map {
371+
print("\(key) -> \(value)")
372+
}
373+
// 单独遍历键 Key
374+
for key in map.keys {
375+
print(key)
376+
}
377+
// 单独遍历值 Value
378+
for value in map.values {
379+
print(value)
380+
}
352381
```
353382

354383
## 哈希函数
@@ -771,7 +800,55 @@ $$
771800
=== "Swift"
772801

773802
```swift title="array_hash_map.swift"
803+
/* 键值对 int->String */
804+
class Entry {
805+
var key: Int
806+
var val: String
807+
808+
init(key: Int, val: String) {
809+
self.key = key
810+
self.val = val
811+
}
812+
}
813+
814+
/* 基于数组简易实现的哈希表 */
815+
class ArrayHashMap {
816+
private var bucket: [Entry?] = []
817+
818+
init() {
819+
// 初始化一个长度为 100 的桶(数组)
820+
for _ in 0 ..< 100 {
821+
bucket.append(nil)
822+
}
823+
}
824+
825+
/* 哈希函数 */
826+
private func hashFunc(key: Int) -> Int {
827+
let index = key % 100
828+
return index
829+
}
830+
831+
/* 查询操作 */
832+
func get(key: Int) -> String? {
833+
let index = hashFunc(key: key)
834+
let pair = bucket[index]
835+
return pair?.val
836+
}
774837

838+
/* 添加操作 */
839+
func put(key: Int, val: String) {
840+
let pair = Entry(key: key, val: val)
841+
let index = hashFunc(key: key)
842+
bucket[index] = pair
843+
}
844+
845+
/* 删除操作 */
846+
func remove(key: Int) {
847+
let index = hashFunc(key: key)
848+
// 置为 nil ,代表删除
849+
bucket[index] = nil
850+
}
851+
}
775852
```
776853

777854
## 哈希冲突

0 commit comments

Comments
 (0)