feat(container): Add NewXXXWithChecker function for gmap/gset/gtree (#4610)

为了解决开发者需要通过`var`在代码顶部创建`gmap/gset/gtree`时需要同时设置`nilchecker`的需求,为这几个容易增加带有`checker`入参的构造函数`NewxxxxWithChecker`和`NewxxxWithCheckerFrom`

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
Lance Add
2026-01-15 14:26:42 +08:00
committed by GitHub
parent 9dd43cd331
commit c600f3aae8
10 changed files with 299 additions and 4 deletions

View File

@ -28,12 +28,18 @@ type KVMap[K comparable, V any] struct {
}
// NewKVMap creates and returns an empty hash map.
// The parameter `safe` is used to specify whether to use the map in concurrent-safety mode,
// which is false by default.
// The parameter `safe` is used to specify whether to use the map in concurrent-safety mode, which is false by default.
func NewKVMap[K comparable, V any](safe ...bool) *KVMap[K, V] {
return NewKVMapFrom(make(map[K]V), safe...)
}
// NewKVMapWithChecker creates and returns an empty hash map with a custom nil checker.
// The parameter `checker` is a function used to determine if a value is nil.
// The parameter `safe` is used to specify whether to use the map in concurrent-safety mode, which is false by default.
func NewKVMapWithChecker[K comparable, V any](checker NilChecker[V], safe ...bool) *KVMap[K, V] {
return NewKVMapWithCheckerFrom(make(map[K]V), checker, safe...)
}
// NewKVMapFrom creates and returns a hash map from given map `data`.
// Note that, the param `data` map will be set as the underlying data map (no deep copy),
// there might be some concurrent-safe issues when changing the map outside.
@ -45,6 +51,17 @@ func NewKVMapFrom[K comparable, V any](data map[K]V, safe ...bool) *KVMap[K, V]
return m
}
// NewKVMapWithCheckerFrom creates and returns a hash map from given map `data` with a custom nil checker.
// Note that, the param `data` map will be set as the underlying data map (no deep copy),
// and there might be some concurrent-safe issues when changing the map outside.
// The parameter `checker` is a function used to determine if a value is nil.
// The parameter `safe` is used to specify whether to use the map in concurrent-safety mode, which is false by default.
func NewKVMapWithCheckerFrom[K comparable, V any](data map[K]V, checker NilChecker[V], safe ...bool) *KVMap[K, V] {
m := NewKVMapFrom[K, V](data, safe...)
m.RegisterNilChecker(checker)
return m
}
// RegisterNilChecker registers a custom nil checker function for the map values.
// This function is used to determine if a value should be considered as nil.
// The nil checker function takes a value of type V and returns a boolean indicating

View File

@ -50,6 +50,16 @@ func NewListKVMap[K comparable, V any](safe ...bool) *ListKVMap[K, V] {
}
}
// NewListKVMapWithChecker creates and returns a new ListKVMap instance with a custom nil checker.
// The parameter `checker` is a function used to determine if a value is nil.
// The parameter `safe` is used to specify whether using map in concurrent-safety,
// which is false by default.
func NewListKVMapWithChecker[K comparable, V any](checker NilChecker[V], safe ...bool) *ListKVMap[K, V] {
m := NewListKVMap[K, V](safe...)
m.RegisterNilChecker(checker)
return m
}
// NewListKVMapFrom returns a link map from given map `data`.
// Note that, the param `data` map will be copied to the underlying data structure,
// so changes to the original map will not affect the link map.
@ -59,6 +69,18 @@ func NewListKVMapFrom[K comparable, V any](data map[K]V, safe ...bool) *ListKVMa
return m
}
// NewListKVMapWithCheckerFrom returns a link map from given map `data` with a custom nil checker.
// Note that, the param `data` map will be copied to the underlying data structure,
// so changes to the original map will not affect the link map.
// The parameter `checker` is a function used to determine if a value is nil.
// The parameter `safe` is used to specify whether using map in concurrent-safety,
// which is false by default.
func NewListKVMapWithCheckerFrom[K comparable, V any](data map[K]V, nilChecker NilChecker[V], safe ...bool) *ListKVMap[K, V] {
m := NewListKVMapWithChecker[K, V](nilChecker, safe...)
m.Sets(data)
return m
}
// RegisterNilChecker registers a custom nil checker function for the map values.
// This function is used to determine if a value should be considered as nil.
// The nil checker function takes a value of type V and returns a boolean indicating

View File

@ -1663,3 +1663,34 @@ func Test_KVMap_TypedNil(t *testing.T) {
t.Assert(m2.Size(), 5)
})
}
func Test_NewKVMapWithChecker_TypedNil(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
type Student struct {
Name string
Age int
}
m1 := gmap.NewKVMap[int, *Student](true)
for i := 0; i < 10; i++ {
m1.GetOrSetFuncLock(i, func() *Student {
if i%2 == 0 {
return &Student{}
}
return nil
})
}
t.Assert(m1.Size(), 10)
m2 := gmap.NewKVMapWithChecker[int, *Student](func(student *Student) bool {
return student == nil
}, true)
for i := 0; i < 10; i++ {
m2.GetOrSetFuncLock(i, func() *Student {
if i%2 == 0 {
return &Student{}
}
return nil
})
}
t.Assert(m2.Size(), 5)
})
}

View File

@ -1374,3 +1374,34 @@ func Test_ListKVMap_TypedNil(t *testing.T) {
t.Assert(m2.Size(), 5)
})
}
func Test_NewListKVMapWithChecker_TypedNil(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
type Student struct {
Name string
Age int
}
m1 := gmap.NewListKVMap[int, *Student](true)
for i := 0; i < 10; i++ {
m1.GetOrSetFuncLock(i, func() *Student {
if i%2 == 0 {
return &Student{}
}
return nil
})
}
t.Assert(m1.Size(), 10)
m2 := gmap.NewListKVMapWithChecker[int, *Student](func(student *Student) bool {
return student == nil
}, true)
for i := 0; i < 10; i++ {
m2.GetOrSetFuncLock(i, func() *Student {
if i%2 == 0 {
return &Student{}
}
return nil
})
}
t.Assert(m2.Size(), 5)
})
}

View File

@ -34,6 +34,15 @@ func NewTSet[T comparable](safe ...bool) *TSet[T] {
}
}
// NewTSetWithChecker creates and returns a new set with a custom nil checker.
// The parameter `nilChecker` is a function used to determine if a value is nil.
// The parameter `safe` is used to specify whether using set in concurrent-safety mode.
func NewTSetWithChecker[T comparable](checker NilChecker[T], safe ...bool) *TSet[T] {
s := NewTSet[T](safe...)
s.RegisterNilChecker(checker)
return s
}
// NewTSetFrom returns a new set from `items`.
// `items` - A slice of type T.
func NewTSetFrom[T comparable](items []T, safe ...bool) *TSet[T] {
@ -47,6 +56,16 @@ func NewTSetFrom[T comparable](items []T, safe ...bool) *TSet[T] {
}
}
// NewTSetWithCheckerFrom returns a new set from `items` with a custom nil checker.
// The parameter `items` is a slice of elements to be added to the set.
// The parameter `checker` is a function used to determine if a value is nil.
// The parameter `safe` is used to specify whether using set in concurrent-safety mode.
func NewTSetWithCheckerFrom[T comparable](items []T, checker NilChecker[T], safe ...bool) *TSet[T] {
set := NewTSetWithChecker[T](checker, safe...)
set.Add(items...)
return set
}
// RegisterNilChecker registers a custom nil checker function for the set elements.
// This function is used to determine if an element should be considered as nil.
// The nil checker function takes an element of type T and returns a boolean indicating
@ -80,13 +99,13 @@ func (set *TSet[T]) Iterator(f func(v T) bool) {
// Add adds one or multiple items to the set.
func (set *TSet[T]) Add(items ...T) {
set.mu.Lock()
defer set.mu.Unlock()
if set.data == nil {
set.data = make(map[T]struct{})
}
for _, v := range items {
set.data[v] = struct{}{}
}
set.mu.Unlock()
}
// AddIfNotExist checks whether item exists in the set,

View File

@ -611,3 +611,22 @@ func Test_TSet_TypedNil(t *testing.T) {
t.Assert(exist2, false)
})
}
func Test_NewTSetWithChecker_TypedNil(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
type Student struct {
Name string
Age int
}
set := gset.NewTSet[*Student](true)
var s *Student = nil
exist := set.AddIfNotExist(s)
t.Assert(exist, true)
set2 := gset.NewTSetWithChecker[*Student](func(student *Student) bool {
return student == nil
}, true)
exist2 := set2.AddIfNotExist(s)
t.Assert(exist2, false)
})
}

View File

@ -47,6 +47,15 @@ func NewAVLKVTree[K comparable, V any](comparator func(v1, v2 K) int, safe ...bo
}
}
// NewAVLKVTreeWithChecker instantiates an AVL tree with the custom key comparator and nil checker.
// The parameter `safe` is used to specify whether using tree in concurrent-safety, which is false in default.
// The parameter `checker` is used to specify whether the given value is nil.
func NewAVLKVTreeWithChecker[K comparable, V any](comparator func(v1, v2 K) int, checker NilChecker[V], safe ...bool) *AVLKVTree[K, V] {
t := NewAVLKVTree[K, V](comparator, safe...)
t.RegisterNilChecker(checker)
return t
}
// NewAVLKVTreeFrom 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.
@ -58,6 +67,17 @@ func NewAVLKVTreeFrom[K comparable, V any](comparator func(v1, v2 K) int, data m
return tree
}
// NewAVLKVTreeWithCheckerFrom instantiates an AVL tree with the custom key comparator, nil checker and data map.
// The parameter `safe` is used to specify whether using tree in concurrent-safety, which is false in default.
// The parameter `checker` is used to specify whether the given value is nil.
func NewAVLKVTreeWithCheckerFrom[K comparable, V any](comparator func(v1, v2 K) int, data map[K]V, checker NilChecker[V], safe ...bool) *AVLKVTree[K, V] {
tree := NewAVLKVTreeWithChecker[K, V](comparator, checker, safe...)
for k, v := range data {
tree.doSet(k, v)
}
return tree
}
// RegisterNilChecker registers a custom nil checker function for the map values.
// This function is used to determine if a value should be considered as nil.
// The nil checker function takes a value of type V and returns a boolean indicating

View File

@ -46,6 +46,15 @@ func NewBKVTree[K comparable, V any](m int, comparator func(v1, v2 K) int, safe
}
}
// NewBKVTreeWithChecker instantiates a B-tree with `m` (maximum number of children), a custom key comparator and nil checker.
// The parameter `safe` is used to specify whether using tree in concurrent-safety, which is false in default.
// The parameter `checker` is used to specify whether the given value is nil.
func NewBKVTreeWithChecker[K comparable, V any](m int, comparator func(v1, v2 K) int, checker NilChecker[V], safe ...bool) *BKVTree[K, V] {
t := NewBKVTree[K, V](m, comparator, safe...)
t.RegisterNilChecker(checker)
return t
}
// NewBKVTreeFrom 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.
@ -57,6 +66,17 @@ func NewBKVTreeFrom[K comparable, V any](m int, comparator func(v1, v2 K) int, d
return tree
}
// NewBKVTreeWithCheckerFrom instantiates a B-tree with `m` (maximum number of children), a custom key comparator, nil checker and data map.
// The parameter `safe` is used to specify whether using tree in concurrent-safety, which is false in default.
// The parameter `checker` is used to specify whether the given value is nil.
func NewBKVTreeWithCheckerFrom[K comparable, V any](m int, comparator func(v1, v2 K) int, data map[K]V, checker NilChecker[V], safe ...bool) *BKVTree[K, V] {
tree := NewBKVTreeWithChecker[K, V](m, comparator, checker, safe...)
for k, v := range data {
tree.doSet(k, v)
}
return tree
}
// RegisterNilChecker registers a custom nil checker function for the map values.
// This function is used to determine if a value should be considered as nil.
// The nil checker function takes a value of type V and returns a boolean indicating

View File

@ -42,6 +42,15 @@ func NewRedBlackKVTree[K comparable, V any](comparator func(v1, v2 K) int, safe
return &tree
}
// NewRedBlackKVTreeWithChecker instantiates a red-black tree with the custom key comparator and `nilChecker`.
// The parameter `safe` is used to specify whether using tree in concurrent-safety, which is false in default.
// The parameter `checker` is used to specify whether the given value is nil.
func NewRedBlackKVTreeWithChecker[K comparable, V any](comparator func(v1, v2 K) int, checker NilChecker[V], safe ...bool) *RedBlackKVTree[K, V] {
t := NewRedBlackKVTree[K, V](comparator, safe...)
t.RegisterNilChecker(checker)
return t
}
// NewRedBlackKVTreeFrom 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.
@ -51,6 +60,17 @@ func NewRedBlackKVTreeFrom[K comparable, V any](comparator func(v1, v2 K) int, d
return &tree
}
// NewRedBlackKVTreeWithCheckerFrom instantiates a red-black tree with the custom key comparator, `data` map and `nilChecker`.
// The parameter `safe` is used to specify whether using tree in concurrent-safety, which is false in default.
// The parameter `checker` is used to specify whether the given value is nil.
func NewRedBlackKVTreeWithCheckerFrom[K comparable, V any](comparator func(v1, v2 K) int, data map[K]V, checker NilChecker[V], safe ...bool) *RedBlackKVTree[K, V] {
t := NewRedBlackKVTreeWithChecker[K, V](comparator, checker, safe...)
for k, v := range data {
t.doSet(k, v)
}
return t
}
// RedBlackKVTreeInit 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.
@ -210,7 +230,7 @@ func (tree *RedBlackKVTree[K, V]) GetOrSetFunc(key K, f func() V) V {
// 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.
// GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function `f` within mutex lock.
func (tree *RedBlackKVTree[K, V]) GetOrSetFuncLock(key K, f func() V) V {
tree.mu.Lock()
defer tree.mu.Unlock()

View File

@ -112,3 +112,99 @@ func Test_KVRedBlackTree_TypedNil(t *testing.T) {
t.Assert(redBlackTree2.Size(), 5)
})
}
func Test_NewKVAVLTreeWithChecker_TypedNil(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
type Student struct {
Name string
Age int
}
avlTree := gtree.NewAVLKVTree[int, *Student](gutil.ComparatorTStr[int], true)
for i := 0; i < 10; i++ {
if i%2 == 0 {
avlTree.Set(i, &Student{})
} else {
var s *Student = nil
avlTree.Set(i, s)
}
}
t.Assert(avlTree.Size(), 10)
avlTree2 := gtree.NewAVLKVTreeWithChecker[int, *Student](gutil.ComparatorTStr[int], func(student *Student) bool {
return student == nil
}, true)
for i := 0; i < 10; i++ {
if i%2 == 0 {
avlTree2.Set(i, &Student{})
} else {
var s *Student = nil
avlTree2.Set(i, s)
}
}
t.Assert(avlTree2.Size(), 5)
})
}
func Test_NewKVBTreeWithChecker_TypedNil(t *testing.T) {
type Student struct {
Name string
Age int
}
gtest.C(t, func(t *gtest.T) {
btree := gtree.NewBKVTree[int, *Student](100, gutil.ComparatorTStr[int], true)
for i := 0; i < 10; i++ {
if i%2 == 0 {
btree.Set(i, &Student{})
} else {
var s *Student = nil
btree.Set(i, s)
}
}
t.Assert(btree.Size(), 10)
btree2 := gtree.NewBKVTreeWithChecker[int, *Student](100, gutil.ComparatorTStr[int], func(student *Student) bool {
return student == nil
}, true)
for i := 0; i < 10; i++ {
if i%2 == 0 {
btree2.Set(i, &Student{})
} else {
var s *Student = nil
btree2.Set(i, s)
}
}
t.Assert(btree2.Size(), 5)
})
}
func Test_NewRedBlackKVTreeWithChecker_TypedNil(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
type Student struct {
Name string
Age int
}
redBlackTree := gtree.NewRedBlackKVTree[int, *Student](gutil.ComparatorTStr[int], true)
for i := 0; i < 10; i++ {
if i%2 == 0 {
redBlackTree.Set(i, &Student{})
} else {
var s *Student = nil
redBlackTree.Set(i, s)
}
}
t.Assert(redBlackTree.Size(), 10)
redBlackTree2 := gtree.NewRedBlackKVTreeWithChecker[int, *Student](gutil.ComparatorTStr[int], func(student *Student) bool {
return student == nil
}, true)
for i := 0; i < 10; i++ {
if i%2 == 0 {
redBlackTree2.Set(i, &Student{})
} else {
var s *Student = nil
redBlackTree2.Set(i, s)
}
}
t.Assert(redBlackTree2.Size(), 5)
})
}