-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsafemapx.go
69 lines (59 loc) · 1.55 KB
/
safemapx.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
64
65
66
67
68
69
package mapx
import "sync"
// generic type map which is thread safe.
type safeMapx[Key comparable, Value any] struct {
mux sync.RWMutex
m map[Key]Value
}
// newSafeMapx creates a new instance of safeMapx.
func newSafeMapx[Key comparable, Value any]() *safeMapx[Key, Value] {
return &safeMapx[Key, Value]{m: make(map[Key]Value)}
}
// Get returns the value and a boolean indicating if the key exists in the map.
// Returns:
//
// Value: The value associated with the key.
// bool: true if the key exists in the map, false otherwise.
func (m *safeMapx[Key, Value]) Get(key Key) (Value, bool) {
m.mux.RLock()
defer m.mux.RUnlock()
val, ok := m.m[key]
return val, ok
}
// Set assigns a value to the key.
func (m *safeMapx[Key, Value]) Set(key Key, value Value) {
m.mux.Lock()
defer m.mux.Unlock()
m.m[key] = value
}
// Delete delete the value by key.
func (m *safeMapx[Key, Value]) Delete(key Key) {
m.mux.Lock()
defer m.mux.Unlock()
delete(m.m, key)
}
// Len returns the length of the safeMapx.
func (m *safeMapx[Key, Value]) Len() int {
m.mux.RLock()
defer m.mux.RUnlock()
return len(m.m)
}
// Range iterates over the map and calls the given function for each key-value pair.
func (m *safeMapx[Key, Value]) Range(fn func(Key, Value)) {
m.mux.RLock()
data := m.m
m.mux.RUnlock()
for k, v := range data {
fn(k, v)
}
}
// Keys returns a slice of keys in the safeMapx[Key, Value].
func (m *safeMapx[Key, Value]) Keys() []Key {
m.mux.RLock()
defer m.mux.RUnlock()
keys := make([]Key, 0, len(m.m))
for k := range m.m {
keys = append(keys, k)
}
return keys
}