Skip to content

Commit

Permalink
Add ContainsAny functionality to Set (deckarep#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryclarke authored Jun 29, 2023
1 parent 054f236 commit 6ea1fbe
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 0 deletions.
4 changes: 4 additions & 0 deletions set.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ type Set[T comparable] interface {
// are all in the set.
Contains(val ...T) bool

// ContainsAny returns whether at least one of the
// given items are in the set.
ContainsAny(val ...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
32 changes: 32 additions & 0 deletions set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,38 @@ func Test_ContainsMultipleUnsafeSet(t *testing.T) {
}
}

func Test_ContainsAnySet(t *testing.T) {
a := NewSet[int]()

a.Add(71)

if !a.ContainsAny(71) {
t.Error("ContainsSet should contain 71")
}

if !a.ContainsAny(71, 10) {
t.Error("ContainsSet should contain 71 or 10")
}

a.Remove(71)

if a.ContainsAny(71) {
t.Error("ContainsSet should not contain 71")
}

if a.ContainsAny(71, 10) {
t.Error("ContainsSet should not contain 71 or 10")
}

a.Add(13)
a.Add(7)
a.Add(1)

if !(a.ContainsAny(13, 17, 10)) {
t.Error("ContainsSet should contain 13, 17, or 10")
}
}

func Test_ClearSet(t *testing.T) {
a := makeSetInt([]int{2, 5, 9, 10})

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

func (t *threadSafeSet[T]) ContainsAny(v ...T) bool {
t.RLock()
ret := t.uss.ContainsAny(v...)
t.RUnlock()

return ret
}

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

Expand Down
24 changes: 24 additions & 0 deletions threadsafe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,30 @@ func Test_ContainsConcurrent(t *testing.T) {
wg.Wait()
}

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

s := NewSet[int]()
ints := rand.Perm(N)
integers := make([]int, 0)
for _, v := range ints {
if v%N == 0 {
s.Add(v)
}
integers = append(integers, v)
}

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

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

Expand Down
9 changes: 9 additions & 0 deletions threadunsafe.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@ func (s threadUnsafeSet[T]) Contains(v ...T) bool {
return true
}

func (s threadUnsafeSet[T]) ContainsAny(v ...T) bool {
for _, val := range v {
if _, ok := s[val]; ok {
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 6ea1fbe

Please sign in to comment.