-
Notifications
You must be signed in to change notification settings - Fork 16
/
typed.go
295 lines (246 loc) · 6.56 KB
/
typed.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
Copyright 2019 Jim Zhang ([email protected])
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package goset
import (
"fmt"
"sync/atomic"
)
// typedSet is a set with specified type
type typedSet interface {
// Add adds an element to the set anyway.
Add(elem ...interface{})
// Remove removes an element from the set.
Remove(elem ...interface{})
// Contains checks whether the given item is in the set.
Contains(item interface{}) bool
// Copy clones the set.
Copy() typedSet
// Len returns the size of set. aka Cardinality.
Len() int
// Range calls f sequentially for each element present in the set.
// If f returns false, range stops the iteration.
//
// Note: the iteration order is not specified and is not guaranteed
// to be the same from one iteration to the next. The index only
// means how many elements have been visited in the iteration, it not
// specifies the index of an element in the set
Range(foreach func(index int, elem interface{}) bool)
// // ContainsAny returns true if any elems are contained in the set
// ContainsAny(elems ...interface{}) bool
// Equal checks whether this set is equal to the given one.
// There are two constraints if set a is equal to set b.
// the two set must have the same size and contain the same elements.
Equal(b typedSet) bool
// IsSubsetOf checks whether this set is the subset of the given set
// In other words, all elements in this set are also the elements
// of the given set.
IsSubsetOf(b typedSet) bool
// ---------------------------------------------------------------------
// Set Oprations
// Diff returns the difference between the set and this given
// one, aka Difference Set
// math formula: a - b
Diff(b typedSet) typedSet
// SymmetricDiff returns the symmetric difference between this set
// and the given one. aka Symmetric Difference Set
// math formula: (a - b) ∪ (b - a)
SymmetricDiff(b typedSet) typedSet
// Unite combines two sets into a new one, aka Union Set
// math formula: a ∪ b
Unite(b typedSet) typedSet
// Intersect returns the intersection of two set, aka Intersection Set
// math formula: a ∩ b
Intersect(b typedSet) typedSet
}
type typed int
const (
typedInt typed = iota
typedString
typedAny
)
var (
allTyped = []typed{
typedInt,
typedString,
typedAny,
}
)
func typedAssert(elem interface{}) typed {
switch elem.(type) {
case int:
return typedInt
case string:
return typedString
}
return typedAny
}
// func concurrent(f func(typed)) {
// wg := sync.WaitGroup{}
// for _, t := range allTyped {
// wg.Add(1)
// go func(t typed) {
// defer wg.Done()
// f(t)
// }(t)
// }
// wg.Wait()
// }
func synchronous(f func(typed)) {
for _, t := range allTyped {
f(t)
}
}
var visitAll = synchronous
type typedSetGroup map[typed]typedSet
func newTypedSetGroup(elems ...interface{}) typedSetGroup {
s := make(typedSetGroup)
s.store(typedInt, newInts())
s.store(typedString, newStrings())
s.store(typedAny, newAny())
s.Add(elems...) //nolint:errcheck
return s
}
func (s typedSetGroup) store(t typed, in typedSet) {
s[t] = in
}
func (s typedSetGroup) load(t typed) typedSet {
return s[t].(typedSet)
}
func (s typedSetGroup) typedSetFor(elem interface{}) typedSet {
v := s.load(typedAssert(elem))
return v.(typedSet)
}
func (s typedSetGroup) Add(elems ...interface{}) (err error) {
defer func() {
// recover unhashable error
if e := recover(); e != nil {
err = fmt.Errorf("%v", e)
}
}()
for _, elem := range elems {
s.typedSetFor(elem).Add(elem)
}
return nil
}
func (s typedSetGroup) Remove(elems ...interface{}) {
for _, elem := range elems {
s.typedSetFor(elem).Remove(elem)
}
}
func (s typedSetGroup) Range(foreach func(index int, elem interface{}) bool) {
var i int64 = -1
visitAll(func(t typed) {
s.load(t).Range(func(_ int, elem interface{}) bool {
// add firstly to get the right index
newi := atomic.AddInt64(&i, 1)
return foreach(int(newi), elem)
})
})
}
func (s typedSetGroup) Contains(elem interface{}) (ret bool) {
defer func() {
// recover unhashable error
if e := recover(); e != nil {
ret = false
return
}
}()
return s.typedSetFor(elem).Contains(elem)
}
func (s typedSetGroup) ContainsAll(elems ...interface{}) bool {
for _, elem := range elems {
if !s.Contains(elem) {
return false
}
}
return true
}
func (s typedSetGroup) ContainsAny(elems ...interface{}) bool {
for _, elem := range elems {
if s.Contains(elem) {
return true
}
}
return false
}
func (s typedSetGroup) Copy() typedSetGroup {
ret := make(typedSetGroup)
visitAll(func(t typed) {
ret.store(t, s.load(t).Copy())
})
return ret
}
func (s typedSetGroup) Len() int {
var l int64
visitAll(func(t typed) {
atomic.AddInt64(&l, int64(s.load(t).Len()))
})
return int(l)
}
func (s typedSetGroup) Equal(b typedSetGroup) bool {
var fail int64
visitAll(func(t typed) {
if !s.load(t).Equal(b.load(t)) {
atomic.AddInt64(&fail, 1)
}
})
return fail == 0
}
func (s typedSetGroup) IsSubsetOf(b typedSetGroup) bool {
var fail int64
visitAll(func(t typed) {
if !s.load(t).IsSubsetOf(b.load(t)) {
atomic.AddInt64(&fail, 1)
}
})
return fail == 0
}
func (s typedSetGroup) Diff(b typedSetGroup) typedSetGroup {
ret := make(typedSetGroup)
visitAll(func(t typed) {
ret.store(t, s.load(t).Diff(b.load(t)))
})
return ret
}
func (s typedSetGroup) SymmetricDiff(b typedSetGroup) typedSetGroup {
ret := make(typedSetGroup)
visitAll(func(t typed) {
ret.store(t, s.load(t).SymmetricDiff(b.load(t)))
})
return ret
}
func (s typedSetGroup) Unite(b typedSetGroup) typedSetGroup {
ret := make(typedSetGroup)
visitAll(func(t typed) {
ret.store(t, s.load(t).Unite(b.load(t)))
})
return ret
}
func (s typedSetGroup) Intersect(b typedSetGroup) typedSetGroup {
ret := make(typedSetGroup)
visitAll(func(t typed) {
ret.store(t, s.load(t).Intersect(b.load(t)))
})
return ret
}
func (s typedSetGroup) Elements() []interface{} {
ret := make([]interface{}, 0, s.Len())
// synchronous to avoid write race
synchronous(func(t typed) {
s.load(t).Range(func(_ int, elem interface{}) bool {
ret = append(ret, elem)
return true
})
})
return ret
}