| 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.Int is a set of ints, implemented via map[int]struct{} for minimal memory consumption. |
| 27 |
type Int map[int]Empty |
| 28 |
|
| 29 |
// NewInt creates a Int from a list of values. |
| 30 |
func NewInt(items ...int) Int { |
| 31 |
ss := make(Int, len(items)) |
| 32 |
ss.Insert(items...) |
| 33 |
return ss |
| 34 |
} |
| 35 |
|
| 36 |
// IntKeySet creates a Int from a keys of a map[int](? extends interface{}). |
| 37 |
// If the value passed in is not actually a map, this will panic. |
| 38 |
func IntKeySet(theMap interface{}) Int { |
| 39 |
v := reflect.ValueOf(theMap) |
| 40 |
ret := Int{} |
| 41 |
|
| 42 |
for _, keyValue := range v.MapKeys() { |
| 43 |
ret.Insert(keyValue.Interface().(int)) |
| 44 |
} |
| 45 |
return ret |
| 46 |
} |
| 47 |
|
| 48 |
// Insert adds items to the set. |
| 49 |
func (s Int) Insert(items ...int) Int { |
| 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 Int) Delete(items ...int) Int { |
| 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 Int) Has(item int) 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 Int) HasAll(items ...int) 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 Int) HasAny(items ...int) bool { |
| 82 |
for _, item := range items { |
| 83 |
if s.Has(item) { |
| 84 |
return true |
| 85 |
} |
| 86 |
} |
| 87 |
return false |
| 88 |
} |
| 89 |
|
| 90 |
// Clone returns a new set which is a copy of the current set. |
| 91 |
func (s Int) Clone() Int { |
| 92 |
result := make(Int, len(s)) |
| 93 |
for key := range s { |
| 94 |
result.Insert(key) |
| 95 |
} |
| 96 |
return result |
| 97 |
} |
| 98 |
|
| 99 |
// Difference returns a set of objects that are not in s2. |
| 100 |
// For example: |
| 101 |
// s1 = {a1, a2, a3} |
| 102 |
// s2 = {a1, a2, a4, a5} |
| 103 |
// s1.Difference(s2) = {a3} |
| 104 |
// s2.Difference(s1) = {a4, a5} |
| 105 |
func (s1 Int) Difference(s2 Int) Int { |
| 106 |
result := NewInt() |
| 107 |
for key := range s1 { |
| 108 |
if !s2.Has(key) { |
| 109 |
result.Insert(key) |
| 110 |
} |
| 111 |
} |
| 112 |
return result |
| 113 |
} |
| 114 |
|
| 115 |
// SymmetricDifference returns a set of elements which are in either of the sets, but not in their intersection. |
| 116 |
// For example: |
| 117 |
// s1 = {a1, a2, a3} |
| 118 |
// s2 = {a1, a2, a4, a5} |
| 119 |
// s1.SymmetricDifference(s2) = {a3, a4, a5} |
| 120 |
// s2.SymmetricDifference(s1) = {a3, a4, a5} |
| 121 |
func (s1 Int) SymmetricDifference(s2 Int) Int { |
| 122 |
return s1.Difference(s2).Union(s2.Difference(s1)) |
| 123 |
} |
| 124 |
|
| 125 |
// Union returns a new set which includes items in either s1 or s2. |
| 126 |
// For example: |
| 127 |
// s1 = {a1, a2} |
| 128 |
// s2 = {a3, a4} |
| 129 |
// s1.Union(s2) = {a1, a2, a3, a4} |
| 130 |
// s2.Union(s1) = {a1, a2, a3, a4} |
| 131 |
func (s1 Int) Union(s2 Int) Int { |
| 132 |
result := s1.Clone() |
| 133 |
for key := range s2 { |
| 134 |
result.Insert(key) |
| 135 |
} |
| 136 |
return result |
| 137 |
} |
| 138 |
|
| 139 |
// Intersection returns a new set which includes the item in BOTH s1 and s2 |
| 140 |
// For example: |
| 141 |
// s1 = {a1, a2} |
| 142 |
// s2 = {a2, a3} |
| 143 |
// s1.Intersection(s2) = {a2} |
| 144 |
func (s1 Int) Intersection(s2 Int) Int { |
| 145 |
var walk, other Int |
| 146 |
result := NewInt() |
| 147 |
if s1.Len() < s2.Len() { |
| 148 |
walk = s1 |
| 149 |
other = s2 |
| 150 |
} else { |
| 151 |
walk = s2 |
| 152 |
other = s1 |
| 153 |
} |
| 154 |
for key := range walk { |
| 155 |
if other.Has(key) { |
| 156 |
result.Insert(key) |
| 157 |
} |
| 158 |
} |
| 159 |
return result |
| 160 |
} |
| 161 |
|
| 162 |
// IsSuperset returns true if and only if s1 is a superset of s2. |
| 163 |
func (s1 Int) IsSuperset(s2 Int) bool { |
| 164 |
for item := range s2 { |
| 165 |
if !s1.Has(item) { |
| 166 |
return false |
| 167 |
} |
| 168 |
} |
| 169 |
return true |
| 170 |
} |
| 171 |
|
| 172 |
// Equal returns true if and only if s1 is equal (as a set) to s2. |
| 173 |
// Two sets are equal if their membership is identical. |
| 174 |
// (In practice, this means same elements, order doesn't matter) |
| 175 |
func (s1 Int) Equal(s2 Int) bool { |
| 176 |
return len(s1) == len(s2) && s1.IsSuperset(s2) |
| 177 |
} |
| 178 |
|
| 179 |
type sortableSliceOfInt []int |
| 180 |
|
| 181 |
func (s sortableSliceOfInt) Len() int { return len(s) } |
| 182 |
func (s sortableSliceOfInt) Less(i, j int) bool { return lessInt(s[i], s[j]) } |
| 183 |
func (s sortableSliceOfInt) Swap(i, j int) { s[i], s[j] = s[j], s[i] } |
| 184 |
|
| 185 |
// List returns the contents as a sorted int slice. |
| 186 |
func (s Int) List() []int { |
| 187 |
res := make(sortableSliceOfInt, 0, len(s)) |
| 188 |
for key := range s { |
| 189 |
res = append(res, key) |
| 190 |
} |
| 191 |
sort.Sort(res) |
| 192 |
return []int(res) |
| 193 |
} |
| 194 |
|
| 195 |
// UnsortedList returns the slice with contents in random order. |
| 196 |
func (s Int) UnsortedList() []int { |
| 197 |
res := make([]int, 0, len(s)) |
| 198 |
for key := range s { |
| 199 |
res = append(res, key) |
| 200 |
} |
| 201 |
return res |
| 202 |
} |
| 203 |
|
| 204 |
// Returns a single element from the set. |
| 205 |
func (s Int) PopAny() (int, bool) { |
| 206 |
for key := range s { |
| 207 |
s.Delete(key) |
| 208 |
return key, true |
| 209 |
} |
| 210 |
var zeroValue int |
| 211 |
return zeroValue, false |
| 212 |
} |
| 213 |
|
| 214 |
// Len returns the size of the set. |
| 215 |
func (s Int) Len() int { |
| 216 |
return len(s) |
| 217 |
} |
| 218 |
|
| 219 |
func lessInt(lhs, rhs int) bool { |
| 220 |
return lhs < rhs |
| 221 |
} |