Debian Codegraph @

examples/set-gen/sets/byte.go

raw
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.Byte is a set of bytes, implemented via map[byte]struct{} for minimal memory consumption.
27 type Byte map[byte]Empty
28
29 // NewByte creates a Byte from a list of values.
30 func NewByte(items ...byte) Byte {
31 ss := make(Byte, len(items))
32 ss.Insert(items...)
33 return ss
34 }
35
36 // ByteKeySet creates a Byte from a keys of a map[byte](? extends interface{}).
37 // If the value passed in is not actually a map, this will panic.
38 func ByteKeySet(theMap interface{}) Byte {
39 v := reflect.ValueOf(theMap)
40 ret := Byte{}
41
42 for _, keyValue := range v.MapKeys() {
43 ret.Insert(keyValue.Interface().(byte))
44 }
45 return ret
46 }
47
48 // Insert adds items to the set.
49 func (s Byte) Insert(items ...byte) Byte {
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 Byte) Delete(items ...byte) Byte {
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 Byte) Has(item byte) 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 Byte) HasAll(items ...byte) 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 Byte) HasAny(items ...byte) 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 Byte) Clone() Byte {
92 result := make(Byte, 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 Byte) Difference(s2 Byte) Byte {
106 result := NewByte()
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 Byte) SymmetricDifference(s2 Byte) Byte {
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 Byte) Union(s2 Byte) Byte {
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 Byte) Intersection(s2 Byte) Byte {
145 var walk, other Byte
146 result := NewByte()
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 Byte) IsSuperset(s2 Byte) 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 Byte) Equal(s2 Byte) bool {
176 return len(s1) == len(s2) && s1.IsSuperset(s2)
177 }
178
179 type sortableSliceOfByte []byte
180
181 func (s sortableSliceOfByte) Len() int { return len(s) }
182 func (s sortableSliceOfByte) Less(i, j int) bool { return lessByte(s[i], s[j]) }
183 func (s sortableSliceOfByte) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
184
185 // List returns the contents as a sorted byte slice.
186 func (s Byte) List() []byte {
187 res := make(sortableSliceOfByte, 0, len(s))
188 for key := range s {
189 res = append(res, key)
190 }
191 sort.Sort(res)
192 return []byte(res)
193 }
194
195 // UnsortedList returns the slice with contents in random order.
196 func (s Byte) UnsortedList() []byte {
197 res := make([]byte, 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 Byte) PopAny() (byte, bool) {
206 for key := range s {
207 s.Delete(key)
208 return key, true
209 }
210 var zeroValue byte
211 return zeroValue, false
212 }
213
214 // Len returns the size of the set.
215 func (s Byte) Len() int {
216 return len(s)
217 }
218
219 func lessByte(lhs, rhs byte) bool {
220 return lhs < rhs
221 }