v0.0.543
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 2m7s
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 2m7s
This commit is contained in:
@@ -7,8 +7,12 @@ type SyncSet[TData comparable] struct {
|
||||
lock sync.Mutex
|
||||
}
|
||||
|
||||
func NewSyncSet[TData comparable]() *SyncSet[TData] {
|
||||
return &SyncSet[TData]{data: make(map[TData]bool), lock: sync.Mutex{}}
|
||||
}
|
||||
|
||||
// Add adds `value` to the set
|
||||
// returns true if the value was actually inserted
|
||||
// returns true if the value was actually inserted (value did not exist beforehand)
|
||||
// returns false if the value already existed
|
||||
func (s *SyncSet[TData]) Add(value TData) bool {
|
||||
s.lock.Lock()
|
||||
@@ -19,9 +23,12 @@ func (s *SyncSet[TData]) Add(value TData) bool {
|
||||
}
|
||||
|
||||
_, existsInPreState := s.data[value]
|
||||
s.data[value] = true
|
||||
if existsInPreState {
|
||||
return false
|
||||
}
|
||||
|
||||
return !existsInPreState
|
||||
s.data[value] = true
|
||||
return true
|
||||
}
|
||||
|
||||
func (s *SyncSet[TData]) AddAll(values []TData) {
|
||||
@@ -37,6 +44,36 @@ func (s *SyncSet[TData]) AddAll(values []TData) {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *SyncSet[TData]) Remove(value TData) bool {
|
||||
s.lock.Lock()
|
||||
defer s.lock.Unlock()
|
||||
|
||||
if s.data == nil {
|
||||
s.data = make(map[TData]bool)
|
||||
}
|
||||
|
||||
_, existsInPreState := s.data[value]
|
||||
if !existsInPreState {
|
||||
return false
|
||||
}
|
||||
|
||||
delete(s.data, value)
|
||||
return true
|
||||
}
|
||||
|
||||
func (s *SyncSet[TData]) RemoveAll(values []TData) {
|
||||
s.lock.Lock()
|
||||
defer s.lock.Unlock()
|
||||
|
||||
if s.data == nil {
|
||||
s.data = make(map[TData]bool)
|
||||
}
|
||||
|
||||
for _, value := range values {
|
||||
delete(s.data, value)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *SyncSet[TData]) Contains(value TData) bool {
|
||||
s.lock.Lock()
|
||||
defer s.lock.Unlock()
|
||||
@@ -66,3 +103,17 @@ func (s *SyncSet[TData]) Get() []TData {
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
// AddIfNotContains
|
||||
// returns true if the value was actually added (value did not exist beforehand)
|
||||
// returns false if the value already existed
|
||||
func (s *SyncSet[TData]) AddIfNotContains(key TData) bool {
|
||||
return s.Add(key)
|
||||
}
|
||||
|
||||
// RemoveIfContains
|
||||
// returns true if the value was actually removed (value did exist beforehand)
|
||||
// returns false if the value did not exist in the set
|
||||
func (s *SyncSet[TData]) RemoveIfContains(key TData) bool {
|
||||
return s.Remove(key)
|
||||
}
|
||||
|
Reference in New Issue
Block a user