threadsafe.go 5.4 KB

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