-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsyncmap.go
More file actions
109 lines (94 loc) · 2.86 KB
/
syncmap.go
File metadata and controls
109 lines (94 loc) · 2.86 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
// A typed, thread-safe map built on top of sync.Map, using generics.
package syncmap
import "sync"
// SyncMap provides a thread-safe generic map built on top of sync.Map.
type SyncMap[K comparable, V any] struct {
m sync.Map
}
// NewSyncMap creates a new thread-safe generic map
func NewSyncMap[K comparable, V any]() *SyncMap[K, V] {
return &SyncMap[K, V]{}
}
// Store sets a key-value pair
func (sm *SyncMap[K, V]) Store(key K, value V) {
sm.m.Store(key, value)
}
// Load gets a value by key, returns value and whether it was found
func (sm *SyncMap[K, V]) Load(key K) (V, bool) {
if val, ok := sm.m.Load(key); ok {
return val.(V), true
}
var zero V
return zero, false
}
// LoadOrStore gets existing value or stores new one, returns actual value and whether it was loaded
func (sm *SyncMap[K, V]) LoadOrStore(key K, value V) (V, bool) {
actual, loaded := sm.m.LoadOrStore(key, value)
return actual.(V), loaded
}
// LoadAndDelete gets existing value and deletes it, returns value and whether it was loaded (deleted)
func (sm *SyncMap[K, V]) LoadAndDelete(key K) (V, bool) {
val, loaded := sm.m.LoadAndDelete(key)
if loaded {
return val.(V), true
}
var zero V
return zero, false
}
// Delete removes a key
func (sm *SyncMap[K, V]) Delete(key K) {
sm.m.Delete(key)
}
// Swap swaps the value for a key and returns the previous value if any.
// The loaded result reports whether the key was present.
func (sm *SyncMap[K, V]) Swap(key K, value V) (previous V, loaded bool) {
prev, loaded := sm.m.Swap(key, value)
if loaded {
return prev.(V), true
}
var zero V
return zero, false
}
// CompareAndSwap swaps the old and new values for key if the value stored in the map is equal to old.
// The old value must be of a comparable type.
func (sm *SyncMap[K, V]) CompareAndSwap(key K, old V, new V) (swapped bool) {
return sm.m.CompareAndSwap(key, old, new)
}
// CompareAndDelete deletes the entry for key if its value is equal to old.
// The old value must be of a comparable type.
func (sm *SyncMap[K, V]) CompareAndDelete(key K, old V) (deleted bool) {
return sm.m.CompareAndDelete(key, old)
}
// Range calls fn for each key-value pair. Returning false quits the iteration
func (sm *SyncMap[K, V]) Range(fn func(key K, value V) bool) {
sm.m.Range(func(key, value any) bool {
return fn(key.(K), value.(V))
})
}
// Keys returns all keys as a slice
func (sm *SyncMap[K, V]) Keys() []K {
var keys []K
sm.Range(func(key K, value V) bool {
keys = append(keys, key)
return true
})
return keys
}
// Values returns all values as a slice
func (sm *SyncMap[K, V]) Values() []V {
var values []V
sm.Range(func(key K, value V) bool {
values = append(values, value)
return true
})
return values
}
// Len returns the number of items (expensive operation)
func (sm *SyncMap[K, V]) Len() int {
count := 0
sm.Range(func(key K, value V) bool {
count++
return true
})
return count
}