set.go 5.3 KB

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