Skip to content

Commit 957b5c4

Browse files
committed
add UpdateOrInsert
1 parent 490120d commit 957b5c4

File tree

3 files changed

+52
-6
lines changed

3 files changed

+52
-6
lines changed

cmap/cmap.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,19 @@ func (c *CMap[K, V]) Delete(key K) {
9696
item.rw.Unlock()
9797
}
9898

99+
type UpdataOrInsertCb[K constraints.Ordered, V any] func(exits bool, old V) (newVal V)
100+
101+
// 删除或者更新
102+
func (c *CMap[K, V]) UpdateOrInsert(k K, cb UpdataOrInsertCb[K, V]) {
103+
item := c.findIndex(k)
104+
item.rw.Lock()
105+
old, ok := item.m.GetWithBool(k)
106+
newVal := cb(ok, old)
107+
item.m.Set(k, newVal)
108+
item.rw.Unlock()
109+
110+
}
111+
99112
func (c *CMap[K, V]) Load(key K) (value V, ok bool) {
100113
item := c.findIndex(key)
101114
item.rw.RLock()

cmap/cmap_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,41 @@ func Test_Values(t *testing.T) {
182182
m2 := New[string, string]()
183183
assert.Equal(t, len(m2.Keys()), 0)
184184
}
185+
186+
func Test_UpdateOrInsert(t *testing.T) {
187+
t.Run("Update", func(t *testing.T) {
188+
m := New[string, string]()
189+
m.Store("a", "1")
190+
m.Store("b", "2")
191+
m.Store("c", "3")
192+
m.UpdateOrInsert("a", func(exist bool, old string) string {
193+
if !exist {
194+
t.Error("should exist")
195+
}
196+
if exist {
197+
return "4"
198+
}
199+
return old
200+
})
201+
get, _ := m.Load("a")
202+
if get != "4" {
203+
t.Error("should be 4")
204+
}
205+
})
206+
207+
t.Run("Insert", func(t *testing.T) {
208+
m := New[string, string]()
209+
m.Store("a", "1")
210+
m.UpdateOrInsert("b", func(exist bool, old string) string {
211+
if !exist {
212+
return "2"
213+
}
214+
return ""
215+
})
216+
217+
get, _ := m.Load("b")
218+
if get != "2" {
219+
t.Error("should be 2")
220+
}
221+
})
222+
}

rwmap/rwmap.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ import (
99
"github.com/antlabs/gstl/mapex"
1010
)
1111

12-
//type Pair[K comparable, V any] = mapex.Pair[K comparable, V any]
13-
12+
// type Pair[K comparable, V any] = mapex.Pair[K comparable, V any]
1413
type Pair[K comparable, V any] struct {
1514
Key K
1615
Val V
@@ -47,7 +46,6 @@ func (r *RWMap[K, V]) Load(key K) (value V, ok bool) {
4746
value, ok = r.m[key]
4847
r.rw.RUnlock()
4948
return
50-
5149
}
5250

5351
// 获取值,然后并删除
@@ -98,7 +96,6 @@ func (r *RWMap[K, V]) Iter() <-chan Pair[K, V] {
9896
}
9997
close(p)
10098
r.rw.RUnlock()
101-
10299
}()
103100
return p
104101
}
@@ -115,7 +112,6 @@ func (r *RWMap[K, V]) Store(key K, value V) {
115112

116113
// keys
117114
func (r *RWMap[K, V]) Keys() (keys []K) {
118-
119115
r.rw.RLock()
120116
if r.m == nil {
121117
r.rw.RUnlock()
@@ -128,7 +124,6 @@ func (r *RWMap[K, V]) Keys() (keys []K) {
128124

129125
// vals
130126
func (r *RWMap[K, V]) Values() (values []V) {
131-
132127
r.rw.RLock()
133128
if r.m == nil {
134129
r.rw.RUnlock()

0 commit comments

Comments
 (0)