forked from TrueCloudLab/distribution
1266f13afa
As part of the efforts to break up the common package before disaster strikes, a new collections package has been created. More may belong there but for now, it only includes an implementation of StringSet. Signed-off-by: Stephen J Day <stephen.day@docker.com>
35 lines
779 B
Go
35 lines
779 B
Go
package collections
|
|
|
|
// StringSet is a useful type for looking up strings.
|
|
type StringSet map[string]struct{}
|
|
|
|
// NewStringSet creates a new StringSet with the given strings.
|
|
func NewStringSet(keys ...string) StringSet {
|
|
ss := make(StringSet, len(keys))
|
|
ss.Add(keys...)
|
|
return ss
|
|
}
|
|
|
|
// Add inserts the given keys into this StringSet.
|
|
func (ss StringSet) Add(keys ...string) {
|
|
for _, key := range keys {
|
|
ss[key] = struct{}{}
|
|
}
|
|
}
|
|
|
|
// Contains returns whether the given key is in this StringSet.
|
|
func (ss StringSet) Contains(key string) bool {
|
|
_, ok := ss[key]
|
|
return ok
|
|
}
|
|
|
|
// Keys returns a slice of all keys in this StringSet.
|
|
func (ss StringSet) Keys() []string {
|
|
keys := make([]string, 0, len(ss))
|
|
|
|
for key := range ss {
|
|
keys = append(keys, key)
|
|
}
|
|
|
|
return keys
|
|
}
|