-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhash_map.go
150 lines (126 loc) · 2.73 KB
/
hash_map.go
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
package main
import (
"fmt"
"hash/fnv"
)
const INITIAL_CAPACITY = 10
const LOAD_FACTOR = 0.75
type Entry struct {
Key string
Value int
Next *Entry
}
type HashMap struct {
Table []*Entry
Size int
Capacity int
}
func hash(key string, capacity int) int {
h := fnv.New32a()
h.Write([]byte(key))
return int(h.Sum32()) % capacity
}
func initHashMap() *HashMap {
mapObj := &HashMap{
Capacity: INITIAL_CAPACITY,
Table: make([]*Entry, INITIAL_CAPACITY),
}
return mapObj
}
func resizeMap(mapObj *HashMap) {
newCapacity := mapObj.Capacity * 2
newTable := make([]*Entry, newCapacity)
for i := 0; i < mapObj.Capacity; i++ {
entry := mapObj.Table[i]
for entry != nil {
newIndex := hash(entry.Key, newCapacity)
newEntry := &Entry{
Key: entry.Key,
Value: entry.Value,
Next: newTable[newIndex],
}
newTable[newIndex] = newEntry
entry = entry.Next
}
}
mapObj.Table = newTable
mapObj.Capacity = newCapacity
}
func put(mapObj *HashMap, key string, value int) {
if float64(mapObj.Size)/float64(mapObj.Capacity) > LOAD_FACTOR {
resizeMap(mapObj)
}
index := hash(key, mapObj.Capacity)
entry := mapObj.Table[index]
for entry != nil {
if entry.Key == key {
entry.Value = value
return
}
entry = entry.Next
}
newEntry := &Entry{
Key: key,
Value: value,
Next: mapObj.Table[index],
}
mapObj.Table[index] = newEntry
mapObj.Size++
}
func get(mapObj *HashMap, key string) int {
index := hash(key, mapObj.Capacity)
entry := mapObj.Table[index]
for entry != nil {
if entry.Key == key {
return entry.Value
}
entry = entry.Next
}
return -1
}
func delete(mapObj *HashMap, key string) {
index := hash(key, mapObj.Capacity)
entry := mapObj.Table[index]
var prev *Entry
for entry != nil {
if entry.Key == key {
if prev != nil {
prev.Next = entry.Next
} else {
mapObj.Table[index] = entry.Next
}
mapObj.Size--
return
}
prev = entry
entry = entry.Next
}
}
func freeHashMap(mapObj *HashMap) {
for i := 0; i < mapObj.Capacity; i++ {
entry := mapObj.Table[i]
for entry != nil {
next := entry.Next
entry = next
}
}
}
func main() {
mapObj := initHashMap()
put(mapObj, "apple", 10)
put(mapObj, "banana", 20)
put(mapObj, "orange", 30)
fmt.Println("apple:", get(mapObj, "apple"))
fmt.Println("banana:", get(mapObj, "banana"))
fmt.Println("grape:", get(mapObj, "grape"))
delete(mapObj, "banana")
fmt.Println("banana after delete:", get(mapObj, "banana"))
freeHashMap(mapObj)
}
/*
jarry@MacBook-Pro hash % go run hash_map.go
apple: 10
banana: 20
grape: -1
banana after delete: -1
*/