set.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. Open Source Initiative OSI - The MIT License (MIT):Licensing
  3. The MIT License (MIT)
  4. Copyright (c) 2013 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 generic 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 interface {
  36. // Adds an element to the set. Returns whether
  37. // the item was added.
  38. Add(i interface{}) bool
  39. // Returns the number of elements in the set.
  40. Cardinality() int
  41. // Removes all elements from the set, leaving
  42. // the empty set.
  43. Clear()
  44. // Returns a clone of the set using the same
  45. // implementation, duplicating all keys.
  46. Clone() Set
  47. // Returns whether the given items
  48. // are all in the set.
  49. Contains(i ...interface{}) bool
  50. // Returns the difference between this set
  51. // and other. The returned set will contain
  52. // all elements of this set that are not also
  53. // elements of other.
  54. //
  55. // Note that the argument to Difference
  56. // must be of the same type as the receiver
  57. // of the method. Otherwise, Difference will
  58. // panic.
  59. Difference(other Set) Set
  60. // Determines if two sets are equal to each
  61. // other. If they have the same cardinality
  62. // and contain the same elements, they are
  63. // considered equal. The order in which
  64. // the elements were added is irrelevant.
  65. //
  66. // Note that the argument to Equal must be
  67. // of the same type as the receiver of the
  68. // method. Otherwise, Equal will panic.
  69. Equal(other Set) bool
  70. // Returns a new set containing only the elements
  71. // that exist only in both sets.
  72. //
  73. // Note that the argument to Intersect
  74. // must be of the same type as the receiver
  75. // of the method. Otherwise, Intersect will
  76. // panic.
  77. Intersect(other Set) Set
  78. // Determines if every element in this set is in
  79. // the other set but the two sets are not equal.
  80. //
  81. // Note that the argument to IsProperSubset
  82. // must be of the same type as the receiver
  83. // of the method. Otherwise, IsProperSubset
  84. // will panic.
  85. IsProperSubset(other Set) bool
  86. // Determines if every element in the other set
  87. // is in this set but the two sets are not
  88. // equal.
  89. //
  90. // Note that the argument to IsSuperset
  91. // must be of the same type as the receiver
  92. // of the method. Otherwise, IsSuperset will
  93. // panic.
  94. IsProperSuperset(other Set) bool
  95. // Determines if every element in this set is in
  96. // the other set.
  97. //
  98. // Note that the argument to IsSubset
  99. // must be of the same type as the receiver
  100. // of the method. Otherwise, IsSubset will
  101. // panic.
  102. IsSubset(other Set) bool
  103. // Determines if every element in the other set
  104. // is in this set.
  105. //
  106. // Note that the argument to IsSuperset
  107. // must be of the same type as the receiver
  108. // of the method. Otherwise, IsSuperset will
  109. // panic.
  110. IsSuperset(other Set) bool
  111. // Iterates over elements and executes the passed func against each element.
  112. // If passed func returns true, stop iteration at the time.
  113. Each(func(interface{}) bool)
  114. // Returns a channel of elements that you can
  115. // range over.
  116. Iter() <-chan interface{}
  117. // Returns an Iterator object that you can
  118. // use to range over the set.
  119. Iterator() *Iterator
  120. // Remove a single element from the set.
  121. Remove(i interface{})
  122. // Provides a convenient string representation
  123. // of the current state of the set.
  124. String() string
  125. // Returns a new set with all elements which are
  126. // in either this set or the other set but not in both.
  127. //
  128. // Note that the argument to SymmetricDifference
  129. // must be of the same type as the receiver
  130. // of the method. Otherwise, SymmetricDifference
  131. // will panic.
  132. SymmetricDifference(other Set) Set
  133. // Returns a new set with all elements in both sets.
  134. //
  135. // Note that the argument to Union must be of the
  136. // same type as the receiver of the method.
  137. // Otherwise, IsSuperset will panic.
  138. Union(other Set) Set
  139. // Pop removes and returns an arbitrary item from the set.
  140. Pop() interface{}
  141. // Returns all subsets of a given set (Power Set).
  142. PowerSet() Set
  143. // Returns the Cartesian Product of two sets.
  144. CartesianProduct(other Set) Set
  145. // Returns the members of the set as a slice.
  146. ToSlice() []interface{}
  147. }
  148. // NewSet creates and returns a reference to an empty set. Operations
  149. // on the resulting set are thread-safe.
  150. func NewSet(s ...interface{}) Set {
  151. set := newThreadSafeSet()
  152. for _, item := range s {
  153. set.Add(item)
  154. }
  155. return &set
  156. }
  157. // NewSetWith creates and returns a new set with the given elements.
  158. // Operations on the resulting set are thread-safe.
  159. func NewSetWith(elts ...interface{}) Set {
  160. return NewSetFromSlice(elts)
  161. }
  162. // NewSetFromSlice creates and returns a reference to a set from an
  163. // existing slice. Operations on the resulting set are thread-safe.
  164. func NewSetFromSlice(s []interface{}) Set {
  165. a := NewSet(s...)
  166. return a
  167. }
  168. // NewThreadUnsafeSet creates and returns a reference to an empty set.
  169. // Operations on the resulting set are not thread-safe.
  170. func NewThreadUnsafeSet() Set {
  171. set := newThreadUnsafeSet()
  172. return &set
  173. }
  174. // NewThreadUnsafeSetFromSlice creates and returns a reference to a
  175. // set from an existing slice. Operations on the resulting set are
  176. // not thread-safe.
  177. func NewThreadUnsafeSetFromSlice(s []interface{}) Set {
  178. a := NewThreadUnsafeSet()
  179. for _, item := range s {
  180. a.Add(item)
  181. }
  182. return a
  183. }