diff --git a/api/api.go b/api/api.go index 4ef5834..8da93b4 100644 --- a/api/api.go +++ b/api/api.go @@ -12,7 +12,7 @@ type Map[K constraints.Ordered, V any] interface { // 设置 Set(k K, v V) // 设置值 - SetWithPrev(k K, v V) (prev V, replaced bool) + Swap(k K, v V) (prev V, replaced bool) // int Len() int // 遍历 @@ -32,7 +32,7 @@ type Set[K constraints.Ordered] interface { type Trie[V any] interface { Get(k string) (v V) - SetWithPrev(k string, v V) (prev V, replaced bool) + Swap(k string, v V) (prev V, replaced bool) HasPrefix(k string) bool TryGet(k string) (v V, found bool) Delete(k string) diff --git a/avltree/avltree.go b/avltree/avltree.go index 55c3a2c..ae2f472 100644 --- a/avltree/avltree.go +++ b/avltree/avltree.go @@ -245,11 +245,11 @@ func (a *AvlTree[K, V]) TryGet(k K) (v V, ok bool) { } func (a *AvlTree[K, V]) Set(k K, v V) { - _, _ = a.SetWithPrev(k, v) + _, _ = a.Swap(k, v) } // 设置接口, 如果有值, 把prev值带返回, 并且被替换, 没有就新加 -func (a *AvlTree[K, V]) SetWithPrev(k K, v V) (prev V, replaced bool) { +func (a *AvlTree[K, V]) Swap(k K, v V) (prev V, replaced bool) { link := &a.root.node var parent *node[K, V] node := &node[K, V]{pair: pair[K, V]{key: k, val: v}} diff --git a/avltree/avltree_test.go b/avltree/avltree_test.go index 7b42e54..1ca78bb 100644 --- a/avltree/avltree_test.go +++ b/avltree/avltree_test.go @@ -12,7 +12,7 @@ func Test_SetAndGet(t *testing.T) { b := New[int, int]() max := 1000 for i := 0; i < max; i++ { - b.SetWithPrev(i, i) + b.Swap(i, i) } for i := 0; i < max; i++ { @@ -31,7 +31,7 @@ func Test_SetAndGet2(t *testing.T) { b := New[int, int]() max := 1000 for i := max; i >= 0; i-- { - b.SetWithPrev(i, i) + b.Swap(i, i) } for i := max; i >= 0; i-- { diff --git a/btree/btree.go b/btree/btree.go index 49376a5..f55dbb5 100644 --- a/btree/btree.go +++ b/btree/btree.go @@ -61,7 +61,7 @@ func (b *Btree[K, V]) Len() int { // 设置接口, 如果有这个值, 有值就替换, 没有就新加 func (b *Btree[K, V]) Set(k K, v V) { - _, _ = b.SetWithPrev(k, v) + _, _ = b.Swap(k, v) } // 新建一个节点 @@ -155,7 +155,7 @@ func (b *Btree[K, V]) nodeSet(n *node[K, V], item pair[K, V]) (prev V, replaced } // 设置接口, 如果有值, 把prev值带返回, 并且被替换, 没有就新加 -func (b *Btree[K, V]) SetWithPrev(k K, v V) (prev V, replaced bool) { +func (b *Btree[K, V]) Swap(k K, v V) (prev V, replaced bool) { item := pair[K, V]{key: k, val: v} // 如果是每一个节点, 直接加入到root节点 if b.root == nil { @@ -184,8 +184,8 @@ func (b *Btree[K, V]) SetWithPrev(k K, v V) (prev V, replaced bool) { b.root.items.Push(median) } - // 再调用下SetWithPrev, 结点分裂好了, 就有空间放数据 - return b.SetWithPrev(item.key, item.val) + // 再调用下Swap, 结点分裂好了, 就有空间放数据 + return b.Swap(item.key, item.val) } if replaced { diff --git a/btree/btree_test.go b/btree/btree_test.go index 402da0b..28a1bac 100644 --- a/btree/btree_test.go +++ b/btree/btree_test.go @@ -79,7 +79,7 @@ func Test_Btree_SetAndGet_Replace(t *testing.T) { } for i := 0; i < max; i++ { - prev, replace := b.SetWithPrev(i, i+1) + prev, replace := b.Swap(i, i+1) if !replace { t.Errorf("Expected true, got false for key %d", i) } diff --git a/cmap/cmap_stdmap.go b/cmap/cmap_stdmap.go index b044832..145adea 100644 --- a/cmap/cmap_stdmap.go +++ b/cmap/cmap_stdmap.go @@ -37,7 +37,7 @@ func (s *stdmap[K, V]) Set(key K, value V) { } // 设置值 -func (s *stdmap[K, V]) SetWithPrev(key K, value V) (prev V, replaced bool) { +func (s *stdmap[K, V]) Swap(key K, value V) (prev V, replaced bool) { prev, replaced = s.m[key] s.m[key] = value return diff --git a/radix/radix.go b/radix/radix.go index 19212d0..dc18d0b 100644 --- a/radix/radix.go +++ b/radix/radix.go @@ -67,7 +67,7 @@ func (r *Radix[V]) newEdge(label rune, p pair[V], prefix string) edge[V] { } // 设置 -func (r *Radix[V]) SetWithPrev(k string, v V) (prev V, replaced bool) { +func (r *Radix[V]) Swap(k string, v V) (prev V, replaced bool) { var parent *node[V] var found bool diff --git a/rbtree/rbtree.go b/rbtree/rbtree.go index 130d5f5..a630811 100644 --- a/rbtree/rbtree.go +++ b/rbtree/rbtree.go @@ -217,11 +217,11 @@ func (r *RBTree[K, V]) Last() (v V, ok bool) { } func (r *RBTree[K, V]) Set(k K, v V) { - _, _ = r.SetWithPrev(k, v) + _, _ = r.Swap(k, v) } // 设置 -func (r *RBTree[K, V]) SetWithPrev(k K, v V) (prev V, replaced bool) { +func (r *RBTree[K, V]) Swap(k K, v V) (prev V, replaced bool) { link := &r.root.node var parent *node[K, V] diff --git a/rbtree/rbtree_test.go b/rbtree/rbtree_test.go index 8aeab06..3a648d5 100644 --- a/rbtree/rbtree_test.go +++ b/rbtree/rbtree_test.go @@ -12,7 +12,7 @@ func Test_SetAndGet(t *testing.T) { b := New[int, int]() max := 1000 for i := 0; i < max; i++ { - b.SetWithPrev(i, i) + b.Swap(i, i) } for i := 0; i < max; i++ { @@ -31,7 +31,7 @@ func Test_SetAndGet2(t *testing.T) { b := New[int, int]() max := 1000 for i := max; i >= 0; i-- { - b.SetWithPrev(i, i) + b.Swap(i, i) } for i := max; i >= 0; i-- { diff --git a/rhashmap/rhashmap.go b/rhashmap/rhashmap.go index 23bc0f2..8542d52 100644 --- a/rhashmap/rhashmap.go +++ b/rhashmap/rhashmap.go @@ -375,11 +375,11 @@ func (h *HashMap[K, V]) Range(pr func(key K, val V) bool) { } func (h *HashMap[K, V]) Set(k K, v V) { - h.SetWithPrev(k, v) + h.Swap(k, v) } // 设置 -func (h *HashMap[K, V]) SetWithPrev(k K, v V) (prev V, replaced bool) { +func (h *HashMap[K, V]) Swap(k K, v V) (prev V, replaced bool) { h.lazyinit() if h.isRehashing() { h.rehash(1) diff --git a/skiplist/skiplist.go b/skiplist/skiplist.go index db9c415..0f5f7f0 100644 --- a/skiplist/skiplist.go +++ b/skiplist/skiplist.go @@ -186,7 +186,7 @@ func (s *SkipList[K, T]) InsertOrUpdate(score K, elem T, cb InsertOrUpdateCb[T]) s.InsertInner(score, elem, s.rand()) } -func (s *SkipList[K, T]) SetWithPrev(score K, elem T) (prev T, replaced bool) { +func (s *SkipList[K, T]) Swap(score K, elem T) (prev T, replaced bool) { return s.InsertInner(score, elem, s.rand()) } diff --git a/trie/trie_map.go b/trie/trie_map.go index 4597a3e..3e936ba 100644 --- a/trie/trie_map.go +++ b/trie/trie_map.go @@ -24,10 +24,10 @@ func New[V any]() *Trie[V] { } func (t *Trie[V]) Set(k string, v V) { - _, _ = t.SetWithPrev(k, v) + _, _ = t.Swap(k, v) } -func (t *Trie[V]) SetWithPrev(k string, v V) (prev V, replaced bool) { +func (t *Trie[V]) Swap(k string, v V) (prev V, replaced bool) { n := t for _, r := range k { c := n.children[r]