From 804f124632375d24f0f7a7505eb90e8fcf0b844c Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Thu, 14 Mar 2024 06:51:44 -0400 Subject: [PATCH] feat: create a hashset subclass with a rwlock --- structs/set.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 structs/set.go diff --git a/structs/set.go b/structs/set.go new file mode 100644 index 0000000..9121cce --- /dev/null +++ b/structs/set.go @@ -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...) +}