| 1 | /* |
| 2 | Copyright 2023 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 | package setscip-go gomod k8s.io/utils . `k8s.io/utils/set`/ |
| 18 | |
| 19 | import ( |
| 20 | "sortscip-go gomod github.com/golang/go/src go1.23 sort/" |
| 21 | ) |
| 22 | |
| 23 | // Empty is public since it is used by some internal API objects for conversions between external |
| 24 | // string arrays and internal sets, and conversion logic requires public types today. |
| 25 | type Emptyscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Empty# struct{} |
| 26 | |
| 27 | // Set is a set of the same type elements, implemented via map[ordered]struct{} for minimal memory consumption. |
| 28 | type Setscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#[E orderedscip-go gomod k8s.io/utils . `k8s.io/utils/set`/ordered#] map[E]Emptyscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Empty# |
| 29 | |
| 30 | // New creates a new set. |
| 31 | func Newscip-go gomod k8s.io/utils . `k8s.io/utils/set`/New().[E orderedscip-go gomod k8s.io/utils . `k8s.io/utils/set`/ordered#](items ...E) Setscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#[E] { |
| 32 | ss := Setscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#[E]{} |
| 33 | ss.Insertscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#Insert().(items...) |
| 34 | return ss |
| 35 | } |
| 36 | |
| 37 | // Clear empties the set. |
| 38 | // It is preferable to replace the set with a newly constructed set, |
| 39 | // but not all callers can do that (when there are other references to the map). |
| 40 | func (s Setscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#[T]) Clearscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#Clear().() Setscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#[T] { |
| 41 | clear(s) |
| 42 | return s |
| 43 | } |
| 44 | |
| 45 | // KeySet creates a Set[E] from a keys of a map[E](? extends interface{}). |
| 46 | func KeySetscip-go gomod k8s.io/utils . `k8s.io/utils/set`/KeySet().[E orderedscip-go gomod k8s.io/utils . `k8s.io/utils/set`/ordered#, A any](theMap map[E]A) Setscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#[E] { |
| 47 | ret := Setscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#[E]{} |
| 48 | for key := range theMap { |
| 49 | ret.Insertscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#Insert().(key) |
| 50 | } |
| 51 | return ret |
| 52 | } |
| 53 | |
| 54 | // Insert adds items to the set. |
| 55 | func (s Setscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#[E]) Insertscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#Insert().(items ...E) Setscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#[E] { |
| 56 | for _, item := range items { |
| 57 | s[item] = Emptyscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Empty#{} |
| 58 | } |
| 59 | return s |
| 60 | } |
| 61 | |
| 62 | // Delete removes all items from the set. |
| 63 | func (s Setscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#[E]) Deletescip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#Delete().(items ...E) Setscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#[E] { |
| 64 | for _, item := range items { |
| 65 | delete(s, item) |
| 66 | } |
| 67 | return s |
| 68 | } |
| 69 | |
| 70 | // Has returns true if and only if item is contained in the set. |
| 71 | func (s Setscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#[E]) Hasscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#Has().(item E) bool { |
| 72 | _, contained := s[item] |
| 73 | return contained |
| 74 | } |
| 75 | |
| 76 | // HasAll returns true if and only if all items are contained in the set. |
| 77 | func (s Setscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#[E]) HasAllscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#HasAll().(items ...E) bool { |
| 78 | for _, item := range items { |
| 79 | if !s.Hasscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#Has().(item) { |
| 80 | return false |
| 81 | } |
| 82 | } |
| 83 | return true |
| 84 | } |
| 85 | |
| 86 | // HasAny returns true if any items are contained in the set. |
| 87 | func (s Setscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#[E]) HasAnyscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#HasAny().(items ...E) bool { |
| 88 | for _, item := range items { |
| 89 | if s.Hasscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#Has().(item) { |
| 90 | return true |
| 91 | } |
| 92 | } |
| 93 | return false |
| 94 | } |
| 95 | |
| 96 | // Union returns a new set which includes items in either s1 or s2. |
| 97 | // For example: |
| 98 | // s1 = {a1, a2} |
| 99 | // s2 = {a3, a4} |
| 100 | // s1.Union(s2) = {a1, a2, a3, a4} |
| 101 | // s2.Union(s1) = {a1, a2, a3, a4} |
| 102 | func (s Setscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#[E]) Unionscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#Union().(s2 Setscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#[E]) Setscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#[E] { |
| 103 | result := Setscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#[E]{} |
| 104 | result.Insertscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#Insert().(s.UnsortedListscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#UnsortedList().()...) |
| 105 | result.Insertscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#Insert().(s2.UnsortedListscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#UnsortedList().()...) |
| 106 | return result |
| 107 | } |
| 108 | |
| 109 | // Len returns the number of elements in the set. |
| 110 | func (s Setscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#[E]) Lenscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#Len().() int { |
| 111 | return len(s) |
| 112 | } |
| 113 | |
| 114 | // Intersection returns a new set which includes the item in BOTH s1 and s2 |
| 115 | // For example: |
| 116 | // s1 = {a1, a2} |
| 117 | // s2 = {a2, a3} |
| 118 | // s1.Intersection(s2) = {a2} |
| 119 | func (s Setscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#[E]) Intersectionscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#Intersection().(s2 Setscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#[E]) Setscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#[E] { |
| 120 | var walk, other Setscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#[E] |
| 121 | result := Setscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#[E]{} |
| 122 | if s.Lenscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#Len().() < s2.Lenscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#Len().() { |
| 123 | walk = s |
| 124 | other = s2 |
| 125 | } else { |
| 126 | walk = s2 |
| 127 | other = s |
| 128 | } |
| 129 | for key := range walk { |
| 130 | if other.Hasscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#Has().(key) { |
| 131 | result.Insertscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#Insert().(key) |
| 132 | } |
| 133 | } |
| 134 | return result |
| 135 | } |
| 136 | |
| 137 | // IsSuperset returns true if and only if s1 is a superset of s2. |
| 138 | func (s Setscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#[E]) IsSupersetscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#IsSuperset().(s2 Setscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#[E]) bool { |
| 139 | for item := range s2 { |
| 140 | if !s.Hasscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#Has().(item) { |
| 141 | return false |
| 142 | } |
| 143 | } |
| 144 | return true |
| 145 | } |
| 146 | |
| 147 | // Difference returns a set of objects that are not in s2 |
| 148 | // For example: |
| 149 | // s1 = {a1, a2, a3} |
| 150 | // s2 = {a1, a2, a4, a5} |
| 151 | // s1.Difference(s2) = {a3} |
| 152 | // s2.Difference(s1) = {a4, a5} |
| 153 | func (s Setscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#[E]) Differencescip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#Difference().(s2 Setscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#[E]) Setscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#[E] { |
| 154 | result := Setscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#[E]{} |
| 155 | for key := range s { |
| 156 | if !s2.Hasscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#Has().(key) { |
| 157 | result.Insertscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#Insert().(key) |
| 158 | } |
| 159 | } |
| 160 | return result |
| 161 | } |
| 162 | |
| 163 | // Equal returns true if and only if s1 is equal (as a set) to s2. |
| 164 | // Two sets are equal if their membership is identical. |
| 165 | func (s Setscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#[E]) Equalscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#Equal().(s2 Setscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#[E]) bool { |
| 166 | return s.Lenscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#Len().() == s2.Lenscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#Len().() && s.IsSupersetscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#IsSuperset().(s2) |
| 167 | } |
| 168 | |
| 169 | type sortableSlicescip-go gomod k8s.io/utils . `k8s.io/utils/set`/sortableSlice#[E orderedscip-go gomod k8s.io/utils . `k8s.io/utils/set`/ordered#] []E |
| 170 | |
| 171 | func (s sortableSlicescip-go gomod k8s.io/utils . `k8s.io/utils/set`/sortableSlice#[E]) Lenscip-go gomod k8s.io/utils . `k8s.io/utils/set`/sortableSlice#Len().() int { |
| 172 | return len(s) |
| 173 | } |
| 174 | func (s sortableSlicescip-go gomod k8s.io/utils . `k8s.io/utils/set`/sortableSlice#[E]) Lessscip-go gomod k8s.io/utils . `k8s.io/utils/set`/sortableSlice#Less().(i, j int) bool { return s[i] < s[j] } |
| 175 | func (s sortableSlicescip-go gomod k8s.io/utils . `k8s.io/utils/set`/sortableSlice#[E]) Swapscip-go gomod k8s.io/utils . `k8s.io/utils/set`/sortableSlice#Swap().(i, j int) { s[i], s[j] = s[j], s[i] } |
| 176 | |
| 177 | // SortedList returns the contents as a sorted slice. |
| 178 | func (s Setscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#[E]) SortedListscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#SortedList().() []E { |
| 179 | res := make(sortableSlicescip-go gomod k8s.io/utils . `k8s.io/utils/set`/sortableSlice#[E], 0, s.Lenscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#Len().()) |
| 180 | for key := range s { |
| 181 | res = append(res, key) |
| 182 | } |
| 183 | sortscip-go gomod github.com/golang/go/src go1.23 sort/.Sortscip-go gomod github.com/golang/go/src go1.23 sort/Sort().(res) |
| 184 | return res |
| 185 | } |
| 186 | |
| 187 | // UnsortedList returns the slice with contents in random order. |
| 188 | func (s Setscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#[E]) UnsortedListscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#UnsortedList().() []E { |
| 189 | res := make([]E, 0, len(s)) |
| 190 | for key := range s { |
| 191 | res = append(res, key) |
| 192 | } |
| 193 | return res |
| 194 | } |
| 195 | |
| 196 | // PopAny returns a single element from the set. |
| 197 | func (s Setscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#[E]) PopAnyscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#PopAny().() (E, bool) { |
| 198 | for key := range s { |
| 199 | s.Deletescip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#Delete().(key) |
| 200 | return key, true |
| 201 | } |
| 202 | var zeroValue E |
| 203 | return zeroValue, false |
| 204 | } |
| 205 | |
| 206 | // Clone returns a new set which is a copy of the current set. |
| 207 | func (s Setscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#[T]) Clonescip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#Clone().() Setscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#[T] { |
| 208 | result := make(Setscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#[T], len(s)) |
| 209 | for key := range s { |
| 210 | result.Insertscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#Insert().(key) |
| 211 | } |
| 212 | return result |
| 213 | } |
| 214 | |
| 215 | // SymmetricDifference returns a set of elements which are in either of the sets, but not in their intersection. |
| 216 | // For example: |
| 217 | // s1 = {a1, a2, a3} |
| 218 | // s2 = {a1, a2, a4, a5} |
| 219 | // s1.SymmetricDifference(s2) = {a3, a4, a5} |
| 220 | // s2.SymmetricDifference(s1) = {a3, a4, a5} |
| 221 | func (s Setscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#[T]) SymmetricDifferencescip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#SymmetricDifference().(s2 Setscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#[T]) Setscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#[T] { |
| 222 | return s.Differencescip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#Difference().(s2).Unionscip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#Union().(s2.Differencescip-go gomod k8s.io/utils . `k8s.io/utils/set`/Set#Difference().(s)) |
| 223 | } |