2020-06-04 18:42:59 +00:00
|
|
|
package internal
|
|
|
|
|
2020-11-17 13:25:01 +00:00
|
|
|
import "sort"
|
|
|
|
|
|
|
|
// StringSet represents a set of string types.
|
2020-07-07 22:04:27 +00:00
|
|
|
type StringSet map[string]struct{}
|
2020-06-04 18:42:59 +00:00
|
|
|
|
2020-11-17 13:25:01 +00:00
|
|
|
// NewStringSet creates a new empty StringSet.
|
2022-03-04 22:22:40 +00:00
|
|
|
func NewStringSet(start ...string) StringSet {
|
2020-07-07 22:04:27 +00:00
|
|
|
ret := make(StringSet)
|
2020-06-04 18:42:59 +00:00
|
|
|
for _, s := range start {
|
|
|
|
ret.Add(s)
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2020-11-17 13:25:01 +00:00
|
|
|
// Add a string to the set.
|
2023-05-15 20:23:39 +00:00
|
|
|
func (s StringSet) Add(i ...string) {
|
|
|
|
for _, str := range i {
|
|
|
|
s[str] = struct{}{}
|
|
|
|
}
|
2020-06-04 18:42:59 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 13:25:01 +00:00
|
|
|
// Remove a string from the set.
|
2020-07-07 22:04:27 +00:00
|
|
|
func (s StringSet) Remove(i string) {
|
2020-06-04 18:42:59 +00:00
|
|
|
delete(s, i)
|
|
|
|
}
|
|
|
|
|
2020-11-17 13:25:01 +00:00
|
|
|
// Contains indicates if the given string is contained within the set.
|
2020-07-07 22:04:27 +00:00
|
|
|
func (s StringSet) Contains(i string) bool {
|
2020-06-04 18:42:59 +00:00
|
|
|
_, ok := s[i]
|
|
|
|
return ok
|
|
|
|
}
|
2020-07-15 11:42:35 +00:00
|
|
|
|
2020-11-17 13:25:01 +00:00
|
|
|
// ToSlice returns a sorted slice of strings that are contained within the set.
|
2020-07-15 11:42:35 +00:00
|
|
|
func (s StringSet) ToSlice() []string {
|
|
|
|
ret := make([]string, len(s))
|
|
|
|
idx := 0
|
|
|
|
for v := range s {
|
|
|
|
ret[idx] = v
|
|
|
|
idx++
|
|
|
|
}
|
2020-11-17 13:25:01 +00:00
|
|
|
sort.Strings(ret)
|
2020-07-15 11:42:35 +00:00
|
|
|
return ret
|
|
|
|
}
|
2023-05-15 20:23:39 +00:00
|
|
|
|
|
|
|
func (s StringSet) Equals(o StringSet) bool {
|
|
|
|
if len(s) != len(o) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for k := range s {
|
|
|
|
if !o.Contains(k) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s StringSet) Empty() bool {
|
|
|
|
return len(s) < 1
|
|
|
|
}
|