Skip to content

Commit de701ae

Browse files
authored
Hash doc (ardanlabs#317)
* hash doc * wikipedia link * package last * linters * removed nolint
1 parent bdd8528 commit de701ae

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

topics/go/algorithms/data/hash/doc.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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

topics/go/algorithms/data/hash/hash.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ func (h *Hash) hashKey(key string) int {
133133
h.hash.Reset()
134134

135135
// Write the key to the maphash to update the current state.
136+
// We don't check error value since WriteString never fails.
136137
h.hash.WriteString(key)
137138

138139
// Ask the maphash for its current state which we will

0 commit comments

Comments
 (0)