-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhashmap.go
360 lines (323 loc) · 6.76 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package hashmap
import (
"encoding/json"
"fmt"
"math"
"sync"
"sync/atomic"
"time"
"unsafe"
)
const MaxInt = 2147483647
type HashMap struct {
sync.RWMutex
size int64
table *Table
loadFactor float64
}
type Table struct {
nodes []*Node
ab int
}
type Node struct {
sync.Mutex
head *Entry
tail *Entry
size int64
}
type Entry struct {
k interface{}
p unsafe.Pointer
hash uint64
flag int32 // 1 deleted
next []*Entry
prev []*Entry
}
func New() *HashMap {
return &HashMap{
table: &Table{
nodes: allocate(16),
ab: 0,
},
loadFactor: 0.7 * 3,
}
}
func allocate(capacity int) (nodes []*Node) {
nodes = make([]*Node, capacity)
for i := 0; i < capacity; i++ {
nodes[i] = &Node{}
}
return
}
func hash(k interface{}) uint64 {
if k == nil {
return 0
}
switch x := k.(type) {
case string:
return bytesHash([]byte(x))
case []byte:
return bytesHash(x)
case bool:
if x {
return 0
} else {
return 1
}
case time.Time:
return uint64(x.UnixNano())
case int:
return uint64(x)
case int8:
return uint64(x)
case int16:
return uint64(x)
case int32:
return uint64(x)
case int64:
return uint64(x)
case uint:
return uint64(x)
case uint8:
return uint64(x)
case uint16:
return uint64(x)
case uint32:
return uint64(x)
case uint64:
return x
case float32:
return math.Float64bits(float64(x))
case float64:
return math.Float64bits(x)
case uintptr:
return uint64(x)
}
panic("unsupported key type.")
}
func bytesHash(bytes []byte) uint64 {
hash := uint32(2166136261)
const prime32 = uint32(16777619)
keyLength := len(bytes)
for i := 0; i < keyLength; i++ {
hash *= prime32
hash ^= uint32(bytes[i])
}
return uint64(hash)
}
func indexOf(hash uint64, capacity int) int {
return int(hash & uint64(capacity-1))
}
func (t *Table) len() int {
return len(t.nodes)
}
func (m *HashMap) Size() int64 {
return m.size
}
//Set will CAS the existing value if k exists. If k is new, this function is locked and set node's head
//Similar to Java's hashmap's Put
//returns old value if k previously exists
//returns nil if k is new
func (m *HashMap) Set(k interface{}, v interface{}) interface{} {
m.resize()
m.RLock()
defer m.RUnlock()
h, t := hash(k), m.table
n := t.nodes[indexOf(h, t.len())]
//If key exists
if e := m.getNodeEntry(t, n, k); e != nil {
oldValue := e.Value()
atomic.StorePointer(&e.p, unsafe.Pointer(&v))
return oldValue
}
n.Lock()
if m.setNodeEntry(t, n, &Entry{k: k, p: unsafe.Pointer(&v), hash: h, next: make([]*Entry, 2), prev: make([]*Entry, 2)}, false) {
atomic.AddInt64(&n.size, 1)
atomic.AddInt64(&m.size, 1)
}
n.Unlock()
return nil
}
func (m *HashMap) MSet(ks []interface{}, vs []interface{}) {
if len(ks) != len(vs) {
return
}
for i, k := range ks {
m.Set(k, vs[i])
}
}
func (m *HashMap) SetNX(k interface{}, v interface{}) bool {
m.resize()
m.RLock()
defer m.RUnlock()
t := m.table
n, h := t.getKeyNode(k)
n.Lock()
defer n.Unlock()
return m.setNodeEntry(t, n, &Entry{k: k, p: unsafe.Pointer(&v), hash: h, next: make([]*Entry, 2), prev: make([]*Entry, 2)}, true)
}
func (t *Table) getKeyNode(k interface{}) (*Node, uint64) {
h, nodes := hash(k), t.nodes
i := indexOf(h, len(nodes))
return nodes[i], h
}
func (m *HashMap) setNodeEntry(t *Table, n *Node, e *Entry, nx bool) bool {
if n.head == nil {
n.head, n.tail = e, e
} else {
next := n.head
for next != nil {
if next.k == e.k {
if !nx {
next.p = e.p
}
return false
}
next = next.next[t.ab]
}
n.tail.next[t.ab], e.prev[t.ab], n.tail = e, n.tail, e
}
return true
}
func (m *HashMap) dilate() bool {
return m.size > int64(float64(m.table.len())*m.loadFactor) && m.table.len()*2 <= MaxInt
}
func (m *HashMap) resize() {
if m.dilate() {
m.Lock()
defer m.Unlock()
if m.dilate() {
m.doResize()
}
}
}
func (m *HashMap) doResize() {
oldTable := m.table
newTable := &Table{nodes: allocate(oldTable.len() * 2), ab: m.table.ab ^ 1}
capacity := newTable.len()
size := int64(0)
for _, node := range oldTable.nodes {
next := node.head
for next != nil {
next.next[newTable.ab], next.prev[newTable.ab] = nil, nil
newNode := newTable.nodes[indexOf(next.hash, capacity)]
if newNode.head == nil {
newNode.head, newNode.tail = next, next
} else {
newNode.tail.next[newTable.ab], next.prev[newTable.ab], newNode.tail = next, newNode.tail, next
}
size++
newNode.size++
next = next.next[oldTable.ab]
}
}
m.size = size
m.table = newTable
}
func (m *HashMap) getNodeEntry(t *Table, n *Node, k interface{}) *Entry {
next := n.head
for next != nil {
if next.k == k && next.flag == 0 {
return next
}
next = next.next[t.ab]
}
return nil
}
func (m *HashMap) Get(k interface{}) (interface{}, bool) {
t := m.table
n, _ := t.getKeyNode(k)
e := m.getNodeEntry(t, n, k)
if e != nil {
return e.Value(), true
}
return nil, false
}
func (m *HashMap) Del(k interface{}) bool {
m.RLock()
defer m.RUnlock()
t := m.table
n, _ := t.getKeyNode(k)
n.Lock()
defer n.Unlock()
if e := m.getNodeEntry(t, n, k); e != nil {
if e.prev[t.ab] == nil && e.next[t.ab] == nil {
n.head, n.tail = nil, nil
} else if e.prev[t.ab] == nil {
n.head = e.next[t.ab]
n.head.prev[t.ab] = nil
} else if e.next[t.ab] == nil {
n.tail = e.prev[t.ab]
n.tail.next[t.ab] = nil
} else {
e.prev[t.ab].next[t.ab] = e.next[t.ab]
e.next[t.ab].prev[t.ab] = e.prev[t.ab]
}
oldAb := t.ab ^ 1
if e.prev[oldAb] != nil {
e.prev[oldAb].next[oldAb] = e.next[oldAb]
}
if e.next[oldAb] != nil {
e.next[oldAb].prev[oldAb] = e.prev[oldAb]
}
atomic.AddInt64(&n.size, -1)
atomic.AddInt64(&m.size, -1)
return true
}
return false
}
func (m *HashMap) LogicDel(k interface{}) bool {
h, t := hash(k), m.table
n := t.nodes[indexOf(h, t.len())]
//If key exists
if e := m.getNodeEntry(t, n, k); e != nil {
if atomic.CompareAndSwapInt32(&e.flag, 0, 1) {
atomic.AddInt64(&n.size, -1)
atomic.AddInt64(&m.size, -1)
return true
}
}
return false
}
func (e *Entry) Value() interface{} {
return *(*interface{})(e.p)
}
func (e *Entry) Key() interface{} {
return e.k
}
func (e *Entry) Flag() int32 {
return e.flag
}
func (m *HashMap) Foreach(fn func(e *Entry)) {
t := m.table
for _, node := range t.nodes {
next := node.head
for next != nil {
fn(next)
next = next.next[t.ab]
}
}
}
func (m *HashMap) UnmarshalJSON(b []byte) error {
data := map[string]interface{}{}
err := json.Unmarshal(b, &data)
if err != nil {
return err
}
for k, v := range data {
m.Set(k, v)
}
return nil
}
func (m *HashMap) MarshalJSON() ([]byte, error) {
t := m.table
data := map[string]interface{}{}
for _, node := range t.nodes {
next := node.head
for next != nil {
data[fmt.Sprintf("%v", next.k)] = next.Value()
next = next.next[t.ab]
}
}
return json.Marshal(data)
}