-
Notifications
You must be signed in to change notification settings - Fork 1
/
set.go
444 lines (375 loc) · 7.89 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
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
package set
// helpful to not write everywhere struct{}{}
var keyExists = struct{}{}
type (
Set struct {
records map[string]set
}
set map[interface{}]struct{}
)
/*
Related commands
SADD
SCARD
SDIFF
SDIFFSTORE
SINTER
SINTERSTORE
SISMEMBER
SMEMBERS
SMISMEMBER
SMOVE
SPOP
SRANDMEMBER
SREM
SUNION
SUNIONSTORE
*/
// newSet creates a new non-threadsafe Set.
func newSet() set {
s := make(map[interface{}]struct{})
return s
}
// add includes the specified items (one or more) to the set. The underlying
// Set s is modified. If passed nothing it silently returns.
func (s set) add(items ...interface{}) {
if len(items) == 0 {
return
}
for _, item := range items {
s[item] = keyExists
}
}
// remove deletes the specified items from the set. The underlying Set s is
// modified. If passed nothing it silently returns.
func (s set) remove(items ...interface{}) {
if len(items) == 0 {
return
}
for _, item := range items {
delete(s, item)
}
}
// has looks for the existence of items passed. It returns false if nothing is
// passed. For multiple items it returns true only if all of the items exist.
func (s set) has(items ...interface{}) bool {
// assume checked for empty item, which not exist
if len(items) == 0 {
return false
}
exist := true
for _, item := range items {
if _, exist = s[item]; !exist {
break
}
}
return exist
}
// each traverses the items in the Set, calling the provided function for each
// set member. Traversal will continue until all items in the Set have been
// visited, or if the closure returns false.
func (s set) each(f func(item interface{}) bool) {
for item := range s {
if !f(item) {
break
}
}
}
// Copy returns a new Set with a copy of s.
func (s set) copy() set {
u := newSet()
for item := range s {
u.add(item)
}
return u
}
// list returns a slice of all items. There is also StringSlice() and
// IntSlice() methods for returning slices of type string or int.
func (s set) list() []interface{} {
list := make([]interface{}, 0, len(s))
for item := range s {
list = append(list, item)
}
return list
}
func (s set) size() int {
return len(s)
}
// Merge is like Union, however it modifies the current set it's applied on
// with the given t set.
func (s set) Merge(t set) {
t.each(func(item interface{}) bool {
s[item] = keyExists
return true
})
}
// it's not the opposite of Merge.
// Separate removes the set items containing in t from set s. Please aware that
func (s set) Separate(t set) {
s.remove(t.list()...)
}
// union is the merger of multiple sets. It returns a new set with all the
// elements present in all the sets that are passed.
//
// The dynamic type of the returned set is determined by the first passed set's
// implementation of the New() method.
func union(set1, set2 set, sets ...set) set {
u := set1.copy()
set2.each(func(item interface{}) bool {
u.add(item)
return true
})
for _, set := range sets {
set.each(func(item interface{}) bool {
u.add(item)
return true
})
}
return u
}
// difference returns a new set which contains items which are in in the first
// set but not in the others. Unlike the difference() method you can use this
// function separately with multiple sets.
func difference(set1, set2 set, sets ...set) set {
s := set1.copy()
s.Separate(set2)
for _, set := range sets {
s.Separate(set)
}
return s
}
// intersection returns a new set which contains items that only exist in all given sets.
func intersection(set1, set2 set, sets ...set) set {
all := union(set1, set2, sets...)
result := union(set1, set2, sets...)
all.each(func(item interface{}) bool {
if !set1.has(item) || !set2.has(item) {
result.remove(item)
}
for _, set := range sets {
if !set.has(item) {
result.remove(item)
}
}
return true
})
return result
}
func New() *Set {
return &Set{
records: make(map[string]set),
}
}
func (s *Set) SAdd(key string, member interface{}) int {
if !s.exists(key) {
s.records[key] = make(set)
}
s.records[key].add(member)
return s.records[key].size()
}
func (s *Set) SPop(key string, count int) []interface{} {
var val []interface{}
if !s.exists(key) || count <= 0 {
return val
}
for k := range s.records[key] {
s.records[key].remove(k)
val = append(val, k)
count--
if count == 0 {
break
}
}
return val
}
func (s *Set) SIsMember(key string, member interface{}) bool {
return s.fieldExists(key, member)
}
func (s *Set) SRandMember(key string, count int) []interface{} {
var val []interface{}
if !s.exists(key) || count == 0 {
return val
}
if count > 0 {
for k := range s.records[key] {
val = append(val, k)
if len(val) == count {
break
}
}
} else {
count = -count
randomVal := func() interface{} {
// map indexes are not sorted and will return random
// key when ranged through
for k := range s.records[key] {
return k
}
return nil
}
for count > 0 {
val = append(val, randomVal())
count--
}
}
return val
}
func (s *Set) SRem(key string, member interface{}) bool {
if !s.exists(key) {
return false
}
if ok := s.records[key].has(member); ok {
s.records[key].remove(member)
return true
}
return false
}
func (s *Set) SMove(src, dst string, member interface{}) bool {
if !s.fieldExists(src, member) {
return false
}
if !s.exists(dst) {
s.records[dst] = make(set)
}
s.records[src].remove(member)
s.records[dst].add(member)
return true
}
func (s *Set) SCard(key string) int {
if !s.exists(key) {
return 0
}
return s.records[key].size()
}
func (s *Set) SMembers(key string) (val []interface{}) {
if !s.exists(key) {
return
}
val = s.records[key].list()
return
}
func (s *Set) SUnion(keys ...string) (val []interface{}) {
if len(keys) == 0 {
return
}
if len(keys) == 1 {
if s.exists(keys[0]) {
return s.records[keys[0]].list()
}
return
}
m := make([]set, 0, 1)
for _, k := range keys {
if s.exists(k) {
m = append(m, s.records[k])
}
}
un := union(m[0], m[1], m[2:]...)
for k := range un {
val = append(val, k)
}
return
}
func (s *Set) SUnionStore(key string, keys ...string) int {
un := s.SUnion(keys...)
for _, k := range un {
s.SAdd(key, k)
}
return len(un)
}
func (s *Set) SDiff(keys ...string) (val []interface{}) {
if len(keys) == 0 {
return
}
if len(keys) == 1 {
if s.exists(keys[0]) {
return s.records[keys[0]].list()
}
return
}
m := make([]set, 0, 1)
for _, k := range keys {
if s.exists(k) {
m = append(m, s.records[k])
}
}
diff := difference(m[0], m[1], m[2:]...)
for k := range diff {
val = append(val, k)
}
return
}
func (s *Set) SDiffStore(key string, keys ...string) int {
diff := s.SDiff(keys...)
for _, k := range diff {
s.SAdd(key, k)
}
return len(diff)
}
func (s *Set) SInter(keys ...string) (val []interface{}) {
if len(keys) == 0 {
return
}
if len(keys) == 1 {
if s.exists(keys[0]) {
return s.records[keys[0]].list()
}
return
}
inter := make([]set, 0, 1)
for _, k := range keys {
if s.exists(k) {
inter = append(inter, s.records[k])
}
}
m := intersection(inter[0], inter[1], inter[2:]...)
for v := range m {
val = append(val, v)
}
return
}
func (s *Set) SInterStore(key string, keys ...string) int {
if len(keys) == 0 {
return 0
}
if len(keys) == 1 {
if s.exists(keys[0]) {
return len(s.records[keys[0]])
}
return 0
}
m := s.SInter(keys...)
for _, k := range m {
s.SAdd(key, k)
}
return len(m)
}
func (s *Set) SKeyExists(key string) (ok bool) {
return s.exists(key)
}
func (s *Set) SClear(key string) {
if s.SKeyExists(key) {
delete(s.records, key)
}
}
func (s *Set) exists(key string) bool {
_, exist := s.records[key]
return exist
}
func (s *Set) fieldExists(key string, field interface{}) bool {
fieldSet, exist := s.records[key]
if !exist {
return false
}
ok := fieldSet.has(field)
return ok
}
func (s *Set) Keys() []string {
keys := make([]string, len(s.records))
i := 0
for k := range s.records {
keys[i] = k
i++
}
return keys
}