From f0297a5c47ad175316bcef13ad719b83fbf039b8 Mon Sep 17 00:00:00 2001 From: GouJie <507762441@qq.com> Date: Wed, 17 Jan 2024 13:52:58 +0800 Subject: [PATCH 1/2] Modify code comments --- datastruct/dict/concurrent.go | 4 ++-- datastruct/dict/simple.go | 2 +- datastruct/lock/lock_map.go | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/datastruct/dict/concurrent.go b/datastruct/dict/concurrent.go index 2ece9cd6..217143bf 100644 --- a/datastruct/dict/concurrent.go +++ b/datastruct/dict/concurrent.go @@ -185,7 +185,7 @@ func (dict *ConcurrentDict) PutIfAbsentWithLock(key string, val interface{}) (re return 1 } -// PutIfExists puts value if the key is exist and returns the number of inserted key-value +// PutIfExists puts value if the key is existed and returns the number of inserted key-value func (dict *ConcurrentDict) PutIfExists(key string, val interface{}) (result int) { if dict == nil { panic("dict is nil") @@ -262,7 +262,7 @@ func (dict *ConcurrentDict) decreaseCount() int32 { } // ForEach traversal the dict -// it may not visits new entry inserted during traversal +// it may not visit new entry inserted during traversal func (dict *ConcurrentDict) ForEach(consumer Consumer) { if dict == nil { panic("dict is nil") diff --git a/datastruct/dict/simple.go b/datastruct/dict/simple.go index 61839656..49be8f33 100644 --- a/datastruct/dict/simple.go +++ b/datastruct/dict/simple.go @@ -46,7 +46,7 @@ func (dict *SimpleDict) PutIfAbsent(key string, val interface{}) (result int) { return 1 } -// PutIfExists puts value if the key is exist and returns the number of inserted key-value +// PutIfExists puts value if the key is existed and returns the number of inserted key-value func (dict *SimpleDict) PutIfExists(key string, val interface{}) (result int) { _, existed := dict.m[key] if existed { diff --git a/datastruct/lock/lock_map.go b/datastruct/lock/lock_map.go index e4bb4ef5..d11bc2d2 100644 --- a/datastruct/lock/lock_map.go +++ b/datastruct/lock/lock_map.go @@ -90,7 +90,7 @@ func (locks *Locks) toLockIndices(keys []string, reverse bool) []uint32 { } // Locks obtains multiple exclusive locks for writing -// invoking Lock in loop may cause dead lock, please use Locks +// invoking Lock in loop may cause deadlock, please use Locks func (locks *Locks) Locks(keys ...string) { indices := locks.toLockIndices(keys, false) for _, index := range indices { @@ -100,7 +100,7 @@ func (locks *Locks) Locks(keys ...string) { } // RLocks obtains multiple shared locks for reading -// invoking RLock in loop may cause dead lock, please use RLocks +// invoking RLock in loop may cause deadlock, please use RLocks func (locks *Locks) RLocks(keys ...string) { indices := locks.toLockIndices(keys, false) for _, index := range indices { From 9e21880a5a4da8860be87371f9e9ffaafeeba6cc Mon Sep 17 00:00:00 2001 From: GouJie <507762441@qq.com> Date: Wed, 17 Jan 2024 14:09:58 +0800 Subject: [PATCH 2/2] Modify unnecessary type conversions --- datastruct/lock/lock_map.go | 2 +- datastruct/sortedset/sortedset.go | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/datastruct/lock/lock_map.go b/datastruct/lock/lock_map.go index d11bc2d2..72fd0963 100644 --- a/datastruct/lock/lock_map.go +++ b/datastruct/lock/lock_map.go @@ -39,7 +39,7 @@ func (locks *Locks) spread(hashCode uint32) uint32 { panic("dict is nil") } tableSize := uint32(len(locks.table)) - return (tableSize - 1) & uint32(hashCode) + return (tableSize - 1) & hashCode } // Lock obtains exclusive lock for writing diff --git a/datastruct/sortedset/sortedset.go b/datastruct/sortedset/sortedset.go index 67f7942d..52eacf3a 100644 --- a/datastruct/sortedset/sortedset.go +++ b/datastruct/sortedset/sortedset.go @@ -78,7 +78,7 @@ func (sortedSet *SortedSet) GetRank(member string, desc bool) (rank int64) { // ForEachByRank visits each member which rank within [start, stop), sort by ascending order, rank starts from 0 func (sortedSet *SortedSet) ForEachByRank(start int64, stop int64, desc bool, consumer func(element *Element) bool) { - size := int64(sortedSet.Len()) + size := sortedSet.Len() if start < 0 || start >= size { panic("illegal start " + strconv.FormatInt(start, 10)) } @@ -91,12 +91,12 @@ func (sortedSet *SortedSet) ForEachByRank(start int64, stop int64, desc bool, co if desc { node = sortedSet.skiplist.tail if start > 0 { - node = sortedSet.skiplist.getByRank(int64(size - start)) + node = sortedSet.skiplist.getByRank(size - start) } } else { node = sortedSet.skiplist.header.level[0].forward if start > 0 { - node = sortedSet.skiplist.getByRank(int64(start + 1)) + node = sortedSet.skiplist.getByRank(start + 1) } }