-
Notifications
You must be signed in to change notification settings - Fork 130
/
set.go
46 lines (39 loc) · 875 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
package set
// Set implements a Set, using the go map as the underlying storage.
type Set[K comparable] struct {
storage map[K]struct{}
}
// New returns an empty Set.
func New[K comparable]() Set[K] {
return Set[K]{
storage: make(map[K]struct{}),
}
}
// Of returns a new Set initialized with the given 'vals'
func Of[K comparable](vals ...K) Set[K] {
s := New[K]()
for _, val := range vals {
s.Put(val)
}
return s
}
// Put adds 'val' to the set.
func (s Set[K]) Put(val K) {
s.storage[val] = struct{}{}
}
// Has returns true only if 'val' is in the set.
func (s Set[K]) Has(val K) bool {
_, ok := s.storage[val]
return ok
}
// Remove removes 'val' from the set.
func (s Set[K]) Remove(val K) {
delete(s.storage, val)
}
// ToArray returns go slice
func (s Set[K]) ToArray() (arr []K) {
for v := range s.storage {
arr = append(arr, v)
}
return arr
}