This commit is contained in:
Mike Schwörer 2022-12-22 10:23:34 +01:00
parent 1aaad66233
commit ac05eff1e8
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF

View File

@ -2,17 +2,17 @@ package dataext
import "sync"
type SyncStringSet struct {
data map[string]bool
type SyncSet[TData comparable] struct {
data map[TData]bool
lock sync.Mutex
}
func (s *SyncStringSet) Add(value string) bool {
func (s *SyncSet[TData]) Add(value TData) bool {
s.lock.Lock()
defer s.lock.Unlock()
if s.data == nil {
s.data = make(map[string]bool)
s.data = make(map[TData]bool)
}
_, ok := s.data[value]
@ -21,12 +21,12 @@ func (s *SyncStringSet) Add(value string) bool {
return !ok
}
func (s *SyncStringSet) AddAll(values []string) {
func (s *SyncSet[TData]) AddAll(values []TData) {
s.lock.Lock()
defer s.lock.Unlock()
if s.data == nil {
s.data = make(map[string]bool)
s.data = make(map[TData]bool)
}
for _, value := range values {
@ -34,12 +34,12 @@ func (s *SyncStringSet) AddAll(values []string) {
}
}
func (s *SyncStringSet) Contains(value string) bool {
func (s *SyncSet[TData]) Contains(value TData) bool {
s.lock.Lock()
defer s.lock.Unlock()
if s.data == nil {
s.data = make(map[string]bool)
s.data = make(map[TData]bool)
}
_, ok := s.data[value]
@ -47,15 +47,15 @@ func (s *SyncStringSet) Contains(value string) bool {
return ok
}
func (s *SyncStringSet) Get() []string {
func (s *SyncSet[TData]) Get() []TData {
s.lock.Lock()
defer s.lock.Unlock()
if s.data == nil {
s.data = make(map[string]bool)
s.data = make(map[TData]bool)
}
r := make([]string, 0, len(s.data))
r := make([]TData, 0, len(s.data))
for k := range s.data {
r = append(r, k)