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