Skip to content

Commit

Permalink
feat: add ContainsAnyElement method (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
Y7n05h authored Dec 9, 2024
1 parent 5227976 commit e1dc81f
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 0 deletions.
4 changes: 4 additions & 0 deletions set.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ type Set[T comparable] interface {
// given items are in the set.
ContainsAny(val ...T) bool

// ContainsAnyElement returns whether at least one of the
// given element are in the set.
ContainsAnyElement(other Set[T]) bool

// Difference returns the difference between this set
// and other. The returned set will contain
// all elements of this set that are not also
Expand Down
27 changes: 27 additions & 0 deletions set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,33 @@ func Test_ContainsAnySet(t *testing.T) {
}
}

func Test_ContainsAnyElement(t *testing.T) {
a := NewSet[int]()
a.Add(1)
a.Add(3)
a.Add(5)

b := NewSet[int]()
a.Add(2)
a.Add(4)
a.Add(6)

if ret := a.ContainsAnyElement(b); ret {
t.Errorf("set a not contain any element in set b")
}

a.Add(10)

if ret := a.ContainsAnyElement(b); ret {
t.Errorf("set a not contain any element in set b")
}

b.Add(10)

if ret := a.ContainsAnyElement(b); !ret {
t.Errorf("set a contain 10")
}
}
func Test_ClearSet(t *testing.T) {
a := makeSetInt([]int{2, 5, 9, 10})

Expand Down
13 changes: 13 additions & 0 deletions threadsafe.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,19 @@ func (t *threadSafeSet[T]) ContainsAny(v ...T) bool {
return ret
}

func (t *threadSafeSet[T]) ContainsAnyElement(other Set[T]) bool {
o := other.(*threadSafeSet[T])

t.RLock()
o.RLock()

ret := t.uss.ContainsAnyElement(o.uss)

t.RUnlock()
o.RUnlock()
return ret
}

func (t *threadSafeSet[T]) IsEmpty() bool {
return t.Cardinality() == 0
}
Expand Down
21 changes: 21 additions & 0 deletions threadsafe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,27 @@ func Test_ContainsAnyConcurrent(t *testing.T) {
wg.Wait()
}

func Test_ContainsAnyElementConcurrent(t *testing.T) {
runtime.GOMAXPROCS(2)

s, ss := NewSet[int](), NewSet[int]()
ints := rand.Perm(N)
for _, v := range ints {
s.Add(v)
ss.Add(v)
}

var wg sync.WaitGroup
for range ints {
wg.Add(1)
go func() {
s.ContainsAnyElement(ss)
wg.Done()
}()
}
wg.Wait()
}

func Test_DifferenceConcurrent(t *testing.T) {
runtime.GOMAXPROCS(2)

Expand Down
20 changes: 20 additions & 0 deletions threadunsafe.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,26 @@ func (s *threadUnsafeSet[T]) ContainsAny(v ...T) bool {
return false
}

func (s *threadUnsafeSet[T]) ContainsAnyElement(other Set[T]) bool {
o := other.(*threadUnsafeSet[T])

// loop over smaller set
if s.Cardinality() < other.Cardinality() {
for elem := range *s {
if o.contains(elem) {
return true
}
}
} else {
for elem := range *o {
if s.contains(elem) {
return true
}
}
}
return false
}

// private version of Contains for a single element v
func (s *threadUnsafeSet[T]) contains(v T) (ok bool) {
_, ok = (*s)[v]
Expand Down

0 comments on commit e1dc81f

Please sign in to comment.