mirror of
https://blitiri.com.ar/repos/chasquid
synced 2025-12-17 14:37:02 +00:00
We will need a few string sets in different places, create it as a separate package for convenience. For now, it only implements strings, but it may be extended to other data types in the future.
33 lines
638 B
Go
33 lines
638 B
Go
// Package set implement sets for various types. Well, only string for now :)
|
|
package set
|
|
|
|
type String struct {
|
|
m map[string]struct{}
|
|
}
|
|
|
|
func NewString(values ...string) *String {
|
|
s := &String{}
|
|
s.Add(values...)
|
|
return s
|
|
}
|
|
|
|
func (s *String) Add(values ...string) {
|
|
if s.m == nil {
|
|
s.m = map[string]struct{}{}
|
|
}
|
|
|
|
for _, v := range values {
|
|
s.m[v] = struct{}{}
|
|
}
|
|
}
|
|
|
|
func (s *String) Has(value string) bool {
|
|
// We explicitly allow s to be nil *in this function* to simplify callers'
|
|
// code. Note that Add will not tolerate it, and will panic.
|
|
if s == nil || s.m == nil {
|
|
return false
|
|
}
|
|
_, ok := s.m[value]
|
|
return ok
|
|
}
|