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 {
9797 // panic.
9898 Intersect (other Set [T ]) Set [T ]
9999
100+ // IsEmpty determines if there are elements in the set.
101+ IsEmpty () bool
102+
100103 // IsProperSubset determines if every element in this set is in
101104 // the other set but the two sets are not equal.
102105 //
Original file line number Diff line number Diff line change @@ -74,6 +74,10 @@ func (t *threadSafeSet[T]) ContainsAny(v ...T) bool {
7474 return ret
7575}
7676
77+ func (t * threadSafeSet [T ]) IsEmpty () bool {
78+ return t .Cardinality () == 0
79+ }
80+
7781func (t * threadSafeSet [T ]) IsSubset (other Set [T ]) bool {
7882 o := other .(* threadSafeSet [T ])
7983
Original file line number Diff line number Diff line change @@ -259,6 +259,29 @@ func Test_IntersectConcurrent(t *testing.T) {
259259 wg .Wait ()
260260}
261261
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+
262285func Test_IsSubsetConcurrent (t * testing.T ) {
263286 runtime .GOMAXPROCS (2 )
264287
Original file line number Diff line number Diff line change @@ -163,6 +163,10 @@ func (s threadUnsafeSet[T]) Intersect(other Set[T]) Set[T] {
163163 return intersection
164164}
165165
166+ func (s threadUnsafeSet [T ]) IsEmpty () bool {
167+ return s .Cardinality () == 0
168+ }
169+
166170func (s threadUnsafeSet [T ]) IsProperSubset (other Set [T ]) bool {
167171 return s .Cardinality () < other .Cardinality () && s .IsSubset (other )
168172}
You can’t perform that action at this time.
0 commit comments