threadsafe.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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) IsSuperset(other Set) bool {
  52. return other.IsSubset(set)
  53. }
  54. func (set *threadSafeSet) Union(other Set) Set {
  55. o := other.(*threadSafeSet)
  56. set.RLock()
  57. o.RLock()
  58. unsafeUnion := set.s.Union(&o.s).(*threadUnsafeSet)
  59. ret := &threadSafeSet{s: *unsafeUnion}
  60. set.RUnlock()
  61. o.RUnlock()
  62. return ret
  63. }
  64. func (set *threadSafeSet) Intersect(other Set) Set {
  65. o := other.(*threadSafeSet)
  66. set.RLock()
  67. o.RLock()
  68. unsafeIntersection := set.s.Intersect(&o.s).(*threadUnsafeSet)
  69. ret := &threadSafeSet{s: *unsafeIntersection}
  70. set.RUnlock()
  71. o.RUnlock()
  72. return ret
  73. }
  74. func (set *threadSafeSet) Difference(other Set) Set {
  75. o := other.(*threadSafeSet)
  76. set.RLock()
  77. o.RLock()
  78. unsafeDifference := set.s.Difference(&o.s).(*threadUnsafeSet)
  79. ret := &threadSafeSet{s: *unsafeDifference}
  80. set.RUnlock()
  81. o.RUnlock()
  82. return ret
  83. }
  84. func (set *threadSafeSet) SymmetricDifference(other Set) Set {
  85. o := other.(*threadSafeSet)
  86. unsafeDifference := set.s.SymmetricDifference(&o.s).(*threadUnsafeSet)
  87. return &threadSafeSet{s: *unsafeDifference}
  88. }
  89. func (set *threadSafeSet) Clear() {
  90. set.Lock()
  91. set.s = newThreadUnsafeSet()
  92. set.Unlock()
  93. }
  94. func (set *threadSafeSet) Remove(i interface{}) {
  95. set.Lock()
  96. delete(set.s, i)
  97. set.Unlock()
  98. }
  99. func (set *threadSafeSet) Cardinality() int {
  100. set.RLock()
  101. defer set.RUnlock()
  102. return len(set.s)
  103. }
  104. func (set *threadSafeSet) Iter() <-chan interface{} {
  105. ch := make(chan interface{})
  106. go func() {
  107. set.RLock()
  108. for elem := range set.s {
  109. ch <- elem
  110. }
  111. close(ch)
  112. set.RUnlock()
  113. }()
  114. return ch
  115. }
  116. func (set *threadSafeSet) Equal(other Set) bool {
  117. o := other.(*threadSafeSet)
  118. set.RLock()
  119. o.RLock()
  120. ret := set.s.Equal(&o.s)
  121. set.RUnlock()
  122. o.RUnlock()
  123. return ret
  124. }
  125. func (set *threadSafeSet) Clone() Set {
  126. set.RLock()
  127. unsafeClone := set.s.Clone().(*threadUnsafeSet)
  128. ret := &threadSafeSet{s: *unsafeClone}
  129. set.RUnlock()
  130. return ret
  131. }
  132. func (set *threadSafeSet) String() string {
  133. set.RLock()
  134. ret := set.s.String()
  135. set.RUnlock()
  136. return ret
  137. }
  138. func (set *threadSafeSet) PowerSet() Set {
  139. set.RLock()
  140. ret := set.s.PowerSet()
  141. set.RUnlock()
  142. return ret
  143. }
  144. func (set *threadSafeSet) CartesianProduct(other Set) Set {
  145. o := other.(*threadSafeSet)
  146. set.RLock()
  147. o.RLock()
  148. unsafeCartProduct := set.s.CartesianProduct(&o.s).(*threadUnsafeSet)
  149. ret := &threadSafeSet{s: *unsafeCartProduct}
  150. set.RUnlock()
  151. o.RUnlock()
  152. return ret
  153. }
  154. func (set *threadSafeSet) ToSlice() []interface{} {
  155. set.RLock()
  156. keys := make([]interface{}, 0, set.Cardinality())
  157. for elem := range set.s {
  158. keys = append(keys, elem)
  159. }
  160. set.RUnlock()
  161. return keys
  162. }