byte.go 5.1 KB

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