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