Debian Codegraph @

kyaml/yaml/internal/k8sgen/pkg/util/sets/string.go

raw symbols SCIP
1 // Code generated by k8scopy from k8s.io/[email protected]; DO NOT EDIT.
2 // File content copied from k8s.io/[email protected]/pkg/util/sets/string.go
3
4 /*
5 Copyright The Kubernetes Authors.
6
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11 http://www.apache.org/licenses/LICENSE-2.0
12
13 Unless required by applicable law or agreed to in writing, software
14 distributed under the License is distributed on an "AS IS" BASIS,
15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 See the License for the specific language governing permissions and
17 limitations under the License.
18 */
19
20 package sets
21
22 import (
23 "reflect"
24 "sort"
25 )
26
27 // sets.String is a set of strings, implemented via map[string]struct{} for minimal memory consumption.
28 type String map[string]Empty
29
30 // NewString creates a String from a list of values.
31 func NewString(items ...string) String {
32 ss := String{}
33 ss.Insert(items...)
34 return ss
35 }
36
37 // StringKeySet creates a String from a keys of a map[string](? extends interface{}).
38 // If the value passed in is not actually a map, this will panic.
39 func StringKeySet(theMap interface{}) String {
40 v := reflect.ValueOf(theMap)
41 ret := String{}
42
43 for _, keyValue := range v.MapKeys() {
44 ret.Insert(keyValue.Interface().(string))
45 }
46 return ret
47 }
48
49 // Insert adds items to the set.
50 func (s String) Insert(items ...string) String {
51 for _, item := range items {
52 s[item] = Empty{}
53 }
54 return s
55 }
56
57 // Delete removes all items from the set.
58 func (s String) Delete(items ...string) String {
59 for _, item := range items {
60 delete(s, item)
61 }
62 return s
63 }
64
65 // Has returns true if and only if item is contained in the set.
66 func (s String) Has(item string) bool {
67 _, contained := s[item]
68 return contained
69 }
70
71 // HasAll returns true if and only if all items are contained in the set.
72 func (s String) HasAll(items ...string) bool {
73 for _, item := range items {
74 if !s.Has(item) {
75 return false
76 }
77 }
78 return true
79 }
80
81 // HasAny returns true if any items are contained in the set.
82 func (s String) HasAny(items ...string) bool {
83 for _, item := range items {
84 if s.Has(item) {
85 return true
86 }
87 }
88 return false
89 }
90
91 // Difference returns a set of objects that are not in s2
92 // For example:
93 // s1 = {a1, a2, a3}
94 // s2 = {a1, a2, a4, a5}
95 // s1.Difference(s2) = {a3}
96 // s2.Difference(s1) = {a4, a5}
97 func (s String) Difference(s2 String) String {
98 result := NewString()
99 for key := range s {
100 if !s2.Has(key) {
101 result.Insert(key)
102 }
103 }
104 return result
105 }
106
107 // Union returns a new set which includes items in either s1 or s2.
108 // For example:
109 // s1 = {a1, a2}
110 // s2 = {a3, a4}
111 // s1.Union(s2) = {a1, a2, a3, a4}
112 // s2.Union(s1) = {a1, a2, a3, a4}
113 func (s1 String) Union(s2 String) String {
114 result := NewString()
115 for key := range s1 {
116 result.Insert(key)
117 }
118 for key := range s2 {
119 result.Insert(key)
120 }
121 return result
122 }
123
124 // Intersection returns a new set which includes the item in BOTH s1 and s2
125 // For example:
126 // s1 = {a1, a2}
127 // s2 = {a2, a3}
128 // s1.Intersection(s2) = {a2}
129 func (s1 String) Intersection(s2 String) String {
130 var walk, other String
131 result := NewString()
132 if s1.Len() < s2.Len() {
133 walk = s1
134 other = s2
135 } else {
136 walk = s2
137 other = s1
138 }
139 for key := range walk {
140 if other.Has(key) {
141 result.Insert(key)
142 }
143 }
144 return result
145 }
146
147 // IsSuperset returns true if and only if s1 is a superset of s2.
148 func (s1 String) IsSuperset(s2 String) bool {
149 for item := range s2 {
150 if !s1.Has(item) {
151 return false
152 }
153 }
154 return true
155 }
156
157 // Equal returns true if and only if s1 is equal (as a set) to s2.
158 // Two sets are equal if their membership is identical.
159 // (In practice, this means same elements, order doesn't matter)
160 func (s1 String) Equal(s2 String) bool {
161 return len(s1) == len(s2) && s1.IsSuperset(s2)
162 }
163
164 type sortableSliceOfString []string
165
166 func (s sortableSliceOfString) Len() int { return len(s) }
167 func (s sortableSliceOfString) Less(i, j int) bool { return lessString(s[i], s[j]) }
168 func (s sortableSliceOfString) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
169
170 // List returns the contents as a sorted string slice.
171 func (s String) List() []string {
172 res := make(sortableSliceOfString, 0, len(s))
173 for key := range s {
174 res = append(res, key)
175 }
176 sort.Sort(res)
177 return []string(res)
178 }
179
180 // UnsortedList returns the slice with contents in random order.
181 func (s String) UnsortedList() []string {
182 res := make([]string, 0, len(s))
183 for key := range s {
184 res = append(res, key)
185 }
186 return res
187 }
188
189 // Returns a single element from the set.
190 func (s String) PopAny() (string, bool) {
191 for key := range s {
192 s.Delete(key)
193 return key, true
194 }
195 var zeroValue string
196 return zeroValue, false
197 }
198
199 // Len returns the size of the set.
200 func (s String) Len() int {
201 return len(s)
202 }
203
204 func lessString(lhs, rhs string) bool {
205 return lhs < rhs
206 }