-
Notifications
You must be signed in to change notification settings - Fork 0
/
cowmap.go
143 lines (123 loc) · 2.16 KB
/
cowmap.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package container
import (
"sync"
"sync/atomic"
its "github.com/wrmsr/bane/core/iterators"
"github.com/wrmsr/bane/core/maps"
bt "github.com/wrmsr/bane/core/types"
)
//
type CowMap[K comparable, V any] struct {
mtx sync.Mutex
r atomic.Value
}
func NewCowMap[K comparable, V any](it bt.Iterable[bt.Kv[K, V]]) *CowMap[K, V] {
cm := &CowMap[K, V]{}
if it != nil {
m := make(map[K]V)
for it := it.Iterate(); it.HasNext(); {
c := it.Next()
m[c.K] = c.V
}
cm.r.Store(m)
}
return cm
}
func (m *CowMap[K, V]) get() map[K]V {
if r := m.r.Load(); r != nil {
return r.(map[K]V)
}
return nil
}
func (m *CowMap[K, V]) Clone() *CowMap[K, V] {
r := &CowMap[K, V]{}
if v := m.r.Load(); v != nil {
r.r.Store(v)
}
return r
}
var _ SyncMutMap[int, any] = &CowMap[int, any]{}
func (m *CowMap[K, V]) isMut() {}
func (m *CowMap[K, V]) isSync() {}
func (m *CowMap[K, V]) Len() int {
r := m.get()
if r == nil {
return 0
}
return len(r)
}
func (m *CowMap[K, V]) Contains(k K) bool {
r := m.get()
if r == nil {
return false
}
_, ok := r[k]
return ok
}
func (m *CowMap[K, V]) Get(k K) V {
r := m.get()
if r == nil {
var z V
return z
}
return r[k]
}
func (m *CowMap[K, V]) TryGet(k K) (V, bool) {
r := m.get()
if r == nil {
var z V
return z, false
}
v, ok := r[k]
return v, ok
}
func (m *CowMap[K, V]) Iterate() bt.Iterator[bt.Kv[K, V]] {
r := m.get()
if r == nil {
return its.Empty[bt.Kv[K, V]]().Iterate()
}
return its.OfMap[K, V](r).Iterate()
}
func (m *CowMap[K, V]) ForEach(fn func(bt.Kv[K, V]) bool) bool {
r := m.get()
if r == nil {
return true
}
for k, v := range r {
if !fn(bt.KvOf(k, v)) {
return false
}
}
return true
}
func (m *CowMap[K, V]) mut(fn func(map[K]V)) {
m.mtx.Lock()
defer m.mtx.Unlock()
r := m.get()
if r == nil {
r = make(map[K]V)
}
nm := maps.Clone(r)
fn(nm)
m.r.Store(nm)
}
func (m *CowMap[K, V]) Put(k K, v V) {
m.mut(func(m map[K]V) {
m[k] = v
})
}
func (m *CowMap[K, V]) Delete(k K) {
m.mut(func(m map[K]V) {
delete(m, k)
})
}
func (m *CowMap[K, V]) Default(k K, v V) bool {
var r bool
m.mut(func(m map[K]V) {
if _, ok := m[k]; !ok {
m[k] = v
r = true
}
})
return r
}