From 79a1aa1946c3930361f401b8a2f4bea02b6bea13 Mon Sep 17 00:00:00 2001 From: ssannkkallpp Date: Thu, 13 Nov 2025 17:41:47 -0500 Subject: [PATCH] Added MatchesFunc() set method --- set.go | 4 ++++ threadsafe.go | 8 ++++++++ threadunsafe.go | 9 +++++++++ 3 files changed, 21 insertions(+) diff --git a/set.go b/set.go index 92b67bf..856de6d 100644 --- a/set.go +++ b/set.go @@ -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) diff --git a/threadsafe.go b/threadsafe.go index 835797e..75403fe 100644 --- a/threadsafe.go +++ b/threadsafe.go @@ -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...) diff --git a/threadunsafe.go b/threadunsafe.go index 2ed0e06..4e76674 100644 --- a/threadunsafe.go +++ b/threadunsafe.go @@ -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 {