-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathset.go
44 lines (37 loc) · 800 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
package collection
// Set is a map[T]struct{} wrapper with by-value access
type Set[T comparable] struct {
m map[T]struct{}
}
func NewSet[T comparable]() Set[T] {
return Set[T]{
m: make(map[T]struct{}),
}
}
// Add adds an element to the set
func (s *Set[T]) Add(key T) {
s.m[key] = struct{}{}
}
// Contains returns an element is in the set
func (s *Set[T]) Contains(key T) bool {
_, ok := s.m[key]
return ok
}
// Remove removes an element from the set
func (s *Set[T]) Remove(key T) {
delete(s.m, key)
}
// ForEach applies a function to each key
func (s *Set[T]) ForEach(fn func(key T)) {
for key := range s.m {
fn(key)
}
}
// Keys returns all keys
func (s *Set[T]) Keys() []T {
keys := make([]T, 0, len(s.m))
for key := range s.m {
keys = append(keys, key)
}
return keys
}