-
Notifications
You must be signed in to change notification settings - Fork 0
/
set.go
180 lines (156 loc) · 2.86 KB
/
set.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// The set package provides a concurrency-safe set implementation.
package set
import (
"fmt"
"strings"
"sync"
)
// Set implements a set.
type Set[T comparable] struct {
m map[T]struct{}
mu sync.RWMutex
}
// New returns a new Set.
func New[T comparable]() *Set[T] {
return &Set[T]{m: make(map[T]struct{})}
}
// Intersection returns the intersection of sets.
func Intersection[T comparable](sets ...*Set[T]) *Set[T] {
i := New[T]()
if len(sets) == 0 {
return i
}
smallest := sets[0]
for _, s := range sets {
s.mu.RLock()
defer s.mu.RUnlock()
if len(s.m) < len(smallest.m) {
smallest = s
}
}
next:
for e := range smallest.m {
for _, s := range sets {
if _, ok := s.m[e]; !ok {
continue next
}
}
i.m[e] = struct{}{}
}
return i
}
// Union returns the union of sets.
func Union[T comparable](sets ...*Set[T]) *Set[T] {
u := New[T]()
for _, s := range sets {
s.Range(func(e T) bool {
u.m[e] = struct{}{}
return true
})
}
return u
}
// Put puts e into s.
func (s *Set[T]) Put(e T) {
s.mu.Lock()
s.m[e] = struct{}{}
s.mu.Unlock()
}
// Delete deletes e from s.
func (s *Set[T]) Delete(e T) {
s.mu.Lock()
delete(s.m, e)
s.mu.Unlock()
}
// Has returns true if s has e.
func (s *Set[T]) Has(e T) bool {
s.mu.RLock()
_, ok := s.m[e]
s.mu.RUnlock()
return ok
}
// Len returns the number of members of s.
func (s *Set[T]) Len() int {
s.mu.RLock()
l := len(s.m)
s.mu.RUnlock()
return l
}
// Range calls f for each member of s, so long as it returns true.
func (s *Set[T]) Range(f func(T) bool) {
s.mu.RLock()
defer s.mu.RUnlock()
for e := range s.m {
if !f(e) {
break
}
}
}
// Copy returns a copy of s.
func (s *Set[T]) Copy() *Set[T] {
c := New[T]()
s.Range(func(e T) bool {
c.m[e] = struct{}{}
return true
})
return c
}
// Diff returns the result of s-t.
func (s *Set[T]) Diff(t *Set[T]) *Set[T] {
t.mu.RLock()
defer t.mu.RUnlock()
d := New[T]()
s.Range(func(e T) bool {
if _, ok := t.m[e]; !ok {
d.m[e] = struct{}{}
}
return true
})
return d
}
// Subset returns true if s is a subset of t.
func (s *Set[T]) Subset(t *Set[T]) bool {
s.mu.RLock()
defer s.mu.RUnlock()
t.mu.RLock()
defer t.mu.RUnlock()
for e := range s.m {
if _, ok := t.m[e]; !ok {
return false
}
}
return true
}
// Equal returns true if s and t have the same members.
func (s *Set[T]) Equal(t *Set[T]) bool {
s.mu.RLock()
defer s.mu.RUnlock()
t.mu.RLock()
defer t.mu.RUnlock()
if len(s.m) != len(t.m) {
return false
}
for e := range s.m {
if _, ok := t.m[e]; !ok {
return false
}
}
return true
}
// String returns the string representation of s.
func (s *Set[T]) String() string {
s.mu.RLock()
defer s.mu.RUnlock()
var sb strings.Builder
sb.WriteRune('{')
var i int
for e := range s.m {
i++
fmt.Fprint(&sb, e)
if i < len(s.m) {
sb.WriteString(", ")
}
}
sb.WriteRune('}')
return sb.String()
}