-
Notifications
You must be signed in to change notification settings - Fork 2
/
set.go
399 lines (346 loc) · 10 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
package goset
import (
"fmt"
"reflect"
)
// Uniq the slice of objects, the objects must be the same type, both builtin and custom types are supported.
func Uniq(elements interface{}) interface{} {
v := reflect.ValueOf(elements)
if !isAvailableSlice(v) {
return elements
}
if v.Len() <= 1 {
return elements
}
slim := reflect.MakeSlice(reflect.TypeOf(elements), 0, v.Cap())
for i := 0; i < v.Len(); i++ {
found := false
for j := 0; j < slim.Len(); j++ {
if reflect.DeepEqual(
v.Index(i).Interface(),
slim.Index(j).Interface(),
) {
found = true
}
}
if found {
continue
}
slim = reflect.Append(slim, v.Index(i))
}
return slim.Interface()
}
// Return the intersection of aSet and bSet, aSet and bSet must be a slice type, and the two slice must have the same type of elements.
// Empty slice is allowed, but nil is not allowed to represent the null set concept.
func Intersect(aSet interface{}, bSet interface{}) interface{} {
_, iSet, _, _ := Difference(aSet, bSet)
return iSet
}
// Return the union of aSet and bSet, aSet and bSet must be a slice type, and the two slice must have the same type of elements.
// Empty slice is allowed, but nil is not allowed to represent the null set concept.
func Union(aSet interface{}, bSet interface{}) interface{} {
uSet, _, _, _ := Difference(aSet, bSet)
return uSet
}
// Return the difference of aSet and bSet, aSet and bSet must be a slice type, and the two slice must have the same type of elements.
// Empty slice is allowed, but nil is not allowed to represent the null set concept.
func Difference(aSet interface{}, bSet interface{}) (iUnion, iIntersection, iADifferenceSet, iBDifferenceSet interface{}) {
av := reflect.ValueOf(aSet)
bv := reflect.ValueOf(bSet)
if !areAvailableSlices(av, bv) {
panic("A set and B set should be slices and have the same type of elements")
}
var union = reflect.MakeSlice(reflect.TypeOf(aSet), 0, av.Cap()+bv.Cap())
var intersection = reflect.MakeSlice(reflect.TypeOf(aSet), 0, av.Cap()+bv.Cap())
var aDifferenceSet = reflect.MakeSlice(reflect.TypeOf(aSet), 0, av.Cap())
var bDifferenceSet = reflect.MakeSlice(reflect.TypeOf(aSet), 0, bv.Cap())
for i := 0; i < av.Len(); i++ {
skip := false
for j := 0; j < bv.Len(); j++ {
if reflect.DeepEqual(
bv.Index(j).Interface(),
av.Index(i).Interface(),
) {
intersection = reflect.Append(intersection, bv.Index(j))
skip = true
}
}
if !skip {
aDifferenceSet = reflect.Append(aDifferenceSet, av.Index(i))
}
}
for i := 0; i < bv.Len(); i++ {
skip := false
for j := 0; j < intersection.Len(); j++ {
if reflect.DeepEqual(
intersection.Index(j).Interface(),
bv.Index(i).Interface(),
) {
skip = true
}
}
if !skip {
bDifferenceSet = reflect.Append(bDifferenceSet, bv.Index(i))
}
}
union = reflect.AppendSlice(aDifferenceSet, intersection)
union = reflect.AppendSlice(union, bDifferenceSet)
iUnion = union.Interface()
iIntersection = intersection.Interface()
iADifferenceSet = aDifferenceSet.Interface()
iBDifferenceSet = bDifferenceSet.Interface()
return iUnion, iIntersection, iADifferenceSet, iBDifferenceSet
}
// Add an element to a set, the element's type must be the same as the type of existed set's element
// If the element's value already exists on the set, the return set keep the same as the old set.
func AddElement(set interface{}, e interface{}) interface{} {
v := reflect.ValueOf(set)
if v.Type().Elem() != reflect.TypeOf(e) {
panic("Set and element are not the same type")
}
if !isAvailableSlice(v) {
panic("Invalid Slice")
}
if !IsUniq(set) {
panic("Set should be uniq")
}
ev := reflect.ValueOf(e)
for i := 0; i < v.Len(); i++ {
if reflect.DeepEqual(
e,
v.Index(i).Interface(),
) {
return set
}
}
v = reflect.Append(v, ev)
return v.Interface()
}
// Merge two sets, aSet and bSet must be the same type
// Same effect as the union aSet and bSet.
func AddElements(aSet interface{}, bSet interface{}) interface{} {
av := reflect.ValueOf(aSet)
bv := reflect.ValueOf(bSet)
if !areAvailableSlices(av, bv) {
panic("Invalid Slices")
}
for i := 0; i < bv.Len(); i++ {
aSet = AddElement(aSet, bv.Index(i).Interface())
}
return Uniq(aSet)
}
// Remove an element to a set, the element's type must be the same as the type of existed set's element
// If the element's value did not exists on the set, the return set keep the same as the old set.
func RemoveElement(set interface{}, e interface{}) interface{} {
v := reflect.ValueOf(set)
if !isAvailableSlice(v) {
panic("Invalid Slice")
}
if v.Len() == 0 {
return set
}
ev := reflect.ValueOf(e)
if !ev.IsValid() {
return set
}
for i := 0; i < v.Len(); i++ {
if reflect.DeepEqual(
e,
v.Index(i).Interface(),
) {
v = reflect.AppendSlice(
v.Slice(0, i),
v.Slice(i+1, v.Len()),
)
return v.Interface()
}
}
return set
}
// Reduce aSet's elements by looking up the bSet, aSet and bSet must be the same type
// Same effect as the difference aSet and bSet.
func RemoveElements(aSet interface{}, bSet interface{}) interface{} {
av := reflect.ValueOf(aSet)
bv := reflect.ValueOf(bSet)
if !areAvailableSlices(av, bv) {
panic("Invalid Slices")
}
newSetSlice := reflect.MakeSlice(reflect.TypeOf(aSet), av.Len(), av.Cap())
for i := 0; i < av.Len(); i++ {
newSetSlice.Index(i).Set(av.Index(i))
}
var newSet = newSetSlice.Interface()
for i := 0; i < bv.Len(); i++ {
newSet = RemoveElement(newSet, bv.Index(i).Interface())
}
return Uniq(newSet)
}
// To tell if the aSet is uniq, aSet must be a slices.
func IsUniq(aSet interface{}) bool {
v := reflect.ValueOf(aSet)
if !isAvailableSlice(v) {
return false
}
if v.Len() <= 1 {
return true
}
ele := v.Index(0).Interface()
others := reflect.MakeSlice(reflect.TypeOf(aSet), v.Len()-1, v.Cap())
for i := 1; i < v.Len(); i++ {
if reflect.DeepEqual(
ele,
v.Index(i).Interface(),
) {
return false
}
others = v.Slice(1, v.Len())
}
return IsUniq(others.Interface())
}
// Tell if the two set is equal, both aSet and bSet must be slices with same types.
// Sequence of elements is ignored to be the factor of equal detection.
func IsEqual(aSet interface{}, bSet interface{}) bool {
av := reflect.ValueOf(aSet)
bv := reflect.ValueOf(bSet)
if av.Len() != bv.Len() {
return false
}
if av.Len() == 0 && bv.Len() == 0 {
return true
}
if !areAvailableSlices(av, bv) {
return false
}
aMap := make(map[int]bool)
bMap := make(map[int]bool)
hits := 0
for i := 0; i < av.Len(); i++ {
if aMap[i] {
continue
}
found := false
for j := 0; j < bv.Len(); j++ {
if bMap[j] {
continue
}
if reflect.DeepEqual(
av.Index(i).Interface(),
bv.Index(j).Interface(),
) {
aMap[i] = true
bMap[j] = true
hits += 1
found = true
}
}
if !found {
return false
}
}
return hits == av.Len() && hits == bv.Len()
}
// Return true if set contains the ele element, set must be a slice with the same type of ele.
func IsIncluded(set interface{}, ele interface{}) bool {
ev := reflect.ValueOf(ele)
if !ev.IsValid() {
return true
}
v := reflect.ValueOf(set)
if !isAvailableSlice(v) {
return false
}
if v.Len() == 0 {
return false
}
if reflect.TypeOf(ev).String() != reflect.TypeOf(v.Index(0)).String() {
return false
}
for i := 0; i < v.Len(); i++ {
if reflect.DeepEqual(
v.Index(i).Interface(),
ev.Interface(),
) {
return true
}
}
return false
}
// Return true if aSet is the subset of bSet, bothe aSet and bSet must be slices and have the same type of elements.
func IsSubset(aSet interface{}, bSet interface{}) bool {
_, _, aSubSet, _ := Difference(aSet, bSet)
return reflect.ValueOf(aSubSet).Len() == 0
}
// Return true if aSet is the superset of bSet, bothe aSet and bSet must be slices and their elements's type must be the same too.
func IsSuperset(aSet interface{}, bSet interface{}) bool {
_, _, _, bSubSet := Difference(aSet, bSet)
return reflect.ValueOf(bSubSet).Len() == 0
}
// mapFunc accepts one parameter and output one parameter
// like func(string)int
// defaultReturnSlice is exacly mapped when the set is an empty slice
func Map(set interface{}, mapFunc interface{}, defaultReturnSlice interface{}) (mapped interface{}) {
vSet := reflect.ValueOf(set)
if vSet.Kind() != reflect.Slice {
panic("source set must be slice")
}
vFunc := reflect.ValueOf(mapFunc)
if vFunc.Kind() != reflect.Func {
panic("mapper must be a func")
}
if vSet.Len() == 0 {
return defaultReturnSlice
}
slim := reflect.MakeSlice(reflect.TypeOf(defaultReturnSlice), 0, vSet.Cap())
for i := 0; i < vSet.Len(); i++ {
ele := vFunc.Call([]reflect.Value{vSet.Index(i)})
slim = reflect.Append(slim, ele[0])
}
return slim.Interface()
}
// ids is a slice of buildin types
// objs is a slice of structs
// fieldName is the name of ids
// reorderObjects is the same set of objs with order changed according to the ids
func Reorder(ids interface{}, objs interface{}, fieldName string) (reordedObjects interface{}) {
objsType := reflect.TypeOf(objs)
if objsType.Kind() != reflect.Slice {
panic("objs should be slice")
}
idsType := reflect.TypeOf(ids)
if idsType.Kind() != reflect.Slice {
panic("ids should be slice")
}
idsValue := reflect.ValueOf(ids)
objsValue := reflect.ValueOf(objs)
// fast return if objs is empty or ids is abnormal
if objsValue.Len() == 0 || objsValue.Len() != idsValue.Len() {
return objs
}
idSlice := reflect.MakeSlice(idsType, 0, idsValue.Cap())
for i := 0; i < idsValue.Len(); i++ {
ev := idsValue.Index(i)
idSlice = reflect.Append(idSlice, ev)
}
m := make(map[string]reflect.Value)
for i := 0; i < objsValue.Len(); i++ {
var ev reflect.Value
old_ev := objsValue.Index(i)
if old_ev.Kind() == reflect.Ptr {
ev = old_ev.Elem()
} else {
ev = old_ev
}
id := ev.FieldByName(fieldName)
s := fmt.Sprintf("%v", id)
m[s] = old_ev
}
newObjs := reflect.MakeSlice(objsType, 0, objsValue.Cap())
for i := 0; i < idSlice.Len(); i++ {
id := idSlice.Index(i)
k := fmt.Sprintf("%v", id)
if v, ok := m[k]; ok {
newObjs = reflect.Append(newObjs, v)
}
}
return newObjs.Interface()
}