-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.go
58 lines (47 loc) · 932 Bytes
/
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
package standard
import "sync"
type Map[K comparable, V any] struct {
mutex_ sync.RWMutex
value_ map[K]V
}
func NewMap[K comparable, V any]() Map[K, V] {
return Map[K, V]{
mutex_: sync.RWMutex{},
value_: make(map[K]V),
}
}
func (m *Map[K, V]) Size() int {
m.mutex_.RLock()
defer m.mutex_.RUnlock()
return len(m.value_)
}
func (m *Map[K, V]) Clear() {
m.mutex_.Lock()
m.value_ = make(map[K]V)
m.mutex_.Unlock()
}
func (m *Map[K, V]) Insert(key K, value V) {
m.mutex_.Lock()
m.value_[key] = value
m.mutex_.Unlock()
}
func (m *Map[K, V]) Find(key K) (V, bool) {
m.mutex_.RLock()
defer m.mutex_.RUnlock()
v, ok := m.value_[key]
return v, ok
}
func (m *Map[K, V]) Range(f func(key K, value V) bool) {
m.mutex_.RLock()
defer m.mutex_.RUnlock()
for k, v := range m.value_ {
if !f(k, v) {
break
}
}
}
func (m *Map[K, V]) Erase(key K) {
m.mutex_.Lock()
delete(m.value_, key)
m.mutex_.Unlock()
}