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