-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.go
More file actions
368 lines (319 loc) · 8.77 KB
/
map.go
File metadata and controls
368 lines (319 loc) · 8.77 KB
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
package armap
import (
"fmt"
"unsafe"
"github.com/dolthub/maphash"
)
type bucketState byte
const (
stateEmpty bucketState = 0
stateUsed bucketState = 1
)
type bucket[K comparable, V any] struct {
key K
value V
state bucketState
}
type Map[K comparable, V any] struct {
arena Arena
hasher maphash.Hasher[K]
buckets []byte // Unsafe storage to skip GC scanning
bucketSize uintptr
count int
capacity int
loadFactor float64
}
func (m *Map[K, V]) getBucket(idx int) *bucket[K, V] {
offset := uintptr(idx) * m.bucketSize
return (*bucket[K, V])(unsafe.Pointer(&m.buckets[offset]))
}
func (m *Map[K, V]) Len() int {
return m.count
}
func (m *Map[K, V]) index(key K) int {
return int(m.hasher.Hash(key)) & (m.capacity - 1)
}
func (m *Map[K, V]) Set(key K, value V) (old V, found bool) {
if m.loadFactor < (float64(m.count) / float64(m.capacity)) {
m.resize(m.capacity * 2)
}
idx := m.index(key)
startIdx := idx
ka := NewTypeArena[K](m.arena)
va := NewTypeArena[V](m.arena)
for {
b := m.getBucket(idx)
if b.state == stateEmpty {
// Found empty slot, insert new
b.key = ka.Clone(key)
b.value = va.Clone(value)
b.state = stateUsed
m.count += 1
return
}
if b.state == stateUsed && b.key == key {
// Update existing
old = b.value
found = true
b.value = va.Clone(value)
return
}
idx = (idx + 1) & (m.capacity - 1)
if idx == startIdx {
m.resize(m.capacity * 2)
idx = m.index(key)
startIdx = idx
}
}
}
func (m *Map[K, V]) Get(key K) (val V, found bool) {
if m.capacity == 0 {
return
}
idx := m.index(key)
startIdx := idx
for {
b := m.getBucket(idx)
if b.state == stateEmpty {
return
}
if b.state == stateUsed && b.key == key {
return b.value, true
}
idx = (idx + 1) & (m.capacity - 1)
if idx == startIdx {
return
}
}
}
func (m *Map[K, V]) Scan(iter func(K, V) bool) {
if m.capacity == 0 {
return
}
for i := 0; i < m.capacity; i += 1 {
b := m.getBucket(i)
if b.state == stateUsed {
if iter(b.key, b.value) != true {
return
}
}
}
}
func (m *Map[K, V]) Delete(key K) (old V, found bool) {
if m.capacity == 0 {
return
}
idx := m.index(key)
startIdx := idx
for {
b := m.getBucket(idx)
if b.state == stateEmpty {
return
}
if b.state == stateUsed && b.key == key {
old = b.value
found = true
m.count -= 1
m.shiftBack(idx)
return
}
idx = (idx + 1) & (m.capacity - 1)
if idx == startIdx {
return
}
}
}
func (m *Map[K, V]) shiftBack(idx int) {
// Linear probing backward shift deletion
curr := idx
for {
next := (curr + 1) & (m.capacity - 1)
bNext := m.getBucket(next)
if bNext.state == stateEmpty {
// Found empty slot, clear current and return
bCurr := m.getBucket(curr)
bCurr.state = stateEmpty
var zeroK K
var zeroV V
bCurr.key = zeroK
bCurr.value = zeroV
return
}
// Check if the element at `next` belongs to the block of elements
// that should be shifted back to `curr`.
// It should be shifted if its ideal position (hash index) is <= curr.
// We must handle wrapping around the buffer.
ideal := m.index(bNext.key)
// Determine if `ideal` is logically "before or at" `curr` in the circular buffer.
// Valid position range for an element at `next` starts at `ideal` and goes up to `next`.
// We want to know if `curr` is within [ideal, next).
// Three cases due to wrap-around:
// 1. ideal <= next: range is [ideal, next]. normal case.
// We shift if curr is in [ideal, next) -> ideal <= curr < next.
// 2. ideal > next: range wraps. [ideal, cap) U [0, next].
// We shift if curr is in that range.
shouldShift := false
if ideal <= next {
if ideal <= curr && curr < next {
shouldShift = true
}
} else {
// Wrapped range
if ideal <= curr || curr < next {
shouldShift = true
}
}
if shouldShift {
// Move bucket data from next to curr
bCurr := m.getBucket(curr)
*bCurr = *bNext // Struct copy
curr = next
} else {
// Cannot shift this element, check the next one.
// curr remains empty (logically), we look for a candidate to fill it from further down.
// Wait, standard algorithm shifts bucket `next` to `curr` and then `next` becomes the new hole (`curr`).
// If we DON'T shift, the hole remains at `curr`, and we check `next+1`.
// BUT standard backward shift implementation usually is:
// Scan forward until we find an element that can fill the hole.
// If we find one, move it to hole, and the old position becomes the new hole.
// If we hit EMPTY, we are done.
// My implementation above was: "check `next`, if it fits, move it".
// If it DOES NOT fit (it belongs strictly to `next` or later due to its hash),
// we must skip it and check `next+1`?
// NO. In linear probing, the cluster must be contiguous.
// We cannot skip `next` and move `next+1` to `curr`, because that would break the probe chain for `next`.
// So if `next` cannot be shifted, NO subsequent element can be shifted past `next` to `curr`?
// Actually, if `next` is correctly placed (e.g. hash(next) == next), it stays.
// But maybe `next+1` collided and probed past `next`?
// Correct algorithm (Knuth):
// 1. Let i = index of empty slot.
// 2. j = (i + 1) % M.
// 3. If T[j] is empty, done.
// 4. r = hash(T[j].key).
// 5. If (j > i and (r <= i or r > j)) or (j < i and (r <= i and r > j)):
// T[i] = T[j]
// i = j
// 6. j = (j + 1) % M
// 7. Goto 3
// My logic:
// curr is hole.
// next is candidate.
// If bucket at next CAN be moved to curr (without violating property), move it and hole moves to next.
// If NOT, we just loop again with same curr, incrementing next?
// No, `next` is always `curr+1` in my loop.
// I need to scan `k = (curr+1)...` until I find a shifter or empty.
// Re-implementing inner loop properly.
scan := (curr + 1) & (m.capacity - 1)
for {
bScan := m.getBucket(scan)
if bScan.state == stateEmpty {
// End of cluster, clear hole and done
bCurr := m.getBucket(curr)
bCurr.state = stateEmpty
var zeroK K
var zeroV V
bCurr.key = zeroK
bCurr.value = zeroV
return
}
ideal := m.index(bScan.key)
// Check if `ideal` is NOT in cyclic interval (curr, scan].
// If hash(key) is "outside" the interval from hole to current pos,
// it means this element "wants" to be closer to hole (or at hole).
// Interval (curr, scan] means:
// if curr < scan: ideal <= curr OR ideal > scan
// if scan < curr: ideal <= curr AND ideal > scan
inInterval := false
if curr < scan {
if curr < ideal && ideal <= scan {
inInterval = true
}
} else {
if curr < ideal || ideal <= scan {
inInterval = true
}
}
if !inInterval {
// Found a candidate to fill `curr`
bCurr := m.getBucket(curr)
*bCurr = *bScan
curr = scan // Hole moves to `scan`
break // Break inner loop, continue outer `shiftBack` with new `curr`
}
scan = (scan + 1) & (m.capacity - 1)
}
}
}
}
func (m *Map[K, V]) resize(newCapacity int) {
oldBuckets := m.buckets
oldCapacity := m.capacity // Save old capacity before updating
m.capacity = newCapacity
// Allocate new buckets as raw bytes
var b bucket[K, V]
m.bucketSize = unsafe.Sizeof(b)
totalSize := uintptr(newCapacity) * m.bucketSize
m.buckets = make([]byte, totalSize)
m.count = 0
if 0 < oldCapacity {
oldBucketSize := m.bucketSize
for i := 0; i < oldCapacity; i += 1 {
offset := uintptr(i) * oldBucketSize
b := (*bucket[K, V])(unsafe.Pointer(&oldBuckets[offset]))
if b.state == stateUsed {
m.insertRaw(b.key, b.value)
}
}
}
}
func (m *Map[K, V]) insertRaw(key K, value V) {
idx := m.index(key)
for {
b := m.getBucket(idx)
if b.state == stateEmpty {
b.key = key
b.value = value
b.state = stateUsed
m.count += 1
return
}
idx = (idx + 1) & (m.capacity - 1)
}
}
func (m *Map[K, V]) Clear() {
var b bucket[K, V]
m.bucketSize = unsafe.Sizeof(b)
totalSize := uintptr(m.capacity) * m.bucketSize
m.buckets = make([]byte, totalSize)
m.count = 0
}
func NewMap[K comparable, V any](arena Arena, funcs ...OptionFunc) *Map[K, V] {
checkType[K](arena)
checkType[V](arena)
opt := newOption()
for _, fn := range funcs {
fn(opt)
}
capacity := 1
for capacity < opt.capacity {
capacity *= 2
}
m := &Map[K, V]{
arena: arena,
hasher: maphash.NewHasher[K](),
capacity: 0, // Initialize to 0 so resize treats it as fresh
loadFactor: opt.loadFactor,
}
m.resize(capacity)
return m
}
func checkType[T any](arena Arena) {
defer func() {
if r := recover(); r != nil {
panic(fmt.Sprintf("armap: type %T cannot be used in Map (likely due to unexported fields preventing clone): %v", *new(T), r))
}
}()
ta := NewTypeArena[T](arena)
var zero T
_ = ta.Clone(zero)
}