byte.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. Copyright The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. // Code generated by set-gen. DO NOT EDIT.
  14. package sets
  15. import (
  16. "reflect"
  17. "sort"
  18. )
  19. // sets.Byte is a set of bytes, implemented via map[byte]struct{} for minimal memory consumption.
  20. type Byte map[byte]Empty
  21. // NewByte creates a Byte from a list of values.
  22. func NewByte(items ...byte) Byte {
  23. ss := Byte{}
  24. ss.Insert(items...)
  25. return ss
  26. }
  27. // ByteKeySet creates a Byte from a keys of a map[byte](? extends interface{}).
  28. // If the value passed in is not actually a map, this will panic.
  29. func ByteKeySet(theMap interface{}) Byte {
  30. v := reflect.ValueOf(theMap)
  31. ret := Byte{}
  32. for _, keyValue := range v.MapKeys() {
  33. ret.Insert(keyValue.Interface().(byte))
  34. }
  35. return ret
  36. }
  37. // Insert adds items to the set.
  38. func (s Byte) Insert(items ...byte) Byte {
  39. for _, item := range items {
  40. s[item] = Empty{}
  41. }
  42. return s
  43. }
  44. // Delete removes all items from the set.
  45. func (s Byte) Delete(items ...byte) Byte {
  46. for _, item := range items {
  47. delete(s, item)
  48. }
  49. return s
  50. }
  51. // Has returns true if and only if item is contained in the set.
  52. func (s Byte) Has(item byte) bool {
  53. _, contained := s[item]
  54. return contained
  55. }
  56. // HasAll returns true if and only if all items are contained in the set.
  57. func (s Byte) HasAll(items ...byte) bool {
  58. for _, item := range items {
  59. if !s.Has(item) {
  60. return false
  61. }
  62. }
  63. return true
  64. }
  65. // HasAny returns true if any items are contained in the set.
  66. func (s Byte) HasAny(items ...byte) bool {
  67. for _, item := range items {
  68. if s.Has(item) {
  69. return true
  70. }
  71. }
  72. return false
  73. }
  74. // Difference returns a set of objects that are not in s2
  75. // For example:
  76. // s1 = {a1, a2, a3}
  77. // s2 = {a1, a2, a4, a5}
  78. // s1.Difference(s2) = {a3}
  79. // s2.Difference(s1) = {a4, a5}
  80. func (s Byte) Difference(s2 Byte) Byte {
  81. result := NewByte()
  82. for key := range s {
  83. if !s2.Has(key) {
  84. result.Insert(key)
  85. }
  86. }
  87. return result
  88. }
  89. // Union returns a new set which includes items in either s1 or s2.
  90. // For example:
  91. // s1 = {a1, a2}
  92. // s2 = {a3, a4}
  93. // s1.Union(s2) = {a1, a2, a3, a4}
  94. // s2.Union(s1) = {a1, a2, a3, a4}
  95. func (s1 Byte) Union(s2 Byte) Byte {
  96. result := NewByte()
  97. for key := range s1 {
  98. result.Insert(key)
  99. }
  100. for key := range s2 {
  101. result.Insert(key)
  102. }
  103. return result
  104. }
  105. // Intersection returns a new set which includes the item in BOTH s1 and s2
  106. // For example:
  107. // s1 = {a1, a2}
  108. // s2 = {a2, a3}
  109. // s1.Intersection(s2) = {a2}
  110. func (s1 Byte) Intersection(s2 Byte) Byte {
  111. var walk, other Byte
  112. result := NewByte()
  113. if s1.Len() < s2.Len() {
  114. walk = s1
  115. other = s2
  116. } else {
  117. walk = s2
  118. other = s1
  119. }
  120. for key := range walk {
  121. if other.Has(key) {
  122. result.Insert(key)
  123. }
  124. }
  125. return result
  126. }
  127. // IsSuperset returns true if and only if s1 is a superset of s2.
  128. func (s1 Byte) IsSuperset(s2 Byte) bool {
  129. for item := range s2 {
  130. if !s1.Has(item) {
  131. return false
  132. }
  133. }
  134. return true
  135. }
  136. // Equal returns true if and only if s1 is equal (as a set) to s2.
  137. // Two sets are equal if their membership is identical.
  138. // (In practice, this means same elements, order doesn't matter)
  139. func (s1 Byte) Equal(s2 Byte) bool {
  140. return len(s1) == len(s2) && s1.IsSuperset(s2)
  141. }
  142. type sortableSliceOfByte []byte
  143. func (s sortableSliceOfByte) Len() int { return len(s) }
  144. func (s sortableSliceOfByte) Less(i, j int) bool { return lessByte(s[i], s[j]) }
  145. func (s sortableSliceOfByte) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
  146. // List returns the contents as a sorted byte slice.
  147. func (s Byte) List() []byte {
  148. res := make(sortableSliceOfByte, 0, len(s))
  149. for key := range s {
  150. res = append(res, key)
  151. }
  152. sort.Sort(res)
  153. return []byte(res)
  154. }
  155. // UnsortedList returns the slice with contents in random order.
  156. func (s Byte) UnsortedList() []byte {
  157. res := make([]byte, 0, len(s))
  158. for key := range s {
  159. res = append(res, key)
  160. }
  161. return res
  162. }
  163. // Returns a single element from the set.
  164. func (s Byte) PopAny() (byte, bool) {
  165. for key := range s {
  166. s.Delete(key)
  167. return key, true
  168. }
  169. var zeroValue byte
  170. return zeroValue, false
  171. }
  172. // Len returns the size of the set.
  173. func (s Byte) Len() int {
  174. return len(s)
  175. }
  176. func lessByte(lhs, rhs byte) bool {
  177. return lhs < rhs
  178. }