um/tag/set.go

39 lines
680 B
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package tag
import (
"maps"
"slices"
)
// convenience shorthand for this awkward map type.
type Set map[string]bool
// add members to the "set"
// TODO: this probably should have been a pointer receiver to handle the nil map case.
// currently doing some roundabout checks before calling.
func (s Set) Add(mems ...string) {
for _, m := range mems {
s[m] = true
}
}
// get all members in a slice
func (s Set) Members() []string {
return slices.Collect(maps.Keys(s))
}
// s ∪ t
func (s Set) Union(tt ...Set) {
for _, t := range tt {
s.Add(t.Members()...)
}
}
// s ∩ t
func (s Set) Intersect(t Set) {
for m, _ := range s {
if !t[m] {
delete(s, m)
}
}
}