set.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. Open Source Initiative OSI - The MIT License (MIT):Licensing
  3. The MIT License (MIT)
  4. Copyright (c) 2013 - 2022 Ralph Caraveo (deckarep@gmail.com)
  5. Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. this software and associated documentation files (the "Software"), to deal in
  7. the Software without restriction, including without limitation the rights to
  8. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  9. of the Software, and to permit persons to whom the Software is furnished to do
  10. so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in all
  12. copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  19. SOFTWARE.
  20. */
  21. // Package mapset implements a simple and set collection.
  22. // Items stored within it are unordered and unique. It supports
  23. // typical set operations: membership testing, intersection, union,
  24. // difference, symmetric difference and cloning.
  25. //
  26. // Package mapset provides two implementations of the Set
  27. // interface. The default implementation is safe for concurrent
  28. // access, but a non-thread-safe implementation is also provided for
  29. // programs that can benefit from the slight speed improvement and
  30. // that can enforce mutual exclusion through other means.
  31. package mapset
  32. // Set is the primary interface provided by the mapset package. It
  33. // represents an unordered set of data and a large number of
  34. // operations that can be applied to that set.
  35. type Set[T comparable] interface {
  36. // Add adds an element to the set. Returns whether
  37. // the item was added.
  38. Add(val T) bool
  39. // Append multiple elements to the set. Returns
  40. // the number of elements added.
  41. Append(val ...T) int
  42. // Cardinality returns the number of elements in the set.
  43. Cardinality() int
  44. // Clear removes all elements from the set, leaving
  45. // the empty set.
  46. Clear()
  47. // Clone returns a clone of the set using the same
  48. // implementation, duplicating all keys.
  49. Clone() Set[T]
  50. // Contains returns whether the given items
  51. // are all in the set.
  52. Contains(val ...T) bool
  53. // Difference returns the difference between this set
  54. // and other. The returned set will contain
  55. // all elements of this set that are not also
  56. // elements of other.
  57. //
  58. // Note that the argument to Difference
  59. // must be of the same type as the receiver
  60. // of the method. Otherwise, Difference will
  61. // panic.
  62. Difference(other Set[T]) Set[T]
  63. // Equal determines if two sets are equal to each
  64. // other. If they have the same cardinality
  65. // and contain the same elements, they are
  66. // considered equal. The order in which
  67. // the elements were added is irrelevant.
  68. //
  69. // Note that the argument to Equal must be
  70. // of the same type as the receiver of the
  71. // method. Otherwise, Equal will panic.
  72. Equal(other Set[T]) bool
  73. // Intersect returns a new set containing only the elements
  74. // that exist only in both sets.
  75. //
  76. // Note that the argument to Intersect
  77. // must be of the same type as the receiver
  78. // of the method. Otherwise, Intersect will
  79. // panic.
  80. Intersect(other Set[T]) Set[T]
  81. // IsProperSubset determines if every element in this set is in
  82. // the other set but the two sets are not equal.
  83. //
  84. // Note that the argument to IsProperSubset
  85. // must be of the same type as the receiver
  86. // of the method. Otherwise, IsProperSubset
  87. // will panic.
  88. IsProperSubset(other Set[T]) bool
  89. // IsProperSuperset determines if every element in the other set
  90. // is in this set but the two sets are not
  91. // equal.
  92. //
  93. // Note that the argument to IsSuperset
  94. // must be of the same type as the receiver
  95. // of the method. Otherwise, IsSuperset will
  96. // panic.
  97. IsProperSuperset(other Set[T]) bool
  98. // IsSubset determines if every element in this set is in
  99. // the other set.
  100. //
  101. // Note that the argument to IsSubset
  102. // must be of the same type as the receiver
  103. // of the method. Otherwise, IsSubset will
  104. // panic.
  105. IsSubset(other Set[T]) bool
  106. // IsSuperset determines if every element in the other set
  107. // is in this set.
  108. //
  109. // Note that the argument to IsSuperset
  110. // must be of the same type as the receiver
  111. // of the method. Otherwise, IsSuperset will
  112. // panic.
  113. IsSuperset(other Set[T]) bool
  114. // Each iterates over elements and executes the passed func against each element.
  115. // If passed func returns true, stop iteration at the time.
  116. Each(func(T) bool)
  117. // Iter returns a channel of elements that you can
  118. // range over.
  119. Iter() <-chan T
  120. // Iterator returns an Iterator object that you can
  121. // use to range over the set.
  122. Iterator() *Iterator[T]
  123. // Remove removes a single element from the set.
  124. Remove(i T)
  125. // RemoveAll removes multiple elements from the set.
  126. RemoveAll(i ...T)
  127. // String provides a convenient string representation
  128. // of the current state of the set.
  129. String() string
  130. // SymmetricDifference returns a new set with all elements which are
  131. // in either this set or the other set but not in both.
  132. //
  133. // Note that the argument to SymmetricDifference
  134. // must be of the same type as the receiver
  135. // of the method. Otherwise, SymmetricDifference
  136. // will panic.
  137. SymmetricDifference(other Set[T]) Set[T]
  138. // Union returns a new set with all elements in both sets.
  139. //
  140. // Note that the argument to Union must be of the
  141. // same type as the receiver of the method.
  142. // Otherwise, IsSuperset will panic.
  143. Union(other Set[T]) Set[T]
  144. // Pop removes and returns an arbitrary item from the set.
  145. Pop() (T, bool)
  146. // ToSlice returns the members of the set as a slice.
  147. ToSlice() []T
  148. // MarshalJSON will marshal the set into a JSON-based representation.
  149. MarshalJSON() ([]byte, error)
  150. // UnmarshalJSON will unmarshal a JSON-based byte slice into a full Set datastructure.
  151. // For this to work, set subtypes must implemented the Marshal/Unmarshal interface.
  152. UnmarshalJSON(b []byte) error
  153. }
  154. // NewSet creates and returns a new set with the given elements.
  155. // Operations on the resulting set are thread-safe.
  156. func NewSet[T comparable](vals ...T) Set[T] {
  157. s := newThreadSafeSetWithSize[T](len(vals))
  158. for _, item := range vals {
  159. s.Add(item)
  160. }
  161. return s
  162. }
  163. // NewSetWithSize creates and returns a reference to an empty set with a specified
  164. // capacity. Operations on the resulting set are thread-safe.
  165. func NewSetWithSize[T comparable](cardinality int) Set[T] {
  166. s := newThreadSafeSetWithSize[T](cardinality)
  167. return s
  168. }
  169. // NewThreadUnsafeSet creates and returns a new set with the given elements.
  170. // Operations on the resulting set are not thread-safe.
  171. func NewThreadUnsafeSet[T comparable](vals ...T) Set[T] {
  172. s := newThreadUnsafeSetWithSize[T](len(vals))
  173. for _, item := range vals {
  174. s.Add(item)
  175. }
  176. return s
  177. }
  178. // NewThreadUnsafeSetWithSize creates and returns a reference to an empty set with
  179. // a specified capacity. Operations on the resulting set are not thread-safe.
  180. func NewThreadUnsafeSetWithSize[T comparable](cardinality int) Set[T] {
  181. s := newThreadUnsafeSetWithSize[T](cardinality)
  182. return s
  183. }
  184. // NewSetFromMapKeys creates and returns a new set with the given keys of the map.
  185. // Operations on the resulting set are thread-safe.
  186. func NewSetFromMapKeys[T comparable, V any](val map[T]V) Set[T] {
  187. s := NewSetWithSize[T](len(val))
  188. for k := range val {
  189. s.Add(k)
  190. }
  191. return s
  192. }
  193. // NewThreadUnsafeSetFromMapKeys creates and returns a new set with the given keys of the map.
  194. // Operations on the resulting set are not thread-safe.
  195. func NewThreadUnsafeSetFromMapKeys[T comparable, V any](val map[T]V) Set[T] {
  196. s := NewThreadUnsafeSetWithSize[T](len(val))
  197. for k := range val {
  198. s.Add(k)
  199. }
  200. return s
  201. }