-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhypermap.go
More file actions
542 lines (507 loc) · 12 KB
/
Copy pathhypermap.go
File metadata and controls
542 lines (507 loc) · 12 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
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
// Package hypermap provides compact, generic insertion-ordered maps for
// allocation-conscious Go services.
package hypermap
import (
"iter"
"strings"
)
const queryEscapeHex = "0123456789ABCDEF"
// orderedMap stores its entries in a slice whose first element (index 0) is a
// permanent sentinel. The sentinel anchors a circular doubly-linked list, so
// entries[0].next is the head, entries[0].prev is the tail, and an empty list
// is the sentinel pointing at itself. Anchoring the list this way removes the
// need for separate head/tail fields and lets link/unlink run without
// boundary checks. Deleted slots are recycled through a singly-linked free
// chain rooted at free, so steady-state insert/delete reuses storage instead
// of growing it.
type orderedMap[T comparable, T2 any] struct {
index map[T]int
entries []entries[T, T2]
free int
}
type entries[T comparable, T2 any] struct {
key T
value T2
prev, next int
}
// Map is an insertion-ordered hash map.
//
// The zero value is ready to use. Single-key lookup, insertion, deletion,
// movement, and front/back access are O(1) average. Use [New] or [Map.Init]
// with the maximum live entry count to keep steady-state writes within existing
// storage. Do not copy a Map after first use. Map does not synchronize access;
// callers that share a [Map] across goroutines need external synchronization.
// Use independent [Map] values per shard or worker when write contention is
// expected.
type Map[T comparable, T2 any] orderedMap[T, T2]
// New returns an initialized [Map] with storage reserved for up to capacity
// live entries.
func New[T comparable, T2 any](capacity int) Map[T, T2] {
var m Map[T, T2]
m.Init(capacity)
return m
}
// Init prepares m with storage reserved for up to capacity live entries.
func (m *Map[T, T2]) Init(capacity int) {
if capacity < 0 {
panic("hypermap: negative ordered map capacity")
}
var zero Map[T, T2]
*m = zero
if capacity == 0 {
return
}
m.index = make(map[T]int, capacity)
m.entries = make([]entries[T, T2], 1, capacity+1)
}
// Len reports the number of entries in m.
func (m *Map[T, T2]) Len() int {
return len(m.index)
}
// Cap reports the maximum live entries m can hold before growing its slot slice.
func (m *Map[T, T2]) Cap() int {
c := cap(m.entries)
if c == 0 {
return 0
}
return c - 1
}
// Get returns the value for key and reports whether key is present.
func (m *Map[T, T2]) Get(key T) (T2, bool) {
ref := m.index[key]
if ref == 0 {
var zero T2
return zero, false
}
return m.entries[ref].value, true
}
// Has reports whether key is present in m.
func (m *Map[T, T2]) Has(key T) bool {
return m.index[key] != 0
}
// Set stores value for key and returns the replaced value when key is present.
func (m *Map[T, T2]) Set(key T, value T2) (T2, bool) {
if ref := m.index[key]; ref != 0 {
var (
entry = &m.entries[ref]
old = entry.value
)
entry.value = value
return old, true
}
if m.index == nil {
m.index = make(map[T]int, 1)
}
if len(m.entries) == 0 {
m.entries = append(m.entries, entries[T, T2]{}) // sentinel
}
ref := m.addSlot(key, value)
m.index[key] = ref
m.linkBack(ref)
var zero T2
return zero, false
}
// Delete removes key and returns the removed value when key is present.
func (m *Map[T, T2]) Delete(key T) (T2, bool) {
ref := m.index[key]
if ref == 0 {
var zero T2
return zero, false
}
old := m.entries[ref].value
m.deleteRef(ref, &m.entries[ref], key)
return old, true
}
// Clear removes all entries while keeping allocated storage for reuse.
func (m *Map[T, T2]) Clear() {
clear(m.index)
if len(m.entries) != 0 {
clear(m.entries)
m.entries = m.entries[:1] // keep the zeroed sentinel
}
m.free = 0
}
// Reset removes all entries and releases m backing storage.
func (m *Map[T, T2]) Reset() {
var zero Map[T, T2]
*m = zero
}
// Front returns the first key and value in insertion order and reports whether
// m is non-empty.
func (m *Map[T, T2]) Front() (T, T2, bool) {
if len(m.entries) == 0 {
var (
zeroKey T
zeroValue T2
)
return zeroKey, zeroValue, false
}
ref := m.entries[0].next
if ref == 0 {
var (
zeroKey T
zeroValue T2
)
return zeroKey, zeroValue, false
}
entry := &m.entries[ref]
return entry.key, entry.value, true
}
// Back returns the last key and value in insertion order and reports whether m
// is non-empty.
func (m *Map[T, T2]) Back() (T, T2, bool) {
if len(m.entries) == 0 {
var (
zeroKey T
zeroValue T2
)
return zeroKey, zeroValue, false
}
ref := m.entries[0].prev
if ref == 0 {
var (
zeroKey T
zeroValue T2
)
return zeroKey, zeroValue, false
}
entry := &m.entries[ref]
return entry.key, entry.value, true
}
// Next returns the key and value after key in insertion order and reports
// whether key has a successor.
func (m *Map[T, T2]) Next(key T) (T, T2, bool) {
ref := m.index[key]
if ref == 0 {
var (
zeroKey T
zeroValue T2
)
return zeroKey, zeroValue, false
}
next := m.entries[ref].next
if next == 0 {
var (
zeroKey T
zeroValue T2
)
return zeroKey, zeroValue, false
}
entry := &m.entries[next]
return entry.key, entry.value, true
}
// Prev returns the key and value before key in insertion order and reports
// whether key has a predecessor.
func (m *Map[T, T2]) Prev(key T) (T, T2, bool) {
ref := m.index[key]
if ref == 0 {
var (
zeroKey T
zeroValue T2
)
return zeroKey, zeroValue, false
}
prev := m.entries[ref].prev
if prev == 0 {
var (
zeroKey T
zeroValue T2
)
return zeroKey, zeroValue, false
}
entry := &m.entries[prev]
return entry.key, entry.value, true
}
// MoveToFront moves key to the front of m when key is present.
func (m *Map[T, T2]) MoveToFront(key T) bool {
ref := m.index[key]
if ref == 0 {
return false
}
var (
e = m.entries
head = e[0].next
)
if head == ref {
return true
}
var (
entry = &e[ref]
prev, next = entry.prev, entry.next
)
e[prev].next = next
e[next].prev = prev
entry.prev = 0
entry.next = head
e[head].prev = ref
e[0].next = ref
return true
}
// MoveToBack moves key to the back of m when key is present.
func (m *Map[T, T2]) MoveToBack(key T) bool {
ref := m.index[key]
if ref == 0 {
return false
}
var (
e = m.entries
tail = e[0].prev
)
if tail == ref {
return true
}
var (
entry = &e[ref]
prev, next = entry.prev, entry.next
)
e[prev].next = next
e[next].prev = prev
entry.next = 0
entry.prev = tail
e[tail].next = ref
e[0].prev = ref
return true
}
// PopFront removes and returns the first key and value in insertion order and
// reports whether an entry is removed.
func (m *Map[T, T2]) PopFront() (T, T2, bool) {
if len(m.entries) == 0 {
var (
zeroKey T
zeroValue T2
)
return zeroKey, zeroValue, false
}
ref := m.entries[0].next
if ref == 0 {
var (
zeroKey T
zeroValue T2
)
return zeroKey, zeroValue, false
}
var (
entry = &m.entries[ref]
key, value = entry.key, entry.value
)
m.deleteRef(ref, entry, key)
return key, value, true
}
// PopBack removes and returns the last key and value in insertion order and
// reports whether an entry is removed.
func (m *Map[T, T2]) PopBack() (T, T2, bool) {
if len(m.entries) == 0 {
var (
zeroKey T
zeroValue T2
)
return zeroKey, zeroValue, false
}
ref := m.entries[0].prev
if ref == 0 {
var (
zeroKey T
zeroValue T2
)
return zeroKey, zeroValue, false
}
var (
entry = &m.entries[ref]
key, value = entry.key, entry.value
)
m.deleteRef(ref, entry, key)
return key, value, true
}
// Range returns an [iter.Seq2] over each key and value in insertion order.
// Range leaves remaining iteration behavior unspecified when yield mutates m.
func (m *Map[T, T2]) Range() iter.Seq2[T, T2] {
return func(yield func(T, T2) bool) {
e := m.entries
if len(e) == 0 {
return
}
for ref := e[0].next; ref != 0; {
var (
entry = &e[ref]
next = entry.next
)
if !yield(entry.key, entry.value) {
return
}
ref = next
}
}
}
// QueryMap is a [Map] specialized for URL query parameters. It maps string keys
// to repeated string values and adds [QueryMap.Encode]. Every [Map] method is
// promoted for ordered string keys and []string values.
type QueryMap struct {
Map[string, []string]
}
// NewQueryMap returns an initialized [QueryMap] with storage reserved for up to
// capacity live entries.
func NewQueryMap(capacity int) QueryMap {
return QueryMap{New[string, []string](capacity)}
}
// Encode returns qm encoded as URL query parameters in key insertion order.
// Values for each key are encoded in slice order. A nil or empty [QueryMap]
// encodes to an empty string.
func (qm *QueryMap) Encode() string {
if qm == nil || qm.Len() == 0 {
return ""
}
var (
e = qm.entries
size int
)
for ref := e[0].next; ref != 0; {
var (
entry = &e[ref]
next = entry.next
values = entry.value
)
if len(values) == 0 {
ref = next
continue
}
keySize := queryEscapedLen(entry.key)
for _, value := range values {
if size > 0 {
size++
}
size += keySize + 1 + queryEscapedLen(value)
}
ref = next
}
if size == 0 {
return ""
}
var sb strings.Builder
sb.Grow(size)
for ref := e[0].next; ref != 0; {
var (
entry = &e[ref]
next = entry.next
)
for _, value := range entry.value {
if sb.Len() > 0 {
sb.WriteByte('&')
}
appendQueryEscaped(&sb, entry.key)
sb.WriteByte('=')
appendQueryEscaped(&sb, value)
}
ref = next
}
return sb.String()
}
// queryEscape[c] reports whether byte c must be percent-escaped in a query
// component. Precomputing it turns the per-byte classification in the encode
// hot loops into a single branch-free load. Indexing a [256]bool with a byte
// needs no bounds check.
var queryEscape = func() (t [256]bool) {
for c := range len(t) {
t[c] = shouldEscapeQuery(byte(c))
}
return t
}()
func queryEscapedLen(s string) int {
n := len(s)
for i := 0; i < len(s); i++ {
c := s[i]
if c != ' ' && queryEscape[c] {
n += 2
}
}
return n
}
func appendQueryEscaped(sb *strings.Builder, s string) {
var start int
for i := 0; i < len(s); i++ {
c := s[i]
if !queryEscape[c] {
continue
}
if start < i {
sb.WriteString(s[start:i])
}
if c == ' ' {
sb.WriteByte('+')
} else {
sb.WriteByte('%')
sb.WriteByte(queryEscapeHex[c>>4])
sb.WriteByte(queryEscapeHex[c&15])
}
start = i + 1
}
if start < len(s) {
sb.WriteString(s[start:])
}
}
func shouldEscapeQuery(c byte) bool {
switch {
case 'a' <= c && c <= 'z':
return false
case 'A' <= c && c <= 'Z':
return false
case '0' <= c && c <= '9':
return false
}
switch c {
case '-', '_', '.', '~':
return false
}
return true
}
// addSlot reserves a slot for key/value and returns its slice index, reusing a
// freed slot when one is available. The caller links the returned slot into the
// list.
func (m *Map[T, T2]) addSlot(key T, value T2) int {
if m.free != 0 {
var (
ref = m.free
entry = &m.entries[ref]
)
m.free = entry.next
entry.key = key
entry.value = value
entry.prev = 0
entry.next = 0
return ref
}
m.entries = append(m.entries, entries[T, T2]{key: key, value: value})
return len(m.entries) - 1
}
// linkBack appends ref to the tail of the circular list.
func (m *Map[T, T2]) linkBack(ref int) {
var (
tail = m.entries[0].prev
entry = &m.entries[ref]
)
entry.prev = tail
entry.next = 0
m.entries[tail].next = ref
m.entries[0].prev = ref
}
// unlink removes entry from the list. The circular sentinel makes both the
// head and tail cases fall through to the same two writes, so no branching is
// needed.
func (m *Map[T, T2]) unlink(entry *entries[T, T2]) {
m.entries[entry.prev].next = entry.next
m.entries[entry.next].prev = entry.prev
}
// deleteRef removes entry (the slot at ref) from the list and pushes its slot
// onto the free chain for reuse by a later insert.
func (m *Map[T, T2]) deleteRef(ref int, entry *entries[T, T2], key T) {
delete(m.index, key)
m.unlink(entry)
var (
zeroKey T
zeroValue T2
)
entry.key = zeroKey
entry.value = zeroValue
entry.prev = 0
entry.next = m.free
m.free = ref
}