-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.go
More file actions
164 lines (136 loc) · 2.92 KB
/
map.go
File metadata and controls
164 lines (136 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//go:build ignore
// This hashmap implementation is for key with data type `string`
// The hashing function implementation is from: https://cp-algorithms.com/string/string-hashing.html
package main
import "fmt"
const MAP_SIZE = 100
type HashTable[T any] struct {
key string
value T
}
type Node[T any] struct {
data HashTable[T]
next *Node[T]
}
type HashMap[T any] struct {
arr [MAP_SIZE]*Node[T]
size int
}
func NewMap[T any]() *HashMap[T] {
var items [MAP_SIZE]*Node[T]
return &HashMap[T]{
arr: items,
size: 0,
}
}
func (m *HashMap[T]) Insert(k string, v T) {
idx := ComputeHash(k) % MAP_SIZE
node := &Node[T]{
data: HashTable[T]{
key: k,
value: v,
},
next: m.arr[idx],
}
m.arr[idx] = node
m.size++
}
func (m *HashMap[T]) Get(k string) *T {
idx := ComputeHash(k) % MAP_SIZE
curr := m.arr[idx]
for curr != nil {
table := curr.data
if table.key == k {
return &table.value
}
curr = curr.next
}
return nil
}
func (m *HashMap[T]) Delete(k string) bool {
isDelete := false
idx := ComputeHash(k) % MAP_SIZE
curr := m.arr[idx]
if curr == nil {
return false
}
if curr.data.key == k {
m.arr[idx] = curr.next
m.size--
return true
}
for curr.next != nil {
table := curr.next.data
if table.key == k {
// found the key, now remove it and shift all the later nodes towards it
curr.next = curr.next.next
m.size--
return true
}
curr = curr.next
}
return isDelete
}
// modulo the hash result with the size of the array
func ComputeHash(input string) uint {
const p uint = 31
const m uint = 1e9 + 9
var hashValue uint = 0
var pPow uint = 1
for _, c := range input {
hashValue = (hashValue + (uint(c)-uint('a')+1)*pPow) % m
pPow = (pPow * p) % m
}
return hashValue
}
func main() {
hashMap := NewMap[int]()
hashMap.Insert("John", 18)
hashMap.Insert("Abraham", 24)
hashMap.Insert("Jack", 30)
hashMap.Insert("Luis", 50)
hashMap.Insert("Maple", 25)
hashMap.Insert("Ron", 20)
john := hashMap.Get("John")
if john == nil {
fmt.Println("Key: `John` not found")
} else {
fmt.Printf("For John: %d\n", *john)
}
abra := hashMap.Get("Abraham")
if abra == nil {
fmt.Println("Key: `Abraham` not found")
} else {
fmt.Printf("For Abraham: %d\n", *abra)
}
luis := hashMap.Get("Luis")
if luis == nil {
fmt.Println("Key: `Luis` not found")
} else {
fmt.Printf("For Luis: %d\n", *luis)
}
maple := hashMap.Get("Maple")
if maple == nil {
fmt.Println("Key: `Maple` not found")
} else {
fmt.Printf("For Maple: %d\n", *maple)
}
rohn := hashMap.Get("Apple")
if rohn == nil {
fmt.Println("Key: `Apple` not found")
} else {
fmt.Printf("For Apple: %d\n", *rohn)
}
status := hashMap.Delete("Jack")
if !status {
fmt.Println("Key not found, so unable to delete it")
} else {
// delete so not found in the table
jack := hashMap.Get("Jack")
if jack == nil {
fmt.Println("Key: `Jack` not found")
} else {
fmt.Printf("For Jack: %d\n", *jack)
}
}
}