Skip to content

Commit 543b3d7

Browse files
flrdvdeckarep
authored andcommitted
codestyle improvements
renamed method receivers to be a single-letter equal to a first letter in object's name, as community recommends
1 parent 7aad8e9 commit 543b3d7

File tree

1 file changed

+97
-98
lines changed

1 file changed

+97
-98
lines changed

threadsafe.go

Lines changed: 97 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -32,229 +32,228 @@ type threadSafeSet[T comparable] struct {
3232
uss threadUnsafeSet[T]
3333
}
3434

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](),
3938
}
4039
}
4140

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()
4645
return ret
4746
}
4847

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()
5352
return ret
5453
}
5554

56-
func (s *threadSafeSet[T]) IsSubset(other Set[T]) bool {
55+
func (t *threadSafeSet[T]) IsSubset(other Set[T]) bool {
5756
o := other.(*threadSafeSet[T])
5857

59-
s.RLock()
58+
t.RLock()
6059
o.RLock()
6160

62-
ret := s.uss.IsSubset(&o.uss)
63-
s.RUnlock()
61+
ret := t.uss.IsSubset(o.uss)
62+
t.RUnlock()
6463
o.RUnlock()
6564
return ret
6665
}
6766

68-
func (s *threadSafeSet[T]) IsProperSubset(other Set[T]) bool {
67+
func (t *threadSafeSet[T]) IsProperSubset(other Set[T]) bool {
6968
o := other.(*threadSafeSet[T])
7069

71-
s.RLock()
72-
defer s.RUnlock()
70+
t.RLock()
71+
defer t.RUnlock()
7372
o.RLock()
7473
defer o.RUnlock()
7574

76-
return s.uss.IsProperSubset(&o.uss)
75+
return t.uss.IsProperSubset(o.uss)
7776
}
7877

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)
8180
}
8281

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)
8584
}
8685

87-
func (s *threadSafeSet[T]) Union(other Set[T]) Set[T] {
86+
func (t *threadSafeSet[T]) Union(other Set[T]) Set[T] {
8887
o := other.(*threadSafeSet[T])
8988

90-
s.RLock()
89+
t.RLock()
9190
o.RLock()
9291

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()
9695
o.RUnlock()
9796
return ret
9897
}
9998

100-
func (s *threadSafeSet[T]) Intersect(other Set[T]) Set[T] {
99+
func (t *threadSafeSet[T]) Intersect(other Set[T]) Set[T] {
101100
o := other.(*threadSafeSet[T])
102101

103-
s.RLock()
102+
t.RLock()
104103
o.RLock()
105104

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()
109108
o.RUnlock()
110109
return ret
111110
}
112111

113-
func (s *threadSafeSet[T]) Difference(other Set[T]) Set[T] {
112+
func (t *threadSafeSet[T]) Difference(other Set[T]) Set[T] {
114113
o := other.(*threadSafeSet[T])
115114

116-
s.RLock()
115+
t.RLock()
117116
o.RLock()
118117

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()
122121
o.RUnlock()
123122
return ret
124123
}
125124

126-
func (s *threadSafeSet[T]) SymmetricDifference(other Set[T]) Set[T] {
125+
func (t *threadSafeSet[T]) SymmetricDifference(other Set[T]) Set[T] {
127126
o := other.(*threadSafeSet[T])
128127

129-
s.RLock()
128+
t.RLock()
130129
o.RLock()
131130

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()
135134
o.RUnlock()
136135
return ret
137136
}
138137

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()
143142
}
144143

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()
149148
}
150149

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)
155154
}
156155

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 {
160159
if cb(elem) {
161160
break
162161
}
163162
}
164-
s.RUnlock()
163+
t.RUnlock()
165164
}
166165

167-
func (s *threadSafeSet[T]) Iter() <-chan T {
166+
func (t *threadSafeSet[T]) Iter() <-chan T {
168167
ch := make(chan T)
169168
go func() {
170-
s.RLock()
169+
t.RLock()
171170

172-
for elem := range s.uss {
171+
for elem := range t.uss {
173172
ch <- elem
174173
}
175174
close(ch)
176-
s.RUnlock()
175+
t.RUnlock()
177176
}()
178177

179178
return ch
180179
}
181180

182-
func (s *threadSafeSet[T]) Iterator() *Iterator[T] {
181+
func (t *threadSafeSet[T]) Iterator() *Iterator[T] {
183182
iterator, ch, stopCh := newIterator[T]()
184183

185184
go func() {
186-
s.RLock()
185+
t.RLock()
187186
L:
188-
for elem := range s.uss {
187+
for elem := range t.uss {
189188
select {
190189
case <-stopCh:
191190
break L
192191
case ch <- elem:
193192
}
194193
}
195194
close(ch)
196-
s.RUnlock()
195+
t.RUnlock()
197196
}()
198197

199198
return iterator
200199
}
201200

202-
func (s *threadSafeSet[T]) Equal(other Set[T]) bool {
201+
func (t *threadSafeSet[T]) Equal(other Set[T]) bool {
203202
o := other.(*threadSafeSet[T])
204203

205-
s.RLock()
204+
t.RLock()
206205
o.RLock()
207206

208-
ret := s.uss.Equal(&o.uss)
209-
s.RUnlock()
207+
ret := t.uss.Equal(o.uss)
208+
t.RUnlock()
210209
o.RUnlock()
211210
return ret
212211
}
213212

214-
func (s *threadSafeSet[T]) Clone() Set[T] {
215-
s.RLock()
213+
func (t *threadSafeSet[T]) Clone() Set[T] {
214+
t.RLock()
216215

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()
220219
return ret
221220
}
222221

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()
227226
return ret
228227
}
229228

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()
234233
}
235234

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 {
240239
keys = append(keys, elem)
241240
}
242-
s.RUnlock()
241+
t.RUnlock()
243242
return keys
244243
}
245244

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()
250249

251250
return b, err
252251
}
253252

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()
258257

259258
return err
260259
}

0 commit comments

Comments
 (0)