File tree Expand file tree Collapse file tree 2 files changed +33
-0
lines changed
topics/go/algorithms/data/hash Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ Package hash implements a hash table.
3
+
4
+ Hash table
5
+
6
+ hashKey(key) ────────┐
7
+ │
8
+ ↓
9
+ ┌────┬─────┬─────┬────┬─────┬─────┬─────┬─────┐
10
+ │ │ │ │ │ │ │ │ │ ←── bucket
11
+ └────┴─────┴─────┴────┴─────┴─────┴─────┴─────┘
12
+ │ │
13
+ ↓ ↓
14
+ ┌─────────────┐ ┌─────────────┐
15
+ │ key │ value │ │ key │ value │ ←── entry
16
+ ├─────────────┤ ├─────────────┤
17
+ │ key │ value │ │ key │ value │
18
+ ├─────────────┤ └─────────────┘
19
+ │ key │ value │
20
+ ├─────────────┤
21
+ │ key │ value │
22
+ ├─────────────┤
23
+ │ key │ value │
24
+ └─────────────┘
25
+
26
+
27
+ - hashKey(key) returns a number between 0 to len(buckets)-1
28
+ - We use a slice of entries as a bucket to handles cases where two or more keys
29
+ are hashed to the same bucket
30
+ - See more at https://en.wikipedia.org/wiki/Hash_table
31
+ */
32
+ package hash
Original file line number Diff line number Diff line change @@ -133,6 +133,7 @@ func (h *Hash) hashKey(key string) int {
133
133
h .hash .Reset ()
134
134
135
135
// Write the key to the maphash to update the current state.
136
+ // We don't check error value since WriteString never fails.
136
137
h .hash .WriteString (key )
137
138
138
139
// Ask the maphash for its current state which we will
You can’t perform that action at this time.
0 commit comments