summaryrefslogtreecommitdiffstats
path: root/src/go/parser/testdata/set.go2
blob: 0da6377cbdceba9ba4776933ad597e2c1700a4d4 (plain)
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
// Package set implements sets of any type.
package set

type Set[Elem comparable] map[Elem]struct{}

func Make[Elem comparable]() Set[Elem] {
	return make(Set(Elem))
}

func (s Set[Elem]) Add(v Elem) {
	s[v] = struct{}{}
}

func (s Set[Elem]) Delete(v Elem) {
	delete(s, v)
}

func (s Set[Elem]) Contains(v Elem) bool {
	_, ok := s[v]
	return ok
}

func (s Set[Elem]) Len() int {
	return len(s)
}

func (s Set[Elem]) Iterate(f func(Elem)) {
	for v := range s {
		f(v)
	}
}