-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.go
63 lines (51 loc) · 1.3 KB
/
map.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
package syncmap
import "sync"
// Map wraps sync.Map to provide generic type constraints.
type Map[K comparable, V any] struct {
items sync.Map
}
func (m *Map[K, V]) Clear() {
m.items.Clear()
}
func (m *Map[K, V]) CompareAndDelete(key K, old V) (deleted bool) {
return m.items.CompareAndDelete(key, old)
}
func (m *Map[K, V]) CompareAndSwap(key K, old, new V) (swapped bool) {
return m.items.CompareAndSwap(key, old, new)
}
func (m *Map[K, V]) Delete(key K) {
m.items.Delete(key)
}
func (m *Map[K, V]) Load(key K) (value V, ok bool) {
v, ok := m.items.Load(key)
if !ok {
return value, false
}
return v.(V), true
}
func (m *Map[K, V]) LoadAndDelete(key K) (value V, loaded bool) {
v, loaded := m.items.LoadAndDelete(key)
if !loaded {
return value, false
}
return v.(V), loaded
}
func (m *Map[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool) {
a, loaded := m.items.LoadOrStore(key, value)
return a.(V), loaded
}
func (m *Map[K, V]) Range(f func(key K, value V) bool) {
m.items.Range(func(key, value any) bool {
return f(key.(K), value.(V))
})
}
func (m *Map[K, V]) Store(key K, value V) {
m.items.Store(key, value)
}
func (m *Map[K, V]) Swap(key K, value V) (previous V, loaded bool) {
p, loaded := m.items.Swap(key, value)
if !loaded {
return previous, false
}
return p.(V), loaded
}