-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringset.go
More file actions
79 lines (69 loc) · 1.68 KB
/
stringset.go
File metadata and controls
79 lines (69 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package databuilder
import "sort"
// stringSet is an internal replacement for k8s.io/apimachinery/pkg/util/sets.String.
// It implements only the subset of methods used by data-builder.
type stringSet map[string]struct{}
// newStringSet creates a stringSet from a list of values.
func newStringSet(items ...string) stringSet {
s := make(stringSet, len(items))
for _, item := range items {
s[item] = struct{}{}
}
return s
}
// Has returns true if the set contains the given item.
func (s stringSet) Has(item string) bool {
_, found := s[item]
return found
}
// Insert adds items to the set.
func (s stringSet) Insert(items ...string) {
for _, item := range items {
s[item] = struct{}{}
}
}
// IsSuperset returns true if s contains all items in other.
func (s stringSet) IsSuperset(other stringSet) bool {
for item := range other {
if !s.Has(item) {
return false
}
}
return true
}
// Len returns the number of items in the set.
func (s stringSet) Len() int {
return len(s)
}
// List returns sorted list of items in the set.
func (s stringSet) List() []string {
res := make([]string, 0, len(s))
for item := range s {
res = append(res, item)
}
sort.Strings(res)
return res
}
// String returns a human-readable representation of the set, e.g. "[a b c]".
func (s stringSet) String() string {
items := s.List()
result := "["
for i, item := range items {
if i > 0 {
result += " "
}
result += item
}
result += "]"
return result
}
// Difference returns a new set with items in s but not in other.
func (s stringSet) Difference(other stringSet) stringSet {
result := newStringSet()
for item := range s {
if !other.Has(item) {
result.Insert(item)
}
}
return result
}