threadsafe.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 "sync"
  23. type threadSafeSet struct {
  24. s threadUnsafeSet
  25. sync.RWMutex
  26. }
  27. func newThreadSafeSet() threadSafeSet {
  28. return threadSafeSet{s: newThreadUnsafeSet()}
  29. }
  30. func (set *threadSafeSet) Add(i interface{}) bool {
  31. set.Lock()
  32. ret := set.s.Add(i)
  33. set.Unlock()
  34. return ret
  35. }
  36. func (set *threadSafeSet) Contains(i ...interface{}) bool {
  37. set.RLock()
  38. ret := set.s.Contains(i...)
  39. set.RUnlock()
  40. return ret
  41. }
  42. func (set *threadSafeSet) IsSubset(other Set) bool {
  43. o := other.(*threadSafeSet)
  44. set.RLock()
  45. o.RLock()
  46. ret := set.s.IsSubset(&o.s)
  47. set.RUnlock()
  48. o.RUnlock()
  49. return ret
  50. }
  51. func (set *threadSafeSet) IsProperSubset(other Set) bool {
  52. o := other.(*threadSafeSet)
  53. set.RLock()
  54. defer set.RUnlock()
  55. o.RLock()
  56. defer o.RUnlock()
  57. return set.s.IsProperSubset(&o.s)
  58. }
  59. func (set *threadSafeSet) IsSuperset(other Set) bool {
  60. return other.IsSubset(set)
  61. }
  62. func (set *threadSafeSet) IsProperSuperset(other Set) bool {
  63. return other.IsProperSubset(set)
  64. }
  65. func (set *threadSafeSet) Union(other Set) Set {
  66. o := other.(*threadSafeSet)
  67. set.RLock()
  68. o.RLock()
  69. unsafeUnion := set.s.Union(&o.s).(*threadUnsafeSet)
  70. ret := &threadSafeSet{s: *unsafeUnion}
  71. set.RUnlock()
  72. o.RUnlock()
  73. return ret
  74. }
  75. func (set *threadSafeSet) Intersect(other Set) Set {
  76. o := other.(*threadSafeSet)
  77. set.RLock()
  78. o.RLock()
  79. unsafeIntersection := set.s.Intersect(&o.s).(*threadUnsafeSet)
  80. ret := &threadSafeSet{s: *unsafeIntersection}
  81. set.RUnlock()
  82. o.RUnlock()
  83. return ret
  84. }
  85. func (set *threadSafeSet) Difference(other Set) Set {
  86. o := other.(*threadSafeSet)
  87. set.RLock()
  88. o.RLock()
  89. unsafeDifference := set.s.Difference(&o.s).(*threadUnsafeSet)
  90. ret := &threadSafeSet{s: *unsafeDifference}
  91. set.RUnlock()
  92. o.RUnlock()
  93. return ret
  94. }
  95. func (set *threadSafeSet) SymmetricDifference(other Set) Set {
  96. o := other.(*threadSafeSet)
  97. set.RLock()
  98. o.RLock()
  99. unsafeDifference := set.s.SymmetricDifference(&o.s).(*threadUnsafeSet)
  100. ret := &threadSafeSet{s: *unsafeDifference}
  101. set.RUnlock()
  102. o.RUnlock()
  103. return ret
  104. }
  105. func (set *threadSafeSet) Clear() {
  106. set.Lock()
  107. set.s = newThreadUnsafeSet()
  108. set.Unlock()
  109. }
  110. func (set *threadSafeSet) Remove(i interface{}) {
  111. set.Lock()
  112. delete(set.s, i)
  113. set.Unlock()
  114. }
  115. func (set *threadSafeSet) Cardinality() int {
  116. set.RLock()
  117. defer set.RUnlock()
  118. return len(set.s)
  119. }
  120. func (set *threadSafeSet) Each(cb func(interface{}) bool) {
  121. set.RLock()
  122. for elem := range set.s {
  123. if cb(elem) {
  124. break
  125. }
  126. }
  127. set.RUnlock()
  128. }
  129. func (set *threadSafeSet) Iter() <-chan interface{} {
  130. ch := make(chan interface{})
  131. go func() {
  132. set.RLock()
  133. for elem := range set.s {
  134. ch <- elem
  135. }
  136. close(ch)
  137. set.RUnlock()
  138. }()
  139. return ch
  140. }
  141. func (set *threadSafeSet) Iterator() *Iterator {
  142. iterator, ch, stopCh := newIterator()
  143. go func() {
  144. set.RLock()
  145. L:
  146. for elem := range set.s {
  147. select {
  148. case <-stopCh:
  149. break L
  150. case ch <- elem:
  151. }
  152. }
  153. close(ch)
  154. set.RUnlock()
  155. }()
  156. return iterator
  157. }
  158. func (set *threadSafeSet) Equal(other Set) bool {
  159. o := other.(*threadSafeSet)
  160. set.RLock()
  161. o.RLock()
  162. ret := set.s.Equal(&o.s)
  163. set.RUnlock()
  164. o.RUnlock()
  165. return ret
  166. }
  167. func (set *threadSafeSet) Clone() Set {
  168. set.RLock()
  169. unsafeClone := set.s.Clone().(*threadUnsafeSet)
  170. ret := &threadSafeSet{s: *unsafeClone}
  171. set.RUnlock()
  172. return ret
  173. }
  174. func (set *threadSafeSet) String() string {
  175. set.RLock()
  176. ret := set.s.String()
  177. set.RUnlock()
  178. return ret
  179. }
  180. func (set *threadSafeSet) PowerSet() Set {
  181. set.RLock()
  182. unsafePowerSet := set.s.PowerSet().(*threadUnsafeSet)
  183. set.RUnlock()
  184. ret := &threadSafeSet{s: newThreadUnsafeSet()}
  185. for subset := range unsafePowerSet.Iter() {
  186. unsafeSubset := subset.(*threadUnsafeSet)
  187. ret.Add(&threadSafeSet{s: *unsafeSubset})
  188. }
  189. return ret
  190. }
  191. func (set *threadSafeSet) Pop() interface{} {
  192. set.Lock()
  193. defer set.Unlock()
  194. return set.s.Pop()
  195. }
  196. func (set *threadSafeSet) CartesianProduct(other Set) Set {
  197. o := other.(*threadSafeSet)
  198. set.RLock()
  199. o.RLock()
  200. unsafeCartProduct := set.s.CartesianProduct(&o.s).(*threadUnsafeSet)
  201. ret := &threadSafeSet{s: *unsafeCartProduct}
  202. set.RUnlock()
  203. o.RUnlock()
  204. return ret
  205. }
  206. func (set *threadSafeSet) ToSlice() []interface{} {
  207. keys := make([]interface{}, 0, set.Cardinality())
  208. set.RLock()
  209. for elem := range set.s {
  210. keys = append(keys, elem)
  211. }
  212. set.RUnlock()
  213. return keys
  214. }
  215. func (set *threadSafeSet) MarshalJSON() ([]byte, error) {
  216. set.RLock()
  217. b, err := set.s.MarshalJSON()
  218. set.RUnlock()
  219. return b, err
  220. }
  221. func (set *threadSafeSet) UnmarshalJSON(p []byte) error {
  222. set.RLock()
  223. err := set.s.UnmarshalJSON(p)
  224. set.RUnlock()
  225. return err
  226. }