-
Notifications
You must be signed in to change notification settings - Fork 241
/
set.go
66 lines (62 loc) · 1.3 KB
/
set.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package set
import (
"github.com/samber/lo"
"github.com/superfly/flyctl/helpers"
)
type Set[T comparable] struct {
values map[T]struct{}
}
// We can't use interfaces because we have generics, so we'll just lazy init on setters.
// Alternatively, we could just require that the user use a constructor, but that doesn't
// *guarantee* that we can't get a nil map.
func (s *Set[T]) ensureNotNull() {
if s.values == nil {
s.values = map[T]struct{}{}
}
}
func (s *Set[T]) Set(values ...T) {
s.ensureNotNull()
for _, value := range values {
s.values[value] = struct{}{}
}
}
func (s *Set[T]) Unset(values ...T) {
s.ensureNotNull()
for _, value := range values {
delete(s.values, value)
}
}
func (s *Set[T]) Has(value T) bool {
_, ok := s.values[value]
return ok
}
func (s *Set[T]) HasAll(values ...T) bool {
for _, value := range values {
if !s.Has(value) {
return false
}
}
return true
}
func (s *Set[T]) HasAny(values ...T) bool {
for _, value := range values {
if s.Has(value) {
return true
}
}
return false
}
func (s *Set[T]) Values() []T {
s.ensureNotNull()
return lo.Keys(s.values)
}
func (s *Set[T]) Len() int {
s.ensureNotNull()
return len(s.values)
}
func (s *Set[T]) Clear() {
s.Unset(s.Values()...)
}
func (s *Set[T]) Copy() Set[T] {
return Set[T]{helpers.Clone(s.values)}
}