-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorderedmap.go
More file actions
417 lines (354 loc) · 9.96 KB
/
orderedmap.go
File metadata and controls
417 lines (354 loc) · 9.96 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
// Package orderedmap provides a thread-safe, generic ordered map that maintains insertion order
// while providing O(1) lookups. Unlike Go's built-in map, OrderedMap iterates in the order items
// were added, enabling predictable, deterministic behavior.
package orderedmap
import "sync"
// node represents an element in the doubly-linked list
type node[K comparable, V any] struct {
key K
value V
prev *node[K, V]
next *node[K, V]
}
// OrderedMap maintains insertion order while providing O(1) lookups, deletes, and moves
//
// Thread-safe: All operations use internal locking
// Zero value is usable: var om OrderedMap[K,V] works without initialization
// All operations are O(1) except Range which is O(n)
// Range/RangeBreak: Iterate over snapshot at call time, not live data (safe for re-entrant calls)
type OrderedMap[K comparable, V any] struct {
mu sync.RWMutex // Protects all fields
index map[K]*node[K, V] // Key -> node pointer (for O(1) lookup)
head *node[K, V] // First node in list
tail *node[K, V] // Last node in list
len int // Number of entries
}
// NewOrderedMap creates a new ordered map
// Note: The zero value is also usable without calling this constructor
func NewOrderedMap[K comparable, V any]() *OrderedMap[K, V] {
return &OrderedMap[K, V]{
index: make(map[K]*node[K, V]),
}
}
// ensureInit initializes the index map if needed (makes zero value usable)
func (om *OrderedMap[K, V]) ensureInit() {
if om.index == nil {
om.index = make(map[K]*node[K, V])
}
}
// appendToEnd adds a node to the end of the list
// Caller must hold om.mu.Lock()
func (om *OrderedMap[K, V]) appendToEnd(n *node[K, V]) {
if om.tail == nil {
om.head, om.tail = n, n
return
}
n.prev = om.tail
n.next = nil
om.tail.next = n
om.tail = n
}
// Set adds or updates a key-value pair (O(1))
// If key exists, updates the value in-place (preserves order)
// If key is new, appends to the end
func (om *OrderedMap[K, V]) Set(key K, value V) {
om.mu.Lock()
defer om.mu.Unlock()
om.ensureInit()
if n, exists := om.index[key]; exists {
// Update existing value in-place
n.value = value
return
}
// Create new node and append
n := &node[K, V]{
key: key,
value: value,
}
om.appendToEnd(n)
om.index[key] = n
om.len++
}
// Get retrieves a value by key (O(1) lookup)
func (om *OrderedMap[K, V]) Get(key K) (V, bool) {
om.mu.RLock()
defer om.mu.RUnlock()
if n, exists := om.index[key]; exists {
return n.value, true
}
var zero V
return zero, false
}
// Has checks if a key exists (O(1) lookup)
func (om *OrderedMap[K, V]) Has(key K) bool {
om.mu.RLock()
defer om.mu.RUnlock()
_, exists := om.index[key]
return exists
}
// Len returns the number of entries (O(1))
func (om *OrderedMap[K, V]) Len() int {
om.mu.RLock()
defer om.mu.RUnlock()
return om.len
}
// Delete removes a key, preserving insertion order (O(1) complexity)
// Returns true if the key existed and was deleted
func (om *OrderedMap[K, V]) Delete(key K) bool {
om.mu.Lock()
defer om.mu.Unlock()
n, exists := om.index[key]
if !exists {
return false
}
om.removeNode(n)
delete(om.index, key)
om.len--
return true
}
// DeleteWithValue removes a key and returns its value (O(1) complexity)
// Returns the value and true if the key existed, or zero value and false otherwise
func (om *OrderedMap[K, V]) DeleteWithValue(key K) (V, bool) {
om.mu.Lock()
defer om.mu.Unlock()
n, exists := om.index[key]
if !exists {
var zero V
return zero, false
}
om.removeNode(n)
delete(om.index, key)
om.len--
return n.value, true
}
// removeNode removes a node from the doubly-linked list
// Caller must hold om.mu.Lock()
func (om *OrderedMap[K, V]) removeNode(n *node[K, V]) {
if p := n.prev; p != nil {
p.next = n.next
} else {
om.head = n.next
}
if q := n.next; q != nil {
q.prev = n.prev
} else {
om.tail = n.prev
}
n.prev, n.next = nil, nil // help GC
}
// Clear removes all entries while preserving allocated capacity
func (om *OrderedMap[K, V]) Clear() {
om.mu.Lock()
defer om.mu.Unlock()
// Clear map
for k := range om.index {
delete(om.index, k)
}
om.head = nil
om.tail = nil
om.len = 0
}
// Reset removes all entries and frees allocated memory
func (om *OrderedMap[K, V]) Reset() {
om.mu.Lock()
defer om.mu.Unlock()
om.index = nil
om.head = nil
om.tail = nil
om.len = 0
}
// Range iterates over all entries IN INSERTION ORDER at the time of call
// Takes a snapshot before iterating, so fn can safely call other methods without deadlock
//
// Note: Snapshots both keys and values. Mutations to the value V after the snapshot
// are not reflected in the iteration. For pointer or reference types, changes to the
// underlying data will be visible.
func (om *OrderedMap[K, V]) Range(fn func(key K, value V)) {
om.mu.RLock()
// Snapshot keys and values to avoid holding lock during callbacks
keys := make([]K, 0, om.len)
values := make([]V, 0, om.len)
for n := om.head; n != nil; n = n.next {
keys = append(keys, n.key)
values = append(values, n.value)
}
om.mu.RUnlock()
for i := range keys {
fn(keys[i], values[i])
}
}
// RangeBreak iterates in insertion order and stops when fn returns false
// Takes a snapshot before iterating, so fn can safely call other methods without deadlock
//
// Note: Snapshots both keys and values. Mutations to the value V after the snapshot
// are not reflected in the iteration. For pointer or reference types, changes to the
// underlying data will be visible.
func (om *OrderedMap[K, V]) RangeBreak(fn func(key K, value V) bool) {
om.mu.RLock()
// Snapshot keys and values to avoid holding lock during callbacks
keys := make([]K, 0, om.len)
values := make([]V, 0, om.len)
for n := om.head; n != nil; n = n.next {
keys = append(keys, n.key)
values = append(values, n.value)
}
om.mu.RUnlock()
for i := range keys {
if !fn(keys[i], values[i]) {
return
}
}
}
// RangeLocked iterates while holding the RLock (no snapshot allocation)
// WARNING: fn MUST NOT call any OrderedMap methods or deadlock will occur
// Use Range() if you need to modify the map during iteration
func (om *OrderedMap[K, V]) RangeLocked(fn func(key K, value V)) {
om.mu.RLock()
defer om.mu.RUnlock()
for n := om.head; n != nil; n = n.next {
fn(n.key, n.value)
}
}
// RangeBreakLocked iterates while holding the RLock and stops when fn returns false
// WARNING: fn MUST NOT call any OrderedMap methods or deadlock will occur
// Use RangeBreak() if you need to modify the map during iteration
func (om *OrderedMap[K, V]) RangeBreakLocked(fn func(key K, value V) bool) {
om.mu.RLock()
defer om.mu.RUnlock()
for n := om.head; n != nil; n = n.next {
if !fn(n.key, n.value) {
return
}
}
}
// Keys returns a copy of all keys in insertion order
func (om *OrderedMap[K, V]) Keys() []K {
om.mu.RLock()
defer om.mu.RUnlock()
keys := make([]K, 0, om.len)
for n := om.head; n != nil; n = n.next {
keys = append(keys, n.key)
}
return keys
}
// Values returns a copy of all values in insertion order
func (om *OrderedMap[K, V]) Values() []V {
om.mu.RLock()
defer om.mu.RUnlock()
values := make([]V, 0, om.len)
for n := om.head; n != nil; n = n.next {
values = append(values, n.value)
}
return values
}
// Front returns the first key-value pair, or zero values if empty
func (om *OrderedMap[K, V]) Front() (K, V, bool) {
om.mu.RLock()
defer om.mu.RUnlock()
if om.head == nil {
var zeroK K
var zeroV V
return zeroK, zeroV, false
}
return om.head.key, om.head.value, true
}
// Back returns the last key-value pair, or zero values if empty
func (om *OrderedMap[K, V]) Back() (K, V, bool) {
om.mu.RLock()
defer om.mu.RUnlock()
if om.tail == nil {
var zeroK K
var zeroV V
return zeroK, zeroV, false
}
return om.tail.key, om.tail.value, true
}
// At returns the key-value pair at the given index, or zero values if out of bounds (O(n))
func (om *OrderedMap[K, V]) At(i int) (K, V, bool) {
om.mu.RLock()
defer om.mu.RUnlock()
if i < 0 || i >= om.len {
var zeroK K
var zeroV V
return zeroK, zeroV, false
}
n := om.head
for j := 0; j < i; j++ {
n = n.next
}
return n.key, n.value, true
}
// PopFront removes and returns the first key-value pair (O(1) complexity)
// Returns zero values and false if the map is empty
func (om *OrderedMap[K, V]) PopFront() (K, V, bool) {
om.mu.Lock()
defer om.mu.Unlock()
if om.head == nil {
var zeroK K
var zeroV V
return zeroK, zeroV, false
}
n := om.head
om.removeNode(n)
delete(om.index, n.key)
om.len--
return n.key, n.value, true
}
// PopBack removes and returns the last key-value pair (O(1) complexity)
// Returns zero values and false if the map is empty
func (om *OrderedMap[K, V]) PopBack() (K, V, bool) {
om.mu.Lock()
defer om.mu.Unlock()
if om.tail == nil {
var zeroK K
var zeroV V
return zeroK, zeroV, false
}
n := om.tail
om.removeNode(n)
delete(om.index, n.key)
om.len--
return n.key, n.value, true
}
// MoveToEnd moves the given key to the end of the insertion order (O(1) complexity)
// Returns true if the key existed and was moved, false otherwise
// Useful for implementing LRU-like behavior
func (om *OrderedMap[K, V]) MoveToEnd(key K) bool {
om.mu.Lock()
defer om.mu.Unlock()
n, exists := om.index[key]
if !exists {
return false
}
// Already at end
if n == om.tail {
return true
}
om.removeNode(n)
om.appendToEnd(n)
return true
}
// GetOrSet returns the existing value if key exists, otherwise calls mk to create
// a new value, stores it, and returns it. The second return value indicates whether
// the value already existed (true) or was newly created (false).
//
// WARNING: mk is called while holding the write lock. Keep it fast and simple.
// This avoids double lookups when implementing caching patterns.
func (om *OrderedMap[K, V]) GetOrSet(key K, mk func() V) (V, bool) {
om.mu.Lock()
defer om.mu.Unlock()
om.ensureInit()
if n, exists := om.index[key]; exists {
return n.value, true
}
// Create new node and append
v := mk()
n := &node[K, V]{
key: key,
value: v,
}
om.appendToEnd(n)
om.index[key] = n
om.len++
return v, false
}