int64.go 5.2 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.Int64 is a set of int64s, implemented via map[int64]struct{} for minimal memory consumption.
  33. type Int64 map[int64]Empty
  34. // New creates a Int64 from a list of values.
  35. func NewInt64(items ...int64) Int64 {
  36. ss := Int64{}
  37. ss.Insert(items...)
  38. return ss
  39. }
  40. // Int64KeySet creates a Int64 from a keys of a map[int64](? extends interface{}).
  41. // If the value passed in is not actually a map, this will panic.
  42. func Int64KeySet(theMap interface{}) Int64 {
  43. v := reflect.ValueOf(theMap)
  44. ret := Int64{}
  45. for _, keyValue := range v.MapKeys() {
  46. ret.Insert(keyValue.Interface().(int64))
  47. }
  48. return ret
  49. }
  50. // Insert adds items to the set.
  51. func (s Int64) Insert(items ...int64) {
  52. for _, item := range items {
  53. s[item] = Empty{}
  54. }
  55. }
  56. // Delete removes all items from the set.
  57. func (s Int64) Delete(items ...int64) {
  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 Int64) Has(item int64) 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 Int64) HasAll(items ...int64) 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 Int64) HasAny(items ...int64) 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 Int64) Difference(s2 Int64) Int64 {
  92. result := NewInt64()
  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 Int64) Union(s2 Int64) Int64 {
  107. result := NewInt64()
  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 Int64) Intersection(s2 Int64) Int64 {
  122. var walk, other Int64
  123. result := NewInt64()
  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 Int64) IsSuperset(s2 Int64) 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 Int64) Equal(s2 Int64) bool {
  151. return len(s1) == len(s2) && s1.IsSuperset(s2)
  152. }
  153. type sortableSliceOfInt64 []int64
  154. func (s sortableSliceOfInt64) Len() int { return len(s) }
  155. func (s sortableSliceOfInt64) Less(i, j int) bool { return lessInt64(s[i], s[j]) }
  156. func (s sortableSliceOfInt64) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
  157. // List returns the contents as a sorted int64 slice.
  158. func (s Int64) List() []int64 {
  159. res := make(sortableSliceOfInt64, 0, len(s))
  160. for key := range s {
  161. res = append(res, key)
  162. }
  163. sort.Sort(res)
  164. return []int64(res)
  165. }
  166. // UnsortedList returns the slice with contents in random order.
  167. func (s Int64) UnsortedList() []int64 {
  168. res := make([]int64, 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 Int64) PopAny() (int64, bool) {
  176. for key := range s {
  177. s.Delete(key)
  178. return key, true
  179. }
  180. var zeroValue int64
  181. return zeroValue, false
  182. }
  183. // Len returns the size of the set.
  184. func (s Int64) Len() int {
  185. return len(s)
  186. }
  187. func lessInt64(lhs, rhs int64) bool {
  188. return lhs < rhs
  189. }