forked from brentp/intintmap
-
Notifications
You must be signed in to change notification settings - Fork 4
/
map64.go
442 lines (380 loc) · 8.52 KB
/
map64.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
// Package intmap contains a fast hashmap implementation for maps with keys of any integer type
package intmap
import (
"iter"
"math"
)
// IntKey is a type constraint for values that can be used as keys in Map
type IntKey interface {
~int | ~uint | ~int64 | ~uint64 | ~int32 | ~uint32 | ~int16 | ~uint16 | ~int8 | ~uint8 | ~uintptr
}
type pair[K IntKey, V any] struct {
K K
V V
}
const fillFactor64 = 0.7
func phiMix64(x int) int {
h := int64(x) * int64(0x9E3779B9)
return int(h ^ (h >> 16))
}
// Map is a hashmap where the keys are some any integer type.
// It is valid to call methods that read a nil map, similar to a standard Go map.
// Methods valid on a nil map are Has, Get, Len, and ForEach.
type Map[K IntKey, V any] struct {
data []pair[K, V] // key-value pairs
size int
zeroVal V // value of 'zero' key
hasZeroKey bool // do we have 'zero' key in the map?
}
// New creates a new map with keys being any integer subtype.
// The map can store up to the given capacity before reallocation and rehashing occurs.
func New[K IntKey, V any](capacity int) *Map[K, V] {
return &Map[K, V]{
data: make([]pair[K, V], arraySize(capacity, fillFactor64)),
}
}
// Has checks if the given key exists in the map.
// Calling this method on a nil map will return false.
func (m *Map[K, V]) Has(key K) bool {
if m == nil {
return false
}
if key == K(0) {
return m.hasZeroKey
}
idx := m.startIndex(key)
p := m.data[idx]
if p.K == K(0) { // end of chain already
return false
}
if p.K == key { // we check zero prior to this call
return true
}
// hash collision, seek next hash match, bailing on first empty
for {
idx = m.nextIndex(idx)
p = m.data[idx]
if p.K == K(0) {
return false
}
if p.K == key {
return true
}
}
}
// Get returns the value if the key is found.
// If you just need to check for existence it is easier to use Has.
// Calling this method on a nil map will return the zero value for V and false.
func (m *Map[K, V]) Get(key K) (V, bool) {
if m == nil {
var zero V
return zero, false
}
if key == K(0) {
if m.hasZeroKey {
return m.zeroVal, true
}
var zero V
return zero, false
}
idx := m.startIndex(key)
p := m.data[idx]
if p.K == K(0) { // end of chain already
var zero V
return zero, false
}
if p.K == key { // we check zero prior to this call
return p.V, true
}
// hash collision, seek next hash match, bailing on first empty
for {
idx = m.nextIndex(idx)
p = m.data[idx]
if p.K == K(0) {
var zero V
return zero, false
}
if p.K == key {
return p.V, true
}
}
}
// Put adds or updates key with value val.
func (m *Map[K, V]) Put(key K, val V) {
if key == K(0) {
if !m.hasZeroKey {
m.size++
}
m.zeroVal = val
m.hasZeroKey = true
return
}
idx := m.startIndex(key)
p := &m.data[idx]
if p.K == K(0) { // end of chain already
p.K = key
p.V = val
if m.size >= m.sizeThreshold() {
m.rehash()
} else {
m.size++
}
return
} else if p.K == key { // overwrite existing value
p.V = val
return
}
// hash collision, seek next empty or key match
for {
idx = m.nextIndex(idx)
p = &m.data[idx]
if p.K == K(0) {
p.K = key
p.V = val
if m.size >= m.sizeThreshold() {
m.rehash()
} else {
m.size++
}
return
} else if p.K == key {
p.V = val
return
}
}
}
// PutIfNotExists adds the key-value pair only if the key does not already exist
// in the map, and returns the current value associated with the key and a boolean
// indicating whether the value was newly added or not.
func (m *Map[K, V]) PutIfNotExists(key K, val V) (V, bool) {
if key == K(0) {
if m.hasZeroKey {
return m.zeroVal, false
}
m.zeroVal = val
m.hasZeroKey = true
m.size++
return val, true
}
idx := m.startIndex(key)
p := &m.data[idx]
if p.K == K(0) { // end of chain already
p.K = key
p.V = val
m.size++
if m.size >= m.sizeThreshold() {
m.rehash()
}
return val, true
} else if p.K == key {
return p.V, false
}
// hash collision, seek next hash match, bailing on first empty
for {
idx = m.nextIndex(idx)
p = &m.data[idx]
if p.K == K(0) {
p.K = key
p.V = val
m.size++
if m.size >= m.sizeThreshold() {
m.rehash()
}
return val, true
} else if p.K == key {
return p.V, false
}
}
}
// ForEach iterates through key-value pairs in the map while the function f returns true.
// This method returns immediately if invoked on a nil map.
//
// The iteration order of a Map is not defined, so please avoid relying on it.
func (m *Map[K, V]) ForEach(f func(K, V) bool) {
if m == nil {
return
}
if m.hasZeroKey && !f(K(0), m.zeroVal) {
return
}
forEach64(m.data, f)
}
// All returns an iterator over key-value pairs from m.
// The iterator returns immediately if invoked on a nil map.
//
// The iteration order of a Map is not defined, so please avoid relying on it.
func (m *Map[K, V]) All() iter.Seq2[K, V] {
return m.ForEach
}
// Keys returns an iterator over keys in m.
// The iterator returns immediately if invoked on a nil map.
//
// The iteration order of a Map is not defined, so please avoid relying on it.
func (m *Map[K, V]) Keys() iter.Seq[K] {
return func(yield func(k K) bool) {
if m == nil {
return
}
if m.hasZeroKey && !yield(K(0)) {
return
}
for _, p := range m.data {
if p.K != K(0) && !yield(p.K) {
return
}
}
}
}
// Values returns an iterator over values in m.
// The iterator returns immediately if invoked on a nil map.
//
// The iteration order of a Map is not defined, so please avoid relying on it.
func (m *Map[K, V]) Values() iter.Seq[V] {
return func(yield func(v V) bool) {
if m == nil {
return
}
if m.hasZeroKey && !yield(m.zeroVal) {
return
}
for _, p := range m.data {
if p.K != K(0) && !yield(p.V) {
return
}
}
}
}
// Clear removes all items from the map, but keeps the internal buffers for reuse.
func (m *Map[K, V]) Clear() {
var zero V
m.hasZeroKey = false
m.zeroVal = zero
// compiles down to runtime.memclr()
for i := range m.data {
m.data[i] = pair[K, V]{}
}
m.size = 0
}
func (m *Map[K, V]) rehash() {
oldData := m.data
m.data = make([]pair[K, V], 2*len(m.data))
// reset size
if m.hasZeroKey {
m.size = 1
} else {
m.size = 0
}
forEach64(oldData, func(k K, v V) bool {
m.Put(k, v)
return true
})
}
// Len returns the number of elements in the map.
// The length of a nil map is defined to be zero.
func (m *Map[K, V]) Len() int {
if m == nil {
return 0
}
return m.size
}
func (m *Map[K, V]) sizeThreshold() int {
return int(math.Floor(float64(len(m.data)) * fillFactor64))
}
func (m *Map[K, V]) startIndex(key K) int {
return phiMix64(int(key)) & (len(m.data) - 1)
}
func (m *Map[K, V]) nextIndex(idx int) int {
return (idx + 1) & (len(m.data) - 1)
}
func forEach64[K IntKey, V any](pairs []pair[K, V], f func(k K, v V) bool) {
for _, p := range pairs {
if p.K != K(0) && !f(p.K, p.V) {
return
}
}
}
// Del deletes a key and its value, returning true iff the key was found
func (m *Map[K, V]) Del(key K) bool {
if key == K(0) {
if m.hasZeroKey {
m.hasZeroKey = false
m.size--
return true
}
return false
}
idx := m.startIndex(key)
p := m.data[idx]
if p.K == key {
// any keys that were pushed back needs to be shifted nack into the empty slot
// to avoid breaking the chain
m.shiftKeys(idx)
m.size--
return true
} else if p.K == K(0) { // end of chain already
return false
}
for {
idx = m.nextIndex(idx)
p = m.data[idx]
if p.K == key {
// any keys that were pushed back needs to be shifted nack into the empty slot
// to avoid breaking the chain
m.shiftKeys(idx)
m.size--
return true
} else if p.K == K(0) {
return false
}
}
}
func (m *Map[K, V]) shiftKeys(idx int) int {
// Shift entries with the same hash.
// We need to do this on deletion to ensure we don't have zeroes in the hash chain
for {
var p pair[K, V]
lastIdx := idx
idx = m.nextIndex(idx)
for {
p = m.data[idx]
if p.K == K(0) {
m.data[lastIdx] = pair[K, V]{}
return lastIdx
}
slot := m.startIndex(p.K)
if lastIdx <= idx {
if lastIdx >= slot || slot > idx {
break
}
} else {
if lastIdx >= slot && slot > idx {
break
}
}
idx = m.nextIndex(idx)
}
m.data[lastIdx] = p
}
}
func nextPowerOf2(x uint32) uint32 {
if x == math.MaxUint32 {
return x
}
if x == 0 {
return 1
}
x--
x |= x >> 1
x |= x >> 2
x |= x >> 4
x |= x >> 8
x |= x >> 16
return x + 1
}
func arraySize(exp int, fill float64) int {
s := nextPowerOf2(uint32(math.Ceil(float64(exp) / fill)))
if s < 2 {
s = 2
}
return int(s)
}