set.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. Copyright 2022 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. package sets
  14. import (
  15. "sort"
  16. )
  17. // Set is a set of the same type elements, implemented via map[comparable]struct{} for minimal memory consumption.
  18. type Set[T comparable] map[T]Empty
  19. // cast transforms specified set to generic Set[T].
  20. func cast[T comparable](s map[T]Empty) Set[T] { return s }
  21. // New creates a Set from a list of values.
  22. // NOTE: type param must be explicitly instantiated if given items are empty.
  23. func New[T comparable](items ...T) Set[T] {
  24. ss := make(Set[T], len(items))
  25. ss.Insert(items...)
  26. return ss
  27. }
  28. // KeySet creates a Set from a keys of a map[comparable](? extends interface{}).
  29. // If the value passed in is not actually a map, this will panic.
  30. func KeySet[T comparable, V any](theMap map[T]V) Set[T] {
  31. ret := Set[T]{}
  32. for keyValue := range theMap {
  33. ret.Insert(keyValue)
  34. }
  35. return ret
  36. }
  37. // Insert adds items to the set.
  38. func (s Set[T]) Insert(items ...T) Set[T] {
  39. for _, item := range items {
  40. s[item] = Empty{}
  41. }
  42. return s
  43. }
  44. func Insert[T comparable](set Set[T], items ...T) Set[T] {
  45. return set.Insert(items...)
  46. }
  47. // Delete removes all items from the set.
  48. func (s Set[T]) Delete(items ...T) Set[T] {
  49. for _, item := range items {
  50. delete(s, item)
  51. }
  52. return s
  53. }
  54. // Has returns true if and only if item is contained in the set.
  55. func (s Set[T]) Has(item T) bool {
  56. _, contained := s[item]
  57. return contained
  58. }
  59. // HasAll returns true if and only if all items are contained in the set.
  60. func (s Set[T]) HasAll(items ...T) bool {
  61. for _, item := range items {
  62. if !s.Has(item) {
  63. return false
  64. }
  65. }
  66. return true
  67. }
  68. // HasAny returns true if any items are contained in the set.
  69. func (s Set[T]) HasAny(items ...T) bool {
  70. for _, item := range items {
  71. if s.Has(item) {
  72. return true
  73. }
  74. }
  75. return false
  76. }
  77. // Clone returns a new set which is a copy of the current set.
  78. func (s Set[T]) Clone() Set[T] {
  79. result := make(Set[T], len(s))
  80. for key := range s {
  81. result.Insert(key)
  82. }
  83. return result
  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 (s1 Set[T]) Difference(s2 Set[T]) Set[T] {
  92. result := New[T]()
  93. for key := range s1 {
  94. if !s2.Has(key) {
  95. result.Insert(key)
  96. }
  97. }
  98. return result
  99. }
  100. // SymmetricDifference returns a set of elements which are in either of the sets, but not in their intersection.
  101. // For example:
  102. // s1 = {a1, a2, a3}
  103. // s2 = {a1, a2, a4, a5}
  104. // s1.SymmetricDifference(s2) = {a3, a4, a5}
  105. // s2.SymmetricDifference(s1) = {a3, a4, a5}
  106. func (s1 Set[T]) SymmetricDifference(s2 Set[T]) Set[T] {
  107. return s1.Difference(s2).Union(s2.Difference(s1))
  108. }
  109. // Union returns a new set which includes items in either s1 or s2.
  110. // For example:
  111. // s1 = {a1, a2}
  112. // s2 = {a3, a4}
  113. // s1.Union(s2) = {a1, a2, a3, a4}
  114. // s2.Union(s1) = {a1, a2, a3, a4}
  115. func (s1 Set[T]) Union(s2 Set[T]) Set[T] {
  116. result := s1.Clone()
  117. for key := range s2 {
  118. result.Insert(key)
  119. }
  120. return result
  121. }
  122. // Intersection returns a new set which includes the item in BOTH s1 and s2
  123. // For example:
  124. // s1 = {a1, a2}
  125. // s2 = {a2, a3}
  126. // s1.Intersection(s2) = {a2}
  127. func (s1 Set[T]) Intersection(s2 Set[T]) Set[T] {
  128. var walk, other Set[T]
  129. result := New[T]()
  130. if s1.Len() < s2.Len() {
  131. walk = s1
  132. other = s2
  133. } else {
  134. walk = s2
  135. other = s1
  136. }
  137. for key := range walk {
  138. if other.Has(key) {
  139. result.Insert(key)
  140. }
  141. }
  142. return result
  143. }
  144. // IsSuperset returns true if and only if s1 is a superset of s2.
  145. func (s1 Set[T]) IsSuperset(s2 Set[T]) bool {
  146. for item := range s2 {
  147. if !s1.Has(item) {
  148. return false
  149. }
  150. }
  151. return true
  152. }
  153. // Equal returns true if and only if s1 is equal (as a set) to s2.
  154. // Two sets are equal if their membership is identical.
  155. // (In practice, this means same elements, order doesn't matter)
  156. func (s1 Set[T]) Equal(s2 Set[T]) bool {
  157. return len(s1) == len(s2) && s1.IsSuperset(s2)
  158. }
  159. type sortableSliceOfGeneric[T ordered] []T
  160. func (g sortableSliceOfGeneric[T]) Len() int { return len(g) }
  161. func (g sortableSliceOfGeneric[T]) Less(i, j int) bool { return less[T](g[i], g[j]) }
  162. func (g sortableSliceOfGeneric[T]) Swap(i, j int) { g[i], g[j] = g[j], g[i] }
  163. // List returns the contents as a sorted T slice.
  164. //
  165. // This is a separate function and not a method because not all types supported
  166. // by Generic are ordered and only those can be sorted.
  167. func List[T ordered](s Set[T]) []T {
  168. res := make(sortableSliceOfGeneric[T], 0, len(s))
  169. for key := range s {
  170. res = append(res, key)
  171. }
  172. sort.Sort(res)
  173. return res
  174. }
  175. // UnsortedList returns the slice with contents in random order.
  176. func (s Set[T]) UnsortedList() []T {
  177. res := make([]T, 0, len(s))
  178. for key := range s {
  179. res = append(res, key)
  180. }
  181. return res
  182. }
  183. // PopAny returns a single element from the set.
  184. func (s Set[T]) PopAny() (T, bool) {
  185. for key := range s {
  186. s.Delete(key)
  187. return key, true
  188. }
  189. var zeroValue T
  190. return zeroValue, false
  191. }
  192. // Len returns the size of the set.
  193. func (s Set[T]) Len() int {
  194. return len(s)
  195. }
  196. func less[T ordered](lhs, rhs T) bool {
  197. return lhs < rhs
  198. }