-
Notifications
You must be signed in to change notification settings - Fork 3
/
sync.go
101 lines (83 loc) · 1.78 KB
/
sync.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package set
import "sync"
// SyncSet provide nearly same methods as Set, but it is thread-safe
type SyncSet[T comparable] struct {
mu sync.RWMutex
in *Set[T]
}
// NewSync Create a new SyncSet
func NewSync[T comparable](initial ...T) *SyncSet[T] {
s := New(initial...)
return &SyncSet[T]{
in: s,
}
}
// NewSyncWithCapacity Create an empty SyncSet with specific capacity
func NewSyncWithCapacity[T comparable](capacity int) *SyncSet[T] {
s := WithCapacity[T](capacity)
return &SyncSet[T]{
in: s,
}
}
func (s *SyncSet[T]) rw(f func(*Set[T])) {
s.mu.Lock()
defer s.mu.Unlock()
f(s.in)
}
func (s *SyncSet[T]) r(f func(*Set[T])) {
s.mu.RLock()
defer s.mu.RUnlock()
f(s.in)
}
// Has Test to see whether the element is in the set
func (s *SyncSet[T]) Has(element T) (exists bool) {
s.r(func(in *Set[T]) {
exists = in.Has(element)
})
return
}
func (s *SyncSet[T]) All() (all []T) {
s.r(func(in *Set[T]) {
all = in.All()
})
return
}
// Add element(s) to the set
func (s *SyncSet[T]) Add(elements ...T) {
s.rw(func(in *Set[T]) {
in.InsertAll(elements)
})
}
// AddAll add elements to the set
func (s *SyncSet[T]) AddAll(elements []T) {
s.rw(func(in *Set[T]) {
in.InsertAll(elements)
})
}
// Len Return the number of items in the set
func (s *SyncSet[T]) Len() (size int) {
s.r(func(in *Set[T]) {
size = in.Len()
})
return
}
// Size Return the number of items in the set
func (s *SyncSet[T]) Size() (size int) {
s.r(func(in *Set[T]) {
size = in.Len()
})
return
}
// Remove an element from the set
func (s *SyncSet[T]) Remove(element T) {
s.rw(func(in *Set[T]) {
in.Remove(element)
})
}
// ContainsOrAdd add element if not exist
func (s *SyncSet[T]) ContainsOrAdd(element T) (added bool) {
s.rw(func(in *Set[T]) {
added = in.ContainsOrAdd(element)
})
return
}