diff --git a/container/gset/gset_any_set.go b/container/gset/gset_any_set.go index de816c652b3..152fe1e9121 100644 --- a/container/gset/gset_any_set.go +++ b/container/gset/gset_any_set.go @@ -8,18 +8,15 @@ package gset import ( - "bytes" + "sync" - "github.com/gogf/gf/v2/internal/json" - "github.com/gogf/gf/v2/internal/rwmutex" - "github.com/gogf/gf/v2/text/gstr" "github.com/gogf/gf/v2/util/gconv" ) // Set is consisted of any items. type Set struct { - mu rwmutex.RWMutex - data map[any]struct{} + *TSet[any] + once sync.Once } // New create and returns a new set, which contains un-repeated items. @@ -33,44 +30,38 @@ func New(safe ...bool) *Set { // Also see New. func NewSet(safe ...bool) *Set { return &Set{ - data: make(map[any]struct{}), - mu: rwmutex.Create(safe...), + TSet: NewTSet[any](safe...), } } // NewFrom returns a new set from `items`. // Parameter `items` can be either a variable of any type, or a slice. func NewFrom(items any, safe ...bool) *Set { - m := make(map[any]struct{}) - for _, v := range gconv.Interfaces(items) { - m[v] = struct{}{} - } return &Set{ - data: m, - mu: rwmutex.Create(safe...), + TSet: NewTSetFrom[any](gconv.Interfaces(items), safe...), } } +// lazyInit lazily initializes the set. +func (a *Set) lazyInit() { + a.once.Do(func() { + if a.TSet == nil { + a.TSet = NewTSet[any]() + } + }) +} + // Iterator iterates the set readonly with given callback function `f`, // if `f` returns true then continue iterating; or false to stop. func (set *Set) Iterator(f func(v any) bool) { - for _, k := range set.Slice() { - if !f(k) { - break - } - } + set.lazyInit() + set.TSet.Iterator(f) } // Add adds one or multiple items to the set. func (set *Set) Add(items ...any) { - set.mu.Lock() - if set.data == nil { - set.data = make(map[any]struct{}) - } - for _, v := range items { - set.data[v] = struct{}{} - } - set.mu.Unlock() + set.lazyInit() + set.TSet.Add(items...) } // AddIfNotExist checks whether item exists in the set, @@ -79,21 +70,8 @@ func (set *Set) Add(items ...any) { // // Note that, if `item` is nil, it does nothing and returns false. func (set *Set) AddIfNotExist(item any) bool { - if item == nil { - return false - } - if !set.Contains(item) { - set.mu.Lock() - defer set.mu.Unlock() - if set.data == nil { - set.data = make(map[any]struct{}) - } - if _, ok := set.data[item]; !ok { - set.data[item] = struct{}{} - return true - } - } - return false + set.lazyInit() + return set.TSet.AddIfNotExist(item) } // AddIfNotExistFunc checks whether item exists in the set, @@ -103,23 +81,8 @@ func (set *Set) AddIfNotExist(item any) bool { // Note that, if `item` is nil, it does nothing and returns false. The function `f` // is executed without writing lock. func (set *Set) AddIfNotExistFunc(item any, f func() bool) bool { - if item == nil { - return false - } - if !set.Contains(item) { - if f() { - set.mu.Lock() - defer set.mu.Unlock() - if set.data == nil { - set.data = make(map[any]struct{}) - } - if _, ok := set.data[item]; !ok { - set.data[item] = struct{}{} - return true - } - } - } - return false + set.lazyInit() + return set.TSet.AddIfNotExistFunc(item, f) } // AddIfNotExistFuncLock checks whether item exists in the set, @@ -129,95 +92,44 @@ func (set *Set) AddIfNotExistFunc(item any, f func() bool) bool { // Note that, if `item` is nil, it does nothing and returns false. The function `f` // is executed within writing lock. func (set *Set) AddIfNotExistFuncLock(item any, f func() bool) bool { - if item == nil { - return false - } - if !set.Contains(item) { - set.mu.Lock() - defer set.mu.Unlock() - if set.data == nil { - set.data = make(map[any]struct{}) - } - if f() { - if _, ok := set.data[item]; !ok { - set.data[item] = struct{}{} - return true - } - } - } - return false + set.lazyInit() + return set.TSet.AddIfNotExistFuncLock(item, f) } // Contains checks whether the set contains `item`. func (set *Set) Contains(item any) bool { - var ok bool - set.mu.RLock() - if set.data != nil { - _, ok = set.data[item] - } - set.mu.RUnlock() - return ok + set.lazyInit() + return set.TSet.Contains(item) } // Remove deletes `item` from set. func (set *Set) Remove(item any) { - set.mu.Lock() - if set.data != nil { - delete(set.data, item) - } - set.mu.Unlock() + set.lazyInit() + set.TSet.Remove(item) } // Size returns the size of the set. func (set *Set) Size() int { - set.mu.RLock() - l := len(set.data) - set.mu.RUnlock() - return l + set.lazyInit() + return set.TSet.Size() } // Clear deletes all items of the set. func (set *Set) Clear() { - set.mu.Lock() - set.data = make(map[any]struct{}) - set.mu.Unlock() + set.lazyInit() + set.TSet.Clear() } // Slice returns all items of the set as slice. func (set *Set) Slice() []any { - set.mu.RLock() - var ( - i = 0 - ret = make([]any, len(set.data)) - ) - for item := range set.data { - ret[i] = item - i++ - } - set.mu.RUnlock() - return ret + set.lazyInit() + return set.TSet.Slice() } // Join joins items with a string `glue`. func (set *Set) Join(glue string) string { - set.mu.RLock() - defer set.mu.RUnlock() - if len(set.data) == 0 { - return "" - } - var ( - l = len(set.data) - i = 0 - buffer = bytes.NewBuffer(nil) - ) - for k := range set.data { - buffer.WriteString(gconv.String(k)) - if i != l-1 { - buffer.WriteString(glue) - } - i++ - } - return buffer.String() + set.lazyInit() + return set.TSet.Join(glue) } // String returns items as a string, which implements like json.Marshal does. @@ -225,63 +137,27 @@ func (set *Set) String() string { if set == nil { return "" } - set.mu.RLock() - defer set.mu.RUnlock() - var ( - s string - l = len(set.data) - i = 0 - buffer = bytes.NewBuffer(nil) - ) - buffer.WriteByte('[') - for k := range set.data { - s = gconv.String(k) - if gstr.IsNumeric(s) { - buffer.WriteString(s) - } else { - buffer.WriteString(`"` + gstr.QuoteMeta(s, `"\`) + `"`) - } - if i != l-1 { - buffer.WriteByte(',') - } - i++ - } - buffer.WriteByte(']') - return buffer.String() + set.lazyInit() + return set.TSet.String() } // LockFunc locks writing with callback function `f`. func (set *Set) LockFunc(f func(m map[any]struct{})) { - set.mu.Lock() - defer set.mu.Unlock() - f(set.data) + set.lazyInit() + set.TSet.LockFunc(f) } // RLockFunc locks reading with callback function `f`. func (set *Set) RLockFunc(f func(m map[any]struct{})) { - set.mu.RLock() - defer set.mu.RUnlock() - f(set.data) + set.lazyInit() + set.TSet.RLockFunc(f) } // Equal checks whether the two sets equal. func (set *Set) Equal(other *Set) bool { - if set == other { - return true - } - set.mu.RLock() - defer set.mu.RUnlock() - other.mu.RLock() - defer other.mu.RUnlock() - if len(set.data) != len(other.data) { - return false - } - for key := range set.data { - if _, ok := other.data[key]; !ok { - return false - } - } - return true + set.lazyInit() + other.lazyInit() + return set.TSet.Equal(other.TSet) } // IsSubsetOf checks whether the current set is a sub-set of `other`. @@ -289,85 +165,40 @@ func (set *Set) IsSubsetOf(other *Set) bool { if set == other { return true } - set.mu.RLock() - defer set.mu.RUnlock() - other.mu.RLock() - defer other.mu.RUnlock() - for key := range set.data { - if _, ok := other.data[key]; !ok { - return false - } - } - return true + + set.lazyInit() + other.lazyInit() + + return set.TSet.IsSubsetOf(other.TSet) } // Union returns a new set which is the union of `set` and `others`. // Which means, all the items in `newSet` are in `set` or in `others`. func (set *Set) Union(others ...*Set) (newSet *Set) { - newSet = NewSet() - set.mu.RLock() - defer set.mu.RUnlock() - for _, other := range others { - if set != other { - other.mu.RLock() - } - for k, v := range set.data { - newSet.data[k] = v - } - if set != other { - for k, v := range other.data { - newSet.data[k] = v - } - } - if set != other { - other.mu.RUnlock() - } - } + set.lazyInit() - return + return &Set{ + TSet: set.TSet.Union(set.toTSetSlice(others)...), + } } // Diff returns a new set which is the difference set from `set` to `others`. // Which means, all the items in `newSet` are in `set` but not in `others`. func (set *Set) Diff(others ...*Set) (newSet *Set) { - newSet = NewSet() - set.mu.RLock() - defer set.mu.RUnlock() - for _, other := range others { - if set == other { - continue - } - other.mu.RLock() - for k, v := range set.data { - if _, ok := other.data[k]; !ok { - newSet.data[k] = v - } - } - other.mu.RUnlock() + set.lazyInit() + + return &Set{ + TSet: set.TSet.Diff(set.toTSetSlice(others)...), } - return } // Intersect returns a new set which is the intersection from `set` to `others`. // Which means, all the items in `newSet` are in `set` and also in `others`. func (set *Set) Intersect(others ...*Set) (newSet *Set) { - newSet = NewSet() - set.mu.RLock() - defer set.mu.RUnlock() - for _, other := range others { - if set != other { - other.mu.RLock() - } - for k, v := range set.data { - if _, ok := other.data[k]; ok { - newSet.data[k] = v - } - } - if set != other { - other.mu.RUnlock() - } + set.lazyInit() + return &Set{ + TSet: set.TSet.Intersect(set.toTSetSlice(others)...), } - return } // Complement returns a new set which is the complement from `set` to `full`. @@ -376,36 +207,22 @@ func (set *Set) Intersect(others ...*Set) (newSet *Set) { // It returns the difference between `full` and `set` // if the given set `full` is not the full set of `set`. func (set *Set) Complement(full *Set) (newSet *Set) { - newSet = NewSet() - set.mu.RLock() - defer set.mu.RUnlock() - if set != full { - full.mu.RLock() - defer full.mu.RUnlock() - } - for k, v := range full.data { - if _, ok := set.data[k]; !ok { - newSet.data[k] = v + set.lazyInit() + if full == nil { + return &Set{ + TSet: NewTSet[any](true), } } - return + full.lazyInit() + return &Set{ + TSet: set.TSet.Complement(full.TSet), + } } // Merge adds items from `others` sets into `set`. func (set *Set) Merge(others ...*Set) *Set { - set.mu.Lock() - defer set.mu.Unlock() - for _, other := range others { - if set != other { - other.mu.RLock() - } - for k, v := range other.data { - set.data[k] = v - } - if set != other { - other.mu.RUnlock() - } - } + set.lazyInit() + set.TSet.Merge(set.toTSetSlice(others)...) return set } @@ -413,101 +230,46 @@ func (set *Set) Merge(others ...*Set) *Set { // Note: The items should be converted to int type, // or you'd get a result that you unexpected. func (set *Set) Sum() (sum int) { - set.mu.RLock() - defer set.mu.RUnlock() - for k := range set.data { - sum += gconv.Int(k) - } - return + set.lazyInit() + return set.TSet.Sum() } // Pop randomly pops an item from set. func (set *Set) Pop() any { - set.mu.Lock() - defer set.mu.Unlock() - for k := range set.data { - delete(set.data, k) - return k - } - return nil + set.lazyInit() + return set.TSet.Pop() } // Pops randomly pops `size` items from set. // It returns all items if size == -1. func (set *Set) Pops(size int) []any { - set.mu.Lock() - defer set.mu.Unlock() - if size > len(set.data) || size == -1 { - size = len(set.data) - } - if size <= 0 { - return nil - } - index := 0 - array := make([]any, size) - for k := range set.data { - delete(set.data, k) - array[index] = k - index++ - if index == size { - break - } - } - return array + set.lazyInit() + return set.TSet.Pops(size) } // Walk applies a user supplied function `f` to every item of set. func (set *Set) Walk(f func(item any) any) *Set { - set.mu.Lock() - defer set.mu.Unlock() - m := make(map[any]struct{}, len(set.data)) - for k, v := range set.data { - m[f(k)] = v - } - set.data = m + set.lazyInit() + set.TSet.Walk(f) return set } // MarshalJSON implements the interface MarshalJSON for json.Marshal. func (set Set) MarshalJSON() ([]byte, error) { - return json.Marshal(set.Slice()) + set.lazyInit() + return set.TSet.MarshalJSON() } // UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal. func (set *Set) UnmarshalJSON(b []byte) error { - set.mu.Lock() - defer set.mu.Unlock() - if set.data == nil { - set.data = make(map[any]struct{}) - } - var array []any - if err := json.UnmarshalUseNumber(b, &array); err != nil { - return err - } - for _, v := range array { - set.data[v] = struct{}{} - } - return nil + set.lazyInit() + return set.TSet.UnmarshalJSON(b) } // UnmarshalValue is an interface implement which sets any type of value for set. func (set *Set) UnmarshalValue(value any) (err error) { - set.mu.Lock() - defer set.mu.Unlock() - if set.data == nil { - set.data = make(map[any]struct{}) - } - var array []any - switch value.(type) { - case string, []byte: - err = json.UnmarshalUseNumber(gconv.Bytes(value), &array) - default: - array = gconv.SliceAny(value) - } - for _, v := range array { - set.data[v] = struct{}{} - } - return + set.lazyInit() + return set.TSet.UnmarshalValue(value) } // DeepCopy implements interface for deep copy of current type. @@ -515,11 +277,21 @@ func (set *Set) DeepCopy() any { if set == nil { return nil } - set.mu.RLock() - defer set.mu.RUnlock() - data := make([]any, 0) - for k := range set.data { - data = append(data, k) + set.lazyInit() + return &Set{ + TSet: set.TSet.DeepCopy().(*TSet[any]), } - return NewFrom(data, set.mu.IsSafe()) +} + +// toTSetSlice converts []*Set to []*TSet[any] +func (set *Set) toTSetSlice(sets []*Set) (tSets []*TSet[any]) { + tSets = make([]*TSet[any], len(sets)) + for i, v := range sets { + if v == nil { + continue + } + v.lazyInit() + tSets[i] = v.TSet + } + return } diff --git a/container/gset/gset_int_set.go b/container/gset/gset_int_set.go index 211c7d86d39..c524bc03e14 100644 --- a/container/gset/gset_int_set.go +++ b/container/gset/gset_int_set.go @@ -8,17 +8,13 @@ package gset import ( - "bytes" - - "github.com/gogf/gf/v2/internal/json" - "github.com/gogf/gf/v2/internal/rwmutex" - "github.com/gogf/gf/v2/util/gconv" + "sync" ) // IntSet is consisted of int items. type IntSet struct { - mu rwmutex.RWMutex - data map[int]struct{} + *TSet[int] + once sync.Once } // NewIntSet create and returns a new set, which contains un-repeated items. @@ -26,43 +22,37 @@ type IntSet struct { // which is false in default. func NewIntSet(safe ...bool) *IntSet { return &IntSet{ - mu: rwmutex.Create(safe...), - data: make(map[int]struct{}), + TSet: NewTSet[int](safe...), } } // NewIntSetFrom returns a new set from `items`. func NewIntSetFrom(items []int, safe ...bool) *IntSet { - m := make(map[int]struct{}) - for _, v := range items { - m[v] = struct{}{} - } return &IntSet{ - mu: rwmutex.Create(safe...), - data: m, + TSet: NewTSetFrom(items, safe...), } } +// lazyInit lazily initializes the set. +func (a *IntSet) lazyInit() { + a.once.Do(func() { + if a.TSet == nil { + a.TSet = NewTSet[int]() + } + }) +} + // Iterator iterates the set readonly with given callback function `f`, // if `f` returns true then continue iterating; or false to stop. func (set *IntSet) Iterator(f func(v int) bool) { - for _, k := range set.Slice() { - if !f(k) { - break - } - } + set.lazyInit() + set.TSet.Iterator(f) } // Add adds one or multiple items to the set. func (set *IntSet) Add(item ...int) { - set.mu.Lock() - if set.data == nil { - set.data = make(map[int]struct{}) - } - for _, v := range item { - set.data[v] = struct{}{} - } - set.mu.Unlock() + set.lazyInit() + set.TSet.Add(item...) } // AddIfNotExist checks whether item exists in the set, @@ -71,18 +61,8 @@ func (set *IntSet) Add(item ...int) { // // Note that, if `item` is nil, it does nothing and returns false. func (set *IntSet) AddIfNotExist(item int) bool { - if !set.Contains(item) { - set.mu.Lock() - defer set.mu.Unlock() - if set.data == nil { - set.data = make(map[int]struct{}) - } - if _, ok := set.data[item]; !ok { - set.data[item] = struct{}{} - return true - } - } - return false + set.lazyInit() + return set.TSet.AddIfNotExist(item) } // AddIfNotExistFunc checks whether item exists in the set, @@ -91,20 +71,8 @@ func (set *IntSet) AddIfNotExist(item int) bool { // // Note that, the function `f` is executed without writing lock. func (set *IntSet) AddIfNotExistFunc(item int, f func() bool) bool { - if !set.Contains(item) { - if f() { - set.mu.Lock() - defer set.mu.Unlock() - if set.data == nil { - set.data = make(map[int]struct{}) - } - if _, ok := set.data[item]; !ok { - set.data[item] = struct{}{} - return true - } - } - } - return false + set.lazyInit() + return set.TSet.AddIfNotExistFunc(item, f) } // AddIfNotExistFuncLock checks whether item exists in the set, @@ -113,92 +81,44 @@ func (set *IntSet) AddIfNotExistFunc(item int, f func() bool) bool { // // Note that, the function `f` is executed without writing lock. func (set *IntSet) AddIfNotExistFuncLock(item int, f func() bool) bool { - if !set.Contains(item) { - set.mu.Lock() - defer set.mu.Unlock() - if set.data == nil { - set.data = make(map[int]struct{}) - } - if f() { - if _, ok := set.data[item]; !ok { - set.data[item] = struct{}{} - return true - } - } - } - return false + set.lazyInit() + return set.TSet.AddIfNotExistFuncLock(item, f) } // Contains checks whether the set contains `item`. func (set *IntSet) Contains(item int) bool { - var ok bool - set.mu.RLock() - if set.data != nil { - _, ok = set.data[item] - } - set.mu.RUnlock() - return ok + set.lazyInit() + return set.TSet.Contains(item) } // Remove deletes `item` from set. func (set *IntSet) Remove(item int) { - set.mu.Lock() - if set.data != nil { - delete(set.data, item) - } - set.mu.Unlock() + set.lazyInit() + set.TSet.Remove(item) } // Size returns the size of the set. func (set *IntSet) Size() int { - set.mu.RLock() - l := len(set.data) - set.mu.RUnlock() - return l + set.lazyInit() + return set.TSet.Size() } // Clear deletes all items of the set. func (set *IntSet) Clear() { - set.mu.Lock() - set.data = make(map[int]struct{}) - set.mu.Unlock() + set.lazyInit() + set.TSet.Clear() } // Slice returns the an of items of the set as slice. func (set *IntSet) Slice() []int { - set.mu.RLock() - var ( - i = 0 - ret = make([]int, len(set.data)) - ) - for k := range set.data { - ret[i] = k - i++ - } - set.mu.RUnlock() - return ret + set.lazyInit() + return set.TSet.Slice() } // Join joins items with a string `glue`. func (set *IntSet) Join(glue string) string { - set.mu.RLock() - defer set.mu.RUnlock() - if len(set.data) == 0 { - return "" - } - var ( - l = len(set.data) - i = 0 - buffer = bytes.NewBuffer(nil) - ) - for k := range set.data { - buffer.WriteString(gconv.String(k)) - if i != l-1 { - buffer.WriteString(glue) - } - i++ - } - return buffer.String() + set.lazyInit() + return set.TSet.Join(glue) } // String returns items as a string, which implements like json.Marshal does. @@ -206,41 +126,27 @@ func (set *IntSet) String() string { if set == nil { return "" } - return "[" + set.Join(",") + "]" + set.lazyInit() + return set.TSet.String() } // LockFunc locks writing with callback function `f`. func (set *IntSet) LockFunc(f func(m map[int]struct{})) { - set.mu.Lock() - defer set.mu.Unlock() - f(set.data) + set.lazyInit() + set.TSet.LockFunc(f) } // RLockFunc locks reading with callback function `f`. func (set *IntSet) RLockFunc(f func(m map[int]struct{})) { - set.mu.RLock() - defer set.mu.RUnlock() - f(set.data) + set.lazyInit() + set.TSet.RLockFunc(f) } // Equal checks whether the two sets equal. func (set *IntSet) Equal(other *IntSet) bool { - if set == other { - return true - } - set.mu.RLock() - defer set.mu.RUnlock() - other.mu.RLock() - defer other.mu.RUnlock() - if len(set.data) != len(other.data) { - return false - } - for key := range set.data { - if _, ok := other.data[key]; !ok { - return false - } - } - return true + set.lazyInit() + other.lazyInit() + return set.TSet.Equal(other.TSet) } // IsSubsetOf checks whether the current set is a sub-set of `other`. @@ -248,85 +154,38 @@ func (set *IntSet) IsSubsetOf(other *IntSet) bool { if set == other { return true } - set.mu.RLock() - defer set.mu.RUnlock() - other.mu.RLock() - defer other.mu.RUnlock() - for key := range set.data { - if _, ok := other.data[key]; !ok { - return false - } - } - return true + + set.lazyInit() + other.lazyInit() + + return set.TSet.IsSubsetOf(other.TSet) } // Union returns a new set which is the union of `set` and `other`. // Which means, all the items in `newSet` are in `set` or in `other`. func (set *IntSet) Union(others ...*IntSet) (newSet *IntSet) { - newSet = NewIntSet() - set.mu.RLock() - defer set.mu.RUnlock() - for _, other := range others { - if set != other { - other.mu.RLock() - } - for k, v := range set.data { - newSet.data[k] = v - } - if set != other { - for k, v := range other.data { - newSet.data[k] = v - } - } - if set != other { - other.mu.RUnlock() - } + set.lazyInit() + return &IntSet{ + TSet: set.TSet.Union(set.toTSetSlice(others)...), } - - return } // Diff returns a new set which is the difference set from `set` to `other`. // Which means, all the items in `newSet` are in `set` but not in `other`. func (set *IntSet) Diff(others ...*IntSet) (newSet *IntSet) { - newSet = NewIntSet() - set.mu.RLock() - defer set.mu.RUnlock() - for _, other := range others { - if set == other { - continue - } - other.mu.RLock() - for k, v := range set.data { - if _, ok := other.data[k]; !ok { - newSet.data[k] = v - } - } - other.mu.RUnlock() + set.lazyInit() + return &IntSet{ + TSet: set.TSet.Diff(set.toTSetSlice(others)...), } - return } // Intersect returns a new set which is the intersection from `set` to `other`. // Which means, all the items in `newSet` are in `set` and also in `other`. func (set *IntSet) Intersect(others ...*IntSet) (newSet *IntSet) { - newSet = NewIntSet() - set.mu.RLock() - defer set.mu.RUnlock() - for _, other := range others { - if set != other { - other.mu.RLock() - } - for k, v := range set.data { - if _, ok := other.data[k]; ok { - newSet.data[k] = v - } - } - if set != other { - other.mu.RUnlock() - } + set.lazyInit() + return &IntSet{ + TSet: set.TSet.Intersect(set.toTSetSlice(others)...), } - return } // Complement returns a new set which is the complement from `set` to `full`. @@ -335,36 +194,22 @@ func (set *IntSet) Intersect(others ...*IntSet) (newSet *IntSet) { // It returns the difference between `full` and `set` // if the given set `full` is not the full set of `set`. func (set *IntSet) Complement(full *IntSet) (newSet *IntSet) { - newSet = NewIntSet() - set.mu.RLock() - defer set.mu.RUnlock() - if set != full { - full.mu.RLock() - defer full.mu.RUnlock() - } - for k, v := range full.data { - if _, ok := set.data[k]; !ok { - newSet.data[k] = v + set.lazyInit() + if full == nil { + return &IntSet{ + TSet: NewTSet[int](), } } - return + full.lazyInit() + return &IntSet{ + TSet: set.TSet.Complement(full.TSet), + } } // Merge adds items from `others` sets into `set`. func (set *IntSet) Merge(others ...*IntSet) *IntSet { - set.mu.Lock() - defer set.mu.Unlock() - for _, other := range others { - if set != other { - other.mu.RLock() - } - for k, v := range other.data { - set.data[k] = v - } - if set != other { - other.mu.RUnlock() - } - } + set.lazyInit() + set.TSet.Merge(set.toTSetSlice(others)...) return set } @@ -372,101 +217,46 @@ func (set *IntSet) Merge(others ...*IntSet) *IntSet { // Note: The items should be converted to int type, // or you'd get a result that you unexpected. func (set *IntSet) Sum() (sum int) { - set.mu.RLock() - defer set.mu.RUnlock() - for k := range set.data { - sum += k - } - return + set.lazyInit() + return set.TSet.Sum() } // Pop randomly pops an item from set. func (set *IntSet) Pop() int { - set.mu.Lock() - defer set.mu.Unlock() - for k := range set.data { - delete(set.data, k) - return k - } - return 0 + set.lazyInit() + return set.TSet.Pop() } // Pops randomly pops `size` items from set. // It returns all items if size == -1. func (set *IntSet) Pops(size int) []int { - set.mu.Lock() - defer set.mu.Unlock() - if size > len(set.data) || size == -1 { - size = len(set.data) - } - if size <= 0 { - return nil - } - index := 0 - array := make([]int, size) - for k := range set.data { - delete(set.data, k) - array[index] = k - index++ - if index == size { - break - } - } - return array + set.lazyInit() + return set.TSet.Pops(size) } // Walk applies a user supplied function `f` to every item of set. func (set *IntSet) Walk(f func(item int) int) *IntSet { - set.mu.Lock() - defer set.mu.Unlock() - m := make(map[int]struct{}, len(set.data)) - for k, v := range set.data { - m[f(k)] = v - } - set.data = m + set.lazyInit() + set.TSet.Walk(f) return set } // MarshalJSON implements the interface MarshalJSON for json.Marshal. func (set IntSet) MarshalJSON() ([]byte, error) { - return json.Marshal(set.Slice()) + set.lazyInit() + return set.TSet.MarshalJSON() } // UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal. func (set *IntSet) UnmarshalJSON(b []byte) error { - set.mu.Lock() - defer set.mu.Unlock() - if set.data == nil { - set.data = make(map[int]struct{}) - } - var array []int - if err := json.UnmarshalUseNumber(b, &array); err != nil { - return err - } - for _, v := range array { - set.data[v] = struct{}{} - } - return nil + set.lazyInit() + return set.TSet.UnmarshalJSON(b) } // UnmarshalValue is an interface implement which sets any type of value for set. func (set *IntSet) UnmarshalValue(value any) (err error) { - set.mu.Lock() - defer set.mu.Unlock() - if set.data == nil { - set.data = make(map[int]struct{}) - } - var array []int - switch value.(type) { - case string, []byte: - err = json.UnmarshalUseNumber(gconv.Bytes(value), &array) - default: - array = gconv.SliceInt(value) - } - for _, v := range array { - set.data[v] = struct{}{} - } - return + set.lazyInit() + return set.TSet.UnmarshalValue(value) } // DeepCopy implements interface for deep copy of current type. @@ -474,15 +264,21 @@ func (set *IntSet) DeepCopy() any { if set == nil { return nil } - set.mu.RLock() - defer set.mu.RUnlock() - var ( - slice = make([]int, len(set.data)) - index = 0 - ) - for k := range set.data { - slice[index] = k - index++ + set.lazyInit() + return &IntSet{ + TSet: set.TSet.DeepCopy().(*TSet[int]), + } +} + +// toTSetSlice converts []*IntSet to []*TSet[int] +func (set *IntSet) toTSetSlice(sets []*IntSet) (tSets []*TSet[int]) { + tSets = make([]*TSet[int], len(sets)) + for i, v := range sets { + if v == nil { + continue + } + v.lazyInit() + tSets[i] = v.TSet } - return NewIntSetFrom(slice, set.mu.IsSafe()) + return } diff --git a/container/gset/gset_str_set.go b/container/gset/gset_str_set.go index 660c85a9378..3fc04199643 100644 --- a/container/gset/gset_str_set.go +++ b/container/gset/gset_str_set.go @@ -8,19 +8,14 @@ package gset import ( - "bytes" "strings" - - "github.com/gogf/gf/v2/internal/json" - "github.com/gogf/gf/v2/internal/rwmutex" - "github.com/gogf/gf/v2/text/gstr" - "github.com/gogf/gf/v2/util/gconv" + "sync" ) // StrSet is consisted of string items. type StrSet struct { - mu rwmutex.RWMutex - data map[string]struct{} + *TSet[string] + once sync.Once } // NewStrSet create and returns a new set, which contains un-repeated items. @@ -28,61 +23,45 @@ type StrSet struct { // which is false in default. func NewStrSet(safe ...bool) *StrSet { return &StrSet{ - mu: rwmutex.Create(safe...), - data: make(map[string]struct{}), + TSet: NewTSet[string](safe...), } } // NewStrSetFrom returns a new set from `items`. func NewStrSetFrom(items []string, safe ...bool) *StrSet { - m := make(map[string]struct{}) - for _, v := range items { - m[v] = struct{}{} - } return &StrSet{ - mu: rwmutex.Create(safe...), - data: m, + TSet: NewTSetFrom(items, safe...), } } +// lazyInit lazily initializes the set. +func (a *StrSet) lazyInit() { + a.once.Do(func() { + if a.TSet == nil { + a.TSet = NewTSet[string]() + } + }) +} + // Iterator iterates the set readonly with given callback function `f`, // if `f` returns true then continue iterating; or false to stop. func (set *StrSet) Iterator(f func(v string) bool) { - for _, k := range set.Slice() { - if !f(k) { - break - } - } + set.lazyInit() + set.TSet.Iterator(f) } // Add adds one or multiple items to the set. func (set *StrSet) Add(item ...string) { - set.mu.Lock() - if set.data == nil { - set.data = make(map[string]struct{}) - } - for _, v := range item { - set.data[v] = struct{}{} - } - set.mu.Unlock() + set.lazyInit() + set.TSet.Add(item...) } // AddIfNotExist checks whether item exists in the set, // it adds the item to set and returns true if it does not exist in the set, // or else it does nothing and returns false. func (set *StrSet) AddIfNotExist(item string) bool { - if !set.Contains(item) { - set.mu.Lock() - defer set.mu.Unlock() - if set.data == nil { - set.data = make(map[string]struct{}) - } - if _, ok := set.data[item]; !ok { - set.data[item] = struct{}{} - return true - } - } - return false + set.lazyInit() + return set.TSet.AddIfNotExist(item) } // AddIfNotExistFunc checks whether item exists in the set, @@ -91,20 +70,8 @@ func (set *StrSet) AddIfNotExist(item string) bool { // // Note that, the function `f` is executed without writing lock. func (set *StrSet) AddIfNotExistFunc(item string, f func() bool) bool { - if !set.Contains(item) { - if f() { - set.mu.Lock() - defer set.mu.Unlock() - if set.data == nil { - set.data = make(map[string]struct{}) - } - if _, ok := set.data[item]; !ok { - set.data[item] = struct{}{} - return true - } - } - } - return false + set.lazyInit() + return set.TSet.AddIfNotExistFunc(item, f) } // AddIfNotExistFuncLock checks whether item exists in the set, @@ -113,36 +80,20 @@ func (set *StrSet) AddIfNotExistFunc(item string, f func() bool) bool { // // Note that, the function `f` is executed without writing lock. func (set *StrSet) AddIfNotExistFuncLock(item string, f func() bool) bool { - if !set.Contains(item) { - set.mu.Lock() - defer set.mu.Unlock() - if set.data == nil { - set.data = make(map[string]struct{}) - } - if f() { - if _, ok := set.data[item]; !ok { - set.data[item] = struct{}{} - return true - } - } - } - return false + set.lazyInit() + return set.TSet.AddIfNotExistFuncLock(item, f) } // Contains checks whether the set contains `item`. func (set *StrSet) Contains(item string) bool { - var ok bool - set.mu.RLock() - if set.data != nil { - _, ok = set.data[item] - } - set.mu.RUnlock() - return ok + set.lazyInit() + return set.TSet.Contains(item) } // ContainsI checks whether a value exists in the set with case-insensitively. // Note that it internally iterates the whole set to do the comparison with case-insensitively. func (set *StrSet) ContainsI(item string) bool { + set.lazyInit() set.mu.RLock() defer set.mu.RUnlock() for k := range set.data { @@ -155,64 +106,32 @@ func (set *StrSet) ContainsI(item string) bool { // Remove deletes `item` from set. func (set *StrSet) Remove(item string) { - set.mu.Lock() - if set.data != nil { - delete(set.data, item) - } - set.mu.Unlock() + set.lazyInit() + set.TSet.Remove(item) } // Size returns the size of the set. func (set *StrSet) Size() int { - set.mu.RLock() - l := len(set.data) - set.mu.RUnlock() - return l + set.lazyInit() + return set.TSet.Size() } // Clear deletes all items of the set. func (set *StrSet) Clear() { - set.mu.Lock() - set.data = make(map[string]struct{}) - set.mu.Unlock() + set.lazyInit() + set.TSet.Clear() } // Slice returns the an of items of the set as slice. func (set *StrSet) Slice() []string { - set.mu.RLock() - var ( - i = 0 - ret = make([]string, len(set.data)) - ) - for item := range set.data { - ret[i] = item - i++ - } - - set.mu.RUnlock() - return ret + set.lazyInit() + return set.TSet.Slice() } // Join joins items with a string `glue`. func (set *StrSet) Join(glue string) string { - set.mu.RLock() - defer set.mu.RUnlock() - if len(set.data) == 0 { - return "" - } - var ( - l = len(set.data) - i = 0 - buffer = bytes.NewBuffer(nil) - ) - for k := range set.data { - buffer.WriteString(k) - if i != l-1 { - buffer.WriteString(glue) - } - i++ - } - return buffer.String() + set.lazyInit() + return set.TSet.Join(glue) } // String returns items as a string, which implements like json.Marshal does. @@ -220,57 +139,27 @@ func (set *StrSet) String() string { if set == nil { return "" } - set.mu.RLock() - defer set.mu.RUnlock() - var ( - l = len(set.data) - i = 0 - buffer = bytes.NewBuffer(nil) - ) - buffer.WriteByte('[') - for k := range set.data { - buffer.WriteString(`"` + gstr.QuoteMeta(k, `"\`) + `"`) - if i != l-1 { - buffer.WriteByte(',') - } - i++ - } - buffer.WriteByte(']') - return buffer.String() + set.lazyInit() + return set.TSet.String() } // LockFunc locks writing with callback function `f`. func (set *StrSet) LockFunc(f func(m map[string]struct{})) { - set.mu.Lock() - defer set.mu.Unlock() - f(set.data) + set.lazyInit() + set.TSet.LockFunc(f) } // RLockFunc locks reading with callback function `f`. func (set *StrSet) RLockFunc(f func(m map[string]struct{})) { - set.mu.RLock() - defer set.mu.RUnlock() - f(set.data) + set.lazyInit() + set.TSet.RLockFunc(f) } // Equal checks whether the two sets equal. func (set *StrSet) Equal(other *StrSet) bool { - if set == other { - return true - } - set.mu.RLock() - defer set.mu.RUnlock() - other.mu.RLock() - defer other.mu.RUnlock() - if len(set.data) != len(other.data) { - return false - } - for key := range set.data { - if _, ok := other.data[key]; !ok { - return false - } - } - return true + set.lazyInit() + other.lazyInit() + return set.TSet.Equal(other.TSet) } // IsSubsetOf checks whether the current set is a sub-set of `other`. @@ -278,85 +167,38 @@ func (set *StrSet) IsSubsetOf(other *StrSet) bool { if set == other { return true } - set.mu.RLock() - defer set.mu.RUnlock() - other.mu.RLock() - defer other.mu.RUnlock() - for key := range set.data { - if _, ok := other.data[key]; !ok { - return false - } - } - return true + + set.lazyInit() + other.lazyInit() + + return set.TSet.IsSubsetOf(other.TSet) } // Union returns a new set which is the union of `set` and `other`. // Which means, all the items in `newSet` are in `set` or in `other`. func (set *StrSet) Union(others ...*StrSet) (newSet *StrSet) { - newSet = NewStrSet() - set.mu.RLock() - defer set.mu.RUnlock() - for _, other := range others { - if set != other { - other.mu.RLock() - } - for k, v := range set.data { - newSet.data[k] = v - } - if set != other { - for k, v := range other.data { - newSet.data[k] = v - } - } - if set != other { - other.mu.RUnlock() - } + set.lazyInit() + return &StrSet{ + TSet: set.TSet.Union(set.toTSetSlice(others)...), } - - return } // Diff returns a new set which is the difference set from `set` to `other`. // Which means, all the items in `newSet` are in `set` but not in `other`. func (set *StrSet) Diff(others ...*StrSet) (newSet *StrSet) { - newSet = NewStrSet() - set.mu.RLock() - defer set.mu.RUnlock() - for _, other := range others { - if set == other { - continue - } - other.mu.RLock() - for k, v := range set.data { - if _, ok := other.data[k]; !ok { - newSet.data[k] = v - } - } - other.mu.RUnlock() + set.lazyInit() + return &StrSet{ + TSet: set.TSet.Diff(set.toTSetSlice(others)...), } - return } // Intersect returns a new set which is the intersection from `set` to `other`. // Which means, all the items in `newSet` are in `set` and also in `other`. func (set *StrSet) Intersect(others ...*StrSet) (newSet *StrSet) { - newSet = NewStrSet() - set.mu.RLock() - defer set.mu.RUnlock() - for _, other := range others { - if set != other { - other.mu.RLock() - } - for k, v := range set.data { - if _, ok := other.data[k]; ok { - newSet.data[k] = v - } - } - if set != other { - other.mu.RUnlock() - } + set.lazyInit() + return &StrSet{ + TSet: set.TSet.Intersect(set.toTSetSlice(others)...), } - return } // Complement returns a new set which is the complement from `set` to `full`. @@ -365,36 +207,22 @@ func (set *StrSet) Intersect(others ...*StrSet) (newSet *StrSet) { // It returns the difference between `full` and `set` // if the given set `full` is not the full set of `set`. func (set *StrSet) Complement(full *StrSet) (newSet *StrSet) { - newSet = NewStrSet() - set.mu.RLock() - defer set.mu.RUnlock() - if set != full { - full.mu.RLock() - defer full.mu.RUnlock() - } - for k, v := range full.data { - if _, ok := set.data[k]; !ok { - newSet.data[k] = v + set.lazyInit() + if full == nil { + return &StrSet{ + TSet: NewTSet[string](), } } - return + full.lazyInit() + return &StrSet{ + TSet: set.TSet.Complement(full.TSet), + } } // Merge adds items from `others` sets into `set`. func (set *StrSet) Merge(others ...*StrSet) *StrSet { - set.mu.Lock() - defer set.mu.Unlock() - for _, other := range others { - if set != other { - other.mu.RLock() - } - for k, v := range other.data { - set.data[k] = v - } - if set != other { - other.mu.RUnlock() - } - } + set.lazyInit() + set.TSet.Merge(set.toTSetSlice(others)...) return set } @@ -402,101 +230,46 @@ func (set *StrSet) Merge(others ...*StrSet) *StrSet { // Note: The items should be converted to int type, // or you'd get a result that you unexpected. func (set *StrSet) Sum() (sum int) { - set.mu.RLock() - defer set.mu.RUnlock() - for k := range set.data { - sum += gconv.Int(k) - } - return + set.lazyInit() + return set.TSet.Sum() } // Pop randomly pops an item from set. func (set *StrSet) Pop() string { - set.mu.Lock() - defer set.mu.Unlock() - for k := range set.data { - delete(set.data, k) - return k - } - return "" + set.lazyInit() + return set.TSet.Pop() } // Pops randomly pops `size` items from set. // It returns all items if size == -1. func (set *StrSet) Pops(size int) []string { - set.mu.Lock() - defer set.mu.Unlock() - if size > len(set.data) || size == -1 { - size = len(set.data) - } - if size <= 0 { - return nil - } - index := 0 - array := make([]string, size) - for k := range set.data { - delete(set.data, k) - array[index] = k - index++ - if index == size { - break - } - } - return array + set.lazyInit() + return set.TSet.Pops(size) } // Walk applies a user supplied function `f` to every item of set. func (set *StrSet) Walk(f func(item string) string) *StrSet { - set.mu.Lock() - defer set.mu.Unlock() - m := make(map[string]struct{}, len(set.data)) - for k, v := range set.data { - m[f(k)] = v - } - set.data = m + set.lazyInit() + set.TSet.Walk(f) return set } // MarshalJSON implements the interface MarshalJSON for json.Marshal. func (set StrSet) MarshalJSON() ([]byte, error) { - return json.Marshal(set.Slice()) + set.lazyInit() + return set.TSet.MarshalJSON() } // UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal. func (set *StrSet) UnmarshalJSON(b []byte) error { - set.mu.Lock() - defer set.mu.Unlock() - if set.data == nil { - set.data = make(map[string]struct{}) - } - var array []string - if err := json.UnmarshalUseNumber(b, &array); err != nil { - return err - } - for _, v := range array { - set.data[v] = struct{}{} - } - return nil + set.lazyInit() + return set.TSet.UnmarshalJSON(b) } // UnmarshalValue is an interface implement which sets any type of value for set. func (set *StrSet) UnmarshalValue(value any) (err error) { - set.mu.Lock() - defer set.mu.Unlock() - if set.data == nil { - set.data = make(map[string]struct{}) - } - var array []string - switch value.(type) { - case string, []byte: - err = json.UnmarshalUseNumber(gconv.Bytes(value), &array) - default: - array = gconv.SliceStr(value) - } - for _, v := range array { - set.data[v] = struct{}{} - } - return + set.lazyInit() + return set.TSet.UnmarshalValue(value) } // DeepCopy implements interface for deep copy of current type. @@ -504,15 +277,21 @@ func (set *StrSet) DeepCopy() any { if set == nil { return nil } - set.mu.RLock() - defer set.mu.RUnlock() - var ( - slice = make([]string, len(set.data)) - index = 0 - ) - for k := range set.data { - slice[index] = k - index++ + set.lazyInit() + return &StrSet{ + TSet: set.TSet.DeepCopy().(*TSet[string]), } - return NewStrSetFrom(slice, set.mu.IsSafe()) +} + +// toTSetSlice converts []*StrSet to []*TSet[string] +func (set *StrSet) toTSetSlice(sets []*StrSet) (tSets []*TSet[string]) { + tSets = make([]*TSet[string], len(sets)) + for i, v := range sets { + if v == nil { + continue + } + v.lazyInit() + tSets[i] = v.TSet + } + return } diff --git a/container/gset/gset_t_set.go b/container/gset/gset_t_set.go new file mode 100644 index 00000000000..ae3507ceca4 --- /dev/null +++ b/container/gset/gset_t_set.go @@ -0,0 +1,531 @@ +// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +package gset + +import ( + "bytes" + + "github.com/gogf/gf/v2/internal/json" + "github.com/gogf/gf/v2/internal/rwmutex" + "github.com/gogf/gf/v2/text/gstr" + "github.com/gogf/gf/v2/util/gconv" +) + +// TSet[T] is consisted of any items. +type TSet[T comparable] struct { + mu rwmutex.RWMutex + data map[T]struct{} +} + +// NewTSet creates and returns a new set, which contains un-repeated items. +// Also see New. +func NewTSet[T comparable](safe ...bool) *TSet[T] { + return &TSet[T]{ + data: make(map[T]struct{}), + mu: rwmutex.Create(safe...), + } +} + +// NewTSetFrom returns a new set from `items`. +// `items` - A slice of type T. +func NewTSetFrom[T comparable](items []T, safe ...bool) *TSet[T] { + m := make(map[T]struct{}) + for _, v := range items { + m[v] = struct{}{} + } + return &TSet[T]{ + data: m, + mu: rwmutex.Create(safe...), + } +} + +// Iterator iterates the set readonly with given callback function `f`, +// if `f` returns true then continue iterating; or false to stop. +func (set *TSet[T]) Iterator(f func(v T) bool) { + for _, k := range set.Slice() { + if !f(k) { + break + } + } +} + +// Add adds one or multiple items to the set. +func (set *TSet[T]) Add(items ...T) { + set.mu.Lock() + 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, +// it adds the item to set and returns true if it does not exists in the set, +// or else it does nothing and returns false. +// +// Note that, if `item` is nil, it does nothing and returns false. +func (set *TSet[T]) AddIfNotExist(item T) bool { + if any(item) == nil { + return false + } + if !set.Contains(item) { + set.mu.Lock() + defer set.mu.Unlock() + if set.data == nil { + set.data = make(map[T]struct{}) + } + if _, ok := set.data[item]; !ok { + set.data[item] = struct{}{} + return true + } + } + return false +} + +// AddIfNotExistFunc checks whether item exists in the set, +// it adds the item to set and returns true if it does not exist in the set and +// function `f` returns true, or else it does nothing and returns false. +// +// Note that, if `item` is nil, it does nothing and returns false. The function `f` +// is executed without writing lock. +func (set *TSet[T]) AddIfNotExistFunc(item T, f func() bool) bool { + if any(item) == nil { + return false + } + if !set.Contains(item) { + if f() { + set.mu.Lock() + defer set.mu.Unlock() + if set.data == nil { + set.data = make(map[T]struct{}) + } + if _, ok := set.data[item]; !ok { + set.data[item] = struct{}{} + return true + } + } + } + return false +} + +// AddIfNotExistFuncLock checks whether item exists in the set, +// it adds the item to set and returns true if it does not exists in the set and +// function `f` returns true, or else it does nothing and returns false. +// +// Note that, if `item` is nil, it does nothing and returns false. The function `f` +// is executed within writing lock. +func (set *TSet[T]) AddIfNotExistFuncLock(item T, f func() bool) bool { + if any(item) == nil { + return false + } + if !set.Contains(item) { + set.mu.Lock() + defer set.mu.Unlock() + if set.data == nil { + set.data = make(map[T]struct{}) + } + if f() { + if _, ok := set.data[item]; !ok { + set.data[item] = struct{}{} + return true + } + } + } + return false +} + +// Contains checks whether the set contains `item`. +func (set *TSet[T]) Contains(item T) bool { + var ok bool + set.mu.RLock() + if set.data != nil { + _, ok = set.data[item] + } + set.mu.RUnlock() + return ok +} + +// Remove deletes `item` from set. +func (set *TSet[T]) Remove(item T) { + set.mu.Lock() + if set.data != nil { + delete(set.data, item) + } + set.mu.Unlock() +} + +// Size returns the size of the set. +func (set *TSet[T]) Size() int { + set.mu.RLock() + l := len(set.data) + set.mu.RUnlock() + return l +} + +// Clear deletes all items of the set. +func (set *TSet[T]) Clear() { + set.mu.Lock() + set.data = make(map[T]struct{}) + set.mu.Unlock() +} + +// Slice returns all items of the set as slice. +func (set *TSet[T]) Slice() []T { + set.mu.RLock() + var ( + i = 0 + ret = make([]T, len(set.data)) + ) + for item := range set.data { + ret[i] = item + i++ + } + set.mu.RUnlock() + return ret +} + +// Join joins items with a string `glue`. +func (set *TSet[T]) Join(glue string) string { + set.mu.RLock() + defer set.mu.RUnlock() + if len(set.data) == 0 { + return "" + } + var ( + l = len(set.data) + i = 0 + buffer = bytes.NewBuffer(nil) + ) + for k := range set.data { + buffer.WriteString(gconv.String(k)) + if i != l-1 { + buffer.WriteString(glue) + } + i++ + } + return buffer.String() +} + +// String returns items as a string, which implements like json.Marshal does. +func (set *TSet[T]) String() string { + if set == nil { + return "" + } + set.mu.RLock() + defer set.mu.RUnlock() + var ( + s string + l = len(set.data) + i = 0 + buffer = bytes.NewBuffer(nil) + ) + buffer.WriteByte('[') + for k := range set.data { + s = gconv.String(k) + if gstr.IsNumeric(s) { + buffer.WriteString(s) + } else { + buffer.WriteString(`"` + gstr.QuoteMeta(s, `"\`) + `"`) + } + if i != l-1 { + buffer.WriteByte(',') + } + i++ + } + buffer.WriteByte(']') + return buffer.String() +} + +// LockFunc locks writing with callback function `f`. +func (set *TSet[T]) LockFunc(f func(m map[T]struct{})) { + set.mu.Lock() + defer set.mu.Unlock() + f(set.data) +} + +// RLockFunc locks reading with callback function `f`. +func (set *TSet[T]) RLockFunc(f func(m map[T]struct{})) { + set.mu.RLock() + defer set.mu.RUnlock() + f(set.data) +} + +// Equal checks whether the two sets equal. +func (set *TSet[T]) Equal(other *TSet[T]) bool { + if set == other { + return true + } + set.mu.RLock() + defer set.mu.RUnlock() + other.mu.RLock() + defer other.mu.RUnlock() + if len(set.data) != len(other.data) { + return false + } + for key := range set.data { + if _, ok := other.data[key]; !ok { + return false + } + } + return true +} + +// IsSubsetOf checks whether the current set is a sub-set of `other`. +func (set *TSet[T]) IsSubsetOf(other *TSet[T]) bool { + if set == other { + return true + } + set.mu.RLock() + defer set.mu.RUnlock() + other.mu.RLock() + defer other.mu.RUnlock() + for key := range set.data { + if _, ok := other.data[key]; !ok { + return false + } + } + return true +} + +// Union returns a new set which is the union of `set` and `others`. +// Which means, all the items in `newSet` are in `set` or in `others`. +func (set *TSet[T]) Union(others ...*TSet[T]) (newSet *TSet[T]) { + newSet = NewTSet[T]() + set.mu.RLock() + defer set.mu.RUnlock() + for _, other := range others { + if other == nil { + continue + } + if set != other { + other.mu.RLock() + } + for k, v := range set.data { + newSet.data[k] = v + } + if set != other { + for k, v := range other.data { + newSet.data[k] = v + } + } + if set != other { + other.mu.RUnlock() + } + } + + return +} + +// Diff returns a new set which is the difference set from `set` to `others`. +// Which means, all the items in `newSet` are in `set` but not in `others`. +func (set *TSet[T]) Diff(others ...*TSet[T]) (newSet *TSet[T]) { + newSet = NewTSet[T]() + set.mu.RLock() + defer set.mu.RUnlock() + for _, other := range others { + if other == nil { + continue + } + if set == other { + continue + } + other.mu.RLock() + for k, v := range set.data { + if _, ok := other.data[k]; !ok { + newSet.data[k] = v + } + } + other.mu.RUnlock() + } + return +} + +// Intersect returns a new set which is the intersection from `set` to `others`. +// Which means, all the items in `newSet` are in `set` and also in `others`. +func (set *TSet[T]) Intersect(others ...*TSet[T]) (newSet *TSet[T]) { + newSet = NewTSet[T]() + set.mu.RLock() + defer set.mu.RUnlock() + for _, other := range others { + if other == nil { + continue + } + if set != other { + other.mu.RLock() + } + for k, v := range set.data { + if _, ok := other.data[k]; ok { + newSet.data[k] = v + } + } + if set != other { + other.mu.RUnlock() + } + } + return +} + +// Complement returns a new set which is the complement from `set` to `full`. +// Which means, all the items in `newSet` are in `full` and not in `set`. +// +// It returns the difference between `full` and `set` +// if the given set `full` is not the full set of `set`. +func (set *TSet[T]) Complement(full *TSet[T]) (newSet *TSet[T]) { + newSet = NewTSet[T]() + set.mu.RLock() + defer set.mu.RUnlock() + if set != full { + full.mu.RLock() + defer full.mu.RUnlock() + } + for k, v := range full.data { + if _, ok := set.data[k]; !ok { + newSet.data[k] = v + } + } + return +} + +// Merge adds items from `others` sets into `set`. +func (set *TSet[T]) Merge(others ...*TSet[T]) *TSet[T] { + set.mu.Lock() + defer set.mu.Unlock() + for _, other := range others { + if other == nil { + continue + } + if set != other { + other.mu.RLock() + } + for k, v := range other.data { + set.data[k] = v + } + if set != other { + other.mu.RUnlock() + } + } + return set +} + +// Sum sums items. +// Note: The items should be converted to int type, +// or you'd get a result that you unexpected. +func (set *TSet[T]) Sum() (sum int) { + set.mu.RLock() + defer set.mu.RUnlock() + for k := range set.data { + sum += gconv.Int(k) + } + return +} + +// Pop randomly pops an item from set. +func (set *TSet[T]) Pop() (item T) { + set.mu.Lock() + defer set.mu.Unlock() + for k := range set.data { + delete(set.data, k) + return k + } + return +} + +// Pops randomly pops `size` items from set. +// It returns all items if size == -1. +func (set *TSet[T]) Pops(size int) []T { + set.mu.Lock() + defer set.mu.Unlock() + if size > len(set.data) || size == -1 { + size = len(set.data) + } + if size <= 0 { + return nil + } + index := 0 + array := make([]T, size) + for k := range set.data { + delete(set.data, k) + array[index] = k + index++ + if index == size { + break + } + } + return array +} + +// Walk applies a user supplied function `f` to every item of set. +func (set *TSet[T]) Walk(f func(item T) T) *TSet[T] { + set.mu.Lock() + defer set.mu.Unlock() + m := make(map[T]struct{}, len(set.data)) + for k, v := range set.data { + m[f(k)] = v + } + set.data = m + return set +} + +// MarshalJSON implements the interface MarshalJSON for json.Marshal. +func (set TSet[T]) MarshalJSON() ([]byte, error) { + return json.Marshal(set.Slice()) +} + +// UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal. +func (set *TSet[T]) UnmarshalJSON(b []byte) error { + set.mu.Lock() + defer set.mu.Unlock() + if set.data == nil { + set.data = make(map[T]struct{}) + } + var array []T + if err := json.UnmarshalUseNumber(b, &array); err != nil { + return err + } + for _, v := range array { + set.data[v] = struct{}{} + } + return nil +} + +// UnmarshalValue is an interface implement which sets any type of value for set. +func (set *TSet[T]) UnmarshalValue(value any) (err error) { + set.mu.Lock() + defer set.mu.Unlock() + if set.data == nil { + set.data = make(map[T]struct{}) + } + var array []T + switch value.(type) { + case string, []byte: + err = json.UnmarshalUseNumber(gconv.Bytes(value), &array) + default: + if err = gconv.Scan(value, &array); err != nil { + return + } + } + for _, v := range array { + set.data[v] = struct{}{} + } + return +} + +// DeepCopy implements interface for deep copy of current type. +func (set *TSet[T]) DeepCopy() any { + if set == nil { + return nil + } + set.mu.RLock() + defer set.mu.RUnlock() + data := make([]T, 0) + for k := range set.data { + data = append(data, k) + } + return NewTSetFrom[T](data, set.mu.IsSafe()) +} diff --git a/container/gset/gset_z_unit_any_test.go b/container/gset/gset_z_unit_any_test.go index 39ab7fb8a2a..5f546064046 100644 --- a/container/gset/gset_z_unit_any_test.go +++ b/container/gset/gset_z_unit_any_test.go @@ -187,6 +187,19 @@ func TestSet_Union(t *testing.T) { t.Assert(s3.Contains(3), true) t.Assert(s3.Contains(4), true) }) + + // Test with nil element in slice + gtest.C(t, func(t *gtest.T) { + s1 := gset.NewSet() + s2 := gset.NewSet() + s1.Add(1, 2) + s2.Add(3, 4) + s3 := s1.Union(s2, nil) + t.Assert(s3.Contains(1), true) + t.Assert(s3.Contains(2), true) + t.Assert(s3.Contains(3), true) + t.Assert(s3.Contains(4), true) + }) } func TestSet_Diff(t *testing.T) { @@ -236,6 +249,14 @@ func TestSet_Complement(t *testing.T) { t.Assert(s3.Contains(4), true) t.Assert(s3.Contains(5), true) }) + + // Test with nil full set + gtest.C(t, func(t *gtest.T) { + s1 := gset.NewSet() + s1.Add(1, 2, 3) + s3 := s1.Complement(nil) + t.Assert(s3.Size(), 0) + }) } func TestNewFrom(t *testing.T) { diff --git a/container/gset/gset_z_unit_int_test.go b/container/gset/gset_z_unit_int_test.go index 9dcc4b3be7a..de315efa2ea 100644 --- a/container/gset/gset_z_unit_int_test.go +++ b/container/gset/gset_z_unit_int_test.go @@ -167,6 +167,19 @@ func TestIntSet_Union(t *testing.T) { t.Assert(s3.Contains(3), true) t.Assert(s3.Contains(4), true) }) + + // Test with nil element in slice + gtest.C(t, func(t *gtest.T) { + s1 := gset.NewIntSet() + s2 := gset.NewIntSet() + s1.Add(1, 2) + s2.Add(3, 4) + s3 := s1.Union(s2, nil) + t.Assert(s3.Contains(1), true) + t.Assert(s3.Contains(2), true) + t.Assert(s3.Contains(3), true) + t.Assert(s3.Contains(4), true) + }) } func TestIntSet_Diff(t *testing.T) { @@ -216,6 +229,14 @@ func TestIntSet_Complement(t *testing.T) { t.Assert(s3.Contains(4), true) t.Assert(s3.Contains(5), true) }) + + // Test with nil full set + gtest.C(t, func(t *gtest.T) { + s1 := gset.NewIntSet() + s1.Add(1, 2, 3) + s3 := s1.Complement(nil) + t.Assert(s3.Size(), 0) + }) } func TestIntSet_Size(t *testing.T) { diff --git a/container/gset/gset_z_unit_str_test.go b/container/gset/gset_z_unit_str_test.go index d5da71ab57c..3a7202d1773 100644 --- a/container/gset/gset_z_unit_str_test.go +++ b/container/gset/gset_z_unit_str_test.go @@ -178,6 +178,19 @@ func TestStrSet_Union(t *testing.T) { t.Assert(s3.Contains("3"), true) t.Assert(s3.Contains("4"), true) }) + + // Test with nil element in slice + gtest.C(t, func(t *gtest.T) { + s1 := gset.NewStrSet() + s2 := gset.NewStrSet() + s1.Add("1", "2") + s2.Add("3", "4") + s3 := s1.Union(s2, nil) + t.Assert(s3.Contains("1"), true) + t.Assert(s3.Contains("2"), true) + t.Assert(s3.Contains("3"), true) + t.Assert(s3.Contains("4"), true) + }) } func TestStrSet_Diff(t *testing.T) { @@ -227,6 +240,14 @@ func TestStrSet_Complement(t *testing.T) { t.Assert(s3.Contains("4"), true) t.Assert(s3.Contains("5"), true) }) + + // Test with nil full set + gtest.C(t, func(t *gtest.T) { + s1 := gset.NewStrSet() + s1.Add("1", "2", "3") + s3 := s1.Complement(nil) + t.Assert(s3.Size(), 0) + }) } func TestNewIntSetFrom(t *testing.T) { diff --git a/container/gset/gset_z_unit_t_set_test.go b/container/gset/gset_z_unit_t_set_test.go new file mode 100644 index 00000000000..4db85fb136d --- /dev/null +++ b/container/gset/gset_z_unit_t_set_test.go @@ -0,0 +1,593 @@ +// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +package gset_test + +import ( + "sync" + "testing" + "time" + + "github.com/gogf/gf/v2/container/gset" + "github.com/gogf/gf/v2/internal/json" + "github.com/gogf/gf/v2/test/gtest" +) + +func TestTSet_New(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + s := gset.NewTSet[int]() + s.Add(1, 1, 2) + s.Add([]int{3, 4}...) + t.Assert(s.Size(), 4) + t.AssertIN(1, s.Slice()) + t.AssertIN(2, s.Slice()) + t.AssertIN(3, s.Slice()) + t.AssertIN(4, s.Slice()) + t.AssertNI(0, s.Slice()) + t.Assert(s.Contains(4), true) + t.Assert(s.Contains(5), false) + s.Remove(1) + t.Assert(s.Size(), 3) + s.Clear() + t.Assert(s.Size(), 0) + }) +} + +func TestTSet_NewFrom(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + s := gset.NewTSetFrom[int]([]int{1, 2, 3}, true) + t.Assert(s.Size(), 3) + t.Assert(s.Contains(1), true) + t.Assert(s.Contains(2), true) + t.Assert(s.Contains(3), true) + t.Assert(s.Contains(4), false) + }) +} + +func TestTSet_Add_NilData(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + var s gset.TSet[int] + s.Add(1, 2, 3) + t.Assert(s.Size(), 3) + t.Assert(s.Contains(1), true) + }) +} + +func TestTSet_AddIfNotExist(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + s := gset.NewTSet[int](true) + s.Add(1) + t.Assert(s.Contains(1), true) + t.Assert(s.AddIfNotExist(1), false) + t.Assert(s.AddIfNotExist(2), true) + t.Assert(s.Contains(2), true) + t.Assert(s.AddIfNotExist(2), false) + }) + + // Test with pointer type to test nil check + gtest.C(t, func(t *gtest.T) { + s := gset.NewTSet[*int](true) + + val := 1 + ptr := &val + t.Assert(s.AddIfNotExist(ptr), true) + t.Assert(s.AddIfNotExist(ptr), false) + }) + + // Test nil data map initialization + gtest.C(t, func(t *gtest.T) { + var s gset.TSet[int] + t.Assert(s.AddIfNotExist(1), true) + t.Assert(s.Size(), 1) + }) +} + +func TestTSet_AddIfNotExistFunc(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + s := gset.NewTSet[int](true) + s.Add(1) + t.Assert(s.Contains(1), true) + t.Assert(s.Contains(2), false) + t.Assert(s.AddIfNotExistFunc(2, func() bool { return false }), false) + t.Assert(s.Contains(2), false) + t.Assert(s.AddIfNotExistFunc(2, func() bool { return true }), true) + t.Assert(s.Contains(2), true) + t.Assert(s.AddIfNotExistFunc(2, func() bool { return true }), false) + t.Assert(s.Contains(2), true) + }) + + // Test concurrent scenario + gtest.C(t, func(t *gtest.T) { + s := gset.NewTSet[int](true) + wg := sync.WaitGroup{} + wg.Add(1) + go func() { + defer wg.Done() + r := s.AddIfNotExistFunc(1, func() bool { + time.Sleep(100 * time.Millisecond) + return true + }) + t.Assert(r, false) + }() + s.Add(1) + wg.Wait() + }) + + // Test nil data map initialization + gtest.C(t, func(t *gtest.T) { + var s gset.TSet[int] + t.Assert(s.AddIfNotExistFunc(1, func() bool { return true }), true) + t.Assert(s.Size(), 1) + }) +} + +func TestTSet_AddIfNotExistFuncLock(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + s := gset.NewTSet[int](true) + s.Add(1) + t.Assert(s.Contains(1), true) + t.Assert(s.Contains(2), false) + t.Assert(s.AddIfNotExistFuncLock(2, func() bool { return false }), false) + t.Assert(s.Contains(2), false) + t.Assert(s.AddIfNotExistFuncLock(2, func() bool { return true }), true) + t.Assert(s.Contains(2), true) + t.Assert(s.AddIfNotExistFuncLock(2, func() bool { return true }), false) + t.Assert(s.Contains(2), true) + }) + + // Test nil data map initialization + gtest.C(t, func(t *gtest.T) { + var s gset.TSet[int] + t.Assert(s.AddIfNotExistFuncLock(1, func() bool { return true }), true) + t.Assert(s.Size(), 1) + }) +} + +func TestTSet_Iterator(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + s := gset.NewTSetFrom[int]([]int{1, 2, 3, 4, 5}, true) + var sum int + s.Iterator(func(v int) bool { + sum += v + return true + }) + t.Assert(sum, 15) + }) + + gtest.C(t, func(t *gtest.T) { + s := gset.NewTSetFrom[int]([]int{1, 2, 3, 4, 5}, true) + var count int + s.Iterator(func(v int) bool { + count++ + return count < 3 + }) + t.Assert(count, 3) + }) +} + +func TestTSet_Join(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + s := gset.NewTSet[int]() + t.Assert(s.Join(","), "") + s.Add(1, 2, 3) + result := s.Join(",") + t.Assert(len(result) > 0, true) + }) +} + +func TestTSet_String(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + var s *gset.TSet[int] + t.Assert(s.String(), "") + }) + + gtest.C(t, func(t *gtest.T) { + s := gset.NewTSet[int]() + t.Assert(s.String(), "[]") + }) + + gtest.C(t, func(t *gtest.T) { + s := gset.NewTSetFrom[int]([]int{1, 2, 3}) + result := s.String() + t.Assert(len(result) > 2, true) + }) + + gtest.C(t, func(t *gtest.T) { + s := gset.NewTSetFrom[string]([]string{"a", "b", "c"}) + result := s.String() + t.Assert(len(result) > 2, true) + }) +} + +func TestTSet_Equal(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + s1 := gset.NewTSetFrom[int]([]int{1, 2, 3}) + s2 := gset.NewTSetFrom[int]([]int{1, 2, 3}) + t.Assert(s1.Equal(s2), true) + }) + + gtest.C(t, func(t *gtest.T) { + s1 := gset.NewTSetFrom[int]([]int{1, 2, 3}) + s2 := gset.NewTSetFrom[int]([]int{1, 2, 3, 4}) + t.Assert(s1.Equal(s2), false) + }) + + gtest.C(t, func(t *gtest.T) { + s1 := gset.NewTSetFrom[int]([]int{1, 2, 3}) + t.Assert(s1.Equal(s1), true) + }) + + gtest.C(t, func(t *gtest.T) { + s1 := gset.NewTSetFrom[int]([]int{1, 2, 3}) + s2 := gset.NewTSetFrom[int]([]int{1, 2, 4}) + t.Assert(s1.Equal(s2), false) + }) +} + +func TestTSet_IsSubsetOf(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + s1 := gset.NewTSetFrom[int]([]int{1, 2}) + s2 := gset.NewTSetFrom[int]([]int{1, 2, 3}) + t.Assert(s1.IsSubsetOf(s2), true) + }) + + gtest.C(t, func(t *gtest.T) { + s1 := gset.NewTSetFrom[int]([]int{1, 2, 3}) + s2 := gset.NewTSetFrom[int]([]int{1, 2}) + t.Assert(s1.IsSubsetOf(s2), false) + }) + + gtest.C(t, func(t *gtest.T) { + s1 := gset.NewTSetFrom[int]([]int{1, 2, 3}) + t.Assert(s1.IsSubsetOf(s1), true) + }) +} + +func TestTSet_Union(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + s1 := gset.NewTSetFrom[int]([]int{1, 2, 3}) + s2 := gset.NewTSetFrom[int]([]int{3, 4, 5}) + s := s1.Union(s2) + t.Assert(s.Size(), 5) + t.Assert(s.Contains(1), true) + t.Assert(s.Contains(2), true) + t.Assert(s.Contains(3), true) + t.Assert(s.Contains(4), true) + t.Assert(s.Contains(5), true) + }) + + // Test with nil set - should skip it and copy s1 data + gtest.C(t, func(t *gtest.T) { + s1 := gset.NewTSetFrom[int]([]int{1, 2, 3}) + var s2 *gset.TSet[int] + s := s1.Union(s2) + // Since s2 is nil and skipped, newSet will be empty + // because the loop runs but nothing is copied when other is nil + t.Assert(s.Size(), 0) + }) + + // Test with self + gtest.C(t, func(t *gtest.T) { + s1 := gset.NewTSetFrom[int]([]int{1, 2, 3}) + s := s1.Union(s1) + t.Assert(s.Size(), 3) + }) +} + +func TestTSet_Diff(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + s1 := gset.NewTSetFrom[int]([]int{1, 2, 3}) + s2 := gset.NewTSetFrom[int]([]int{3, 4, 5}) + s := s1.Diff(s2) + t.Assert(s.Size(), 2) + t.Assert(s.Contains(1), true) + t.Assert(s.Contains(2), true) + t.Assert(s.Contains(3), false) + }) + + // Test with nil set - should skip it + gtest.C(t, func(t *gtest.T) { + s1 := gset.NewTSetFrom[int]([]int{1, 2, 3}) + var s2 *gset.TSet[int] + s := s1.Diff(s2) + // Since s2 is nil and skipped, newSet will be empty + // because the loop runs but nothing is copied when other is nil + t.Assert(s.Size(), 0) + }) + + // Test with self + gtest.C(t, func(t *gtest.T) { + s1 := gset.NewTSetFrom[int]([]int{1, 2, 3}) + s := s1.Diff(s1) + t.Assert(s.Size(), 0) + }) +} + +func TestTSet_Intersect(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + s1 := gset.NewTSetFrom[int]([]int{1, 2, 3}) + s2 := gset.NewTSetFrom[int]([]int{3, 4, 5}) + s := s1.Intersect(s2) + t.Assert(s.Size(), 1) + t.Assert(s.Contains(3), true) + }) + + // Test with nil set + gtest.C(t, func(t *gtest.T) { + s1 := gset.NewTSetFrom[int]([]int{1, 2, 3}) + var s2 *gset.TSet[int] + s := s1.Intersect(s2) + t.Assert(s.Size(), 0) + }) + + // Test with self + gtest.C(t, func(t *gtest.T) { + s1 := gset.NewTSetFrom[int]([]int{1, 2, 3}) + s := s1.Intersect(s1) + t.Assert(s.Size(), 3) + }) +} + +func TestTSet_Complement(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + s1 := gset.NewTSetFrom[int]([]int{1, 2, 3}) + s2 := gset.NewTSetFrom[int]([]int{1, 2, 3, 4, 5}) + s := s1.Complement(s2) + t.Assert(s.Size(), 2) + t.Assert(s.Contains(4), true) + t.Assert(s.Contains(5), true) + }) + + // Test with self + gtest.C(t, func(t *gtest.T) { + s1 := gset.NewTSetFrom[int]([]int{1, 2, 3}) + s := s1.Complement(s1) + t.Assert(s.Size(), 0) + }) +} + +func TestTSet_Merge(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + s1 := gset.NewTSetFrom[int]([]int{1, 2, 3}) + s2 := gset.NewTSetFrom[int]([]int{3, 4, 5}) + s1.Merge(s2) + t.Assert(s1.Size(), 5) + t.Assert(s1.Contains(1), true) + t.Assert(s1.Contains(2), true) + t.Assert(s1.Contains(3), true) + t.Assert(s1.Contains(4), true) + t.Assert(s1.Contains(5), true) + }) + + // Test with nil set + gtest.C(t, func(t *gtest.T) { + s1 := gset.NewTSetFrom[int]([]int{1, 2, 3}) + var s2 *gset.TSet[int] + s1.Merge(s2) + t.Assert(s1.Size(), 3) + }) + + // Test with self + gtest.C(t, func(t *gtest.T) { + s1 := gset.NewTSetFrom[int]([]int{1, 2, 3}) + s1.Merge(s1) + t.Assert(s1.Size(), 3) + }) +} + +func TestTSet_Sum(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + s := gset.NewTSetFrom[int]([]int{1, 2, 3}) + t.Assert(s.Sum(), 6) + }) +} + +func TestTSet_Pop(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + s := gset.NewTSetFrom[int]([]int{1, 2, 3}) + item := s.Pop() + t.Assert(s.Size(), 2) + t.Assert(s.Contains(item), false) + }) + + gtest.C(t, func(t *gtest.T) { + s := gset.NewTSet[int]() + item := s.Pop() + t.Assert(item, 0) + }) +} + +func TestTSet_Pops(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + s := gset.NewTSetFrom[int]([]int{1, 2, 3, 4, 5}) + items := s.Pops(3) + t.Assert(len(items), 3) + t.Assert(s.Size(), 2) + }) + + gtest.C(t, func(t *gtest.T) { + s := gset.NewTSetFrom[int]([]int{1, 2, 3}) + items := s.Pops(-1) + t.Assert(len(items), 3) + t.Assert(s.Size(), 0) + }) + + gtest.C(t, func(t *gtest.T) { + s := gset.NewTSetFrom[int]([]int{1, 2, 3}) + items := s.Pops(0) + t.Assert(items, nil) + t.Assert(s.Size(), 3) + }) + + gtest.C(t, func(t *gtest.T) { + s := gset.NewTSetFrom[int]([]int{1, 2, 3}) + items := s.Pops(10) + t.Assert(len(items), 3) + t.Assert(s.Size(), 0) + }) +} + +func TestTSet_Walk(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + s := gset.NewTSetFrom[int]([]int{1, 2}) + s.Walk(func(item int) int { + return item + 10 + }) + t.Assert(s.Size(), 2) + t.Assert(s.Contains(11), true) + t.Assert(s.Contains(12), true) + }) +} + +func TestTSet_MarshalJSON(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + s := gset.NewTSetFrom[int]([]int{1, 2, 3}) + b, err := json.Marshal(s) + t.AssertNil(err) + t.Assert(len(b) > 0, true) + }) +} + +func TestTSet_UnmarshalJSON(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + s := gset.NewTSet[int]() + b := []byte(`[1,2,3]`) + err := json.UnmarshalUseNumber(b, &s) + t.AssertNil(err) + t.Assert(s.Size(), 3) + t.Assert(s.Contains(1), true) + t.Assert(s.Contains(2), true) + t.Assert(s.Contains(3), true) + }) + + // Test with nil data map + gtest.C(t, func(t *gtest.T) { + var s gset.TSet[int] + b := []byte(`[1,2,3]`) + err := json.UnmarshalUseNumber(b, &s) + t.AssertNil(err) + t.Assert(s.Size(), 3) + }) + + // Test with invalid JSON + gtest.C(t, func(t *gtest.T) { + s := gset.NewTSet[int]() + b := []byte(`{invalid}`) + err := json.UnmarshalUseNumber(b, &s) + t.AssertNE(err, nil) + }) + + // Test with empty array + gtest.C(t, func(t *gtest.T) { + s := gset.NewTSet[int]() + b := []byte(`[]`) + err := json.UnmarshalUseNumber(b, &s) + t.AssertNil(err) + t.Assert(s.Size(), 0) + }) +} + +func TestTSet_UnmarshalValue(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + s := gset.NewTSet[int]() + err := s.UnmarshalValue([]byte(`[1,2,3]`)) + t.AssertNil(err) + t.Assert(s.Size(), 3) + t.Assert(s.Contains(1), true) + t.Assert(s.Contains(2), true) + t.Assert(s.Contains(3), true) + }) + + gtest.C(t, func(t *gtest.T) { + s := gset.NewTSet[int]() + err := s.UnmarshalValue(`[1,2,3]`) + t.AssertNil(err) + t.Assert(s.Size(), 3) + }) + + gtest.C(t, func(t *gtest.T) { + s := gset.NewTSet[int]() + err := s.UnmarshalValue([]int{1, 2, 3}) + t.AssertNil(err) + t.Assert(s.Size(), 3) + }) + + // Test with nil data map + gtest.C(t, func(t *gtest.T) { + var s gset.TSet[int] + err := s.UnmarshalValue([]int{1, 2, 3}) + t.AssertNil(err) + t.Assert(s.Size(), 3) + }) + + // Test error case with invalid JSON + gtest.C(t, func(t *gtest.T) { + s := gset.NewTSet[int]() + err := s.UnmarshalValue([]byte(`{invalid}`)) + t.AssertNE(err, nil) + }) + + // Test with empty array for string/bytes case + gtest.C(t, func(t *gtest.T) { + s := gset.NewTSet[int]() + err := s.UnmarshalValue([]byte(`[]`)) + t.AssertNil(err) + t.Assert(s.Size(), 0) + }) + + // Test with empty slice for default case + gtest.C(t, func(t *gtest.T) { + s := gset.NewTSet[int]() + err := s.UnmarshalValue([]int{}) + t.AssertNil(err) + t.Assert(s.Size(), 0) + }) +} + +func TestTSet_DeepCopy(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + s1 := gset.NewTSetFrom[int]([]int{1, 2, 3}, true) + s2 := s1.DeepCopy().(*gset.TSet[int]) + t.Assert(s1.Size(), s2.Size()) + t.Assert(s1.Contains(1), s2.Contains(1)) + t.Assert(s1.Contains(2), s2.Contains(2)) + t.Assert(s1.Contains(3), s2.Contains(3)) + + s1.Add(4) + t.Assert(s1.Size(), 4) + t.Assert(s2.Size(), 3) + }) + + gtest.C(t, func(t *gtest.T) { + var s1 *gset.TSet[int] + s2 := s1.DeepCopy() + t.Assert(s2, nil) + }) +} + +func TestTSet_LockFunc(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + s := gset.NewTSetFrom[int]([]int{1, 2, 3}, true) + s.LockFunc(func(m map[int]struct{}) { + m[4] = struct{}{} + }) + t.Assert(s.Size(), 4) + t.Assert(s.Contains(4), true) + }) +} + +func TestTSet_RLockFunc(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + s := gset.NewTSetFrom[int]([]int{1, 2, 3}, true) + var sum int + s.RLockFunc(func(m map[int]struct{}) { + for k := range m { + sum += k + } + }) + t.Assert(sum, 6) + }) +}