Skip to content

Commit 2f3c2ed

Browse files
committed
export threadunsafe
1 parent 7ea38ba commit 2f3c2ed

2 files changed

Lines changed: 52 additions & 52 deletions

File tree

threadsafe.go

Lines changed: 6 additions & 6 deletions
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] {
@@ -136,7 +136,7 @@ func (t *threadSafeSet[T]) Union(other Set[T]) Set[T] {
136136
t.RLock()
137137
o.RLock()
138138

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

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

165-
unsafeDifference := t.uss.Difference(o.uss).(*threadUnsafeSet[T])
165+
unsafeDifference := t.uss.Difference(o.uss).(*ThreadUnsafeSet[T])
166166
ret := &threadSafeSet[T]{uss: unsafeDifference}
167167
t.RUnlock()
168168
o.RUnlock()
@@ -175,7 +175,7 @@ func (t *threadSafeSet[T]) SymmetricDifference(other Set[T]) Set[T] {
175175
t.RLock()
176176
o.RLock()
177177

178-
unsafeDifference := t.uss.SymmetricDifference(o.uss).(*threadUnsafeSet[T])
178+
unsafeDifference := t.uss.SymmetricDifference(o.uss).(*ThreadUnsafeSet[T])
179179
ret := &threadSafeSet[T]{uss: unsafeDifference}
180180
t.RUnlock()
181181
o.RUnlock()
@@ -266,7 +266,7 @@ func (t *threadSafeSet[T]) Equal(other Set[T]) bool {
266266
func (t *threadSafeSet[T]) Clone() Set[T] {
267267
t.RLock()
268268

269-
unsafeClone := t.uss.Clone().(*threadUnsafeSet[T])
269+
unsafeClone := t.uss.Clone().(*ThreadUnsafeSet[T])
270270
ret := &threadSafeSet[T]{uss: unsafeClone}
271271
t.RUnlock()
272272
return ret

threadunsafe.go

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -31,28 +31,28 @@ import (
3131
"strings"
3232
)
3333

34-
type threadUnsafeSet[T comparable] map[T]struct{}
34+
type ThreadUnsafeSet[T comparable] map[T]struct{}
3535

36-
// Assert concrete type:threadUnsafeSet adheres to Set interface.
37-
var _ Set[string] = (*threadUnsafeSet[string])(nil)
36+
// Assert concrete type:ThreadUnsafeSet adheres to Set interface.
37+
var _ Set[string] = (*ThreadUnsafeSet[string])(nil)
3838

39-
func newThreadUnsafeSet[T comparable]() *threadUnsafeSet[T] {
40-
t := make(threadUnsafeSet[T])
39+
func newThreadUnsafeSet[T comparable]() *ThreadUnsafeSet[T] {
40+
t := make(ThreadUnsafeSet[T])
4141
return &t
4242
}
4343

44-
func newThreadUnsafeSetWithSize[T comparable](cardinality int) *threadUnsafeSet[T] {
45-
t := make(threadUnsafeSet[T], cardinality)
44+
func newThreadUnsafeSetWithSize[T comparable](cardinality int) *ThreadUnsafeSet[T] {
45+
t := make(ThreadUnsafeSet[T], cardinality)
4646
return &t
4747
}
4848

49-
func (s threadUnsafeSet[T]) Add(v T) bool {
49+
func (s ThreadUnsafeSet[T]) Add(v T) bool {
5050
prevLen := len(s)
5151
s[v] = struct{}{}
5252
return prevLen != len(s)
5353
}
5454

55-
func (s *threadUnsafeSet[T]) Append(v ...T) int {
55+
func (s *ThreadUnsafeSet[T]) Append(v ...T) int {
5656
prevLen := len(*s)
5757
for _, val := range v {
5858
(*s)[val] = struct{}{}
@@ -61,15 +61,15 @@ func (s *threadUnsafeSet[T]) Append(v ...T) int {
6161
}
6262

6363
// private version of Add which doesn't return a value
64-
func (s *threadUnsafeSet[T]) add(v T) {
64+
func (s *ThreadUnsafeSet[T]) add(v T) {
6565
(*s)[v] = struct{}{}
6666
}
6767

68-
func (s *threadUnsafeSet[T]) Cardinality() int {
68+
func (s *ThreadUnsafeSet[T]) Cardinality() int {
6969
return len(*s)
7070
}
7171

72-
func (s *threadUnsafeSet[T]) Clear() {
72+
func (s *ThreadUnsafeSet[T]) Clear() {
7373
// Constructions like this are optimised by compiler, and replaced by
7474
// mapclear() function, defined in
7575
// https://github.com/golang/go/blob/29bbca5c2c1ad41b2a9747890d183b6dd3a4ace4/src/runtime/map.go#L993)
@@ -78,15 +78,15 @@ func (s *threadUnsafeSet[T]) Clear() {
7878
}
7979
}
8080

81-
func (s *threadUnsafeSet[T]) Clone() Set[T] {
81+
func (s *ThreadUnsafeSet[T]) Clone() Set[T] {
8282
clonedSet := newThreadUnsafeSetWithSize[T](s.Cardinality())
8383
for elem := range *s {
8484
clonedSet.add(elem)
8585
}
8686
return clonedSet
8787
}
8888

89-
func (s *threadUnsafeSet[T]) Contains(v ...T) bool {
89+
func (s *ThreadUnsafeSet[T]) Contains(v ...T) bool {
9090
for _, val := range v {
9191
if _, ok := (*s)[val]; !ok {
9292
return false
@@ -95,12 +95,12 @@ func (s *threadUnsafeSet[T]) Contains(v ...T) bool {
9595
return true
9696
}
9797

98-
func (s *threadUnsafeSet[T]) ContainsOne(v T) bool {
98+
func (s *ThreadUnsafeSet[T]) ContainsOne(v T) bool {
9999
_, ok := (*s)[v]
100100
return ok
101101
}
102102

103-
func (s *threadUnsafeSet[T]) ContainsAny(v ...T) bool {
103+
func (s *ThreadUnsafeSet[T]) ContainsAny(v ...T) bool {
104104
for _, val := range v {
105105
if _, ok := (*s)[val]; ok {
106106
return true
@@ -109,8 +109,8 @@ func (s *threadUnsafeSet[T]) ContainsAny(v ...T) bool {
109109
return false
110110
}
111111

112-
func (s *threadUnsafeSet[T]) ContainsAnyElement(other Set[T]) bool {
113-
o := other.(*threadUnsafeSet[T])
112+
func (s *ThreadUnsafeSet[T]) ContainsAnyElement(other Set[T]) bool {
113+
o := other.(*ThreadUnsafeSet[T])
114114

115115
// loop over smaller set
116116
if s.Cardinality() < other.Cardinality() {
@@ -130,13 +130,13 @@ func (s *threadUnsafeSet[T]) ContainsAnyElement(other Set[T]) bool {
130130
}
131131

132132
// private version of Contains for a single element v
133-
func (s *threadUnsafeSet[T]) contains(v T) (ok bool) {
133+
func (s *ThreadUnsafeSet[T]) contains(v T) (ok bool) {
134134
_, ok = (*s)[v]
135135
return ok
136136
}
137137

138-
func (s *threadUnsafeSet[T]) Difference(other Set[T]) Set[T] {
139-
o := other.(*threadUnsafeSet[T])
138+
func (s *ThreadUnsafeSet[T]) Difference(other Set[T]) Set[T] {
139+
o := other.(*ThreadUnsafeSet[T])
140140

141141
diff := newThreadUnsafeSet[T]()
142142
for elem := range *s {
@@ -147,16 +147,16 @@ func (s *threadUnsafeSet[T]) Difference(other Set[T]) Set[T] {
147147
return diff
148148
}
149149

150-
func (s *threadUnsafeSet[T]) Each(cb func(T) bool) {
150+
func (s *ThreadUnsafeSet[T]) Each(cb func(T) bool) {
151151
for elem := range *s {
152152
if cb(elem) {
153153
break
154154
}
155155
}
156156
}
157157

158-
func (s *threadUnsafeSet[T]) Equal(other Set[T]) bool {
159-
o := other.(*threadUnsafeSet[T])
158+
func (s *ThreadUnsafeSet[T]) Equal(other Set[T]) bool {
159+
o := other.(*ThreadUnsafeSet[T])
160160

161161
if s.Cardinality() != other.Cardinality() {
162162
return false
@@ -169,8 +169,8 @@ func (s *threadUnsafeSet[T]) Equal(other Set[T]) bool {
169169
return true
170170
}
171171

172-
func (s *threadUnsafeSet[T]) Intersect(other Set[T]) Set[T] {
173-
o := other.(*threadUnsafeSet[T])
172+
func (s *ThreadUnsafeSet[T]) Intersect(other Set[T]) Set[T] {
173+
o := other.(*ThreadUnsafeSet[T])
174174

175175
intersection := newThreadUnsafeSet[T]()
176176
// loop over smaller set
@@ -190,20 +190,20 @@ func (s *threadUnsafeSet[T]) Intersect(other Set[T]) Set[T] {
190190
return intersection
191191
}
192192

193-
func (s *threadUnsafeSet[T]) IsEmpty() bool {
193+
func (s *ThreadUnsafeSet[T]) IsEmpty() bool {
194194
return s.Cardinality() == 0
195195
}
196196

197-
func (s *threadUnsafeSet[T]) IsProperSubset(other Set[T]) bool {
197+
func (s *ThreadUnsafeSet[T]) IsProperSubset(other Set[T]) bool {
198198
return s.Cardinality() < other.Cardinality() && s.IsSubset(other)
199199
}
200200

201-
func (s *threadUnsafeSet[T]) IsProperSuperset(other Set[T]) bool {
201+
func (s *ThreadUnsafeSet[T]) IsProperSuperset(other Set[T]) bool {
202202
return s.Cardinality() > other.Cardinality() && s.IsSuperset(other)
203203
}
204204

205-
func (s *threadUnsafeSet[T]) IsSubset(other Set[T]) bool {
206-
o := other.(*threadUnsafeSet[T])
205+
func (s *ThreadUnsafeSet[T]) IsSubset(other Set[T]) bool {
206+
o := other.(*ThreadUnsafeSet[T])
207207
if s.Cardinality() > other.Cardinality() {
208208
return false
209209
}
@@ -215,11 +215,11 @@ func (s *threadUnsafeSet[T]) IsSubset(other Set[T]) bool {
215215
return true
216216
}
217217

218-
func (s *threadUnsafeSet[T]) IsSuperset(other Set[T]) bool {
218+
func (s *ThreadUnsafeSet[T]) IsSuperset(other Set[T]) bool {
219219
return other.IsSubset(s)
220220
}
221221

222-
func (s *threadUnsafeSet[T]) Iter() <-chan T {
222+
func (s *ThreadUnsafeSet[T]) Iter() <-chan T {
223223
ch := make(chan T)
224224
go func() {
225225
for elem := range *s {
@@ -231,7 +231,7 @@ func (s *threadUnsafeSet[T]) Iter() <-chan T {
231231
return ch
232232
}
233233

234-
func (s *threadUnsafeSet[T]) Iterator() *Iterator[T] {
234+
func (s *ThreadUnsafeSet[T]) Iterator() *Iterator[T] {
235235
iterator, ch, stopCh := newIterator[T]()
236236

237237
go func() {
@@ -251,25 +251,25 @@ func (s *threadUnsafeSet[T]) Iterator() *Iterator[T] {
251251

252252
// Pop returns a popped item in case set is not empty, or nil-value of T
253253
// if set is already empty
254-
func (s *threadUnsafeSet[T]) Pop() (v T, ok bool) {
254+
func (s *ThreadUnsafeSet[T]) Pop() (v T, ok bool) {
255255
for item := range *s {
256256
delete(*s, item)
257257
return item, true
258258
}
259259
return v, false
260260
}
261261

262-
func (s threadUnsafeSet[T]) Remove(v T) {
262+
func (s ThreadUnsafeSet[T]) Remove(v T) {
263263
delete(s, v)
264264
}
265265

266-
func (s threadUnsafeSet[T]) RemoveAll(i ...T) {
266+
func (s ThreadUnsafeSet[T]) RemoveAll(i ...T) {
267267
for _, elem := range i {
268268
delete(s, elem)
269269
}
270270
}
271271

272-
func (s threadUnsafeSet[T]) String() string {
272+
func (s ThreadUnsafeSet[T]) String() string {
273273
items := make([]string, 0, len(s))
274274

275275
for elem := range s {
@@ -278,8 +278,8 @@ func (s threadUnsafeSet[T]) String() string {
278278
return fmt.Sprintf("Set{%s}", strings.Join(items, ", "))
279279
}
280280

281-
func (s *threadUnsafeSet[T]) SymmetricDifference(other Set[T]) Set[T] {
282-
o := other.(*threadUnsafeSet[T])
281+
func (s *ThreadUnsafeSet[T]) SymmetricDifference(other Set[T]) Set[T] {
282+
o := other.(*ThreadUnsafeSet[T])
283283

284284
sd := newThreadUnsafeSet[T]()
285285
for elem := range *s {
@@ -295,7 +295,7 @@ func (s *threadUnsafeSet[T]) SymmetricDifference(other Set[T]) Set[T] {
295295
return sd
296296
}
297297

298-
func (s threadUnsafeSet[T]) ToSlice() []T {
298+
func (s ThreadUnsafeSet[T]) ToSlice() []T {
299299
keys := make([]T, 0, s.Cardinality())
300300
for elem := range s {
301301
keys = append(keys, elem)
@@ -304,14 +304,14 @@ func (s threadUnsafeSet[T]) ToSlice() []T {
304304
return keys
305305
}
306306

307-
func (s threadUnsafeSet[T]) Union(other Set[T]) Set[T] {
308-
o := other.(*threadUnsafeSet[T])
307+
func (s ThreadUnsafeSet[T]) Union(other Set[T]) Set[T] {
308+
o := other.(*ThreadUnsafeSet[T])
309309

310310
n := s.Cardinality()
311311
if o.Cardinality() > n {
312312
n = o.Cardinality()
313313
}
314-
unionedSet := make(threadUnsafeSet[T], n)
314+
unionedSet := make(ThreadUnsafeSet[T], n)
315315

316316
for elem := range s {
317317
unionedSet.add(elem)
@@ -323,7 +323,7 @@ func (s threadUnsafeSet[T]) Union(other Set[T]) Set[T] {
323323
}
324324

325325
// MarshalJSON creates a JSON array from the set, it marshals all elements
326-
func (s threadUnsafeSet[T]) MarshalJSON() ([]byte, error) {
326+
func (s ThreadUnsafeSet[T]) MarshalJSON() ([]byte, error) {
327327
items := make([]string, 0, s.Cardinality())
328328

329329
for elem := range s {
@@ -340,7 +340,7 @@ func (s threadUnsafeSet[T]) MarshalJSON() ([]byte, error) {
340340

341341
// UnmarshalJSON recreates a set from a JSON array, it only decodes
342342
// primitive types. Numbers are decoded as json.Number.
343-
func (s *threadUnsafeSet[T]) UnmarshalJSON(b []byte) error {
343+
func (s *ThreadUnsafeSet[T]) UnmarshalJSON(b []byte) error {
344344
var i []T
345345
err := json.Unmarshal(b, &i)
346346
if err != nil {

0 commit comments

Comments
 (0)