Skip to content

Commit 6ea1fbe

Browse files
author
Ryan Clarke
authored
Add ContainsAny functionality to Set (#120)
1 parent 054f236 commit 6ea1fbe

File tree

5 files changed

+77
-0
lines changed

5 files changed

+77
-0
lines changed

set.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ type Set[T comparable] interface {
6262
// are all in the set.
6363
Contains(val ...T) bool
6464

65+
// ContainsAny returns whether at least one of the
66+
// given items are in the set.
67+
ContainsAny(val ...T) bool
68+
6569
// Difference returns the difference between this set
6670
// and other. The returned set will contain
6771
// all elements of this set that are not also

set_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,38 @@ func Test_ContainsMultipleUnsafeSet(t *testing.T) {
318318
}
319319
}
320320

321+
func Test_ContainsAnySet(t *testing.T) {
322+
a := NewSet[int]()
323+
324+
a.Add(71)
325+
326+
if !a.ContainsAny(71) {
327+
t.Error("ContainsSet should contain 71")
328+
}
329+
330+
if !a.ContainsAny(71, 10) {
331+
t.Error("ContainsSet should contain 71 or 10")
332+
}
333+
334+
a.Remove(71)
335+
336+
if a.ContainsAny(71) {
337+
t.Error("ContainsSet should not contain 71")
338+
}
339+
340+
if a.ContainsAny(71, 10) {
341+
t.Error("ContainsSet should not contain 71 or 10")
342+
}
343+
344+
a.Add(13)
345+
a.Add(7)
346+
a.Add(1)
347+
348+
if !(a.ContainsAny(13, 17, 10)) {
349+
t.Error("ContainsSet should contain 13, 17, or 10")
350+
}
351+
}
352+
321353
func Test_ClearSet(t *testing.T) {
322354
a := makeSetInt([]int{2, 5, 9, 10})
323355

threadsafe.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ func (t *threadSafeSet[T]) Contains(v ...T) bool {
6666
return ret
6767
}
6868

69+
func (t *threadSafeSet[T]) ContainsAny(v ...T) bool {
70+
t.RLock()
71+
ret := t.uss.ContainsAny(v...)
72+
t.RUnlock()
73+
74+
return ret
75+
}
76+
6977
func (t *threadSafeSet[T]) IsSubset(other Set[T]) bool {
7078
o := other.(*threadSafeSet[T])
7179

threadsafe_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,30 @@ func Test_ContainsConcurrent(t *testing.T) {
172172
wg.Wait()
173173
}
174174

175+
func Test_ContainsAnyConcurrent(t *testing.T) {
176+
runtime.GOMAXPROCS(2)
177+
178+
s := NewSet[int]()
179+
ints := rand.Perm(N)
180+
integers := make([]int, 0)
181+
for _, v := range ints {
182+
if v%N == 0 {
183+
s.Add(v)
184+
}
185+
integers = append(integers, v)
186+
}
187+
188+
var wg sync.WaitGroup
189+
for range ints {
190+
wg.Add(1)
191+
go func() {
192+
s.ContainsAny(integers...)
193+
wg.Done()
194+
}()
195+
}
196+
wg.Wait()
197+
}
198+
175199
func Test_DifferenceConcurrent(t *testing.T) {
176200
runtime.GOMAXPROCS(2)
177201

threadunsafe.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,15 @@ func (s threadUnsafeSet[T]) Contains(v ...T) bool {
9494
return true
9595
}
9696

97+
func (s threadUnsafeSet[T]) ContainsAny(v ...T) bool {
98+
for _, val := range v {
99+
if _, ok := s[val]; ok {
100+
return true
101+
}
102+
}
103+
return false
104+
}
105+
97106
// private version of Contains for a single element v
98107
func (s threadUnsafeSet[T]) contains(v T) (ok bool) {
99108
_, ok = s[v]

0 commit comments

Comments
 (0)