-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.go
86 lines (75 loc) · 1.91 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Package syncmap provides a simple and generic map that is safe for concurrent use.
package syncmap
import (
"iter"
"sync"
)
// Map is a map that is safe for concurrent use.
type Map[K comparable, V any] struct {
kv map[K]V // kv is the underlying map.
mu sync.RWMutex
}
// New creates a new [Map] that is safe for concurrent use by multiple goroutines.
func New[K comparable, V any]() *Map[K, V] {
return &Map[K, V]{
kv: make(map[K]V),
}
}
// NewWithCapacity creates a new [Map] with the specified capacity hint.
//
// The capacity hint does not bound the map size, it will create a map with
// an initial space to hold the specified number of elements.
func NewWithCapacity[K comparable, V any](capacity int) *Map[K, V] {
return &Map[K, V]{
kv: make(map[K]V, capacity),
}
}
// Len returns the length of the [Map].
func (m *Map[K, V]) Len() int {
m.mu.RLock()
l := len(m.kv)
m.mu.RUnlock()
return l
}
// Get returns the value stored in the [Map] for a key, or the zero value if no
// value is present.
//
// The ok result indicates whether the value was found in the map.
func (m *Map[K, V]) Get(key K) (value V, ok bool) {
m.mu.RLock()
value, ok = m.kv[key]
m.mu.RUnlock()
return value, ok
}
// Set sets the value for a key in the [Map].
func (m *Map[K, V]) Set(key K, value V) {
m.mu.Lock()
m.kv[key] = value
m.mu.Unlock()
}
// Delete deletes the value for a key in the [Map].
func (m *Map[K, V]) Delete(key K) {
m.mu.Lock()
delete(m.kv, key)
m.mu.Unlock()
}
// Clear deletes all the entries in the [Map].
func (m *Map[K, V]) Clear() {
m.mu.Lock()
clear(m.kv)
m.mu.Unlock()
}
// All returns an iterator over key-value pairs from the [Map].
//
// Similar to the map type, the iteration order is not guaranteed.
func (m *Map[K, V]) All() iter.Seq2[K, V] {
return func(yield func(K, V) bool) {
m.mu.RLock()
defer m.mu.RUnlock()
for key, value := range m.kv {
if !yield(key, value) {
return
}
}
}
}