Other: Better cookies test

This commit is contained in:
James Houlahan
2022-10-11 18:18:08 +02:00
parent 245f2afeac
commit 0de30afba1
2 changed files with 60 additions and 12 deletions

46
internal/safe/set.go Normal file
View File

@ -0,0 +1,46 @@
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))
}