Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions set.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ type Set[T comparable] interface {
// Iterator returns an Iterator object that you can
// use to range over the set.
Iterator() *Iterator[T]

// MatchesFunc() returns whether at least one element in the set
// matches the provided predicate function.
MatchesFunc(func(T) bool) bool

// Remove removes a single element from the set.
Remove(i T)
Expand Down
8 changes: 8 additions & 0 deletions threadsafe.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ func (t *threadSafeSet[T]) Append(v ...T) int {
return ret
}

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

return ret
}

func (t *threadSafeSet[T]) Contains(v ...T) bool {
t.RLock()
ret := t.uss.Contains(v...)
Expand Down
9 changes: 9 additions & 0 deletions threadunsafe.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ func (s *threadUnsafeSet[T]) Clone() Set[T] {
return clonedSet
}

func (s *threadUnsafeSet[T]) MatchesFunc(predicate func(T) bool) bool {
for elem := range *s {
if predicate(elem) {
return true
}
}
return false
}

func (s *threadUnsafeSet[T]) Contains(v ...T) bool {
for _, val := range v {
if _, ok := (*s)[val]; !ok {
Expand Down