-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathset.go
More file actions
46 lines (37 loc) · 676 Bytes
/
set.go
File metadata and controls
46 lines (37 loc) · 676 Bytes
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
package set
type Set[T comparable] map[T]struct{}
func New[T comparable]() Set[T] {
return make(Set[T])
}
func NewWith[T comparable](value ...T) Set[T] {
set := make(Set[T], len(value))
for _, s := range value {
set.Add(s)
}
return set
}
func (s Set[T]) Add(k T) {
s[k] = struct{}{}
}
func (s Set[T]) Remove(k T) {
delete(s, k)
}
func (s Set[T]) Len() int {
return len(s)
}
func (s Set[T]) Has(k T) bool {
_, exists := s[k]
return exists
}
func (s Set[T]) Foreach(fun func(T)) {
for value := range s {
fun(value)
}
}
func (s Set[T]) ToSlice() []T {
list := make([]T, 0, s.Len())
for value := range s {
list = append(list, value)
}
return list
}