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