File tree Expand file tree Collapse file tree 4 files changed +34
-0
lines changed Expand file tree Collapse file tree 4 files changed +34
-0
lines changed Original file line number Diff line number Diff line change @@ -97,6 +97,9 @@ type Set[T comparable] interface {
97
97
// panic.
98
98
Intersect (other Set [T ]) Set [T ]
99
99
100
+ // IsEmpty determines if there are elements in the set.
101
+ IsEmpty () bool
102
+
100
103
// IsProperSubset determines if every element in this set is in
101
104
// the other set but the two sets are not equal.
102
105
//
Original file line number Diff line number Diff line change @@ -74,6 +74,10 @@ func (t *threadSafeSet[T]) ContainsAny(v ...T) bool {
74
74
return ret
75
75
}
76
76
77
+ func (t * threadSafeSet [T ]) IsEmpty () bool {
78
+ return t .Cardinality () == 0
79
+ }
80
+
77
81
func (t * threadSafeSet [T ]) IsSubset (other Set [T ]) bool {
78
82
o := other .(* threadSafeSet [T ])
79
83
Original file line number Diff line number Diff line change @@ -259,6 +259,29 @@ func Test_IntersectConcurrent(t *testing.T) {
259
259
wg .Wait ()
260
260
}
261
261
262
+ func Test_IsEmptyConcurrent (t * testing.T ) {
263
+ runtime .GOMAXPROCS (2 )
264
+
265
+ s := NewSet [int ]()
266
+
267
+ var wg sync.WaitGroup
268
+ wg .Add (1 )
269
+ go func () {
270
+ for i := 0 ; i < N ; i ++ {
271
+ size := s .Cardinality ()
272
+ if s .IsEmpty () && size > 0 {
273
+ t .Errorf ("Is Empty should be return false" )
274
+ }
275
+ }
276
+ wg .Done ()
277
+ }()
278
+
279
+ for i := 0 ; i < N ; i ++ {
280
+ s .Add (rand .Int ())
281
+ }
282
+ wg .Wait ()
283
+ }
284
+
262
285
func Test_IsSubsetConcurrent (t * testing.T ) {
263
286
runtime .GOMAXPROCS (2 )
264
287
Original file line number Diff line number Diff line change @@ -163,6 +163,10 @@ func (s threadUnsafeSet[T]) Intersect(other Set[T]) Set[T] {
163
163
return intersection
164
164
}
165
165
166
+ func (s threadUnsafeSet [T ]) IsEmpty () bool {
167
+ return s .Cardinality () == 0
168
+ }
169
+
166
170
func (s threadUnsafeSet [T ]) IsProperSubset (other Set [T ]) bool {
167
171
return s .Cardinality () < other .Cardinality () && s .IsSubset (other )
168
172
}
You can’t perform that action at this time.
0 commit comments