util/set: add some more Set operations (#10022)

Updates #cleanup

Signed-off-by: Chris Palmer <cpalmer@tailscale.com>
This commit is contained in:
Chris Palmer
2023-10-31 17:15:40 -07:00
committed by GitHub
parent 7f3208592f
commit 00375f56ea
2 changed files with 74 additions and 3 deletions

View File

@ -4,6 +4,10 @@
// Package set contains set types.
package set
import (
"maps"
)
// Set is a set of T.
type Set[T comparable] map[T]struct{}
@ -14,16 +18,28 @@ func SetOf[T comparable](slice []T) Set[T] {
return s
}
// Add adds e to the set.
// Clone returns a new set cloned from the elements in s.
func Clone[T comparable](s Set[T]) Set[T] {
return maps.Clone(s)
}
// Add adds e to s.
func (s Set[T]) Add(e T) { s[e] = struct{}{} }
// AddSlice adds each element of es to the set.
// AddSlice adds each element of es to s.
func (s Set[T]) AddSlice(es []T) {
for _, e := range es {
s.Add(e)
}
}
// AddSet adds each element of es to s.
func (s Set[T]) AddSet(es Set[T]) {
for e := range es {
s.Add(e)
}
}
// Slice returns the elements of the set as a slice. The elements will not be
// in any particular order.
func (s Set[T]) Slice() []T {
@ -45,3 +61,8 @@ func (s Set[T]) Contains(e T) bool {
// Len reports the number of items in s.
func (s Set[T]) Len() int { return len(s) }
// Equal reports whether s is equal to other.
func (s Set[T]) Equal(other Set[T]) bool {
return maps.Equal(s, other)
}