feat: create a hashset subclass with a rwlock

This commit is contained in:
Derrick Hammer 2024-03-14 06:51:44 -04:00
parent 83f71df029
commit 804f124632
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 39 additions and 0 deletions

39
structs/set.go Normal file
View File

@ -0,0 +1,39 @@
package structs
import (
"github.com/emirpasic/gods/sets"
"github.com/emirpasic/gods/sets/hashset"
"sync"
)
var _ sets.Set = (*SetImpl)(nil)
type SetImpl struct {
*hashset.Set
mutex *sync.RWMutex
}
func NewSet() *SetImpl {
return &SetImpl{
Set: hashset.New(),
mutex: &sync.RWMutex{},
}
}
func (s *SetImpl) Add(items ...interface{}) {
s.mutex.Lock()
defer s.mutex.Unlock()
s.Set.Add(items...)
}
func (s *SetImpl) Remove(items ...interface{}) {
s.mutex.Lock()
defer s.mutex.Unlock()
s.Set.Remove(items...)
}
func (s *SetImpl) Contains(items ...interface{}) bool {
s.mutex.RLock()
defer s.mutex.RUnlock()
return s.Set.Contains(items...)
}