Files
proton-bridge/internal/safe/set.go
2022-11-16 12:26:09 +01:00

47 lines
722 B
Go

package safe
import "golang.org/x/exp/maps"
type Set[Val comparable] Map[Val, struct{}]
func NewSet[Val comparable](vals ...Val) *Set[Val] {
set := (*Set[Val])(NewMap[Val, struct{}](nil))
for _, val := range vals {
set.Insert(val)
}
return set
}
func (m *Set[Val]) Has(key Val) bool {
m.lock.RLock()
defer m.lock.RUnlock()
_, ok := m.data[key]
return ok
}
func (m *Set[Val]) Insert(key Val) {
m.lock.Lock()
defer m.lock.Unlock()
m.data[key] = struct{}{}
}
func (m *Set[Val]) Iter(fn func(key Val)) {
m.lock.RLock()
defer m.lock.RUnlock()
for key := range m.data {
fn(key)
}
}
func (m *Set[Val]) Values(fn func(vals []Val)) {
m.lock.RLock()
defer m.lock.RUnlock()
fn(maps.Keys(m.data))
}