byte.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. Copyright 2016 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. // This file was autogenerated by set-gen. Do not edit it manually!
  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. // New 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) {
  39. for _, item := range items {
  40. s[item] = Empty{}
  41. }
  42. }
  43. // Delete removes all items from the set.
  44. func (s Byte) Delete(items ...byte) {
  45. for _, item := range items {
  46. delete(s, item)
  47. }
  48. }
  49. // Has returns true if and only if item is contained in the set.
  50. func (s Byte) Has(item byte) bool {
  51. _, contained := s[item]
  52. return contained
  53. }
  54. // HasAll returns true if and only if all items are contained in the set.
  55. func (s Byte) HasAll(items ...byte) bool {
  56. for _, item := range items {
  57. if !s.Has(item) {
  58. return false
  59. }
  60. }
  61. return true
  62. }
  63. // HasAny returns true if any items are contained in the set.
  64. func (s Byte) HasAny(items ...byte) bool {
  65. for _, item := range items {
  66. if s.Has(item) {
  67. return true
  68. }
  69. }
  70. return false
  71. }
  72. // Difference returns a set of objects that are not in s2
  73. // For example:
  74. // s1 = {a1, a2, a3}
  75. // s2 = {a1, a2, a4, a5}
  76. // s1.Difference(s2) = {a3}
  77. // s2.Difference(s1) = {a4, a5}
  78. func (s Byte) Difference(s2 Byte) Byte {
  79. result := NewByte()
  80. for key := range s {
  81. if !s2.Has(key) {
  82. result.Insert(key)
  83. }
  84. }
  85. return result
  86. }
  87. // Union returns a new set which includes items in either s1 or s2.
  88. // For example:
  89. // s1 = {a1, a2}
  90. // s2 = {a3, a4}
  91. // s1.Union(s2) = {a1, a2, a3, a4}
  92. // s2.Union(s1) = {a1, a2, a3, a4}
  93. func (s1 Byte) Union(s2 Byte) Byte {
  94. result := NewByte()
  95. for key := range s1 {
  96. result.Insert(key)
  97. }
  98. for key := range s2 {
  99. result.Insert(key)
  100. }
  101. return result
  102. }
  103. // Intersection returns a new set which includes the item in BOTH s1 and s2
  104. // For example:
  105. // s1 = {a1, a2}
  106. // s2 = {a2, a3}
  107. // s1.Intersection(s2) = {a2}
  108. func (s1 Byte) Intersection(s2 Byte) Byte {
  109. var walk, other Byte
  110. result := NewByte()
  111. if s1.Len() < s2.Len() {
  112. walk = s1
  113. other = s2
  114. } else {
  115. walk = s2
  116. other = s1
  117. }
  118. for key := range walk {
  119. if other.Has(key) {
  120. result.Insert(key)
  121. }
  122. }
  123. return result
  124. }
  125. // IsSuperset returns true if and only if s1 is a superset of s2.
  126. func (s1 Byte) IsSuperset(s2 Byte) bool {
  127. for item := range s2 {
  128. if !s1.Has(item) {
  129. return false
  130. }
  131. }
  132. return true
  133. }
  134. // Equal returns true if and only if s1 is equal (as a set) to s2.
  135. // Two sets are equal if their membership is identical.
  136. // (In practice, this means same elements, order doesn't matter)
  137. func (s1 Byte) Equal(s2 Byte) bool {
  138. return len(s1) == len(s2) && s1.IsSuperset(s2)
  139. }
  140. type sortableSliceOfByte []byte
  141. func (s sortableSliceOfByte) Len() int { return len(s) }
  142. func (s sortableSliceOfByte) Less(i, j int) bool { return lessByte(s[i], s[j]) }
  143. func (s sortableSliceOfByte) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
  144. // List returns the contents as a sorted byte slice.
  145. func (s Byte) List() []byte {
  146. res := make(sortableSliceOfByte, 0, len(s))
  147. for key := range s {
  148. res = append(res, key)
  149. }
  150. sort.Sort(res)
  151. return []byte(res)
  152. }
  153. // UnsortedList returns the slice with contents in random order.
  154. func (s Byte) UnsortedList() []byte {
  155. res := make([]byte, 0, len(s))
  156. for key := range s {
  157. res = append(res, key)
  158. }
  159. return res
  160. }
  161. // Returns a single element from the set.
  162. func (s Byte) PopAny() (byte, bool) {
  163. for key := range s {
  164. s.Delete(key)
  165. return key, true
  166. }
  167. var zeroValue byte
  168. return zeroValue, false
  169. }
  170. // Len returns the size of the set.
  171. func (s Byte) Len() int {
  172. return len(s)
  173. }
  174. func lessByte(lhs, rhs byte) bool {
  175. return lhs < rhs
  176. }