Skip to content

Commit e00dc23

Browse files
authored
Use pointer receivers for unsafeset (#143)
* Revert "less pointers and more memory-efficient Clear()" This reverts commit 3045cfb. * Revert "less pointers" This reverts commit 7aad8e9. * Switch to pointer receivers, fixes UnmarshalJSON * Expand test coverage for JSON marshaler interface
1 parent b710ba4 commit e00dc23

File tree

4 files changed

+239
-83
lines changed

4 files changed

+239
-83
lines changed

Diff for: threadsafe.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import "sync"
2929

3030
type threadSafeSet[T comparable] struct {
3131
sync.RWMutex
32-
uss threadUnsafeSet[T]
32+
uss *threadUnsafeSet[T]
3333
}
3434

3535
func newThreadSafeSet[T comparable]() *threadSafeSet[T] {
@@ -123,7 +123,7 @@ func (t *threadSafeSet[T]) Union(other Set[T]) Set[T] {
123123
t.RLock()
124124
o.RLock()
125125

126-
unsafeUnion := t.uss.Union(o.uss).(threadUnsafeSet[T])
126+
unsafeUnion := t.uss.Union(o.uss).(*threadUnsafeSet[T])
127127
ret := &threadSafeSet[T]{uss: unsafeUnion}
128128
t.RUnlock()
129129
o.RUnlock()
@@ -136,7 +136,7 @@ func (t *threadSafeSet[T]) Intersect(other Set[T]) Set[T] {
136136
t.RLock()
137137
o.RLock()
138138

139-
unsafeIntersection := t.uss.Intersect(o.uss).(threadUnsafeSet[T])
139+
unsafeIntersection := t.uss.Intersect(o.uss).(*threadUnsafeSet[T])
140140
ret := &threadSafeSet[T]{uss: unsafeIntersection}
141141
t.RUnlock()
142142
o.RUnlock()
@@ -149,7 +149,7 @@ func (t *threadSafeSet[T]) Difference(other Set[T]) Set[T] {
149149
t.RLock()
150150
o.RLock()
151151

152-
unsafeDifference := t.uss.Difference(o.uss).(threadUnsafeSet[T])
152+
unsafeDifference := t.uss.Difference(o.uss).(*threadUnsafeSet[T])
153153
ret := &threadSafeSet[T]{uss: unsafeDifference}
154154
t.RUnlock()
155155
o.RUnlock()
@@ -162,7 +162,7 @@ func (t *threadSafeSet[T]) SymmetricDifference(other Set[T]) Set[T] {
162162
t.RLock()
163163
o.RLock()
164164

165-
unsafeDifference := t.uss.SymmetricDifference(o.uss).(threadUnsafeSet[T])
165+
unsafeDifference := t.uss.SymmetricDifference(o.uss).(*threadUnsafeSet[T])
166166
ret := &threadSafeSet[T]{uss: unsafeDifference}
167167
t.RUnlock()
168168
o.RUnlock()
@@ -177,7 +177,7 @@ func (t *threadSafeSet[T]) Clear() {
177177

178178
func (t *threadSafeSet[T]) Remove(v T) {
179179
t.Lock()
180-
delete(t.uss, v)
180+
delete(*t.uss, v)
181181
t.Unlock()
182182
}
183183

@@ -190,12 +190,12 @@ func (t *threadSafeSet[T]) RemoveAll(i ...T) {
190190
func (t *threadSafeSet[T]) Cardinality() int {
191191
t.RLock()
192192
defer t.RUnlock()
193-
return len(t.uss)
193+
return len(*t.uss)
194194
}
195195

196196
func (t *threadSafeSet[T]) Each(cb func(T) bool) {
197197
t.RLock()
198-
for elem := range t.uss {
198+
for elem := range *t.uss {
199199
if cb(elem) {
200200
break
201201
}
@@ -208,7 +208,7 @@ func (t *threadSafeSet[T]) Iter() <-chan T {
208208
go func() {
209209
t.RLock()
210210

211-
for elem := range t.uss {
211+
for elem := range *t.uss {
212212
ch <- elem
213213
}
214214
close(ch)
@@ -224,7 +224,7 @@ func (t *threadSafeSet[T]) Iterator() *Iterator[T] {
224224
go func() {
225225
t.RLock()
226226
L:
227-
for elem := range t.uss {
227+
for elem := range *t.uss {
228228
select {
229229
case <-stopCh:
230230
break L
@@ -253,7 +253,7 @@ func (t *threadSafeSet[T]) Equal(other Set[T]) bool {
253253
func (t *threadSafeSet[T]) Clone() Set[T] {
254254
t.RLock()
255255

256-
unsafeClone := t.uss.Clone().(threadUnsafeSet[T])
256+
unsafeClone := t.uss.Clone().(*threadUnsafeSet[T])
257257
ret := &threadSafeSet[T]{uss: unsafeClone}
258258
t.RUnlock()
259259
return ret
@@ -275,7 +275,7 @@ func (t *threadSafeSet[T]) Pop() (T, bool) {
275275
func (t *threadSafeSet[T]) ToSlice() []T {
276276
keys := make([]T, 0, t.Cardinality())
277277
t.RLock()
278-
for elem := range t.uss {
278+
for elem := range *t.uss {
279279
keys = append(keys, elem)
280280
}
281281
t.RUnlock()

Diff for: threadsafe_test.go

+1-11
Original file line numberDiff line numberDiff line change
@@ -584,17 +584,7 @@ func Test_UnmarshalJSON(t *testing.T) {
584584
t.Errorf("Expected no difference, got: %v", expected.Difference(actual))
585585
}
586586
}
587-
func TestThreadUnsafeSet_UnmarshalJSON(t *testing.T) {
588-
expected := NewThreadUnsafeSet[int64](1, 2, 3)
589-
actual := NewThreadUnsafeSet[int64]()
590-
err := actual.UnmarshalJSON([]byte(`[1, 2, 3]`))
591-
if err != nil {
592-
t.Errorf("Error should be nil: %v", err)
593-
}
594-
if !expected.Equal(actual) {
595-
t.Errorf("Expected no difference, got: %v", expected.Difference(actual))
596-
}
597-
}
587+
598588
func Test_MarshalJSON(t *testing.T) {
599589
expected := NewSet(
600590
[]string{

0 commit comments

Comments
 (0)