Skip to content

Commit 4f76bdd

Browse files
committed
refactor: renaming
1 parent ebbbbda commit 4f76bdd

File tree

4 files changed

+67
-65
lines changed

4 files changed

+67
-65
lines changed

concurrentset.go

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,55 +2,55 @@ package goset
22

33
import "sync"
44

5-
type concurrentSet[T comparable] struct {
6-
ncs *noconcurrentSet[T]
5+
type safeSet[T comparable] struct {
6+
ncs *unsafeSet[T]
77
sync.RWMutex
88
}
99

10-
// Interface guard to assert concrete type:concurrentSet adheres to Set interface.
11-
var _ Set[string] = (*concurrentSet[string])(nil)
10+
// Interface guard to assert concrete type:safeSet adheres to Set interface.
11+
var _ Set[string] = (*safeSet[string])(nil)
1212

13-
func newConcurrentSet[T comparable](size int) *concurrentSet[T] {
14-
return &concurrentSet[T]{
15-
ncs: newNoConcurrentSet[T](size),
13+
func newSafeSet[T comparable](size int) *safeSet[T] {
14+
return &safeSet[T]{
15+
ncs: newUnsafeSet[T](size),
1616
}
1717
}
1818

19-
func (s *concurrentSet[T]) Add(v T) {
19+
func (s *safeSet[T]) Add(v T) {
2020
s.Lock()
2121
defer s.Unlock()
2222
s.ncs.Add(v)
2323
}
2424

25-
func (s *concurrentSet[T]) Remove(v T) {
25+
func (s *safeSet[T]) Remove(v T) {
2626
s.Lock()
2727
defer s.Unlock()
2828
s.ncs.Remove(v)
2929
}
3030

31-
func (s *concurrentSet[T]) Contains(v T) bool {
31+
func (s *safeSet[T]) Contains(v T) bool {
3232
s.RLock()
3333
defer s.RUnlock()
3434

3535
return s.ncs.Contains(v)
3636
}
3737

38-
func (s *concurrentSet[T]) Size() int {
38+
func (s *safeSet[T]) Size() int {
3939
s.RLock()
4040
defer s.RUnlock()
4141

4242
return s.ncs.Size()
4343
}
4444

45-
func (s *concurrentSet[T]) IsEmpty() bool {
45+
func (s *safeSet[T]) IsEmpty() bool {
4646
s.RLock()
4747
defer s.RUnlock()
4848

4949
return s.ncs.IsEmpty()
5050
}
5151

52-
func (s *concurrentSet[T]) IsEqual(other Set[T]) bool {
53-
o, _ := other.(*concurrentSet[T])
52+
func (s *safeSet[T]) IsEqual(other Set[T]) bool {
53+
o, _ := other.(*safeSet[T])
5454

5555
s.RLock()
5656
o.RLock()
@@ -60,70 +60,70 @@ func (s *concurrentSet[T]) IsEqual(other Set[T]) bool {
6060
return s.ncs.IsEqual(o.ncs)
6161
}
6262

63-
func (s *concurrentSet[T]) Clear() {
63+
func (s *safeSet[T]) Clear() {
6464
s.Lock()
6565
defer s.Unlock()
6666
s.ncs.Clear()
6767
}
6868

69-
func (s *concurrentSet[T]) ToSlice() []T {
69+
func (s *safeSet[T]) ToSlice() []T {
7070
s.RLock()
7171
defer s.RUnlock()
7272

7373
return s.ncs.ToSlice()
7474
}
7575

76-
func (s *concurrentSet[T]) Clone() Set[T] {
76+
func (s *safeSet[T]) Clone() Set[T] {
7777
s.RLock()
7878
defer s.RUnlock()
79-
clone, _ := s.ncs.Clone().(*noconcurrentSet[T])
79+
clone, _ := s.ncs.Clone().(*unsafeSet[T])
8080

81-
return &concurrentSet[T]{
81+
return &safeSet[T]{
8282
ncs: clone,
8383
}
8484
}
8585

86-
func (s *concurrentSet[T]) Union(other Set[T]) Set[T] {
87-
o, _ := other.(*concurrentSet[T])
86+
func (s *safeSet[T]) Union(other Set[T]) Set[T] {
87+
o, _ := other.(*safeSet[T])
8888

8989
s.RLock()
9090
o.RLock()
9191
defer s.RUnlock()
9292
defer o.RUnlock()
9393

94-
union, _ := s.ncs.Union(o.ncs).(*noconcurrentSet[T])
94+
union, _ := s.ncs.Union(o.ncs).(*unsafeSet[T])
9595

96-
return &concurrentSet[T]{
96+
return &safeSet[T]{
9797
ncs: union,
9898
}
9999
}
100100

101-
func (s *concurrentSet[T]) Intersection(other Set[T]) Set[T] {
102-
o, _ := other.(*concurrentSet[T])
101+
func (s *safeSet[T]) Intersection(other Set[T]) Set[T] {
102+
o, _ := other.(*safeSet[T])
103103

104104
s.RLock()
105105
o.RLock()
106106
defer s.RUnlock()
107107
defer o.RUnlock()
108108

109-
intersection, _ := s.ncs.Intersection(o.ncs).(*noconcurrentSet[T])
109+
intersection, _ := s.ncs.Intersection(o.ncs).(*unsafeSet[T])
110110

111-
return &concurrentSet[T]{
111+
return &safeSet[T]{
112112
ncs: intersection,
113113
}
114114
}
115115

116-
func (s *concurrentSet[T]) Difference(other Set[T]) Set[T] {
117-
o, _ := other.(*concurrentSet[T])
116+
func (s *safeSet[T]) Difference(other Set[T]) Set[T] {
117+
o, _ := other.(*safeSet[T])
118118

119119
s.RLock()
120120
o.RLock()
121121
defer s.RUnlock()
122122
defer o.RUnlock()
123123

124-
difference, _ := s.ncs.Difference(o.ncs).(*noconcurrentSet[T])
124+
difference, _ := s.ncs.Difference(o.ncs).(*unsafeSet[T])
125125

126-
return &concurrentSet[T]{
126+
return &safeSet[T]{
127127
ncs: difference,
128128
}
129129
}

example/main.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
set "github.com/kafkaphoenix/goset"
88
)
99

10-
func noConcurrentDemo() {
10+
func nonConcurrentDemo() {
1111
fmt.Println("///////////Non-concurrent set demo")
1212
// Create a new set
1313

@@ -50,9 +50,9 @@ func noConcurrentDemo() {
5050
}
5151

5252
func concurrentDemo() {
53-
fmt.Println("///////////Concurrent set demo")
54-
// Create a new concurrent set
55-
cs := set.NewConcurrentSet[int]()
53+
fmt.Println("///////////Concurrent safe set demo")
54+
// Create a new concurrent safe set
55+
cs := set.NewSafeSet[int]()
5656

5757
const numGoroutines = 5
5858

@@ -61,7 +61,7 @@ func concurrentDemo() {
6161
var wg sync.WaitGroup
6262

6363
// Start multiple goroutines to add and remove elements
64-
fmt.Println("Adding and removing elements in concurrent set...")
64+
fmt.Println("Adding and removing elements in concurrent safe set...")
6565
fmt.Printf("Number of goroutines: %d, Operations per goroutine: %d\n", numGoroutines, opsPerGoroutine)
6666
fmt.Println("Removing even numbers...")
6767
fmt.Println("Adding odd numbers...")
@@ -122,7 +122,7 @@ func setOperations() {
122122
}
123123

124124
func main() {
125-
noConcurrentDemo()
125+
nonConcurrentDemo()
126126
concurrentDemo()
127127
setOperations()
128128
}

noconcurrentset.go

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
11
package goset
22

3-
type noconcurrentSet[T comparable] map[T]struct{}
3+
type unsafeSet[T comparable] map[T]struct{}
44

5-
// Interface guard to assert concrete type:noconcurrentSet adheres to Set interface.
6-
var _ Set[string] = (*noconcurrentSet[string])(nil)
5+
// Interface guard to assert concrete type:unsafeSet adheres to Set interface.
6+
var _ Set[string] = (*unsafeSet[string])(nil)
77

8-
func newNoConcurrentSet[T comparable](size int) *noconcurrentSet[T] {
9-
s := make(noconcurrentSet[T], size)
8+
func newUnsafeSet[T comparable](size int) *unsafeSet[T] {
9+
s := make(unsafeSet[T], size)
1010
return &s
1111
}
1212

13-
func (s *noconcurrentSet[T]) Add(v T) {
13+
func (s *unsafeSet[T]) Add(v T) {
1414
(*s)[v] = struct{}{}
1515
}
1616

17-
func (s *noconcurrentSet[T]) Remove(v T) {
17+
func (s *unsafeSet[T]) Remove(v T) {
1818
delete(*s, v)
1919
}
2020

21-
func (s *noconcurrentSet[T]) Contains(v T) bool {
21+
func (s *unsafeSet[T]) Contains(v T) bool {
2222
_, ok := (*s)[v]
2323
return ok
2424
}
2525

26-
func (s *noconcurrentSet[T]) Size() int {
26+
func (s *unsafeSet[T]) Size() int {
2727
return len(*s)
2828
}
2929

30-
func (s *noconcurrentSet[T]) IsEmpty() bool {
30+
func (s *unsafeSet[T]) IsEmpty() bool {
3131
return s.Size() == 0
3232
}
3333

34-
func (s *noconcurrentSet[T]) IsEqual(other Set[T]) bool {
35-
o, _ := other.(*noconcurrentSet[T])
34+
func (s *unsafeSet[T]) IsEqual(other Set[T]) bool {
35+
o, _ := other.(*unsafeSet[T])
3636

3737
if s.Size() != o.Size() {
3838
return false
@@ -47,11 +47,11 @@ func (s *noconcurrentSet[T]) IsEqual(other Set[T]) bool {
4747
return true
4848
}
4949

50-
func (s *noconcurrentSet[T]) Clear() {
50+
func (s *unsafeSet[T]) Clear() {
5151
*s = make(map[T]struct{})
5252
}
5353

54-
func (s *noconcurrentSet[T]) ToSlice() []T {
54+
func (s *unsafeSet[T]) ToSlice() []T {
5555
list := make([]T, 0, s.Size())
5656
for key := range *s {
5757
list = append(list, key)
@@ -60,20 +60,20 @@ func (s *noconcurrentSet[T]) ToSlice() []T {
6060
return list
6161
}
6262

63-
func (s *noconcurrentSet[T]) Clone() Set[T] {
64-
clone := newNoConcurrentSet[T](s.Size())
63+
func (s *unsafeSet[T]) Clone() Set[T] {
64+
clone := newUnsafeSet[T](s.Size())
6565
for key := range *s {
6666
clone.Add(key)
6767
}
6868

6969
return clone
7070
}
7171

72-
func (s *noconcurrentSet[T]) Union(other Set[T]) Set[T] {
73-
o, _ := other.(*noconcurrentSet[T])
72+
func (s *unsafeSet[T]) Union(other Set[T]) Set[T] {
73+
o, _ := other.(*unsafeSet[T])
7474

7575
n := max(o.Size(), s.Size())
76-
union := newNoConcurrentSet[T](n)
76+
union := newUnsafeSet[T](n)
7777

7878
for key := range *s {
7979
union.Add(key)
@@ -86,8 +86,8 @@ func (s *noconcurrentSet[T]) Union(other Set[T]) Set[T] {
8686
return union
8787
}
8888

89-
func (s *noconcurrentSet[T]) Intersection(other Set[T]) Set[T] {
90-
o, _ := other.(*noconcurrentSet[T])
89+
func (s *unsafeSet[T]) Intersection(other Set[T]) Set[T] {
90+
o, _ := other.(*unsafeSet[T])
9191

9292
intersect := NewSet[T]()
9393

@@ -108,8 +108,8 @@ func (s *noconcurrentSet[T]) Intersection(other Set[T]) Set[T] {
108108
return intersect
109109
}
110110

111-
func (s *noconcurrentSet[T]) Difference(other Set[T]) Set[T] {
112-
o, _ := other.(*noconcurrentSet[T])
111+
func (s *unsafeSet[T]) Difference(other Set[T]) Set[T] {
112+
o, _ := other.(*unsafeSet[T])
113113

114114
diff := NewSet[T]()
115115

set.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,21 @@ type Set[T comparable] interface {
2929
Difference(other Set[T]) Set[T]
3030
}
3131

32-
// NewSet creates a new no concurrent set with the given elements.
32+
// NewSet creates a set with the given elements.
33+
// It is not safe for concurrent use.
34+
// Use NewSafeSet for concurrent use.
3335
func NewSet[T comparable](vs ...T) Set[T] {
34-
s := newNoConcurrentSet[T](len(vs))
36+
s := newUnsafeSet[T](len(vs))
3537
for _, v := range vs {
3638
s.Add(v)
3739
}
3840

3941
return s
4042
}
4143

42-
// NewConcurrentSet creates a new concurrent set with the given elements.
43-
func NewConcurrentSet[T comparable](vs ...T) Set[T] {
44-
s := newConcurrentSet[T](len(vs))
44+
// NewSafeSet creates a concurrent safe set with the given elements.
45+
func NewSafeSet[T comparable](vs ...T) Set[T] {
46+
s := newSafeSet[T](len(vs))
4547
for _, v := range vs {
4648
s.Add(v)
4749
}

0 commit comments

Comments
 (0)