-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync_map.go
More file actions
99 lines (85 loc) · 2.98 KB
/
sync_map.go
File metadata and controls
99 lines (85 loc) · 2.98 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
package syncmap
import "sync"
// Map is a generic type-safe interface to sync.Map.
// Map 是 sync.Map 的泛型类型安全接口。
type Map[K comparable, V any] struct {
mp *sync.Map
}
// New creates a new instance of Map.
// New 创建一个新的 Map 实例。
func New[K comparable, V any]() *Map[K, V] {
return NewMap[K, V]()
}
// NewMap creates a new instance of Map.
// NewMap 创建一个新的 Map 实例。
func NewMap[K comparable, V any]() *Map[K, V] {
return &Map[K, V]{mp: &sync.Map{}}
}
// CompareAndDelete compares the existing value with the given value and deletes the item when both match.
// CompareAndDelete 比较已存在的值与给定的值,当两者匹配时删除该项。
func (m *Map[K, V]) CompareAndDelete(key K, value V) bool {
return m.mp.CompareAndDelete(key, value)
}
// CompareAndSwap compares the existing value with the old value and swaps to the new value when both match.
// CompareAndSwap 将已存在的值与旧值比较,当两者匹配时替换成新值。
func (m *Map[K, V]) CompareAndSwap(key K, old, new V) bool {
return m.mp.CompareAndSwap(key, old, new)
}
// Delete removes the value associated with the given item.
// Delete 删除与给定项关联的值。
func (m *Map[K, V]) Delete(key K) {
m.mp.Delete(key)
}
// Load retrieves the value associated with the given item.
// Load 获取与给定项关联的值。
func (m *Map[K, V]) Load(key K) (res V, ok bool) {
value, ok := m.mp.Load(key)
if !ok {
return res, false
}
return value.(V), true
}
// LoadAndDelete retrieves and removes the value associated with the given item.
// LoadAndDelete 获取并删除与给定项关联的值。
func (m *Map[K, V]) LoadAndDelete(key K) (res V, ok bool) {
value, ok := m.mp.LoadAndDelete(key)
if !ok {
return res, false
}
return value.(V), true
}
// LoadOrStore retrieves the value associated with the item, and when the item does not exist, stores and returns the new value.
// LoadOrStore 获取与项关联的值,当该项不存在时,存储并返回新值。
func (m *Map[K, V]) LoadOrStore(key K, new V) (V, bool) {
value, ok := m.mp.LoadOrStore(key, new)
if !ok {
return value.(V), false
}
return value.(V), true
}
// Range iterates through each item in the map.
// Range 遍历 map 中的每一项。
func (m *Map[K, V]) Range(run func(key K, value V) bool) {
m.mp.Range(func(k, v any) bool {
return run(k.(K), v.(V))
})
}
// Store sets the value of the given item.
// Store 设置给定项的值。
func (m *Map[K, V]) Store(key K, value V) {
m.mp.Store(key, value)
}
// Clear deletes each item from the Map (needs Go 1.23+).
// Clear 删除 Map 中的每一项(需要 Go 1.23+)。
//func (m *Map[K, V]) Clear() {
// m.mp.Clear()
//}
// Swap replaces the value of the given item and returns the old value when it exists.
// Swap 替换给定项的值,并在存在时返回旧值。
func (m *Map[K, V]) Swap(key K, value V) (pre V, ok bool) {
previous, ok := m.mp.Swap(key, value)
if !ok {
return pre, false
}
return previous.(V), true
}