homebox/backend/pkgs/set/set.go
Hayden e8449b3a73
fix: inaccruate 401 & sql busy error (#679)
* fix inaccruate 401 error on SQL db error

* init golangci-lint config

* linter autofix

* testify auto fixes

* fix sqlite busy errors

* fix naming

* more linter errors

* fix rest of linter issues
2024-01-04 08:55:26 -09:00

63 lines
901 B
Go

// Package set provides a simple set implementation.
package set
type key interface {
comparable
}
type Set[T key] struct {
mp map[T]struct{}
}
func Make[T key](size int) Set[T] {
return Set[T]{
mp: make(map[T]struct{}, size),
}
}
func New[T key](v ...T) Set[T] {
mp := make(map[T]struct{}, len(v))
s := Set[T]{mp}
s.Insert(v...)
return s
}
func (s Set[T]) Insert(v ...T) {
for _, e := range v {
s.mp[e] = struct{}{}
}
}
func (s Set[T]) Remove(v ...T) {
for _, e := range v {
delete(s.mp, e)
}
}
func (s Set[T]) Contains(v T) bool {
_, ok := s.mp[v]
return ok
}
func (s Set[T]) ContainsAll(v ...T) bool {
for _, e := range v {
if !s.Contains(e) {
return false
}
}
return true
}
func (s Set[T]) Slice() []T {
slice := make([]T, 0, len(s.mp))
for k := range s.mp {
slice = append(slice, k)
}
return slice
}
func (s Set[T]) Len() int {
return len(s.mp)
}