From 2e9f61df63a2afb59edfdad6c8a134dedff0e1e3 Mon Sep 17 00:00:00 2001 From: guonaihong Date: Sat, 11 Jun 2022 22:37:24 +0800 Subject: [PATCH] =?UTF-8?q?btree=20Get=E6=8E=A5=E5=8F=A3=E6=BB=A1=E8=B6=B3?= =?UTF-8?q?api=E7=BA=A6=E6=9D=9F=20#2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- btree/btree.go | 8 +++++++- btree/btree_test.go | 6 +++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/btree/btree.go b/btree/btree.go index 62942f9..291a068 100644 --- a/btree/btree.go +++ b/btree/btree.go @@ -187,9 +187,15 @@ func (b *Btree[K, V]) SetWithPrev(k K, v V) (prev V, replaced bool) { return } +// 获取值, 忽略找不到的情况 +func (b *Btree[K, V]) Get(k K) (v V) { + v, _ = b.GetWithErr(k) + return +} + // 找到err为nil // 找不到err为ErrNotFound -func (b *Btree[K, V]) Get(k K) (v V, err error) { +func (b *Btree[K, V]) GetWithErr(k K) (v V, err error) { if b.root == nil { err = ErrNotFound return diff --git a/btree/btree_test.go b/btree/btree_test.go index f4c2ac7..8e86f31 100644 --- a/btree/btree_test.go +++ b/btree/btree_test.go @@ -19,7 +19,7 @@ func Test_Btree_SetAndGet(t *testing.T) { } for i := 0; i < max; i++ { - v, err := b.Get(i) + v, err := b.GetWithErr(i) assert.NoError(t, err) assert.Equal(t, v, i) } @@ -36,7 +36,7 @@ func Test_Btree_SetAndGet_Split(t *testing.T) { } for i := 0; i < max; i++ { - v, err := b.Get(i) + v, err := b.GetWithErr(i) assert.NoError(t, err, fmt.Sprintf("index:%d", i)) assert.Equal(t, v, i, fmt.Sprintf("index:%d", i)) } @@ -53,7 +53,7 @@ func Test_Btree_SetAndGet_Split_Big(t *testing.T) { } for i := 0; i < max; i++ { - v, err := b.Get(i) + v, err := b.GetWithErr(i) assert.NoError(t, err, fmt.Sprintf("index:%d", i)) assert.Equal(t, v, i, fmt.Sprintf("index:%d", i)) }