threadunsafe.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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
  22. import (
  23. "fmt"
  24. "reflect"
  25. "strings"
  26. )
  27. type threadUnsafeSet map[interface{}]struct{}
  28. type orderedPair struct {
  29. first interface{}
  30. second interface{}
  31. }
  32. func newThreadUnsafeSet() threadUnsafeSet {
  33. return make(threadUnsafeSet)
  34. }
  35. func (pair *orderedPair) Equal(other orderedPair) bool {
  36. if pair.first == other.first &&
  37. pair.second == other.second {
  38. return true
  39. }
  40. return false
  41. }
  42. func (set *threadUnsafeSet) Add(i interface{}) bool {
  43. _, found := (*set)[i]
  44. (*set)[i] = struct{}{}
  45. return !found //False if it existed already
  46. }
  47. func (set *threadUnsafeSet) Contains(i ...interface{}) bool {
  48. for _, val := range i {
  49. if _, ok := (*set)[val]; !ok {
  50. return false
  51. }
  52. }
  53. return true
  54. }
  55. func (set *threadUnsafeSet) IsSubset(other Set) bool {
  56. _ = other.(*threadUnsafeSet)
  57. for elem := range *set {
  58. if !other.Contains(elem) {
  59. return false
  60. }
  61. }
  62. return true
  63. }
  64. func (set *threadUnsafeSet) IsSuperset(other Set) bool {
  65. return other.IsSubset(set)
  66. }
  67. func (set *threadUnsafeSet) Union(other Set) Set {
  68. o := other.(*threadUnsafeSet)
  69. unionedSet := newThreadUnsafeSet()
  70. for elem := range *set {
  71. unionedSet.Add(elem)
  72. }
  73. for elem := range *o {
  74. unionedSet.Add(elem)
  75. }
  76. return &unionedSet
  77. }
  78. func (set *threadUnsafeSet) Intersect(other Set) Set {
  79. o := other.(*threadUnsafeSet)
  80. intersection := newThreadUnsafeSet()
  81. // loop over smaller set
  82. if set.Cardinality() < other.Cardinality() {
  83. for elem := range *set {
  84. if other.Contains(elem) {
  85. intersection.Add(elem)
  86. }
  87. }
  88. } else {
  89. for elem := range *o {
  90. if set.Contains(elem) {
  91. intersection.Add(elem)
  92. }
  93. }
  94. }
  95. return &intersection
  96. }
  97. func (set *threadUnsafeSet) Difference(other Set) Set {
  98. _ = other.(*threadUnsafeSet)
  99. difference := newThreadUnsafeSet()
  100. for elem := range *set {
  101. if !other.Contains(elem) {
  102. difference.Add(elem)
  103. }
  104. }
  105. return &difference
  106. }
  107. func (set *threadUnsafeSet) SymmetricDifference(other Set) Set {
  108. _ = other.(*threadUnsafeSet)
  109. aDiff := set.Difference(other)
  110. bDiff := other.Difference(set)
  111. return aDiff.Union(bDiff)
  112. }
  113. func (set *threadUnsafeSet) Clear() {
  114. *set = newThreadUnsafeSet()
  115. }
  116. func (set *threadUnsafeSet) Remove(i interface{}) {
  117. delete(*set, i)
  118. }
  119. func (set *threadUnsafeSet) Cardinality() int {
  120. return len(*set)
  121. }
  122. func (set *threadUnsafeSet) Iter() <-chan interface{} {
  123. ch := make(chan interface{})
  124. go func() {
  125. for elem := range *set {
  126. ch <- elem
  127. }
  128. close(ch)
  129. }()
  130. return ch
  131. }
  132. func (set *threadUnsafeSet) Equal(other Set) bool {
  133. _ = other.(*threadUnsafeSet)
  134. if set.Cardinality() != other.Cardinality() {
  135. return false
  136. }
  137. for elem := range *set {
  138. if !other.Contains(elem) {
  139. return false
  140. }
  141. }
  142. return true
  143. }
  144. func (set *threadUnsafeSet) Clone() Set {
  145. clonedSet := newThreadUnsafeSet()
  146. for elem := range *set {
  147. clonedSet.Add(elem)
  148. }
  149. return &clonedSet
  150. }
  151. func (set *threadUnsafeSet) String() string {
  152. items := make([]string, 0, len(*set))
  153. for elem := range *set {
  154. items = append(items, fmt.Sprintf("%v", elem))
  155. }
  156. return fmt.Sprintf("Set{%s}", strings.Join(items, ", "))
  157. }
  158. func (pair orderedPair) String() string {
  159. return fmt.Sprintf("(%v, %v)", pair.first, pair.second)
  160. }
  161. func (set *threadUnsafeSet) PowerSet() Set {
  162. powSet := NewThreadUnsafeSet()
  163. nullset := newThreadUnsafeSet()
  164. powSet.Add(&nullset)
  165. for es := range *set {
  166. u := newThreadUnsafeSet()
  167. j := powSet.Iter()
  168. for er := range j {
  169. p := newThreadUnsafeSet()
  170. if reflect.TypeOf(er).Name() == "" {
  171. k := er.(*threadUnsafeSet)
  172. for ek := range *(k) {
  173. p.Add(ek)
  174. }
  175. } else {
  176. p.Add(er)
  177. }
  178. p.Add(es)
  179. u.Add(&p)
  180. }
  181. powSet = powSet.Union(&u)
  182. }
  183. return powSet
  184. }
  185. func (set *threadUnsafeSet) CartesianProduct(other Set) Set {
  186. o := other.(*threadUnsafeSet)
  187. cartProduct := NewThreadUnsafeSet()
  188. for i := range *set {
  189. for j := range *o {
  190. elem := orderedPair{first: i, second: j}
  191. cartProduct.Add(elem)
  192. }
  193. }
  194. return cartProduct
  195. }
  196. func (set *threadUnsafeSet) ToSlice() []interface{} {
  197. keys := make([]interface{}, 0, set.Cardinality())
  198. for elem := range *set {
  199. keys = append(keys, elem)
  200. }
  201. return keys
  202. }