From 59bd7be496fa10ebf48e42f4dc74b16d99f4780d Mon Sep 17 00:00:00 2001 From: John Guo Date: Mon, 7 Oct 2024 11:16:47 +0800 Subject: [PATCH 1/2] fix(container/gtree): deadlock in Map/MapStrAny functions, comments update --- container/gtree/gtree.go | 115 +++++++---- container/gtree/gtree_avltree.go | 273 +++++++++++++------------- container/gtree/gtree_btree.go | 239 +++++++++++----------- container/gtree/gtree_redblacktree.go | 262 ++++++++++++------------ example/go.mod | 18 +- example/go.sum | 34 ++-- 6 files changed, 496 insertions(+), 445 deletions(-) diff --git a/container/gtree/gtree.go b/container/gtree/gtree.go index 284ffbd4f29..185c7c0206c 100644 --- a/container/gtree/gtree.go +++ b/container/gtree/gtree.go @@ -13,66 +13,77 @@ import "github.com/gogf/gf/v2/container/gvar" // iTree defines the interface for basic operations of a tree. type iTree interface { - // Set inserts node into the tree. + // Set sets key-value pair into the tree. Set(key interface{}, value interface{}) // Sets batch sets key-values to the tree. Sets(data map[interface{}]interface{}) // SetIfNotExist sets `value` to the map if the `key` does not exist, and then returns true. - // It returns false if `key` exists, and `value` would be ignored. + // It returns false if `key` exists, and such setting key-value pair operation would be ignored. SetIfNotExist(key interface{}, value interface{}) bool // SetIfNotExistFunc sets value with return value of callback function `f`, and then returns true. - // It returns false if `key` exists, and `value` would be ignored. + // It returns false if `key` exists, and such setting key-value pair operation would be ignored. SetIfNotExistFunc(key interface{}, f func() interface{}) bool // SetIfNotExistFuncLock sets value with return value of callback function `f`, and then returns true. - // It returns false if `key` exists, and `value` would be ignored. + // It returns false if `key` exists, and such setting key-value pair operation would be ignored. + // // SetIfNotExistFuncLock differs with SetIfNotExistFunc function is that - // it executes function `f` with mutex.Lock of the hash map. + // it executes function `f` within mutex.Lock of the hash map. SetIfNotExistFuncLock(key interface{}, f func() interface{}) bool - // Get searches the node in the tree by `key` and returns its value or nil if key is not found in tree. + // Get searches the `key` in the tree and returns its associated `value` or nil if key is not found in tree. + // + // Note that, the `nil` value from Get function cannot be used to determine key existence, please use Contains + // function to do so. Get(key interface{}) (value interface{}) - // GetOrSet returns the value by key, - // or sets value with given `value` if it does not exist and then returns this value. + // GetOrSet returns its `value` of `key`, or sets value with given `value` if it does not exist and then returns + // this value. GetOrSet(key interface{}, value interface{}) interface{} - // GetOrSetFunc returns the value by key, - // or sets value with returned value of callback function `f` if it does not exist - // and then returns this value. + // GetOrSetFunc returns its `value` of `key`, or sets value with returned value of callback function `f` if it does + // not exist and then returns this value. GetOrSetFunc(key interface{}, f func() interface{}) interface{} - // GetOrSetFuncLock returns the value by key, - // or sets value with returned value of callback function `f` if it does not exist - // and then returns this value. - // GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function `f` - // with mutex.Lock of the hash map. + // GetOrSetFuncLock returns its `value` of `key`, or sets value with returned value of callback function `f` if it + // does not exist and then returns this value. + // + // GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function `f` within mutex.Lock of the + // hash map. GetOrSetFuncLock(key interface{}, f func() interface{}) interface{} // GetVar returns a gvar.Var with the value by given `key`. - // The returned gvar.Var is un-concurrent safe. + // Note that, the returned gvar.Var is un-concurrent safe. + // + // Also see function Get. GetVar(key interface{}) *gvar.Var // GetVarOrSet returns a gvar.Var with result from GetVarOrSet. - // The returned gvar.Var is un-concurrent safe. + // Note that, the returned gvar.Var is un-concurrent safe. + // + // Also see function GetOrSet. GetVarOrSet(key interface{}, value interface{}) *gvar.Var // GetVarOrSetFunc returns a gvar.Var with result from GetOrSetFunc. - // The returned gvar.Var is un-concurrent safe. + // Note that, the returned gvar.Var is un-concurrent safe. + // + // Also see function GetOrSetFunc. GetVarOrSetFunc(key interface{}, f func() interface{}) *gvar.Var // GetVarOrSetFuncLock returns a gvar.Var with result from GetOrSetFuncLock. - // The returned gvar.Var is un-concurrent safe. + // Note that, the returned gvar.Var is un-concurrent safe. + // + // Also see function GetOrSetFuncLock. GetVarOrSetFuncLock(key interface{}, f func() interface{}) *gvar.Var // Search searches the tree with given `key`. // Second return parameter `found` is true if key was found, otherwise false. Search(key interface{}) (value interface{}, found bool) - // Contains checks whether `key` exists in the tree. + // Contains checks and returns whether given `key` exists in the tree. Contains(key interface{}) bool // Size returns number of nodes in the tree. @@ -81,23 +92,23 @@ type iTree interface { // IsEmpty returns true if tree does not contain any nodes. IsEmpty() bool - // Remove removes the node from the tree by key. - // Key should adhere to the comparator's type assertion, otherwise method panics. + // Remove removes the node from the tree by `key`, and returns its associated value of `key`. + // The given `key` should adhere to the comparator's type assertion, otherwise method panics. Remove(key interface{}) (value interface{}) - // Removes batch deletes values of the tree by `keys`. + // Removes batch deletes key-value pairs from the tree by `keys`. Removes(keys []interface{}) // Clear removes all nodes from the tree. Clear() - // Keys returns all keys in asc order. + // Keys returns all keys from the tree in order by its comparator. Keys() []interface{} - // Values returns all values in asc order based on the key. + // Values returns all values from the true in order by its comparator based on the key. Values() []interface{} - // Replace the data of the tree with given `data`. + // Replace clears the data of the tree and sets the nodes by given `data`. Replace(data map[interface{}]interface{}) // Print prints the tree to stdout. @@ -109,32 +120,66 @@ type iTree interface { // MarshalJSON implements the interface MarshalJSON for json.Marshal. MarshalJSON() (jsonBytes []byte, err error) + // Map returns all key-value pairs as map. Map() map[interface{}]interface{} + + // MapStrAny returns all key-value items as map[string]any. MapStrAny() map[string]interface{} // Iterator is alias of IteratorAsc. + // + // Also see IteratorAsc. Iterator(f func(key, value interface{}) bool) // IteratorFrom is alias of IteratorAscFrom. + // + // Also see IteratorAscFrom. IteratorFrom(key interface{}, match bool, f func(key, value interface{}) bool) // IteratorAsc iterates the tree readonly in ascending order with given callback function `f`. - // If `f` returns true, then it continues iterating; or false to stop. + // If callback function `f` returns true, then it continues iterating; or false to stop. IteratorAsc(f func(key, value interface{}) bool) // IteratorAscFrom iterates the tree readonly in ascending order with given callback function `f`. - // The parameter `key` specifies the start entry for iterating. The `match` specifies whether - // starting iterating if the `key` is fully matched, or else using index searching iterating. - // If `f` returns true, then it continues iterating; or false to stop. + // + // The parameter `key` specifies the start entry for iterating. + // The parameter `match` specifies whether starting iterating only if the `key` is fully matched, or else using + // index searching iterating. + // If callback function `f` returns true, then it continues iterating; or false to stop. IteratorAscFrom(key interface{}, match bool, f func(key, value interface{}) bool) // IteratorDesc iterates the tree readonly in descending order with given callback function `f`. - // If `f` returns true, then it continues iterating; or false to stop. + // + // If callback function `f` returns true, then it continues iterating; or false to stop. IteratorDesc(f func(key, value interface{}) bool) // IteratorDescFrom iterates the tree readonly in descending order with given callback function `f`. - // The parameter `key` specifies the start entry for iterating. The `match` specifies whether - // starting iterating if the `key` is fully matched, or else using index searching iterating. - // If `f` returns true, then it continues iterating; or false to stop. + // + // The parameter `key` specifies the start entry for iterating. + // The parameter `match` specifies whether starting iterating only if the `key` is fully matched, or else using + // index searching iterating. + // If callback function `f` returns true, then it continues iterating; or false to stop. IteratorDescFrom(key interface{}, match bool, f func(key, value interface{}) bool) } + +// iteratorFromGetIndex returns the index of the key in the keys slice. +// +// The parameter `match` specifies whether starting iterating only if the `key` is fully matched, +// or else using index searching iterating. +// If `isIterator` is true, iterator is available; or else not. +func iteratorFromGetIndex(key any, keys []any, match bool) (index int, canIterator bool) { + if match { + for i, k := range keys { + if k == key { + canIterator = true + index = i + } + } + } else { + if i, ok := key.(int); ok { + canIterator = true + index = i + } + } + return +} diff --git a/container/gtree/gtree_avltree.go b/container/gtree/gtree_avltree.go index 49c3593699a..32120c4df8a 100644 --- a/container/gtree/gtree_avltree.go +++ b/container/gtree/gtree_avltree.go @@ -22,20 +22,21 @@ var _ iTree = (*AVLTree)(nil) type AVLTree struct { mu rwmutex.RWMutex root *AVLTreeNode - comparator func(v1, v2 interface{}) int + comparator func(v1, v2 any) int tree *avltree.Tree } // AVLTreeNode is a single element within the tree. type AVLTreeNode struct { - Key interface{} - Value interface{} + Key any + Value any } // NewAVLTree instantiates an AVL tree with the custom key comparator. +// // The parameter `safe` is used to specify whether using tree in concurrent-safety, // which is false in default. -func NewAVLTree(comparator func(v1, v2 interface{}) int, safe ...bool) *AVLTree { +func NewAVLTree(comparator func(v1, v2 any) int, safe ...bool) *AVLTree { return &AVLTree{ mu: rwmutex.Create(safe...), comparator: comparator, @@ -44,9 +45,9 @@ func NewAVLTree(comparator func(v1, v2 interface{}) int, safe ...bool) *AVLTree } // NewAVLTreeFrom instantiates an AVL tree with the custom key comparator and data map. -// The parameter `safe` is used to specify whether using tree in concurrent-safety, -// which is false in default. -func NewAVLTreeFrom(comparator func(v1, v2 interface{}) int, data map[interface{}]interface{}, safe ...bool) *AVLTree { +// +// The parameter `safe` is used to specify whether using tree in concurrent-safety, which is false in default. +func NewAVLTreeFrom(comparator func(v1, v2 any) int, data map[any]any, safe ...bool) *AVLTree { tree := NewAVLTree(comparator, safe...) for k, v := range data { tree.doSet(k, v) @@ -54,22 +55,22 @@ func NewAVLTreeFrom(comparator func(v1, v2 interface{}) int, data map[interface{ return tree } -// Clone returns a new tree with a copy of current tree. +// Clone clones and returns a new tree from current tree. func (tree *AVLTree) Clone() *AVLTree { newTree := NewAVLTree(tree.comparator, tree.mu.IsSafe()) newTree.Sets(tree.Map()) return newTree } -// Set inserts node into the tree. -func (tree *AVLTree) Set(key interface{}, value interface{}) { +// Set sets key-value pair into the tree. +func (tree *AVLTree) Set(key any, value any) { tree.mu.Lock() defer tree.mu.Unlock() tree.doSet(key, value) } // Sets batch sets key-values to the tree. -func (tree *AVLTree) Sets(data map[interface{}]interface{}) { +func (tree *AVLTree) Sets(data map[any]any) { tree.mu.Lock() defer tree.mu.Unlock() for key, value := range data { @@ -78,8 +79,8 @@ func (tree *AVLTree) Sets(data map[interface{}]interface{}) { } // SetIfNotExist sets `value` to the map if the `key` does not exist, and then returns true. -// It returns false if `key` exists, and `value` would be ignored. -func (tree *AVLTree) SetIfNotExist(key interface{}, value interface{}) bool { +// It returns false if `key` exists, and such setting key-value pair operation would be ignored. +func (tree *AVLTree) SetIfNotExist(key any, value any) bool { tree.mu.Lock() defer tree.mu.Unlock() if _, ok := tree.doGet(key); !ok { @@ -90,8 +91,8 @@ func (tree *AVLTree) SetIfNotExist(key interface{}, value interface{}) bool { } // SetIfNotExistFunc sets value with return value of callback function `f`, and then returns true. -// It returns false if `key` exists, and `value` would be ignored. -func (tree *AVLTree) SetIfNotExistFunc(key interface{}, f func() interface{}) bool { +// It returns false if `key` exists, and such setting key-value pair operation would be ignored. +func (tree *AVLTree) SetIfNotExistFunc(key any, f func() any) bool { tree.mu.Lock() defer tree.mu.Unlock() if _, ok := tree.doGet(key); !ok { @@ -102,11 +103,11 @@ func (tree *AVLTree) SetIfNotExistFunc(key interface{}, f func() interface{}) bo } // SetIfNotExistFuncLock sets value with return value of callback function `f`, and then returns true. -// It returns false if `key` exists, and `value` would be ignored. +// It returns false if `key` exists, and such setting key-value pair operation would be ignored. // // SetIfNotExistFuncLock differs with SetIfNotExistFunc function is that -// it executes function `f` with mutex.Lock of the hash map. -func (tree *AVLTree) SetIfNotExistFuncLock(key interface{}, f func() interface{}) bool { +// it executes function `f` within mutex.Lock of the hash map. +func (tree *AVLTree) SetIfNotExistFuncLock(key any, f func() any) bool { tree.mu.Lock() defer tree.mu.Unlock() if _, ok := tree.doGet(key); !ok { @@ -116,15 +117,18 @@ func (tree *AVLTree) SetIfNotExistFuncLock(key interface{}, f func() interface{} return false } -// Get searches the node in the tree by `key` and returns its value or nil if key is not found in tree. -func (tree *AVLTree) Get(key interface{}) (value interface{}) { +// Get searches the `key` in the tree and returns its associated `value` or nil if key is not found in tree. +// +// Note that, the `nil` value from Get function cannot be used to determine key existence, please use Contains function +// to do so. +func (tree *AVLTree) Get(key any) (value any) { value, _ = tree.Search(key) return } -// GetOrSet returns the value by key, -// or sets value with given `value` if it does not exist and then returns this value. -func (tree *AVLTree) GetOrSet(key interface{}, value interface{}) interface{} { +// GetOrSet returns its `value` of `key`, or sets value with given `value` if it does not exist and then returns +// this value. +func (tree *AVLTree) GetOrSet(key any, value any) any { tree.mu.Lock() defer tree.mu.Unlock() if v, ok := tree.doGet(key); !ok { @@ -134,10 +138,9 @@ func (tree *AVLTree) GetOrSet(key interface{}, value interface{}) interface{} { } } -// GetOrSetFunc returns the value by key, -// or sets value with returned value of callback function `f` if it does not exist -// and then returns this value. -func (tree *AVLTree) GetOrSetFunc(key interface{}, f func() interface{}) interface{} { +// GetOrSetFunc returns its `value` of `key`, or sets value with returned value of callback function `f` if it does not +// exist and then returns this value. +func (tree *AVLTree) GetOrSetFunc(key any, f func() any) any { tree.mu.Lock() defer tree.mu.Unlock() if v, ok := tree.doGet(key); !ok { @@ -147,13 +150,12 @@ func (tree *AVLTree) GetOrSetFunc(key interface{}, f func() interface{}) interfa } } -// GetOrSetFuncLock returns the value by key, -// or sets value with returned value of callback function `f` if it does not exist -// and then returns this value. +// GetOrSetFuncLock returns its `value` of `key`, or sets value with returned value of callback function `f` if it does +// not exist and then returns this value. // -// GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function `f` -// with mutex.Lock of the hash map. -func (tree *AVLTree) GetOrSetFuncLock(key interface{}, f func() interface{}) interface{} { +// GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function `f` within mutex.Lock of the hash +// map. +func (tree *AVLTree) GetOrSetFuncLock(key any, f func() any) any { tree.mu.Lock() defer tree.mu.Unlock() if v, ok := tree.doGet(key); !ok { @@ -164,32 +166,40 @@ func (tree *AVLTree) GetOrSetFuncLock(key interface{}, f func() interface{}) int } // GetVar returns a gvar.Var with the value by given `key`. -// The returned gvar.Var is un-concurrent safe. -func (tree *AVLTree) GetVar(key interface{}) *gvar.Var { +// Note that, the returned gvar.Var is un-concurrent safe. +// +// Also see function Get. +func (tree *AVLTree) GetVar(key any) *gvar.Var { return gvar.New(tree.Get(key)) } // GetVarOrSet returns a gvar.Var with result from GetVarOrSet. -// The returned gvar.Var is un-concurrent safe. -func (tree *AVLTree) GetVarOrSet(key interface{}, value interface{}) *gvar.Var { +// Note that, the returned gvar.Var is un-concurrent safe. +// +// Also see function GetOrSet. +func (tree *AVLTree) GetVarOrSet(key any, value any) *gvar.Var { return gvar.New(tree.GetOrSet(key, value)) } // GetVarOrSetFunc returns a gvar.Var with result from GetOrSetFunc. -// The returned gvar.Var is un-concurrent safe. -func (tree *AVLTree) GetVarOrSetFunc(key interface{}, f func() interface{}) *gvar.Var { +// Note that, the returned gvar.Var is un-concurrent safe. +// +// Also see function GetOrSetFunc. +func (tree *AVLTree) GetVarOrSetFunc(key any, f func() any) *gvar.Var { return gvar.New(tree.GetOrSetFunc(key, f)) } // GetVarOrSetFuncLock returns a gvar.Var with result from GetOrSetFuncLock. -// The returned gvar.Var is un-concurrent safe. -func (tree *AVLTree) GetVarOrSetFuncLock(key interface{}, f func() interface{}) *gvar.Var { +// Note that, the returned gvar.Var is un-concurrent safe. +// +// Also see function GetOrSetFuncLock. +func (tree *AVLTree) GetVarOrSetFuncLock(key any, f func() any) *gvar.Var { return gvar.New(tree.GetOrSetFuncLock(key, f)) } // Search searches the tree with given `key`. // Second return parameter `found` is true if key was found, otherwise false. -func (tree *AVLTree) Search(key interface{}) (value interface{}, found bool) { +func (tree *AVLTree) Search(key any) (value any, found bool) { tree.mu.RLock() defer tree.mu.RUnlock() if node, found := tree.doGet(key); found { @@ -198,8 +208,8 @@ func (tree *AVLTree) Search(key interface{}) (value interface{}, found bool) { return nil, false } -// Contains checks whether `key` exists in the tree. -func (tree *AVLTree) Contains(key interface{}) bool { +// Contains checks and returns whether given `key` exists in the tree. +func (tree *AVLTree) Contains(key any) bool { tree.mu.RLock() defer tree.mu.RUnlock() _, ok := tree.doGet(key) @@ -213,23 +223,23 @@ func (tree *AVLTree) Size() int { return tree.tree.Size() } -// IsEmpty returns true if tree does not contain any nodes. +// IsEmpty returns true if the tree does not contain any nodes. func (tree *AVLTree) IsEmpty() bool { tree.mu.RLock() defer tree.mu.RUnlock() return tree.tree.Size() == 0 } -// Remove removes the node from the tree by key. -// Key should adhere to the comparator's type assertion, otherwise method panics. -func (tree *AVLTree) Remove(key interface{}) (value interface{}) { +// Remove removes the node from the tree by `key`, and returns its associated value of `key`. +// The given `key` should adhere to the comparator's type assertion, otherwise method panics. +func (tree *AVLTree) Remove(key any) (value any) { tree.mu.Lock() defer tree.mu.Unlock() return tree.doRemove(key) } -// Removes batch deletes values of the tree by `keys`. -func (tree *AVLTree) Removes(keys []interface{}) { +// Removes batch deletes key-value pairs from the tree by `keys`. +func (tree *AVLTree) Removes(keys []any) { tree.mu.Lock() defer tree.mu.Unlock() for _, key := range keys { @@ -244,22 +254,22 @@ func (tree *AVLTree) Clear() { tree.tree.Clear() } -// Keys returns all keys in asc order. -func (tree *AVLTree) Keys() []interface{} { +// Keys returns all keys from the tree in order by its comparator. +func (tree *AVLTree) Keys() []any { tree.mu.RLock() defer tree.mu.RUnlock() return tree.tree.Keys() } -// Values returns all values in asc order based on the key. -func (tree *AVLTree) Values() []interface{} { +// Values returns all values from the true in order by its comparator based on the key. +func (tree *AVLTree) Values() []any { tree.mu.RLock() defer tree.mu.RUnlock() return tree.tree.Values() } -// Replace the data of the tree with given `data`. -func (tree *AVLTree) Replace(data map[interface{}]interface{}) { +// Replace clears the data of the tree and sets the nodes by given `data`. +func (tree *AVLTree) Replace(data map[any]any) { tree.mu.Lock() defer tree.mu.Unlock() tree.tree.Clear() @@ -273,7 +283,7 @@ func (tree *AVLTree) Print() { fmt.Println(tree.String()) } -// String returns a string representation of container +// String returns a string representation of container. func (tree *AVLTree) String() string { tree.mu.RLock() defer tree.mu.RUnlock() @@ -287,24 +297,20 @@ func (tree *AVLTree) MarshalJSON() (jsonBytes []byte, err error) { return tree.tree.MarshalJSON() } -// Map returns all key-value items as map. -func (tree *AVLTree) Map() map[interface{}]interface{} { - tree.mu.RLock() - defer tree.mu.RUnlock() - m := make(map[interface{}]interface{}, tree.Size()) - tree.IteratorAsc(func(key, value interface{}) bool { +// Map returns all key-value pairs as map. +func (tree *AVLTree) Map() map[any]any { + m := make(map[any]any, tree.Size()) + tree.IteratorAsc(func(key, value any) bool { m[key] = value return true }) return m } -// MapStrAny returns all key-value items as map[string]interface{}. -func (tree *AVLTree) MapStrAny() map[string]interface{} { - tree.mu.RLock() - defer tree.mu.RUnlock() - m := make(map[string]interface{}, tree.Size()) - tree.IteratorAsc(func(key, value interface{}) bool { +// MapStrAny returns all key-value items as map[string]any. +func (tree *AVLTree) MapStrAny() map[string]any { + m := make(map[string]any, tree.Size()) + tree.IteratorAsc(func(key, value any) bool { m[gconv.String(key)] = value return true }) @@ -312,39 +318,48 @@ func (tree *AVLTree) MapStrAny() map[string]interface{} { } // Iterator is alias of IteratorAsc. -func (tree *AVLTree) Iterator(f func(key, value interface{}) bool) { +// +// Also see IteratorAsc. +func (tree *AVLTree) Iterator(f func(key, value any) bool) { tree.IteratorAsc(f) } // IteratorFrom is alias of IteratorAscFrom. -func (tree *AVLTree) IteratorFrom(key interface{}, match bool, f func(key, value interface{}) bool) { +// +// Also see IteratorAscFrom. +func (tree *AVLTree) IteratorFrom(key any, match bool, f func(key, value any) bool) { tree.IteratorAscFrom(key, match, f) } // IteratorAsc iterates the tree readonly in ascending order with given callback function `f`. -// If `f` returns true, then it continues iterating; or false to stop. -func (tree *AVLTree) IteratorAsc(f func(key, value interface{}) bool) { +// If callback function `f` returns true, then it continues iterating; or false to stop. +func (tree *AVLTree) IteratorAsc(f func(key, value any) bool) { tree.mu.RLock() defer tree.mu.RUnlock() - it := tree.tree.Iterator() + var ( + ok bool + it = tree.tree.Iterator() + ) for it.Begin(); it.Next(); { index, value := it.Key(), it.Value() - if ok := f(index, value); !ok { + if ok = f(index, value); !ok { break } } } // IteratorAscFrom iterates the tree readonly in ascending order with given callback function `f`. -// The parameter `key` specifies the start entry for iterating. The `match` specifies whether -// starting iterating if the `key` is fully matched, or else using index searching iterating. -// If `f` returns true, then it continues iterating; or false to stop. -func (tree *AVLTree) IteratorAscFrom(key interface{}, match bool, f func(key, value interface{}) bool) { +// +// The parameter `key` specifies the start entry for iterating. +// The parameter `match` specifies whether starting iterating only if the `key` is fully matched, or else using index +// searching iterating. +// If callback function `f` returns true, then it continues iterating; or false to stop. +func (tree *AVLTree) IteratorAscFrom(key any, match bool, f func(key, value any) bool) { tree.mu.RLock() defer tree.mu.RUnlock() var keys = tree.tree.Keys() - index, isIterator := tree.iteratorFromGetIndex(key, keys, match) - if !isIterator { + index, canIterator := iteratorFromGetIndex(key, keys, match) + if !canIterator { return } for ; index < len(keys); index++ { @@ -353,29 +368,35 @@ func (tree *AVLTree) IteratorAscFrom(key interface{}, match bool, f func(key, va } // IteratorDesc iterates the tree readonly in descending order with given callback function `f`. -// If `f` returns true, then it continues iterating; or false to stop. -func (tree *AVLTree) IteratorDesc(f func(key, value interface{}) bool) { +// +// If callback function `f` returns true, then it continues iterating; or false to stop. +func (tree *AVLTree) IteratorDesc(f func(key, value any) bool) { tree.mu.RLock() defer tree.mu.RUnlock() - it := tree.tree.Iterator() + var ( + ok bool + it = tree.tree.Iterator() + ) for it.End(); it.Prev(); { index, value := it.Key(), it.Value() - if ok := f(index, value); !ok { + if ok = f(index, value); !ok { break } } } // IteratorDescFrom iterates the tree readonly in descending order with given callback function `f`. -// The parameter `key` specifies the start entry for iterating. The `match` specifies whether -// starting iterating if the `key` is fully matched, or else using index searching iterating. -// If `f` returns true, then it continues iterating; or false to stop. -func (tree *AVLTree) IteratorDescFrom(key interface{}, match bool, f func(key, value interface{}) bool) { +// +// The parameter `key` specifies the start entry for iterating. +// The parameter `match` specifies whether starting iterating only if the `key` is fully matched, or else using index +// searching iterating. +// If callback function `f` returns true, then it continues iterating; or false to stop. +func (tree *AVLTree) IteratorDescFrom(key any, match bool, f func(key, value any) bool) { tree.mu.RLock() defer tree.mu.RUnlock() var keys = tree.tree.Keys() - index, isIterator := tree.iteratorFromGetIndex(key, keys, match) - if !isIterator { + index, canIterator := iteratorFromGetIndex(key, keys, match) + if !canIterator { return } for ; index >= 0; index-- { @@ -383,8 +404,7 @@ func (tree *AVLTree) IteratorDescFrom(key interface{}, match bool, f func(key, v } } -// Left returns the minimum element of the AVL tree -// or nil if the tree is empty. +// Left returns the minimum element corresponding to the comparator of the tree or nil if the tree is empty. func (tree *AVLTree) Left() *AVLTreeNode { tree.mu.RLock() defer tree.mu.RUnlock() @@ -398,8 +418,7 @@ func (tree *AVLTree) Left() *AVLTreeNode { } } -// Right returns the maximum element of the AVL tree -// or nil if the tree is empty. +// Right returns the maximum element corresponding to the comparator of the tree or nil if the tree is empty. func (tree *AVLTree) Right() *AVLTreeNode { tree.mu.RLock() defer tree.mu.RUnlock() @@ -413,19 +432,19 @@ func (tree *AVLTree) Right() *AVLTreeNode { } } -// Floor Finds floor node of the input key, return the floor node or nil if no floor node is found. -// Second return parameter is true if floor was found, otherwise false. +// Floor Finds floor node of the input key, returns the floor node or nil if no floor node is found. +// The second returned parameter `found` is true if floor was found, otherwise false. // // Floor node is defined as the largest node that is smaller than or equal to the given node. // A floor node may not be found, either because the tree is empty, or because // all nodes in the tree is larger than the given node. // // Key should adhere to the comparator's type assertion, otherwise method panics. -func (tree *AVLTree) Floor(key interface{}) (floor *AVLTreeNode, found bool) { +func (tree *AVLTree) Floor(key any) (floor *AVLTreeNode, found bool) { tree.mu.RLock() defer tree.mu.RUnlock() - node, found := tree.tree.Floor(key) - if !found { + node, ok := tree.tree.Floor(key) + if !ok { return nil, false } return &AVLTreeNode{ @@ -434,19 +453,19 @@ func (tree *AVLTree) Floor(key interface{}) (floor *AVLTreeNode, found bool) { }, true } -// Ceiling finds ceiling node of the input key, return the ceiling node or nil if no ceiling node is found. -// Second return parameter is true if ceiling was found, otherwise false. +// Ceiling finds ceiling node of the input key, returns the ceiling node or nil if no ceiling node is found. +// The second return parameter `found` is true if ceiling was found, otherwise false. // // Ceiling node is defined as the smallest node that is larger than or equal to the given node. // A ceiling node may not be found, either because the tree is empty, or because // all nodes in the tree is smaller than the given node. // // Key should adhere to the comparator's type assertion, otherwise method panics. -func (tree *AVLTree) Ceiling(key interface{}) (ceiling *AVLTreeNode, found bool) { +func (tree *AVLTree) Ceiling(key any) (ceiling *AVLTreeNode, found bool) { tree.mu.RLock() defer tree.mu.RUnlock() - node, found := tree.tree.Ceiling(key) - if !found { + node, ok := tree.tree.Ceiling(key) + if !ok { return nil, false } return &AVLTreeNode{ @@ -460,14 +479,14 @@ func (tree *AVLTree) Ceiling(key interface{}) (ceiling *AVLTreeNode, found bool) // or else the comparator would panic. // // If the type of value is different with key, you pass the new `comparator`. -func (tree *AVLTree) Flip(comparator ...func(v1, v2 interface{}) int) { +func (tree *AVLTree) Flip(comparator ...func(v1, v2 any) int) { var t = new(AVLTree) if len(comparator) > 0 { t = NewAVLTree(comparator[0], tree.mu.IsSafe()) } else { t = NewAVLTree(tree.comparator, tree.mu.IsSafe()) } - tree.IteratorAsc(func(key, value interface{}) bool { + tree.IteratorAsc(func(key, value any) bool { t.doSet(value, key) return true }) @@ -475,9 +494,13 @@ func (tree *AVLTree) Flip(comparator ...func(v1, v2 interface{}) int) { tree.Sets(t.Map()) } -// doSet sets key-value pair to the tree. -func (tree *AVLTree) doSet(key, value interface{}) interface{} { - if f, ok := value.(func() interface{}); ok { +// doSet inserts key-value pair node into the tree without lock. +// If `key` already exists, then its value is updated with the new value. +// If `value` is type of , it will be executed and its return value will be set to the map with `key`. +// +// It returns value with given `key`. +func (tree *AVLTree) doSet(key, value any) any { + if f, ok := value.(func() any); ok { value = f() } if value == nil { @@ -487,35 +510,15 @@ func (tree *AVLTree) doSet(key, value interface{}) interface{} { return value } -// doGet retrieves and returns the value of given key from tree. -func (tree *AVLTree) doGet(key interface{}) (value interface{}, found bool) { +// doGet retrieves and returns the value of given key from tree without lock. +func (tree *AVLTree) doGet(key any) (value any, found bool) { return tree.tree.Get(key) } -// doRemove removes key from tree. -func (tree *AVLTree) doRemove(key interface{}) (value interface{}) { +// doRemove removes key from tree and returns its associated value without lock. +// Note that, the given `key` should adhere to the comparator's type assertion, otherwise method panics. +func (tree *AVLTree) doRemove(key any) (value any) { value, _ = tree.tree.Get(key) tree.tree.Remove(key) return } - -// iteratorFromGetIndex returns the index of the key in the keys slice. -// The parameter `match` specifies whether starting iterating if the `key` is fully matched, -// or else using index searching iterating. -// If `isIterator` is true, iterator is available; or else not. -func (tree *AVLTree) iteratorFromGetIndex(key interface{}, keys []interface{}, match bool) (index int, isIterator bool) { - if match { - for i, k := range keys { - if k == key { - isIterator = true - index = i - } - } - } else { - if i, ok := key.(int); ok { - isIterator = true - index = i - } - } - return -} diff --git a/container/gtree/gtree_btree.go b/container/gtree/gtree_btree.go index a7b56c538d2..8d75684960c 100644 --- a/container/gtree/gtree_btree.go +++ b/container/gtree/gtree_btree.go @@ -21,22 +21,22 @@ var _ iTree = (*BTree)(nil) // BTree holds elements of the B-tree. type BTree struct { mu rwmutex.RWMutex - comparator func(v1, v2 interface{}) int + comparator func(v1, v2 any) int m int // order (maximum number of children) tree *btree.Tree } // BTreeEntry represents the key-value pair contained within nodes. type BTreeEntry struct { - Key interface{} - Value interface{} + Key any + Value any } // NewBTree instantiates a B-tree with `m` (maximum number of children) and a custom key comparator. // The parameter `safe` is used to specify whether using tree in concurrent-safety, // which is false in default. // Note that the `m` must be greater or equal than 3, or else it panics. -func NewBTree(m int, comparator func(v1, v2 interface{}) int, safe ...bool) *BTree { +func NewBTree(m int, comparator func(v1, v2 any) int, safe ...bool) *BTree { return &BTree{ mu: rwmutex.Create(safe...), m: m, @@ -48,7 +48,7 @@ func NewBTree(m int, comparator func(v1, v2 interface{}) int, safe ...bool) *BTr // NewBTreeFrom instantiates a B-tree with `m` (maximum number of children), a custom key comparator and data map. // The parameter `safe` is used to specify whether using tree in concurrent-safety, // which is false in default. -func NewBTreeFrom(m int, comparator func(v1, v2 interface{}) int, data map[interface{}]interface{}, safe ...bool) *BTree { +func NewBTreeFrom(m int, comparator func(v1, v2 any) int, data map[any]any, safe ...bool) *BTree { tree := NewBTree(m, comparator, safe...) for k, v := range data { tree.doSet(k, v) @@ -56,22 +56,22 @@ func NewBTreeFrom(m int, comparator func(v1, v2 interface{}) int, data map[inter return tree } -// Clone returns a new tree with a copy of current tree. +// Clone clones and returns a new tree from current tree. func (tree *BTree) Clone() *BTree { newTree := NewBTree(tree.m, tree.comparator, tree.mu.IsSafe()) newTree.Sets(tree.Map()) return newTree } -// Set inserts key-value item into the tree. -func (tree *BTree) Set(key interface{}, value interface{}) { +// Set sets key-value pair into the tree. +func (tree *BTree) Set(key any, value any) { tree.mu.Lock() defer tree.mu.Unlock() tree.doSet(key, value) } // Sets batch sets key-values to the tree. -func (tree *BTree) Sets(data map[interface{}]interface{}) { +func (tree *BTree) Sets(data map[any]any) { tree.mu.Lock() defer tree.mu.Unlock() for k, v := range data { @@ -80,8 +80,8 @@ func (tree *BTree) Sets(data map[interface{}]interface{}) { } // SetIfNotExist sets `value` to the map if the `key` does not exist, and then returns true. -// It returns false if `key` exists, and `value` would be ignored. -func (tree *BTree) SetIfNotExist(key interface{}, value interface{}) bool { +// It returns false if `key` exists, and such setting key-value pair operation would be ignored. +func (tree *BTree) SetIfNotExist(key any, value any) bool { tree.mu.Lock() defer tree.mu.Unlock() if _, ok := tree.doGet(key); !ok { @@ -92,8 +92,8 @@ func (tree *BTree) SetIfNotExist(key interface{}, value interface{}) bool { } // SetIfNotExistFunc sets value with return value of callback function `f`, and then returns true. -// It returns false if `key` exists, and `value` would be ignored. -func (tree *BTree) SetIfNotExistFunc(key interface{}, f func() interface{}) bool { +// It returns false if `key` exists, and such setting key-value pair operation would be ignored. +func (tree *BTree) SetIfNotExistFunc(key any, f func() any) bool { tree.mu.Lock() defer tree.mu.Unlock() if _, ok := tree.doGet(key); !ok { @@ -104,11 +104,11 @@ func (tree *BTree) SetIfNotExistFunc(key interface{}, f func() interface{}) bool } // SetIfNotExistFuncLock sets value with return value of callback function `f`, and then returns true. -// It returns false if `key` exists, and `value` would be ignored. +// It returns false if `key` exists, and such setting key-value pair operation would be ignored. // // SetIfNotExistFuncLock differs with SetIfNotExistFunc function is that -// it executes function `f` with mutex.Lock of the hash map. -func (tree *BTree) SetIfNotExistFuncLock(key interface{}, f func() interface{}) bool { +// it executes function `f` within mutex.Lock of the hash map. +func (tree *BTree) SetIfNotExistFuncLock(key any, f func() any) bool { tree.mu.Lock() defer tree.mu.Unlock() if _, ok := tree.doGet(key); !ok { @@ -118,17 +118,20 @@ func (tree *BTree) SetIfNotExistFuncLock(key interface{}, f func() interface{}) return false } -// Get searches the node in the tree by `key` and returns its value or nil if key is not found in tree. -func (tree *BTree) Get(key interface{}) (value interface{}) { +// Get searches the `key` in the tree and returns its associated `value` or nil if key is not found in tree. +// +// Note that, the `nil` value from Get function cannot be used to determine key existence, please use Contains function +// to do so. +func (tree *BTree) Get(key any) (value any) { tree.mu.Lock() defer tree.mu.Unlock() value, _ = tree.doGet(key) return } -// GetOrSet returns the value by key, -// or sets value with given `value` if it does not exist and then returns this value. -func (tree *BTree) GetOrSet(key interface{}, value interface{}) interface{} { +// GetOrSet returns its `value` of `key`, or sets value with given `value` if it does not exist and then returns +// this value. +func (tree *BTree) GetOrSet(key any, value any) any { tree.mu.Lock() defer tree.mu.Unlock() if v, ok := tree.doGet(key); !ok { @@ -138,10 +141,9 @@ func (tree *BTree) GetOrSet(key interface{}, value interface{}) interface{} { } } -// GetOrSetFunc returns the value by key, -// or sets value with returned value of callback function `f` if it does not exist -// and then returns this value. -func (tree *BTree) GetOrSetFunc(key interface{}, f func() interface{}) interface{} { +// GetOrSetFunc returns its `value` of `key`, or sets value with returned value of callback function `f` if it does not +// exist and then returns this value. +func (tree *BTree) GetOrSetFunc(key any, f func() any) any { tree.mu.Lock() defer tree.mu.Unlock() if v, ok := tree.doGet(key); !ok { @@ -151,13 +153,12 @@ func (tree *BTree) GetOrSetFunc(key interface{}, f func() interface{}) interface } } -// GetOrSetFuncLock returns the value by key, -// or sets value with returned value of callback function `f` if it does not exist -// and then returns this value. +// GetOrSetFuncLock returns its `value` of `key`, or sets value with returned value of callback function `f` if it does +// not exist and then returns this value. // -// GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function `f` -// with mutex.Lock of the hash map. -func (tree *BTree) GetOrSetFuncLock(key interface{}, f func() interface{}) interface{} { +// GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function `f` within mutex.Lock of the hash +// map. +func (tree *BTree) GetOrSetFuncLock(key any, f func() any) any { tree.mu.Lock() defer tree.mu.Unlock() if v, ok := tree.doGet(key); !ok { @@ -168,39 +169,47 @@ func (tree *BTree) GetOrSetFuncLock(key interface{}, f func() interface{}) inter } // GetVar returns a gvar.Var with the value by given `key`. -// The returned gvar.Var is un-concurrent safe. -func (tree *BTree) GetVar(key interface{}) *gvar.Var { +// Note that, the returned gvar.Var is un-concurrent safe. +// +// Also see function Get. +func (tree *BTree) GetVar(key any) *gvar.Var { return gvar.New(tree.Get(key)) } // GetVarOrSet returns a gvar.Var with result from GetVarOrSet. -// The returned gvar.Var is un-concurrent safe. -func (tree *BTree) GetVarOrSet(key interface{}, value interface{}) *gvar.Var { +// Note that, the returned gvar.Var is un-concurrent safe. +// +// Also see function GetOrSet. +func (tree *BTree) GetVarOrSet(key any, value any) *gvar.Var { return gvar.New(tree.GetOrSet(key, value)) } // GetVarOrSetFunc returns a gvar.Var with result from GetOrSetFunc. -// The returned gvar.Var is un-concurrent safe. -func (tree *BTree) GetVarOrSetFunc(key interface{}, f func() interface{}) *gvar.Var { +// Note that, the returned gvar.Var is un-concurrent safe. +// +// Also see function GetOrSetFunc. +func (tree *BTree) GetVarOrSetFunc(key any, f func() any) *gvar.Var { return gvar.New(tree.GetOrSetFunc(key, f)) } // GetVarOrSetFuncLock returns a gvar.Var with result from GetOrSetFuncLock. -// The returned gvar.Var is un-concurrent safe. -func (tree *BTree) GetVarOrSetFuncLock(key interface{}, f func() interface{}) *gvar.Var { +// Note that, the returned gvar.Var is un-concurrent safe. +// +// Also see function GetOrSetFuncLock. +func (tree *BTree) GetVarOrSetFuncLock(key any, f func() any) *gvar.Var { return gvar.New(tree.GetOrSetFuncLock(key, f)) } // Search searches the tree with given `key`. // Second return parameter `found` is true if key was found, otherwise false. -func (tree *BTree) Search(key interface{}) (value interface{}, found bool) { +func (tree *BTree) Search(key any) (value any, found bool) { tree.mu.RLock() defer tree.mu.RUnlock() return tree.tree.Get(key) } -// Contains checks whether `key` exists in the tree. -func (tree *BTree) Contains(key interface{}) bool { +// Contains checks and returns whether given `key` exists in the tree. +func (tree *BTree) Contains(key any) bool { tree.mu.RLock() defer tree.mu.RUnlock() _, ok := tree.doGet(key) @@ -221,15 +230,16 @@ func (tree *BTree) IsEmpty() bool { return tree.tree.Size() == 0 } -// Remove removes the node from the tree by `key`. -func (tree *BTree) Remove(key interface{}) (value interface{}) { +// Remove removes the node from the tree by `key`, and returns its associated value of `key`. +// The given `key` should adhere to the comparator's type assertion, otherwise method panics. +func (tree *BTree) Remove(key any) (value any) { tree.mu.Lock() defer tree.mu.Unlock() return tree.doRemove(key) } -// Removes batch deletes values of the tree by `keys`. -func (tree *BTree) Removes(keys []interface{}) { +// Removes batch deletes key-value pairs from the tree by `keys`. +func (tree *BTree) Removes(keys []any) { tree.mu.Lock() defer tree.mu.Unlock() for _, key := range keys { @@ -244,22 +254,22 @@ func (tree *BTree) Clear() { tree.tree.Clear() } -// Keys returns all keys in asc order. -func (tree *BTree) Keys() []interface{} { +// Keys returns all keys from the tree in order by its comparator. +func (tree *BTree) Keys() []any { tree.mu.RLock() defer tree.mu.RUnlock() return tree.tree.Keys() } -// Values returns all values in asc order based on the key. -func (tree *BTree) Values() []interface{} { +// Values returns all values from the true in order by its comparator based on the key. +func (tree *BTree) Values() []any { tree.mu.RLock() defer tree.mu.RUnlock() return tree.tree.Values() } -// Replace the data of the tree with given `data`. -func (tree *BTree) Replace(data map[interface{}]interface{}) { +// Replace clears the data of the tree and sets the nodes by given `data`. +func (tree *BTree) Replace(data map[any]any) { tree.mu.Lock() defer tree.mu.Unlock() tree.tree.Clear() @@ -268,24 +278,20 @@ func (tree *BTree) Replace(data map[interface{}]interface{}) { } } -// Map returns all key-value items as map. -func (tree *BTree) Map() map[interface{}]interface{} { - tree.mu.RLock() - defer tree.mu.RUnlock() - m := make(map[interface{}]interface{}, tree.Size()) - tree.IteratorAsc(func(key, value interface{}) bool { +// Map returns all key-value pairs as map. +func (tree *BTree) Map() map[any]any { + m := make(map[any]any, tree.Size()) + tree.IteratorAsc(func(key, value any) bool { m[key] = value return true }) return m } -// MapStrAny returns all key-value items as map[string]interface{}. -func (tree *BTree) MapStrAny() map[string]interface{} { - tree.mu.RLock() - defer tree.mu.RUnlock() - m := make(map[string]interface{}, tree.Size()) - tree.IteratorAsc(func(key, value interface{}) bool { +// MapStrAny returns all key-value items as map[string]any. +func (tree *BTree) MapStrAny() map[string]any { + m := make(map[string]any, tree.Size()) + tree.IteratorAsc(func(key, value any) bool { m[gconv.String(key)] = value return true }) @@ -312,39 +318,48 @@ func (tree *BTree) MarshalJSON() (jsonBytes []byte, err error) { } // Iterator is alias of IteratorAsc. -func (tree *BTree) Iterator(f func(key, value interface{}) bool) { +// +// Also see IteratorAsc. +func (tree *BTree) Iterator(f func(key, value any) bool) { tree.IteratorAsc(f) } // IteratorFrom is alias of IteratorAscFrom. -func (tree *BTree) IteratorFrom(key interface{}, match bool, f func(key, value interface{}) bool) { +// +// Also see IteratorAscFrom. +func (tree *BTree) IteratorFrom(key any, match bool, f func(key, value any) bool) { tree.IteratorAscFrom(key, match, f) } // IteratorAsc iterates the tree readonly in ascending order with given callback function `f`. -// If `f` returns true, then it continues iterating; or false to stop. -func (tree *BTree) IteratorAsc(f func(key, value interface{}) bool) { +// If callback function `f` returns true, then it continues iterating; or false to stop. +func (tree *BTree) IteratorAsc(f func(key, value any) bool) { tree.mu.RLock() defer tree.mu.RUnlock() - it := tree.tree.Iterator() + var ( + ok bool + it = tree.tree.Iterator() + ) for it.Begin(); it.Next(); { index, value := it.Key(), it.Value() - if ok := f(index, value); !ok { + if ok = f(index, value); !ok { break } } } // IteratorAscFrom iterates the tree readonly in ascending order with given callback function `f`. -// The parameter `key` specifies the start entry for iterating. The `match` specifies whether -// starting iterating if the `key` is fully matched, or else using index searching iterating. -// If `f` returns true, then it continues iterating; or false to stop. -func (tree *BTree) IteratorAscFrom(key interface{}, match bool, f func(key, value interface{}) bool) { +// +// The parameter `key` specifies the start entry for iterating. +// The parameter `match` specifies whether starting iterating only if the `key` is fully matched, or else using index +// searching iterating. +// If callback function `f` returns true, then it continues iterating; or false to stop. +func (tree *BTree) IteratorAscFrom(key any, match bool, f func(key, value any) bool) { tree.mu.RLock() defer tree.mu.RUnlock() var keys = tree.tree.Keys() - index, isIterator := tree.iteratorFromGetIndex(key, keys, match) - if !isIterator { + index, canIterator := iteratorFromGetIndex(key, keys, match) + if !canIterator { return } for ; index < len(keys); index++ { @@ -353,29 +368,35 @@ func (tree *BTree) IteratorAscFrom(key interface{}, match bool, f func(key, valu } // IteratorDesc iterates the tree readonly in descending order with given callback function `f`. -// If `f` returns true, then it continues iterating; or false to stop. -func (tree *BTree) IteratorDesc(f func(key, value interface{}) bool) { +// +// If callback function `f` returns true, then it continues iterating; or false to stop. +func (tree *BTree) IteratorDesc(f func(key, value any) bool) { tree.mu.RLock() defer tree.mu.RUnlock() - it := tree.tree.Iterator() + var ( + ok bool + it = tree.tree.Iterator() + ) for it.End(); it.Prev(); { index, value := it.Key(), it.Value() - if ok := f(index, value); !ok { + if ok = f(index, value); !ok { break } } } // IteratorDescFrom iterates the tree readonly in descending order with given callback function `f`. -// The parameter `key` specifies the start entry for iterating. The `match` specifies whether -// starting iterating if the `key` is fully matched, or else using index searching iterating. -// If `f` returns true, then it continues iterating; or false to stop. -func (tree *BTree) IteratorDescFrom(key interface{}, match bool, f func(key, value interface{}) bool) { +// +// The parameter `key` specifies the start entry for iterating. +// The parameter `match` specifies whether starting iterating only if the `key` is fully matched, or else using index +// searching iterating. +// If callback function `f` returns true, then it continues iterating; or false to stop. +func (tree *BTree) IteratorDescFrom(key any, match bool, f func(key, value any) bool) { tree.mu.RLock() defer tree.mu.RUnlock() var keys = tree.tree.Keys() - index, isIterator := tree.iteratorFromGetIndex(key, keys, match) - if !isIterator { + index, canIterator := iteratorFromGetIndex(key, keys, match) + if !canIterator { return } for ; index >= 0; index-- { @@ -390,7 +411,7 @@ func (tree *BTree) Height() int { return tree.tree.Height() } -// Left returns the left-most (min) entry or nil if tree is empty. +// Left returns the minimum element corresponding to the comparator of the tree or nil if the tree is empty. func (tree *BTree) Left() *BTreeEntry { tree.mu.RLock() defer tree.mu.RUnlock() @@ -404,7 +425,7 @@ func (tree *BTree) Left() *BTreeEntry { } } -// Right returns the right-most (max) entry or nil if tree is empty. +// Right returns the maximum element corresponding to the comparator of the tree or nil if the tree is empty. func (tree *BTree) Right() *BTreeEntry { tree.mu.RLock() defer tree.mu.RUnlock() @@ -418,14 +439,13 @@ func (tree *BTree) Right() *BTreeEntry { } } -// doSet inserts key-value pair node into the tree. -// If key already exists, then its value is updated with the new value. -// If `value` is type of , -// it will be executed and its return value will be set to the map with `key`. +// doSet inserts key-value pair node into the tree without lock. +// If `key` already exists, then its value is updated with the new value. +// If `value` is type of , it will be executed and its return value will be set to the map with `key`. // // It returns value with given `key`. -func (tree *BTree) doSet(key interface{}, value interface{}) interface{} { - if f, ok := value.(func() interface{}); ok { +func (tree *BTree) doSet(key any, value any) any { + if f, ok := value.(func() any); ok { value = f() } if value == nil { @@ -435,36 +455,15 @@ func (tree *BTree) doSet(key interface{}, value interface{}) interface{} { return value } -// doGet get the value from the tree by key. -func (tree *BTree) doGet(key interface{}) (value interface{}, ok bool) { +// doGet get the value from the tree by key without lock. +func (tree *BTree) doGet(key any) (value any, ok bool) { return tree.tree.Get(key) } -// doRemove removes the node from the tree by key. -// Key should adhere to the comparator's type assertion, otherwise method panics. -func (tree *BTree) doRemove(key interface{}) (value interface{}) { +// doRemove removes key from tree and returns its associated value without lock. +// Note that, the given `key` should adhere to the comparator's type assertion, otherwise method panics. +func (tree *BTree) doRemove(key any) (value any) { value, _ = tree.tree.Get(key) tree.tree.Remove(key) return } - -// iteratorFromGetIndex returns the index of the key in the keys slice. -// The parameter `match` specifies whether starting iterating if the `key` is fully matched, -// or else using index searching iterating. -// If `isIterator` is true, iterator is available; or else not. -func (tree *BTree) iteratorFromGetIndex(key interface{}, keys []interface{}, match bool) (index int, isIterator bool) { - if match { - for i, k := range keys { - if k == key { - isIterator = true - index = i - } - } - } else { - if i, ok := key.(int); ok { - isIterator = true - index = i - } - } - return -} diff --git a/container/gtree/gtree_redblacktree.go b/container/gtree/gtree_redblacktree.go index 429be2fb6fe..1d275e0ac6c 100644 --- a/container/gtree/gtree_redblacktree.go +++ b/container/gtree/gtree_redblacktree.go @@ -23,20 +23,20 @@ var _ iTree = (*RedBlackTree)(nil) // RedBlackTree holds elements of the red-black tree. type RedBlackTree struct { mu rwmutex.RWMutex - comparator func(v1, v2 interface{}) int + comparator func(v1, v2 any) int tree *redblacktree.Tree } // RedBlackTreeNode is a single element within the tree. type RedBlackTreeNode struct { - Key interface{} - Value interface{} + Key any + Value any } // NewRedBlackTree instantiates a red-black tree with the custom key comparator. // The parameter `safe` is used to specify whether using tree in concurrent-safety, // which is false in default. -func NewRedBlackTree(comparator func(v1, v2 interface{}) int, safe ...bool) *RedBlackTree { +func NewRedBlackTree(comparator func(v1, v2 any) int, safe ...bool) *RedBlackTree { return &RedBlackTree{ mu: rwmutex.Create(safe...), comparator: comparator, @@ -47,7 +47,7 @@ func NewRedBlackTree(comparator func(v1, v2 interface{}) int, safe ...bool) *Red // NewRedBlackTreeFrom instantiates a red-black tree with the custom key comparator and `data` map. // The parameter `safe` is used to specify whether using tree in concurrent-safety, // which is false in default. -func NewRedBlackTreeFrom(comparator func(v1, v2 interface{}) int, data map[interface{}]interface{}, safe ...bool) *RedBlackTree { +func NewRedBlackTreeFrom(comparator func(v1, v2 any) int, data map[any]any, safe ...bool) *RedBlackTree { tree := NewRedBlackTree(comparator, safe...) for k, v := range data { tree.doSet(k, v) @@ -56,7 +56,7 @@ func NewRedBlackTreeFrom(comparator func(v1, v2 interface{}) int, data map[inter } // SetComparator sets/changes the comparator for sorting. -func (tree *RedBlackTree) SetComparator(comparator func(a, b interface{}) int) { +func (tree *RedBlackTree) SetComparator(comparator func(a, b any) int) { tree.comparator = comparator if tree.tree == nil { tree.tree = redblacktree.NewWith(comparator) @@ -68,22 +68,22 @@ func (tree *RedBlackTree) SetComparator(comparator func(a, b interface{}) int) { } } -// Clone returns a new tree with a copy of current tree. +// Clone clones and returns a new tree from current tree. func (tree *RedBlackTree) Clone() *RedBlackTree { newTree := NewRedBlackTree(tree.comparator, tree.mu.IsSafe()) newTree.Sets(tree.Map()) return newTree } -// Set inserts node into the tree. -func (tree *RedBlackTree) Set(key interface{}, value interface{}) { +// Set sets key-value pair into the tree. +func (tree *RedBlackTree) Set(key any, value any) { tree.mu.Lock() defer tree.mu.Unlock() tree.doSet(key, value) } // Sets batch sets key-values to the tree. -func (tree *RedBlackTree) Sets(data map[interface{}]interface{}) { +func (tree *RedBlackTree) Sets(data map[any]any) { tree.mu.Lock() defer tree.mu.Unlock() for key, value := range data { @@ -92,8 +92,8 @@ func (tree *RedBlackTree) Sets(data map[interface{}]interface{}) { } // SetIfNotExist sets `value` to the map if the `key` does not exist, and then returns true. -// It returns false if `key` exists, and `value` would be ignored. -func (tree *RedBlackTree) SetIfNotExist(key interface{}, value interface{}) bool { +// It returns false if `key` exists, and such setting key-value pair operation would be ignored. +func (tree *RedBlackTree) SetIfNotExist(key any, value any) bool { tree.mu.Lock() defer tree.mu.Unlock() if _, ok := tree.doGet(key); !ok { @@ -104,8 +104,8 @@ func (tree *RedBlackTree) SetIfNotExist(key interface{}, value interface{}) bool } // SetIfNotExistFunc sets value with return value of callback function `f`, and then returns true. -// It returns false if `key` exists, and `value` would be ignored. -func (tree *RedBlackTree) SetIfNotExistFunc(key interface{}, f func() interface{}) bool { +// It returns false if `key` exists, and such setting key-value pair operation would be ignored. +func (tree *RedBlackTree) SetIfNotExistFunc(key any, f func() any) bool { tree.mu.Lock() defer tree.mu.Unlock() if _, ok := tree.doGet(key); !ok { @@ -116,11 +116,11 @@ func (tree *RedBlackTree) SetIfNotExistFunc(key interface{}, f func() interface{ } // SetIfNotExistFuncLock sets value with return value of callback function `f`, and then returns true. -// It returns false if `key` exists, and `value` would be ignored. +// It returns false if `key` exists, and such setting key-value pair operation would be ignored. // // SetIfNotExistFuncLock differs with SetIfNotExistFunc function is that -// it executes function `f` with mutex.Lock of the hash map. -func (tree *RedBlackTree) SetIfNotExistFuncLock(key interface{}, f func() interface{}) bool { +// it executes function `f` within mutex.Lock of the hash map. +func (tree *RedBlackTree) SetIfNotExistFuncLock(key any, f func() any) bool { tree.mu.Lock() defer tree.mu.Unlock() if _, ok := tree.doGet(key); !ok { @@ -130,15 +130,18 @@ func (tree *RedBlackTree) SetIfNotExistFuncLock(key interface{}, f func() interf return false } -// Get searches the node in the tree by `key` and returns its value or nil if key is not found in tree. -func (tree *RedBlackTree) Get(key interface{}) (value interface{}) { +// Get searches the `key` in the tree and returns its associated `value` or nil if key is not found in tree. +// +// Note that, the `nil` value from Get function cannot be used to determine key existence, please use Contains function +// to do so. +func (tree *RedBlackTree) Get(key any) (value any) { value, _ = tree.Search(key) return } -// GetOrSet returns the value by key, -// or sets value with given `value` if it does not exist and then returns this value. -func (tree *RedBlackTree) GetOrSet(key interface{}, value interface{}) interface{} { +// GetOrSet returns its `value` of `key`, or sets value with given `value` if it does not exist and then returns +// this value. +func (tree *RedBlackTree) GetOrSet(key any, value any) any { tree.mu.Lock() defer tree.mu.Unlock() if v, ok := tree.doGet(key); !ok { @@ -148,10 +151,9 @@ func (tree *RedBlackTree) GetOrSet(key interface{}, value interface{}) interface } } -// GetOrSetFunc returns the value by key, -// or sets value with returned value of callback function `f` if it does not exist -// and then returns this value. -func (tree *RedBlackTree) GetOrSetFunc(key interface{}, f func() interface{}) interface{} { +// GetOrSetFunc returns its `value` of `key`, or sets value with returned value of callback function `f` if it does not +// exist and then returns this value. +func (tree *RedBlackTree) GetOrSetFunc(key any, f func() any) any { tree.mu.Lock() defer tree.mu.Unlock() if v, ok := tree.doGet(key); !ok { @@ -161,13 +163,12 @@ func (tree *RedBlackTree) GetOrSetFunc(key interface{}, f func() interface{}) in } } -// GetOrSetFuncLock returns the value by key, -// or sets value with returned value of callback function `f` if it does not exist -// and then returns this value. +// GetOrSetFuncLock returns its `value` of `key`, or sets value with returned value of callback function `f` if it does +// not exist and then returns this value. // -// GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function `f` -// with mutex.Lock of the hash map. -func (tree *RedBlackTree) GetOrSetFuncLock(key interface{}, f func() interface{}) interface{} { +// GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function `f` within mutex.Lock of the hash +// map. +func (tree *RedBlackTree) GetOrSetFuncLock(key any, f func() any) any { tree.mu.Lock() defer tree.mu.Unlock() if v, ok := tree.doGet(key); !ok { @@ -178,32 +179,40 @@ func (tree *RedBlackTree) GetOrSetFuncLock(key interface{}, f func() interface{} } // GetVar returns a gvar.Var with the value by given `key`. -// The returned gvar.Var is un-concurrent safe. -func (tree *RedBlackTree) GetVar(key interface{}) *gvar.Var { +// Note that, the returned gvar.Var is un-concurrent safe. +// +// Also see function Get. +func (tree *RedBlackTree) GetVar(key any) *gvar.Var { return gvar.New(tree.Get(key)) } // GetVarOrSet returns a gvar.Var with result from GetVarOrSet. -// The returned gvar.Var is un-concurrent safe. -func (tree *RedBlackTree) GetVarOrSet(key interface{}, value interface{}) *gvar.Var { +// Note that, the returned gvar.Var is un-concurrent safe. +// +// Also see function GetOrSet. +func (tree *RedBlackTree) GetVarOrSet(key any, value any) *gvar.Var { return gvar.New(tree.GetOrSet(key, value)) } // GetVarOrSetFunc returns a gvar.Var with result from GetOrSetFunc. -// The returned gvar.Var is un-concurrent safe. -func (tree *RedBlackTree) GetVarOrSetFunc(key interface{}, f func() interface{}) *gvar.Var { +// Note that, the returned gvar.Var is un-concurrent safe. +// +// Also see function GetOrSetFunc. +func (tree *RedBlackTree) GetVarOrSetFunc(key any, f func() any) *gvar.Var { return gvar.New(tree.GetOrSetFunc(key, f)) } // GetVarOrSetFuncLock returns a gvar.Var with result from GetOrSetFuncLock. -// The returned gvar.Var is un-concurrent safe. -func (tree *RedBlackTree) GetVarOrSetFuncLock(key interface{}, f func() interface{}) *gvar.Var { +// Note that, the returned gvar.Var is un-concurrent safe. +// +// Also see function GetOrSetFuncLock. +func (tree *RedBlackTree) GetVarOrSetFuncLock(key any, f func() any) *gvar.Var { return gvar.New(tree.GetOrSetFuncLock(key, f)) } // Search searches the tree with given `key`. // Second return parameter `found` is true if key was found, otherwise false. -func (tree *RedBlackTree) Search(key interface{}) (value interface{}, found bool) { +func (tree *RedBlackTree) Search(key any) (value any, found bool) { tree.mu.RLock() defer tree.mu.RUnlock() if node, found := tree.doGet(key); found { @@ -212,8 +221,8 @@ func (tree *RedBlackTree) Search(key interface{}) (value interface{}, found bool return nil, false } -// Contains checks whether `key` exists in the tree. -func (tree *RedBlackTree) Contains(key interface{}) bool { +// Contains checks and returns whether given `key` exists in the tree. +func (tree *RedBlackTree) Contains(key any) bool { tree.mu.RLock() defer tree.mu.RUnlock() _, ok := tree.doGet(key) @@ -234,16 +243,16 @@ func (tree *RedBlackTree) IsEmpty() bool { return tree.tree.Size() == 0 } -// Remove removes the node from the tree by key. -// Key should adhere to the comparator's type assertion, otherwise method panics. -func (tree *RedBlackTree) Remove(key interface{}) (value interface{}) { +// Remove removes the node from the tree by `key`, and returns its associated value of `key`. +// The given `key` should adhere to the comparator's type assertion, otherwise method panics. +func (tree *RedBlackTree) Remove(key any) (value any) { tree.mu.Lock() defer tree.mu.Unlock() return tree.doRemove(key) } -// Removes batch deletes values of the tree by `keys`. -func (tree *RedBlackTree) Removes(keys []interface{}) { +// Removes batch deletes key-value pairs from the tree by `keys`. +func (tree *RedBlackTree) Removes(keys []any) { tree.mu.Lock() defer tree.mu.Unlock() for _, key := range keys { @@ -258,22 +267,22 @@ func (tree *RedBlackTree) Clear() { tree.tree.Clear() } -// Keys returns all keys in asc order. -func (tree *RedBlackTree) Keys() []interface{} { +// Keys returns all keys from the tree in order by its comparator. +func (tree *RedBlackTree) Keys() []any { tree.mu.RLock() defer tree.mu.RUnlock() return tree.tree.Keys() } -// Values returns all values in asc order based on the key. -func (tree *RedBlackTree) Values() []interface{} { +// Values returns all values from the true in order by its comparator based on the key. +func (tree *RedBlackTree) Values() []any { tree.mu.RLock() defer tree.mu.RUnlock() return tree.tree.Values() } -// Replace the data of the tree with given `data`. -func (tree *RedBlackTree) Replace(data map[interface{}]interface{}) { +// Replace clears the data of the tree and sets the nodes by given `data`. +func (tree *RedBlackTree) Replace(data map[any]any) { tree.mu.Lock() defer tree.mu.Unlock() tree.tree.Clear() @@ -301,24 +310,20 @@ func (tree *RedBlackTree) MarshalJSON() (jsonBytes []byte, err error) { return tree.tree.MarshalJSON() } -// Map returns all key-value items as map. -func (tree *RedBlackTree) Map() map[interface{}]interface{} { - tree.mu.RLock() - defer tree.mu.RUnlock() - m := make(map[interface{}]interface{}, tree.Size()) - tree.IteratorAsc(func(key, value interface{}) bool { +// Map returns all key-value pairs as map. +func (tree *RedBlackTree) Map() map[any]any { + m := make(map[any]any, tree.Size()) + tree.IteratorAsc(func(key, value any) bool { m[key] = value return true }) return m } -// MapStrAny returns all key-value items as map[string]interface{}. -func (tree *RedBlackTree) MapStrAny() map[string]interface{} { - tree.mu.RLock() - defer tree.mu.RUnlock() - m := make(map[string]interface{}, tree.Size()) - tree.IteratorAsc(func(key, value interface{}) bool { +// MapStrAny returns all key-value items as map[string]any. +func (tree *RedBlackTree) MapStrAny() map[string]any { + m := make(map[string]any, tree.Size()) + tree.IteratorAsc(func(key, value any) bool { m[gconv.String(key)] = value return true }) @@ -326,39 +331,48 @@ func (tree *RedBlackTree) MapStrAny() map[string]interface{} { } // Iterator is alias of IteratorAsc. -func (tree *RedBlackTree) Iterator(f func(key, value interface{}) bool) { +// +// Also see IteratorAsc. +func (tree *RedBlackTree) Iterator(f func(key, value any) bool) { tree.IteratorAsc(f) } // IteratorFrom is alias of IteratorAscFrom. -func (tree *RedBlackTree) IteratorFrom(key interface{}, match bool, f func(key, value interface{}) bool) { +// +// Also see IteratorAscFrom. +func (tree *RedBlackTree) IteratorFrom(key any, match bool, f func(key, value any) bool) { tree.IteratorAscFrom(key, match, f) } // IteratorAsc iterates the tree readonly in ascending order with given callback function `f`. -// If `f` returns true, then it continues iterating; or false to stop. -func (tree *RedBlackTree) IteratorAsc(f func(key, value interface{}) bool) { +// If callback function `f` returns true, then it continues iterating; or false to stop. +func (tree *RedBlackTree) IteratorAsc(f func(key, value any) bool) { tree.mu.RLock() defer tree.mu.RUnlock() - it := tree.tree.Iterator() + var ( + ok bool + it = tree.tree.Iterator() + ) for it.Begin(); it.Next(); { index, value := it.Key(), it.Value() - if ok := f(index, value); !ok { + if ok = f(index, value); !ok { break } } } // IteratorAscFrom iterates the tree readonly in ascending order with given callback function `f`. -// The parameter `key` specifies the start entry for iterating. The `match` specifies whether -// starting iterating if the `key` is fully matched, or else using index searching iterating. -// If `f` returns true, then it continues iterating; or false to stop. -func (tree *RedBlackTree) IteratorAscFrom(key interface{}, match bool, f func(key, value interface{}) bool) { +// +// The parameter `key` specifies the start entry for iterating. +// The parameter `match` specifies whether starting iterating only if the `key` is fully matched, or else using index +// searching iterating. +// If callback function `f` returns true, then it continues iterating; or false to stop. +func (tree *RedBlackTree) IteratorAscFrom(key any, match bool, f func(key, value any) bool) { tree.mu.RLock() defer tree.mu.RUnlock() var keys = tree.tree.Keys() - index, isIterator := tree.iteratorFromGetIndex(key, keys, match) - if !isIterator { + index, canIterator := iteratorFromGetIndex(key, keys, match) + if !canIterator { return } for ; index < len(keys); index++ { @@ -367,29 +381,35 @@ func (tree *RedBlackTree) IteratorAscFrom(key interface{}, match bool, f func(ke } // IteratorDesc iterates the tree readonly in descending order with given callback function `f`. -// If `f` returns true, then it continues iterating; or false to stop. -func (tree *RedBlackTree) IteratorDesc(f func(key, value interface{}) bool) { +// +// If callback function `f` returns true, then it continues iterating; or false to stop. +func (tree *RedBlackTree) IteratorDesc(f func(key, value any) bool) { tree.mu.RLock() defer tree.mu.RUnlock() - it := tree.tree.Iterator() + var ( + ok bool + it = tree.tree.Iterator() + ) for it.End(); it.Prev(); { index, value := it.Key(), it.Value() - if ok := f(index, value); !ok { + if ok = f(index, value); !ok { break } } } // IteratorDescFrom iterates the tree readonly in descending order with given callback function `f`. -// The parameter `key` specifies the start entry for iterating. The `match` specifies whether -// starting iterating if the `key` is fully matched, or else using index searching iterating. -// If `f` returns true, then it continues iterating; or false to stop. -func (tree *RedBlackTree) IteratorDescFrom(key interface{}, match bool, f func(key, value interface{}) bool) { +// +// The parameter `key` specifies the start entry for iterating. +// The parameter `match` specifies whether starting iterating only if the `key` is fully matched, or else using index +// searching iterating. +// If callback function `f` returns true, then it continues iterating; or false to stop. +func (tree *RedBlackTree) IteratorDescFrom(key any, match bool, f func(key, value any) bool) { tree.mu.RLock() defer tree.mu.RUnlock() var keys = tree.tree.Keys() - index, isIterator := tree.iteratorFromGetIndex(key, keys, match) - if !isIterator { + index, canIterator := iteratorFromGetIndex(key, keys, match) + if !canIterator { return } for ; index >= 0; index-- { @@ -397,8 +417,7 @@ func (tree *RedBlackTree) IteratorDescFrom(key interface{}, match bool, f func(k } } -// Left returns the minimum element of the AVL tree -// or nil if the tree is empty. +// Left returns the minimum element corresponding to the comparator of the tree or nil if the tree is empty. func (tree *RedBlackTree) Left() *RedBlackTreeNode { tree.mu.RLock() defer tree.mu.RUnlock() @@ -412,8 +431,7 @@ func (tree *RedBlackTree) Left() *RedBlackTreeNode { } } -// Right returns the maximum element of the AVL tree -// or nil if the tree is empty. +// Right returns the maximum element corresponding to the comparator of the tree or nil if the tree is empty. func (tree *RedBlackTree) Right() *RedBlackTreeNode { tree.mu.RLock() defer tree.mu.RUnlock() @@ -427,15 +445,15 @@ func (tree *RedBlackTree) Right() *RedBlackTreeNode { } } -// Floor Finds floor node of the input key, return the floor node or nil if no floor node is found. -// Second return parameter is true if floor was found, otherwise false. +// Floor Finds floor node of the input key, returns the floor node or nil if no floor node is found. +// The second returned parameter `found` is true if floor was found, otherwise false. // // Floor node is defined as the largest node that is smaller than or equal to the given node. // A floor node may not be found, either because the tree is empty, or because // all nodes in the tree is larger than the given node. // // Key should adhere to the comparator's type assertion, otherwise method panics. -func (tree *RedBlackTree) Floor(key interface{}) (floor *RedBlackTreeNode, found bool) { +func (tree *RedBlackTree) Floor(key any) (floor *RedBlackTreeNode, found bool) { tree.mu.RLock() defer tree.mu.RUnlock() node, found := tree.tree.Floor(key) @@ -448,15 +466,15 @@ func (tree *RedBlackTree) Floor(key interface{}) (floor *RedBlackTreeNode, found }, true } -// Ceiling finds ceiling node of the input key, return the ceiling node or nil if no ceiling node is found. -// Second return parameter is true if ceiling was found, otherwise false. +// Ceiling finds ceiling node of the input key, returns the ceiling node or nil if no ceiling node is found. +// The second return parameter `found` is true if ceiling was found, otherwise false. // // Ceiling node is defined as the smallest node that is larger than or equal to the given node. // A ceiling node may not be found, either because the tree is empty, or because // all nodes in the tree is smaller than the given node. // // Key should adhere to the comparator's type assertion, otherwise method panics. -func (tree *RedBlackTree) Ceiling(key interface{}) (ceiling *RedBlackTreeNode, found bool) { +func (tree *RedBlackTree) Ceiling(key any) (ceiling *RedBlackTreeNode, found bool) { tree.mu.RLock() defer tree.mu.RUnlock() node, found := tree.tree.Ceiling(key) @@ -474,14 +492,14 @@ func (tree *RedBlackTree) Ceiling(key interface{}) (ceiling *RedBlackTreeNode, f // or else the comparator would panic. // // If the type of value is different with key, you pass the new `comparator`. -func (tree *RedBlackTree) Flip(comparator ...func(v1, v2 interface{}) int) { +func (tree *RedBlackTree) Flip(comparator ...func(v1, v2 any) int) { var t = new(RedBlackTree) if len(comparator) > 0 { t = NewRedBlackTree(comparator[0], tree.mu.IsSafe()) } else { t = NewRedBlackTree(tree.comparator, tree.mu.IsSafe()) } - tree.IteratorAsc(func(key, value interface{}) bool { + tree.IteratorAsc(func(key, value any) bool { t.doSet(value, key) return true }) @@ -497,7 +515,7 @@ func (tree *RedBlackTree) UnmarshalJSON(b []byte) error { tree.comparator = gutil.ComparatorString tree.tree = redblacktree.NewWith(tree.comparator) } - var data map[string]interface{} + var data map[string]any if err := json.UnmarshalUseNumber(b, &data); err != nil { return err } @@ -508,7 +526,7 @@ func (tree *RedBlackTree) UnmarshalJSON(b []byte) error { } // UnmarshalValue is an interface implement which sets any type of value for map. -func (tree *RedBlackTree) UnmarshalValue(value interface{}) (err error) { +func (tree *RedBlackTree) UnmarshalValue(value any) (err error) { tree.mu.Lock() defer tree.mu.Unlock() if tree.comparator == nil { @@ -521,9 +539,13 @@ func (tree *RedBlackTree) UnmarshalValue(value interface{}) (err error) { return } -// doSet sets key-value pair to the tree. -func (tree *RedBlackTree) doSet(key, value interface{}) interface{} { - if f, ok := value.(func() interface{}); ok { +// doSet inserts key-value pair node into the tree without lock. +// If `key` already exists, then its value is updated with the new value. +// If `value` is type of , it will be executed and its return value will be set to the map with `key`. +// +// It returns value with given `key`. +func (tree *RedBlackTree) doSet(key, value any) any { + if f, ok := value.(func() any); ok { value = f() } if value == nil { @@ -533,35 +555,15 @@ func (tree *RedBlackTree) doSet(key, value interface{}) interface{} { return value } -// doGet retrieves and returns the value of given key from tree. -func (tree *RedBlackTree) doGet(key interface{}) (value interface{}, found bool) { +// doGet retrieves and returns the value of given key from tree without lock. +func (tree *RedBlackTree) doGet(key any) (value any, found bool) { return tree.tree.Get(key) } -// doRemove removes key from tree. -func (tree *RedBlackTree) doRemove(key interface{}) (value interface{}) { +// doRemove removes key from tree and returns its associated value without lock. +// Note that, the given `key` should adhere to the comparator's type assertion, otherwise method panics. +func (tree *RedBlackTree) doRemove(key any) (value any) { value, _ = tree.tree.Get(key) tree.tree.Remove(key) return } - -// iteratorFromGetIndex returns the index of the key in the keys slice. -// The parameter `match` specifies whether starting iterating if the `key` is fully matched, -// or else using index searching iterating. -// If `isIterator` is true, iterator is available; or else not. -func (tree *RedBlackTree) iteratorFromGetIndex(key interface{}, keys []interface{}, match bool) (index int, isIterator bool) { - if match { - for i, k := range keys { - if k == key { - isIterator = true - index = i - } - } - } else { - if i, ok := key.(int); ok { - isIterator = true - index = i - } - } - return -} diff --git a/example/go.mod b/example/go.mod index db1e33c40d3..8ee0c14b468 100644 --- a/example/go.mod +++ b/example/go.mod @@ -22,7 +22,7 @@ require ( github.com/hashicorp/consul/api v1.24.0 github.com/hashicorp/go-cleanhttp v0.5.2 github.com/nacos-group/nacos-sdk-go/v2 v2.2.7 - github.com/polarismesh/polaris-go v1.5.5 + github.com/polarismesh/polaris-go v1.5.8 github.com/prometheus/client_golang v1.20.2 go.opentelemetry.io/otel/exporters/prometheus v0.46.0 golang.org/x/time v0.6.0 @@ -56,7 +56,7 @@ require ( github.com/coreos/go-systemd/v22 v22.3.2 // indirect github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect - github.com/dlclark/regexp2 v1.7.0 // indirect + github.com/dlclark/regexp2 v1.11.2 // indirect github.com/emicklei/go-restful/v3 v3.9.0 // indirect github.com/emirpasic/gods v1.18.1 // indirect github.com/fatih/color v1.17.0 // indirect @@ -105,7 +105,7 @@ require ( github.com/opentracing/opentracing-go v1.2.1-0.20220228012449-10b1cf09e00b // indirect github.com/pelletier/go-toml v1.9.3 // indirect github.com/pkg/errors v0.9.1 // indirect - github.com/polarismesh/specification v1.4.1 // indirect + github.com/polarismesh/specification v1.5.1 // indirect github.com/prometheus/client_model v0.6.1 // indirect github.com/prometheus/common v0.55.0 // indirect github.com/prometheus/procfs v0.15.1 // indirect @@ -134,14 +134,14 @@ require ( go.opentelemetry.io/proto/otlp v1.1.0 // indirect go.uber.org/multierr v1.10.0 // indirect go.uber.org/zap v1.26.0 // indirect - golang.org/x/crypto v0.25.0 // indirect + golang.org/x/crypto v0.26.0 // indirect golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect - golang.org/x/net v0.27.0 // indirect + golang.org/x/net v0.28.0 // indirect golang.org/x/oauth2 v0.21.0 // indirect - golang.org/x/sync v0.7.0 // indirect - golang.org/x/sys v0.22.0 // indirect - golang.org/x/term v0.22.0 // indirect - golang.org/x/text v0.16.0 // indirect + golang.org/x/sync v0.8.0 // indirect + golang.org/x/sys v0.24.0 // indirect + golang.org/x/term v0.23.0 // indirect + golang.org/x/text v0.17.0 // indirect google.golang.org/genproto/googleapis/api v0.0.0-20240711142825-46eb208f015d // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20240701130421-f6361c86f094 // indirect gopkg.in/inf.v0 v0.9.1 // indirect diff --git a/example/go.sum b/example/go.sum index 4536d7f8b44..fa6f8122e17 100644 --- a/example/go.sum +++ b/example/go.sum @@ -274,8 +274,9 @@ github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1 github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= -github.com/dlclark/regexp2 v1.7.0 h1:7lJfhqlPssTb1WQx4yvTHN0uElPEv52sbaECrAQxjAo= github.com/dlclark/regexp2 v1.7.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8= +github.com/dlclark/regexp2 v1.11.2 h1:/u628IuisSTwri5/UKloiIsH8+qF2Pu7xEQX+yIKg68= +github.com/dlclark/regexp2 v1.11.2/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8= github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE= github.com/emicklei/go-restful/v3 v3.9.0 h1:XwGDlfxEnQZzuopoqxwSEllNcCOM9DhhFyhFIIGKwxE= github.com/emicklei/go-restful/v3 v3.9.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc= @@ -621,10 +622,11 @@ github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/polarismesh/polaris-go v1.5.5 h1:TUxgla21J9Zb3zSynoAA9og0+sYJZ7TpZ2aFwzH7woI= -github.com/polarismesh/polaris-go v1.5.5/go.mod h1:CuXO9bhHGjSoOIMWr4NXf3bJAkRBp5YoM7ibBzENC+c= -github.com/polarismesh/specification v1.4.1 h1:lTZqeyUhhWuKyr6NDKBwmUrNfcUDvKLxWT/uOq71T5A= +github.com/polarismesh/polaris-go v1.5.8 h1:zBZkZBa4wVxsG5Y/6U4PmY9dY/mddXdfNI65edVu6U8= +github.com/polarismesh/polaris-go v1.5.8/go.mod h1:CuXO9bhHGjSoOIMWr4NXf3bJAkRBp5YoM7ibBzENC+c= github.com/polarismesh/specification v1.4.1/go.mod h1:rDvMMtl5qebPmqiBLNa5Ps0XtwkP31ZLirbH4kXA0YU= +github.com/polarismesh/specification v1.5.1 h1:cJ2m0RBepdopGo/e3UpKdsab3NpDZnw5IsVTB1sFc5I= +github.com/polarismesh/specification v1.5.1/go.mod h1:rDvMMtl5qebPmqiBLNa5Ps0XtwkP31ZLirbH4kXA0YU= github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI= github.com/posener/complete v1.2.3/go.mod h1:WZIdtGGp+qx0sLrYKtIRAruyNpv6hFCicSgv7Sy7s/s= github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= @@ -796,8 +798,8 @@ golang.org/x/crypto v0.0.0-20201012173705-84dcc777aaee/go.mod h1:LzIPMQfyMNhhGPh golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.10.0/go.mod h1:o4eNf7Ede1fv+hwOwZsTHl9EsPFO6q6ZvYR8vYfY45I= golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg= -golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30= -golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M= +golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw= +golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54= golang.org/x/exp v0.0.0-20180321215751-8460e604b9de/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20180807140117-3d87b88a115f/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= @@ -900,8 +902,8 @@ golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= golang.org/x/net v0.11.0/go.mod h1:2L/ixqYpgIVXmeoSA/4Lu7BzTG4KIyPIryS4IsOd1oQ= golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY= -golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys= -golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE= +golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE= +golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= @@ -942,8 +944,8 @@ golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= -golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= +golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -1034,8 +1036,8 @@ golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.9.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI= -golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg= +golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc= @@ -1043,8 +1045,8 @@ golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo= golang.org/x/term v0.9.0/go.mod h1:M6DEAAIenWoTxdKrOltXcmDY3rSplQUkrvaDU5FcQyo= golang.org/x/term v0.16.0/go.mod h1:yn7UURbUtPyrVJPGPq404EukNFxcm/foM+bV/bfcDsY= -golang.org/x/term v0.22.0 h1:BbsgPEJULsl2fV/AT3v15Mjva5yXKQDyKf+TbDz7QJk= -golang.org/x/term v0.22.0/go.mod h1:F3qCibpT5AMpCRfhfT53vVJwhLtIVHhB9XDjfFvnMI4= +golang.org/x/term v0.23.0 h1:F6D4vR+EHoL9/sWAWgAR1H2DcHr4PareCbAaCo1RpuU= +golang.org/x/term v0.23.0/go.mod h1:DgV24QBUrK6jhZXl+20l6UWznPlwAHm1Q1mGHtydmSk= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= @@ -1059,8 +1061,8 @@ golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= golang.org/x/text v0.10.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= -golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4= -golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI= +golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc= +golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= From dc3cb1e404170a0a42367e7f7f4cd1f1385cc537 Mon Sep 17 00:00:00 2001 From: John Guo Date: Mon, 7 Oct 2024 11:24:43 +0800 Subject: [PATCH 2/2] fix(container/gtree): deadlock in Map/MapStrAny functions, comments update --- container/gtree/gtree_avltree.go | 5 ++--- container/gtree/gtree_btree.go | 5 ++--- container/gtree/gtree_redblacktree.go | 5 ++--- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/container/gtree/gtree_avltree.go b/container/gtree/gtree_avltree.go index 32120c4df8a..ac8a51a8694 100644 --- a/container/gtree/gtree_avltree.go +++ b/container/gtree/gtree_avltree.go @@ -106,7 +106,7 @@ func (tree *AVLTree) SetIfNotExistFunc(key any, f func() any) bool { // It returns false if `key` exists, and such setting key-value pair operation would be ignored. // // SetIfNotExistFuncLock differs with SetIfNotExistFunc function is that -// it executes function `f` within mutex.Lock of the hash map. +// it executes function `f` within mutex lock. func (tree *AVLTree) SetIfNotExistFuncLock(key any, f func() any) bool { tree.mu.Lock() defer tree.mu.Unlock() @@ -153,8 +153,7 @@ func (tree *AVLTree) GetOrSetFunc(key any, f func() any) any { // GetOrSetFuncLock returns its `value` of `key`, or sets value with returned value of callback function `f` if it does // not exist and then returns this value. // -// GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function `f` within mutex.Lock of the hash -// map. +// GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function `f` within mutex lock. func (tree *AVLTree) GetOrSetFuncLock(key any, f func() any) any { tree.mu.Lock() defer tree.mu.Unlock() diff --git a/container/gtree/gtree_btree.go b/container/gtree/gtree_btree.go index 8d75684960c..59f45053351 100644 --- a/container/gtree/gtree_btree.go +++ b/container/gtree/gtree_btree.go @@ -107,7 +107,7 @@ func (tree *BTree) SetIfNotExistFunc(key any, f func() any) bool { // It returns false if `key` exists, and such setting key-value pair operation would be ignored. // // SetIfNotExistFuncLock differs with SetIfNotExistFunc function is that -// it executes function `f` within mutex.Lock of the hash map. +// it executes function `f` within mutex lock. func (tree *BTree) SetIfNotExistFuncLock(key any, f func() any) bool { tree.mu.Lock() defer tree.mu.Unlock() @@ -156,8 +156,7 @@ func (tree *BTree) GetOrSetFunc(key any, f func() any) any { // GetOrSetFuncLock returns its `value` of `key`, or sets value with returned value of callback function `f` if it does // not exist and then returns this value. // -// GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function `f` within mutex.Lock of the hash -// map. +// GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function `f` within mutex lock. func (tree *BTree) GetOrSetFuncLock(key any, f func() any) any { tree.mu.Lock() defer tree.mu.Unlock() diff --git a/container/gtree/gtree_redblacktree.go b/container/gtree/gtree_redblacktree.go index 1d275e0ac6c..c5a6bddf826 100644 --- a/container/gtree/gtree_redblacktree.go +++ b/container/gtree/gtree_redblacktree.go @@ -119,7 +119,7 @@ func (tree *RedBlackTree) SetIfNotExistFunc(key any, f func() any) bool { // It returns false if `key` exists, and such setting key-value pair operation would be ignored. // // SetIfNotExistFuncLock differs with SetIfNotExistFunc function is that -// it executes function `f` within mutex.Lock of the hash map. +// it executes function `f` within mutex lock. func (tree *RedBlackTree) SetIfNotExistFuncLock(key any, f func() any) bool { tree.mu.Lock() defer tree.mu.Unlock() @@ -166,8 +166,7 @@ func (tree *RedBlackTree) GetOrSetFunc(key any, f func() any) any { // GetOrSetFuncLock returns its `value` of `key`, or sets value with returned value of callback function `f` if it does // not exist and then returns this value. // -// GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function `f` within mutex.Lock of the hash -// map. +// GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function `f`within mutex lock. func (tree *RedBlackTree) GetOrSetFuncLock(key any, f func() any) any { tree.mu.Lock() defer tree.mu.Unlock()