Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 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 | package sets |
| 20 | |
| 21 | import ( |
| 22 | "reflect" |
| 23 | "sort" |
| 24 | ) |
| 25 | |
| 26 | // sets.String is a set of strings, implemented via map[string]struct{} for minimal memory consumption. |
| 27 | type String map[string]Empty |
| 28 | |
| 29 | // NewString creates a String from a list of values. |
| 30 | func NewString(items ...string) String { |
| 31 | ss := String{} |
| 32 | ss.Insert(items...) |
| 33 | return ss |
| 34 | } |
| 35 | |
| 36 | // StringKeySet creates a String from a keys of a map[string](? extends interface{}). |
| 37 | // If the value passed in is not actually a map, this will panic. |
| 38 | func StringKeySet(theMap interface{}) String { |
| 39 | v := reflect.ValueOf(theMap) |
| 40 | ret := String{} |
| 41 | |
| 42 | for _, keyValue := range v.MapKeys() { |
| 43 | ret.Insert(keyValue.Interface().(string)) |
| 44 | } |
| 45 | return ret |
| 46 | } |
| 47 | |
| 48 | // Insert adds items to the set. |
| 49 | func (s String) Insert(items ...string) String { |
| 50 | for _, item := range items { |
| 51 | s[item] = Empty{} |
| 52 | } |
| 53 | return s |
| 54 | } |
| 55 | |
| 56 | // Delete removes all items from the set. |
| 57 | func (s String) Delete(items ...string) String { |
| 58 | for _, item := range items { |
| 59 | delete(s, item) |
| 60 | } |
| 61 | return s |
| 62 | } |
| 63 | |
| 64 | // Has returns true if and only if item is contained in the set. |
| 65 | func (s String) Has(item string) bool { |
| 66 | _, contained := s[item] |
| 67 | return contained |
| 68 | } |
| 69 | |
| 70 | // HasAll returns true if and only if all items are contained in the set. |
| 71 | func (s String) HasAll(items ...string) bool { |
| 72 | for _, item := range items { |
| 73 | if !s.Has(item) { |
| 74 | return false |
| 75 | } |
| 76 | } |
| 77 | return true |
| 78 | } |
| 79 | |
| 80 | // HasAny returns true if any items are contained in the set. |
| 81 | func (s String) HasAny(items ...string) bool { |
| 82 | for _, item := range items { |
| 83 | if s.Has(item) { |
| 84 | return true |
| 85 | } |
| 86 | } |
| 87 | return false |
| 88 | } |
| 89 | |
| 90 | // Difference returns a set of objects that are not in s2 |
| 91 | // For example: |
| 92 | // s1 = {a1, a2, a3} |
| 93 | // s2 = {a1, a2, a4, a5} |
| 94 | // s1.Difference(s2) = {a3} |
| 95 | // s2.Difference(s1) = {a4, a5} |
| 96 | func (s String) Difference(s2 String) String { |
| 97 | result := NewString() |
| 98 | for key := range s { |
| 99 | if !s2.Has(key) { |
| 100 | result.Insert(key) |
| 101 | } |
| 102 | } |
| 103 | return result |
| 104 | } |
| 105 | |
| 106 | // Union returns a new set which includes items in either s1 or s2. |
| 107 | // For example: |
| 108 | // s1 = {a1, a2} |
| 109 | // s2 = {a3, a4} |
| 110 | // s1.Union(s2) = {a1, a2, a3, a4} |
| 111 | // s2.Union(s1) = {a1, a2, a3, a4} |
| 112 | func (s1 String) Union(s2 String) String { |
| 113 | result := NewString() |
| 114 | for key := range s1 { |
| 115 | result.Insert(key) |
| 116 | } |
| 117 | for key := range s2 { |
| 118 | result.Insert(key) |
| 119 | } |
| 120 | return result |
| 121 | } |
| 122 | |
| 123 | // Intersection returns a new set which includes the item in BOTH s1 and s2 |
| 124 | // For example: |
| 125 | // s1 = {a1, a2} |
| 126 | // s2 = {a2, a3} |
| 127 | // s1.Intersection(s2) = {a2} |
| 128 | func (s1 String) Intersection(s2 String) String { |
| 129 | var walk, other String |
| 130 | result := NewString() |
| 131 | if s1.Len() < s2.Len() { |
| 132 | walk = s1 |
| 133 | other = s2 |
| 134 | } else { |
| 135 | walk = s2 |
| 136 | other = s1 |
| 137 | } |
| 138 | for key := range walk { |
| 139 | if other.Has(key) { |
| 140 | result.Insert(key) |
| 141 | } |
| 142 | } |
| 143 | return result |
| 144 | } |
| 145 | |
| 146 | // IsSuperset returns true if and only if s1 is a superset of s2. |
| 147 | func (s1 String) IsSuperset(s2 String) bool { |
| 148 | for item := range s2 { |
| 149 | if !s1.Has(item) { |
| 150 | return false |
| 151 | } |
| 152 | } |
| 153 | return true |
| 154 | } |
| 155 | |
| 156 | // Equal returns true if and only if s1 is equal (as a set) to s2. |
| 157 | // Two sets are equal if their membership is identical. |
| 158 | // (In practice, this means same elements, order doesn't matter) |
| 159 | func (s1 String) Equal(s2 String) bool { |
| 160 | return len(s1) == len(s2) && s1.IsSuperset(s2) |
| 161 | } |
| 162 | |
| 163 | type sortableSliceOfString []string |
| 164 | |
| 165 | func (s sortableSliceOfString) Len() int { return len(s) } |
| 166 | func (s sortableSliceOfString) Less(i, j int) bool { return lessString(s[i], s[j]) } |
| 167 | func (s sortableSliceOfString) Swap(i, j int) { s[i], s[j] = s[j], s[i] } |
| 168 | |
| 169 | // List returns the contents as a sorted string slice. |
| 170 | func (s String) List() []string { |
| 171 | res := make(sortableSliceOfString, 0, len(s)) |
| 172 | for key := range s { |
| 173 | res = append(res, key) |
| 174 | } |
| 175 | sort.Sort(res) |
| 176 | return []string(res) |
| 177 | } |
| 178 | |
| 179 | // UnsortedList returns the slice with contents in random order. |
| 180 | func (s String) UnsortedList() []string { |
| 181 | res := make([]string, 0, len(s)) |
| 182 | for key := range s { |
| 183 | res = append(res, key) |
| 184 | } |
| 185 | return res |
| 186 | } |
| 187 | |
| 188 | // Returns a single element from the set. |
| 189 | func (s String) PopAny() (string, bool) { |
| 190 | for key := range s { |
| 191 | s.Delete(key) |
| 192 | return key, true |
| 193 | } |
| 194 | var zeroValue string |
| 195 | return zeroValue, false |
| 196 | } |
| 197 | |
| 198 | // Len returns the size of the set. |
| 199 | func (s String) Len() int { |
| 200 | return len(s) |
| 201 | } |
| 202 | |
| 203 | func lessString(lhs, rhs string) bool { |
| 204 | return lhs < rhs |
| 205 | } |