-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfimap.go
356 lines (285 loc) · 6.7 KB
/
fimap.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
package fimap
import (
"fmt"
"math"
)
type (
keyType = uint64
valueType = interface{}
)
const (
// PHI is for scrambling the keys
PHI = 0x9E3779B9
freeKey keyType = 0
)
var (
nilValue = valueType(nil)
)
// Map is a fast key (uint64) - value (valueType) map.
type Map struct {
keys []keyType
values []valueType
fillFactor float64
threshold int // we will resize a map once it reaches this size
size int
mask uint64
hasFreeKey bool // have 'free' key in the map?
freeVal valueType // value of 'free' key
}
func phiMix(x uint64) (h uint64) {
h = x * PHI
h ^= h >> 16
return
}
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 uint, fill float64) (s uint32) {
s = nextPowerOf2(uint32(math.Ceil(float64(exp) / fill)))
if s < 2 {
s = 2
}
return
}
// New returns a map initialized with preallocated `size` spaces and uses the stated fillFactor.
//
// When cardinality > capacity * fillFactor, the map will grow with 2x cap.
func New(size uint, fillFactor float64) (m *Map, err error) {
if fillFactor <= 0 || fillFactor >= 1 {
err = fmt.Errorf("FillFactor must be in (0, 1)")
return
}
if size == 0 {
size = 1
}
capacity := arraySize(size, fillFactor)
m = &Map{
keys: make([]keyType, capacity),
values: make([]valueType, capacity),
fillFactor: fillFactor,
threshold: int(math.Floor(float64(capacity>>1) * fillFactor)),
mask: uint64(capacity) - 1,
}
return
}
// Get value by key.
func (m *Map) Get(k keyType) (v valueType, found bool) {
if k != freeKey {
if ind := index(m.keys, m.mask, k); m.keys[ind] != freeKey {
v, found = m.values[ind], true
}
} else {
v, found = m.freeVal, m.hasFreeKey
}
return
}
// Set key - value, overwrite if needed.
func (m *Map) Set(k keyType, v valueType) {
if k != freeKey {
if store(m.keys, m.values, m.mask, k, v) {
if m.size++; m.size > m.threshold {
m.grow()
}
}
} else if m.freeVal = v; !m.hasFreeKey {
m.hasFreeKey = true
m.size++
}
}
func distance(center, ind, nMask uint64) (dis uint64) {
if ind >= center {
dis = ind - center
} else {
dis = nMask + 1 - center + ind
}
return
}
// Remove an element
func (m *Map) Remove(k keyType) {
var (
keys, values, mask = m.keys, m.values, m.mask
phi uint64
key keyType
)
if k == freeKey {
if m.hasFreeKey {
m.freeVal, m.hasFreeKey = nilValue, false
m.size--
}
return
}
// could remove?
if ind := index(keys, mask, k); keys[ind] != freeKey {
m.size--
// find start position of current block
startPos := uint64(ind)
findStartPosLoop:
if key = keys[startPos]; key != freeKey {
phi = phiMix(uint64(key)) & mask
// check if we could jump to phi right now
if phi != startPos && keys[phi] != freeKey {
startPos = phi
goto findStartPosLoop
}
// if not, just move back to 1 step
if startPos == 0 {
startPos = mask
} else {
startPos = (startPos - 1) & mask
}
goto findStartPosLoop
}
// set free at ind
keys[ind], values[ind] = freeKey, nilValue
// now startPos is the position where current block start
freePtr := uint64(ind) // index in uint64 type
dis := distance(startPos, freePtr, mask)
ptr := freePtr
loop:
// iterate each point in block and check if swapable
ptr = (ptr + 1) & mask
if key = keys[ptr]; key != freeKey {
if phi = phiMix(uint64(key)) & mask; distance(startPos, phi, mask) <= dis { // swapable
keys[freePtr], values[freePtr] = key, values[ptr]
keys[ptr], values[ptr] = freeKey, nilValue
freePtr = ptr
dis = distance(startPos, freePtr, mask)
}
goto loop
}
m.shrink()
}
}
// Iterate over map. Iteration will stop when handler return error.
func (m *Map) Iterate(handler func(keyType, valueType) error) (err error) {
if handler != nil {
values := m.values
for i, k := range m.keys {
if k != freeKey {
if err = handler(k, values[i]); err != nil {
return
}
}
}
if m.hasFreeKey {
err = handler(freeKey, m.freeVal)
}
}
return
}
// IterateAll iterates over map.
func (m *Map) IterateAll(handler func(keyType, valueType)) {
if handler != nil {
values := m.values
for i, k := range m.keys {
if k != freeKey {
handler(k, values[i])
}
}
if m.hasFreeKey {
handler(freeKey, m.freeVal)
}
}
}
// Clone creates new map, copied from original one.
func (m *Map) Clone() *Map {
c := *m
c.keys, c.values = make([]keyType, len(m.keys)), make([]valueType, len(m.keys))
copy(c.keys, m.keys)
copy(c.values, m.values)
return &c
}
// Size returns size of the map.
func (m *Map) Size() int {
return m.size
}
// Reset map, keep underlying allocated space.
func (m *Map) Reset() {
for i, k := range m.keys {
if k != freeKey {
m.keys[i], m.values[i] = freeKey, nilValue
}
}
m.hasFreeKey, m.freeVal = false, nilValue
m.size = 0
}
// (k != freeKey)
func index(keys []keyType, mask uint64, k keyType) (ind int) {
ptr := phiMix(uint64(k)) & mask
key := keys[ptr]
if key == k || key == freeKey {
ind = int(ptr)
return
}
loop:
ptr = (ptr + 1) & mask
key = keys[ptr]
if key == k || key == freeKey {
ind = int(ptr)
return
}
goto loop
}
// store on external keys/values collection (k != freeKey)
func store(keys []keyType, values []valueType, mask uint64, k keyType, v valueType) (isNew bool) {
ind := index(keys, mask, k)
isNew = keys[ind] == freeKey
if isNew {
keys[ind], values[ind] = k, v
} else {
values[ind] = v
}
return
}
func (m *Map) grow() {
originalLen := len(m.keys)
// new capacity
newCapacity := keyType(originalLen) << 1
// update threshold and mask
m.threshold = int(math.Floor(float64(newCapacity) * m.fillFactor))
m.mask = newCapacity - 1
mask := m.mask
// original keys, values
oriKeys, oriValues := m.keys, m.values
// write to new data
keys, values := make([]keyType, newCapacity), make([]valueType, newCapacity)
for i, oriKey := range oriKeys {
if oriKey != freeKey {
store(keys, values, mask, oriKey, oriValues[i])
}
}
m.keys, m.values = keys, values
}
func (m *Map) shrink() {
if m.size < m.threshold>>1 {
originalLen := len(m.keys)
// new capacity
newCapacity := keyType(originalLen) >> 1
// update threshold and mask
m.threshold = int(math.Floor(float64(newCapacity) * m.fillFactor))
m.mask = newCapacity - 1
mask := m.mask
// original keys, values
oriKeys, oriValues := m.keys, m.values
// write to new data
keys, values := make([]keyType, newCapacity), make([]valueType, newCapacity)
for i, oriKey := range oriKeys {
if oriKey != freeKey {
store(keys, values, mask, oriKey, oriValues[i])
}
}
m.keys, m.values = keys, values
}
}