set.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. The default
  27. // implementation is safe for concurrent access. There is a non-threadsafe
  28. // implementation which is slightly more performant.
  29. package mapset
  30. type Set interface {
  31. // Adds an element to the set. Returns whether
  32. // the item was added.
  33. Add(i interface{}) bool
  34. // Returns the number of elements in the set.
  35. Cardinality() int
  36. // Removes all elements from the set, leaving
  37. // the emtpy set.
  38. Clear()
  39. // Returns a clone of the set using the same
  40. // implementation, duplicating all keys.
  41. Clone() Set
  42. // Returns whether the given items
  43. // are all in the set.
  44. Contains(i ...interface{}) bool
  45. // Returns the difference between this set
  46. // and other. The returned set will contain
  47. // all elements of this set that are not also
  48. // elements of other.
  49. //
  50. // Note that the argument to Difference
  51. // must be of the same type as the receiver
  52. // of the method. Otherwise, Difference will
  53. // panic.
  54. Difference(other Set) Set
  55. // Determines if two sets are equal to each
  56. // other. If they have the same cardinality
  57. // and contain the same elements, they are
  58. // considered equal. The order in which
  59. // the elements were added is irrelevant.
  60. //
  61. // Note that the argument to Equal must be
  62. // of the same type as the receiver of the
  63. // method. Otherwise, Equal will panic.
  64. Equal(other Set) bool
  65. // Returns a new set containing only the elements
  66. // that exist only in both sets.
  67. //
  68. // Note that the argument to Intersect
  69. // must be of the same type as the receiver
  70. // of the method. Otherwise, Intersect will
  71. // panic.
  72. Intersect(other Set) Set
  73. // Determines if every element in the other set
  74. // is in this set.
  75. //
  76. // Note that the argument to IsSubset
  77. // must be of the same type as the receiver
  78. // of the method. Otherwise, IsSubset will
  79. // panic.
  80. IsSubset(other Set) bool
  81. // Determines if every element in this set is in
  82. // the other set.
  83. //
  84. // Note that the argument to IsSuperset
  85. // must be of the same type as the receiver
  86. // of the method. Otherwise, IsSuperset will
  87. // panic.
  88. IsSuperset(other Set) bool
  89. // Returns a channel of elements that you can
  90. // range over.
  91. Iter() <-chan interface{}
  92. // Remove a single element from the set.
  93. Remove(i interface{})
  94. // Provides a convenient string representation
  95. // of the current state of the set.
  96. String() string
  97. // Returns a new set with all elements which are
  98. // in either this set or the other set but not in both.
  99. //
  100. // Note that the argument to SymmetricDifference
  101. // must be of the same type as the receiver
  102. // of the method. Otherwise, SymmetricDifference
  103. // will panic.
  104. SymmetricDifference(other Set) Set
  105. // Returns a new set with all elements in both sets.
  106. //
  107. // Note that the argument to Union must be of the
  108. // same type as the receiver of the method.
  109. // Otherwise, IsSuperset will panic.
  110. Union(other Set) Set
  111. // Returns all subsets of a given set (Power Set).
  112. PowerSet() Set
  113. // Returns the Cartesian Product of two sets.
  114. CartesianProduct(other Set) Set
  115. // Returns the members of the set as a slice.
  116. ToSlice() []interface{}
  117. }
  118. // Creates and returns a reference to an empty set.
  119. func NewSet() Set {
  120. set := newThreadSafeSet()
  121. return &set
  122. }
  123. // Creates and returns a reference to a set from an existing slice
  124. func NewSetFromSlice(s []interface{}) Set {
  125. a := NewSet()
  126. for _, item := range s {
  127. a.Add(item)
  128. }
  129. return a
  130. }
  131. func NewThreadUnsafeSet() Set {
  132. set := newThreadUnsafeSet()
  133. return &set
  134. }
  135. func NewThreadUnsafeSetFromSlice(s []interface{}) Set {
  136. a := NewThreadUnsafeSet()
  137. for _, item := range s {
  138. a.Add(item)
  139. }
  140. return a
  141. }