1
0
Fork 1
mirror of https://github.com/hay-kot/homebox.git synced 2024-12-23 07:16:30 +00:00
homebox/backend/pkgs/set/set.go
Hayden de419dc37d
feat: auth-roles, image-gallery, click-to-open ()
* schema changes

* db generate

* db migration

* add role based middleware

* implement attachment token access

* generate docs

* implement role based auth

* replace attachment specific tokens with gen token

* run linter

* cleanup temporary token implementation
2022-12-03 10:55:00 -09:00

62 lines
848 B
Go

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)
}