Skip to content

Commit

Permalink
refactor: SetWithPrev -> Swap
Browse files Browse the repository at this point in the history
  • Loading branch information
guonaihong committed Dec 28, 2024
1 parent 7cdb9fa commit 61ad897
Show file tree
Hide file tree
Showing 12 changed files with 22 additions and 22 deletions.
4 changes: 2 additions & 2 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
// 遍历
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions avltree/avltree.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down
4 changes: 2 additions & 2 deletions avltree/avltree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++ {
Expand All @@ -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-- {
Expand Down
8 changes: 4 additions & 4 deletions btree/btree.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

// 新建一个节点
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion btree/btree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion cmap/cmap_stdmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion radix/radix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions rbtree/rbtree.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
4 changes: 2 additions & 2 deletions rbtree/rbtree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++ {
Expand All @@ -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-- {
Expand Down
4 changes: 2 additions & 2 deletions rhashmap/rhashmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion skiplist/skiplist.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down
4 changes: 2 additions & 2 deletions trie/trie_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 61ad897

Please sign in to comment.