/
hashmap.go
202 lines (166 loc) · 4.04 KB
/
hashmap.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package hashmap
import (
"hash/fnv"
"github.com/joetifa2003/mm-go"
"github.com/joetifa2003/mm-go/linkedlist"
"github.com/joetifa2003/mm-go/vector"
)
// Hashable keys must implement this interface
// or use type hashmap.String and hashmap.Int
// which implements the interface
type Hashable interface {
comparable
Hash() uint32
}
// String a string type that implements Hashable Interface
type String string
func (s String) Hash() uint32 {
h := fnv.New32a()
h.Write([]byte(s))
return h.Sum32()
}
// Int an int type that implements Hashable Interface
type Int int
func (i Int) Hash() uint32 {
return uint32(i)
}
type pair[K Hashable, V any] struct {
key K
value V
}
// Hashmap Manually managed hashmap,
// keys can be hashmap.String, hashmap.Int or any type that
// implements the hashmap.Hashable interface
type Hashmap[K Hashable, V any] struct {
pairs *vector.Vector[*linkedlist.LinkedList[pair[K, V]]]
totalTaken int
}
// New creates a new Hashmap with key of type K and value of type V
func New[K Hashable, V any]() *Hashmap[K, V] {
hm := mm.Alloc[Hashmap[K, V]]()
hm.pairs = vector.New[*linkedlist.LinkedList[pair[K, V]]](8)
return hm
}
func (hm *Hashmap[K, V]) extend() {
newPairs := vector.New[*linkedlist.LinkedList[pair[K, V]]](hm.pairs.Len() * 2)
oldPairs := hm.pairs
defer oldPairs.Free()
hm.totalTaken = 0
hm.pairs = newPairs
for _, pairs := range oldPairs.Slice() {
if pairs == nil {
continue
}
pairs.ForEach(func(idx int, p pair[K, V]) {
hm.Insert(p.key, p.value)
})
}
}
// Insert inserts a new value V if key K doesn't exist,
// Otherwise update the key K with value V
func (hm *Hashmap[K, V]) Insert(key K, value V) {
if ptr, exists := hm.GetPtr(key); exists {
*ptr = value
return
}
if hm.totalTaken == hm.pairs.Len() {
hm.extend()
}
idx := int(key.Hash() % uint32(hm.pairs.Len()))
pairs := hm.pairs.At(idx)
if pairs == nil {
newPairs := linkedlist.New[pair[K, V]]()
hm.pairs.Set(idx, newPairs)
pairs = newPairs
}
pairs.PushBack(pair[K, V]{key: key, value: value})
hm.totalTaken++
}
// Get takes key K and return value V
func (hm *Hashmap[K, V]) Get(key K) (value V, exists bool) {
idx := int(key.Hash() % uint32(hm.pairs.Len()))
pairs := hm.pairs.At(idx)
if pairs == nil {
return *new(V), false
}
pairIdx, ok := pairs.FindIndex(func(value pair[K, V]) bool {
return value.key == key
})
if !ok {
return *new(V), false
}
return pairs.At(pairIdx).value, ok
}
// GetPtr takes key K and return a pointer to value V
func (hm *Hashmap[K, V]) GetPtr(key K) (value *V, exists bool) {
idx := int(key.Hash() % uint32(hm.pairs.Len()))
pairs := hm.pairs.At(idx)
if pairs == nil {
return nil, false
}
pairIdx, ok := pairs.FindIndex(func(value pair[K, V]) bool {
return value.key == key
})
if !ok {
return nil, false
}
return &pairs.AtPtr(pairIdx).value, ok
}
// ForEach iterates through all key/value pairs
func (hm *Hashmap[K, V]) ForEach(f func(key K, value V)) {
for _, pairs := range hm.pairs.Slice() {
if pairs == nil {
continue
}
pairs.ForEach(func(idx int, p pair[K, V]) {
f(p.key, p.value)
})
}
}
// Values returns all values as a slice
func (hm *Hashmap[K, V]) Values() []V {
res := make([]V, 0)
for _, pairs := range hm.pairs.Slice() {
if pairs == nil {
continue
}
pairs.ForEach(func(idx int, p pair[K, V]) {
res = append(res, p.value)
})
}
return res
}
// Keys returns all keys as a slice
func (hm *Hashmap[K, V]) Keys() []K {
res := make([]K, 0)
for _, pairs := range hm.pairs.Slice() {
if pairs == nil {
continue
}
pairs.ForEach(func(idx int, p pair[K, V]) {
res = append(res, p.key)
})
}
return res
}
// Delete delete value with key K
func (hm *Hashmap[K, V]) Delete(key K) {
idx := int(key.Hash() % uint32(hm.pairs.Len()))
pairs := hm.pairs.At(idx)
if pairs == nil {
return
}
pairs.Remove(func(idx int, p pair[K, V]) bool {
return p.key == key
})
}
// Free frees the Hashmap
func (hm *Hashmap[K, V]) Free() {
for _, pairs := range hm.pairs.Slice() {
if pairs != nil {
pairs.Free()
}
}
hm.pairs.Free()
mm.Free(hm)
}