Skip to content

Commit 79a1aa1

Browse files
committed
Added MatchesFunc() set method
1 parent 53cf14d commit 79a1aa1

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

set.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,10 @@ type Set[T comparable] interface {
161161
// Iterator returns an Iterator object that you can
162162
// use to range over the set.
163163
Iterator() *Iterator[T]
164+
165+
// MatchesFunc() returns whether at least one element in the set
166+
// matches the provided predicate function.
167+
MatchesFunc(func(T) bool) bool
164168

165169
// Remove removes a single element from the set.
166170
Remove(i T)

threadsafe.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ func (t *threadSafeSet[T]) Append(v ...T) int {
6262
return ret
6363
}
6464

65+
func (t *threadSafeSet[T]) MatchesFunc(predicate func(T) bool) bool {
66+
t.RLock()
67+
ret := t.uss.MatchesFunc(predicate)
68+
t.RUnlock()
69+
70+
return ret
71+
}
72+
6573
func (t *threadSafeSet[T]) Contains(v ...T) bool {
6674
t.RLock()
6775
ret := t.uss.Contains(v...)

threadunsafe.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,15 @@ func (s *threadUnsafeSet[T]) Clone() Set[T] {
8989
return clonedSet
9090
}
9191

92+
func (s *threadUnsafeSet[T]) MatchesFunc(predicate func(T) bool) bool {
93+
for elem := range *s {
94+
if predicate(elem) {
95+
return true
96+
}
97+
}
98+
return false
99+
}
100+
92101
func (s *threadUnsafeSet[T]) Contains(v ...T) bool {
93102
for _, val := range v {
94103
if _, ok := (*s)[val]; !ok {

0 commit comments

Comments
 (0)