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