Skip to content

Commit 7cdb9fa

Browse files
committed
1. GetWithBool->TryGet, 2.新加InsertOrUpdate函数
1 parent 87d3884 commit 7cdb9fa

File tree

20 files changed

+316
-62
lines changed

20 files changed

+316
-62
lines changed

api/api.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ type Map[K constraints.Ordered, V any] interface {
66
// 获取
77
Get(k K) (elem V)
88
// 获取
9-
GetWithBool(k K) (elem V, ok bool)
9+
TryGet(k K) (elem V, ok bool)
1010
// 删除
1111
Delete(k K)
1212
// 设置
@@ -34,7 +34,7 @@ type Trie[V any] interface {
3434
Get(k string) (v V)
3535
SetWithPrev(k string, v V) (prev V, replaced bool)
3636
HasPrefix(k string) bool
37-
GetWithBool(k string) (v V, found bool)
37+
TryGet(k string) (v V, found bool)
3838
Delete(k string)
3939
Len() int
4040
}

avltree/avltree.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@ type AvlTree[K constraints.Ordered, V any] struct {
182182
root root[K, V]
183183
}
184184

185+
// 如果有值,则这个回调函数会被调用
186+
type InsertOrUpdateCb[V any] func(prev V, new V) V
187+
185188
// 构造函数
186189
func New[K constraints.Ordered, V any]() *AvlTree[K, V] {
187190
return &AvlTree[K, V]{}
@@ -219,12 +222,12 @@ func (a *AvlTree[K, V]) Last() (v V, ok bool) {
219222

220223
// Get
221224
func (a *AvlTree[K, V]) Get(k K) (v V) {
222-
v, _ = a.GetWithBool(k)
225+
v, _ = a.TryGet(k)
223226
return
224227
}
225228

226229
// 从avl tree找到需要的值
227-
func (a *AvlTree[K, V]) GetWithBool(k K) (v V, ok bool) {
230+
func (a *AvlTree[K, V]) TryGet(k K) (v V, ok bool) {
228231
n := a.root.node
229232
for n != nil {
230233
if n.key == k {
@@ -272,6 +275,13 @@ func (a *AvlTree[K, V]) SetWithPrev(k K, v V) (prev V, replaced bool) {
272275
return
273276
}
274277

278+
func (a *AvlTree[K, V]) InsertOrUpdate(k K, v V, cb InsertOrUpdateCb[V]) {
279+
if prev, ok := a.TryGet(k); ok {
280+
v = cb(prev, v)
281+
}
282+
a.Set(k, v)
283+
}
284+
275285
func (r *root[K, V]) rebalance(node *node[K, V]) {
276286

277287
for ; node != nil; node = node.parent {

avltree/avltree_test.go

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func Test_SetAndGet(t *testing.T) {
1616
}
1717

1818
for i := 0; i < max; i++ {
19-
v, ok := b.GetWithBool(i)
19+
v, ok := b.TryGet(i)
2020
if !ok {
2121
t.Errorf("expected true, got false for index %d", i)
2222
}
@@ -35,7 +35,7 @@ func Test_SetAndGet2(t *testing.T) {
3535
}
3636

3737
for i := max; i >= 0; i-- {
38-
v, ok := b.GetWithBool(i)
38+
v, ok := b.TryGet(i)
3939
if !ok {
4040
t.Errorf("expected true, got false for index %d", i)
4141
}
@@ -63,7 +63,7 @@ func Test_AVLTree_Delete1(t *testing.T) {
6363

6464
// max/2-max应该能找到
6565
for i := max / 2; i < max; i++ {
66-
v, ok := b.GetWithBool(i)
66+
v, ok := b.TryGet(i)
6767
if !ok {
6868
t.Errorf("expected true, got false for index %d", i)
6969
}
@@ -74,7 +74,7 @@ func Test_AVLTree_Delete1(t *testing.T) {
7474

7575
// 0-max/2应该找不到
7676
for i := 0; i < max/2; i++ {
77-
v, ok := b.GetWithBool(i)
77+
v, ok := b.TryGet(i)
7878
if ok {
7979
t.Errorf("expected false, got true for index %d", i)
8080
}
@@ -255,6 +255,60 @@ func Test_RanePrev(t *testing.T) {
255255
}
256256
}
257257

258+
func Test_AvlTree_InsertOrUpdate(t *testing.T) {
259+
b := New[int, int]()
260+
max := 100
261+
262+
// Insert elements
263+
for i := 0; i < max; i++ {
264+
b.InsertOrUpdate(i, i, func(prev, new int) int {
265+
return prev + new
266+
})
267+
}
268+
269+
// Update elements
270+
for i := 0; i < max; i++ {
271+
b.InsertOrUpdate(i, i, func(prev, new int) int {
272+
return prev + new
273+
})
274+
}
275+
276+
// Verify elements
277+
for i := 0; i < max; i++ {
278+
v, ok := b.TryGet(i)
279+
if !ok || v != i*2 {
280+
t.Errorf("expected %d, got %v", i*2, v)
281+
}
282+
}
283+
}
284+
285+
func Test_AvlTree_InsertOrUpdate2(t *testing.T) {
286+
b := New[int, int]()
287+
max := 100
288+
289+
// Insert elements
290+
for i := 0; i < max; i++ {
291+
b.InsertOrUpdate(i, i, func(prev, new int) int {
292+
return prev + new
293+
})
294+
}
295+
296+
// Update elements
297+
for i := 0; i < max; i++ {
298+
b.InsertOrUpdate(i, i*2, func(prev, new int) int {
299+
return prev + new
300+
})
301+
}
302+
303+
// Verify elements
304+
for i := 0; i < max; i++ {
305+
v, ok := b.TryGet(i)
306+
if !ok || v != i*3 {
307+
t.Errorf("expected %d, got %v", i*3, v)
308+
}
309+
}
310+
}
311+
258312
// 辅助函数,用于比较两个切片是否相等
259313
func equalSlices(a, b []int) bool {
260314
if len(a) != len(b) {

btree/btree.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,13 +197,13 @@ func (b *Btree[K, V]) SetWithPrev(k K, v V) (prev V, replaced bool) {
197197

198198
// 获取值, 忽略找不到的情况
199199
func (b *Btree[K, V]) Get(k K) (v V) {
200-
v, _ = b.GetWithBool(k)
200+
v, _ = b.TryGet(k)
201201
return
202202
}
203203

204204
// 找到ok为true
205205
// 找不到ok为false
206-
func (b *Btree[K, V]) GetWithBool(k K) (v V, ok bool) {
206+
func (b *Btree[K, V]) TryGet(k K) (v V, ok bool) {
207207
if b.root == nil {
208208
return
209209
}

btree/btree_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func Test_Btree_SetAndGet(t *testing.T) {
1717
}
1818

1919
for i := 0; i < max; i++ {
20-
v, ok := b.GetWithBool(i)
20+
v, ok := b.TryGet(i)
2121
if !ok {
2222
t.Errorf("Expected true, got false for key %d", i)
2323
}
@@ -38,7 +38,7 @@ func Test_Btree_SetAndGet_Split(t *testing.T) {
3838
}
3939

4040
for i := 0; i < max; i++ {
41-
v, ok := b.GetWithBool(i)
41+
v, ok := b.TryGet(i)
4242
if !ok {
4343
t.Errorf("Expected true, got false for key %d", i)
4444
}
@@ -59,7 +59,7 @@ func Test_Btree_SetAndGet_Split_Big(t *testing.T) {
5959
}
6060

6161
for i := 0; i < max; i++ {
62-
v, ok := b.GetWithBool(i)
62+
v, ok := b.TryGet(i)
6363
if !ok {
6464
t.Errorf("Expected true, got false for key %d", i)
6565
}
@@ -89,7 +89,7 @@ func Test_Btree_SetAndGet_Replace(t *testing.T) {
8989
}
9090

9191
for i := 0; i < max; i++ {
92-
v, ok := b.GetWithBool(i)
92+
v, ok := b.TryGet(i)
9393
if !ok {
9494
t.Errorf("Expected true, got false for key %d", i)
9595
}
@@ -324,7 +324,7 @@ func Test_Btree_Delete1(t *testing.T) {
324324

325325
// max/2-max应该能找到
326326
for i := max / 2; i < max; i++ {
327-
v, ok := b.GetWithBool(i)
327+
v, ok := b.TryGet(i)
328328
if !ok {
329329
t.Errorf("Expected true, got false for key %d", i)
330330
}
@@ -335,7 +335,7 @@ func Test_Btree_Delete1(t *testing.T) {
335335

336336
// 0-max/2应该找不到
337337
for i := 0; i < max/2; i++ {
338-
v, ok := b.GetWithBool(i)
338+
v, ok := b.TryGet(i)
339339
if ok {
340340
t.Errorf("Expected false, got true for key %d", i)
341341
}
@@ -378,7 +378,7 @@ func Test_Btree_Delete2(t *testing.T) {
378378

379379
// 查找后半段, 应该找不到
380380
for i := start; i < max; i++ {
381-
v, ok := b.GetWithBool(i)
381+
v, ok := b.TryGet(i)
382382
if ok {
383383
t.Errorf("Expected false, got true for key %d", i)
384384
}
@@ -389,7 +389,7 @@ func Test_Btree_Delete2(t *testing.T) {
389389

390390
// 查找前半段
391391
for i := 0; i < start; i++ {
392-
v, ok := b.GetWithBool(i)
392+
v, ok := b.TryGet(i)
393393
if !ok {
394394
t.Errorf("Expected true, got false for key %d", i)
395395
}

cmap/cmap.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ type UpdataOrInsertCb[K constraints.Ordered, V any] func(exist bool, old V) (new
102102
func (c *CMap[K, V]) UpdateOrInsert(k K, cb UpdataOrInsertCb[K, V]) {
103103
item := c.findIndex(k)
104104
item.rw.Lock()
105-
old, ok := item.m.GetWithBool(k)
105+
old, ok := item.m.TryGet(k)
106106
newVal := cb(ok, old)
107107
item.m.Set(k, newVal)
108108
item.rw.Unlock()
@@ -112,15 +112,15 @@ func (c *CMap[K, V]) UpdateOrInsert(k K, cb UpdataOrInsertCb[K, V]) {
112112
func (c *CMap[K, V]) Load(key K) (value V, ok bool) {
113113
item := c.findIndex(key)
114114
item.rw.RLock()
115-
value, ok = item.m.GetWithBool(key)
115+
value, ok = item.m.TryGet(key)
116116
item.rw.RUnlock()
117117
return
118118
}
119119

120120
func (c *CMap[K, V]) LoadAndDelete(key K) (value V, loaded bool) {
121121
item := c.findIndex(key)
122122
item.rw.Lock()
123-
value, loaded = item.m.GetWithBool(key)
123+
value, loaded = item.m.TryGet(key)
124124
if !loaded {
125125
item.rw.Unlock()
126126
return
@@ -133,15 +133,15 @@ func (c *CMap[K, V]) LoadAndDelete(key K) (value V, loaded bool) {
133133
func (c *CMap[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool) {
134134
item := c.findIndex(key)
135135
item.rw.Lock()
136-
actual, loaded = item.m.GetWithBool(key)
136+
actual, loaded = item.m.TryGet(key)
137137
if !loaded {
138138
actual = value
139139
item.m.Set(key, actual)
140140
item.rw.Unlock()
141141
return
142142
}
143143

144-
actual, loaded = item.m.GetWithBool(key)
144+
actual, loaded = item.m.TryGet(key)
145145
item.rw.Unlock()
146146
return
147147
}

cmap/cmap_stdmap.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func (s *stdmap[K, V]) Get(key K) (elem V) {
2121
}
2222

2323
// 获取
24-
func (s *stdmap[K, V]) GetWithBool(key K) (elem V, ok bool) {
24+
func (s *stdmap[K, V]) TryGet(key K) (elem V, ok bool) {
2525
elem, ok = s.m[key]
2626
return
2727
}

linkedlist/linkedlist.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -383,13 +383,13 @@ func (l *LinkedList[T]) ContainsFunc(cb func(value T) bool) bool {
383383

384384
// 获取指定索引数据, 忽略错误
385385
func (l *LinkedList[T]) Get(idx int) (e T) {
386-
e, _ = l.GetWithBool(idx)
386+
e, _ = l.TryGet(idx)
387387
return
388388
}
389389

390390
// 通过索引查找是否包含这个value
391391
// Get是Index的同义词
392-
func (l *LinkedList[T]) GetWithBool(idx int) (e T, ok bool) {
392+
func (l *LinkedList[T]) TryGet(idx int) (e T, ok bool) {
393393
return l.Index(idx)
394394
}
395395

@@ -699,7 +699,7 @@ func (cl *ConcurrentLinkedList[T]) Remove(index int) *ConcurrentLinkedList[T] {
699699
func (cl *ConcurrentLinkedList[T]) Get(idx int) (T, bool) {
700700
cl.mu.RLock()
701701
defer cl.mu.RUnlock()
702-
return cl.list.GetWithBool(idx)
702+
return cl.list.TryGet(idx)
703703
}
704704

705705
// Len 线程安全地获取链表长度

linkedlist/linkedlist_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,30 +122,30 @@ func Test_Last(t *testing.T) {
122122

123123
func Test_Get(t *testing.T) {
124124
// 正索引
125-
if got := must.TakeOneDiscardBool(New[string]().RPush("1", "2", "3", "4").GetWithBool(0)); got != "1" {
125+
if got := must.TakeOneDiscardBool(New[string]().RPush("1", "2", "3", "4").TryGet(0)); got != "1" {
126126
t.Errorf("Get() = %v, want %v", got, "1")
127127
}
128-
if got := must.TakeOneDiscardBool(New[string]().RPush("1", "2", "3", "4").GetWithBool(1)); got != "2" {
128+
if got := must.TakeOneDiscardBool(New[string]().RPush("1", "2", "3", "4").TryGet(1)); got != "2" {
129129
t.Errorf("Get() = %v, want %v", got, "2")
130130
}
131-
if got := must.TakeOneDiscardBool(New[string]().RPush("1", "2", "3", "4").GetWithBool(2)); got != "3" {
131+
if got := must.TakeOneDiscardBool(New[string]().RPush("1", "2", "3", "4").TryGet(2)); got != "3" {
132132
t.Errorf("Get() = %v, want %v", got, "3")
133133
}
134-
if got := must.TakeOneDiscardBool(New[string]().RPush("1", "2", "3", "4").GetWithBool(3)); got != "4" {
134+
if got := must.TakeOneDiscardBool(New[string]().RPush("1", "2", "3", "4").TryGet(3)); got != "4" {
135135
t.Errorf("Get() = %v, want %v", got, "4")
136136
}
137137

138138
// 负索引
139-
if got := must.TakeOneDiscardBool(New[string]().RPush("1", "2", "3", "4").GetWithBool(-1)); got != "4" {
139+
if got := must.TakeOneDiscardBool(New[string]().RPush("1", "2", "3", "4").TryGet(-1)); got != "4" {
140140
t.Errorf("Get() = %v, want %v", got, "4")
141141
}
142-
if got := must.TakeOneDiscardBool(New[string]().RPush("1", "2", "3", "4").GetWithBool(-2)); got != "3" {
142+
if got := must.TakeOneDiscardBool(New[string]().RPush("1", "2", "3", "4").TryGet(-2)); got != "3" {
143143
t.Errorf("Get() = %v, want %v", got, "3")
144144
}
145-
if got := must.TakeOneDiscardBool(New[string]().RPush("1", "2", "3", "4").GetWithBool(-3)); got != "2" {
145+
if got := must.TakeOneDiscardBool(New[string]().RPush("1", "2", "3", "4").TryGet(-3)); got != "2" {
146146
t.Errorf("Get() = %v, want %v", got, "2")
147147
}
148-
if got := must.TakeOneDiscardBool(New[string]().RPush("1", "2", "3", "4").GetWithBool(-4)); got != "1" {
148+
if got := must.TakeOneDiscardBool(New[string]().RPush("1", "2", "3", "4").TryGet(-4)); got != "1" {
149149
t.Errorf("Get() = %v, want %v", got, "1")
150150
}
151151
}

radix/radix.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ type Radix[V any] struct {
4040

4141
// 获取
4242
func (r *Radix[V]) Get(k string) (v V) {
43-
v, _ = r.GetWithBool(k)
43+
v, _ = r.TryGet(k)
4444
return
4545
}
4646

@@ -197,7 +197,7 @@ func (n *node[V]) find(r rune) (index int, found bool) {
197197
}
198198

199199
// 获取返回bool
200-
func (r *Radix[V]) GetWithBool(k string) (v V, found bool) {
200+
func (r *Radix[V]) TryGet(k string) (v V, found bool) {
201201
n := r.root
202202

203203
for {

0 commit comments

Comments
 (0)