...
1// Package set implements sets of any type.
2package set
3
4type Set[Elem comparable] map[Elem]struct{}
5
6func Make[Elem comparable]() Set[Elem] {
7 return make(Set(Elem))
8}
9
10func (s Set[Elem]) Add(v Elem) {
11 s[v] = struct{}{}
12}
13
14func (s Set[Elem]) Delete(v Elem) {
15 delete(s, v)
16}
17
18func (s Set[Elem]) Contains(v Elem) bool {
19 _, ok := s[v]
20 return ok
21}
22
23func (s Set[Elem]) Len() int {
24 return len(s)
25}
26
27func (s Set[Elem]) Iterate(f func(Elem)) {
28 for v := range s {
29 f(v)
30 }
31}
View as plain text