-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset.go
47 lines (37 loc) · 848 Bytes
/
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
package container
import (
"maps"
)
func NewSet[T comparable](data ...T) *Set[T] {
set := Set[T]{data: make(map[T]struct{}, len(data))}
set.Add(data...)
return &set
}
type Set[T comparable] struct {
data map[T]struct{}
}
func (s *Set[T]) Add(values ...T) {
for _, v := range values {
s.data[v] = struct{}{}
}
}
func (s *Set[T]) Remove(values ...T) {
for _, v := range values {
delete(s.data, v)
}
}
func (s *Set[T]) Contain(x T) bool { _, ok := s.data[x]; return ok }
func (s *Set[T]) Clear() { clear(s.data) }
func (s Set[T]) Size() int { return len(s.data) }
func (s Set[T]) Empty() bool { return s.Size() == 0 }
func (s Set[T]) Values() (values []T) {
for v := range s.data {
values = append(values, v)
}
return values
}
func (s Set[T]) Clone() *Set[T] {
return &Set[T]{
data: maps.Clone(s.data),
}
}