-
Notifications
You must be signed in to change notification settings - Fork 0
/
slice.go
300 lines (265 loc) · 6.94 KB
/
slice.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
package util
import (
"reflect"
)
// check if element exists in list
func Contains(list interface{}, e interface{}) bool {
v := reflect.ValueOf(list)
for i := 0; i < v.Len(); i++ {
if v.Index(i).Interface() == e {
return true
}
}
return false
}
// export slice from list by field
func FieldSlice(s interface{}, field string) (reflect.Value, bool) {
sl, ok := getArg(s, reflect.Slice)
if !ok {
return reflect.Value{}, false
}
c := sl.Len()
if c == 0 {
return reflect.Value{}, false
}
typ := reflect.Indirect(sl.Index(0)).FieldByName(field).Type()
out := reflect.MakeSlice(reflect.SliceOf(typ), c, c)
for i := 0; i < c; i++ {
e := reflect.Indirect(sl.Index(i)).FieldByName(field)
out.Index(i).Set(e)
}
return out, true
}
// Distinct returns the unique vals of a slice
//
// [1, 1, 2, 3] >> [1, 2, 3]
func Distinct(arr interface{}) (reflect.Value, bool) {
// create a slice from our input interface
slice, ok := getArg(arr, reflect.Slice)
if !ok {
return reflect.Value{}, ok
}
// put the values of our slice into a map
// the key's of the map will be the slice's unique values
c := slice.Len()
m := make(map[interface{}]bool)
for i := 0; i < c; i++ {
m[slice.Index(i).Interface()] = true
}
mapLen := len(m)
// create the output slice and populate it with the map's keys
out := reflect.MakeSlice(reflect.TypeOf(arr), mapLen, mapLen)
i := 0
for k := range m {
v := reflect.ValueOf(k)
o := out.Index(i)
o.Set(v)
i++
}
return out, ok
}
// Intersect returns a slice of values that are present in all of the input slices
//
// [1, 1, 3, 4, 5, 6] & [2, 3, 6] >> [3, 6]
//
// [1, 1, 3, 4, 5, 6] >> [1, 3, 4, 5, 6]
func Intersect(arrs ...interface{}) (reflect.Value, bool) {
// create a map to count all the instances of the slice elems
arrLength := len(arrs)
var kind reflect.Kind
var kindHasBeenSet bool
tempMap := make(map[interface{}]int)
for _, arg := range arrs {
tempArr, ok := Distinct(arg)
if !ok {
return reflect.Value{}, ok
}
// check to be sure the type hasn't changed
if kindHasBeenSet && tempArr.Len() > 0 && tempArr.Index(0).Kind() != kind {
return reflect.Value{}, false
}
if tempArr.Len() > 0 {
kindHasBeenSet = true
kind = tempArr.Index(0).Kind()
}
c := tempArr.Len()
for idx := 0; idx < c; idx++ {
// how many times have we encountered this elem?
if _, ok := tempMap[tempArr.Index(idx).Interface()]; ok {
tempMap[tempArr.Index(idx).Interface()]++
} else {
tempMap[tempArr.Index(idx).Interface()] = 1
}
}
}
// find the keys equal to the length of the input args
numElems := 0
for _, v := range tempMap {
if v == arrLength {
numElems++
}
}
out := reflect.MakeSlice(reflect.TypeOf(arrs[0]), numElems, numElems)
i := 0
for key, val := range tempMap {
if val == arrLength {
v := reflect.ValueOf(key)
o := out.Index(i)
o.Set(v)
i++
}
}
return out, true
}
// Substract returns a slice of values that are only present in one of the input slices
//
// [1, 2, 2, 4, 6] & [2, 4, 5] >> [1, 6]
//
// [1, 1, 3, 4, 5, 6] >> [1, 3, 4, 5, 6]
func Substract(arrs ...interface{}) (reflect.Value, bool) {
// create a temporary map to hold the contents of the arrays
tempMap := make(map[interface{}]int)
var kind reflect.Kind
var kindHasBeenSet bool
for fi, arg := range arrs {
tempArr, ok := Distinct(arg)
if !ok {
return reflect.Value{}, ok
}
// check to be sure the type hasn't changed
if kindHasBeenSet && tempArr.Len() > 0 && tempArr.Index(0).Kind() != kind {
return reflect.Value{}, false
}
if tempArr.Len() > 0 {
kindHasBeenSet = true
kind = tempArr.Index(0).Kind()
}
c := tempArr.Len()
for idx := 0; idx < c; idx++ {
// how many times have we encountered this elem?
if _, ok := tempMap[tempArr.Index(idx).Interface()]; ok {
tempMap[tempArr.Index(idx).Interface()]++
} else if fi == 0 {
tempMap[tempArr.Index(idx).Interface()] = 1
}
}
}
// write the final val of the diffMap to an array and return
numElems := 0
for _, v := range tempMap {
if v == 1 {
numElems++
}
}
out := reflect.MakeSlice(reflect.TypeOf(arrs[0]), numElems, numElems)
i := 0
for key, val := range tempMap {
if val == 1 {
v := reflect.ValueOf(key)
o := out.Index(i)
o.Set(v)
i++
}
}
return out, true
}
// Union returns a slice that contains the unique values of all the input slices
//
// [1, 2, 2, 4, 6] & [2, 4, 5] >> [1, 2, 4, 5, 6]
//
// [1, 1, 3, 4, 5, 6] >> [1, 3, 4, 5, 6]
func Union(arrs ...interface{}) (reflect.Value, bool) {
// create a temporary map to hold the contents of the arrays
tempMap := make(map[interface{}]uint8)
var kind reflect.Kind
var kindHasBeenSet bool
// write the contents of the arrays as keys to the map. The map values don't matter
for _, arg := range arrs {
tempArr, ok := Distinct(arg)
if !ok {
return reflect.Value{}, ok
}
// check to be sure the type hasn't changed
if kindHasBeenSet && tempArr.Len() > 0 && tempArr.Index(0).Kind() != kind {
return reflect.Value{}, false
}
if tempArr.Len() > 0 {
kindHasBeenSet = true
kind = tempArr.Index(0).Kind()
}
c := tempArr.Len()
for idx := 0; idx < c; idx++ {
tempMap[tempArr.Index(idx).Interface()] = 0
}
}
// the map keys are now unique instances of all of the array contents
mapLen := len(tempMap)
out := reflect.MakeSlice(reflect.TypeOf(arrs[0]), mapLen, mapLen)
i := 0
for key := range tempMap {
v := reflect.ValueOf(key)
o := out.Index(i)
o.Set(v)
i++
}
return out, true
}
// Difference returns a slice of values that are only present in one of the input slices
//
// [1, 2, 2, 4, 6] & [2, 4, 5] >> [1, 5, 6]
//
// [1, 1, 3, 4, 5, 6] >> [1, 3, 4, 5, 6]
func Difference(arrs ...interface{}) (reflect.Value, bool) {
// create a temporary map to hold the contents of the arrays
tempMap := make(map[interface{}]int)
var kind reflect.Kind
var kindHasBeenSet bool
for _, arg := range arrs {
tempArr, ok := Distinct(arg)
if !ok {
return reflect.Value{}, ok
}
// check to be sure the type hasn't changed
if kindHasBeenSet && tempArr.Len() > 0 && tempArr.Index(0).Kind() != kind {
return reflect.Value{}, false
}
if tempArr.Len() > 0 {
kindHasBeenSet = true
kind = tempArr.Index(0).Kind()
}
c := tempArr.Len()
for idx := 0; idx < c; idx++ {
// how many times have we encountered this elem?
if _, ok := tempMap[tempArr.Index(idx).Interface()]; ok {
tempMap[tempArr.Index(idx).Interface()]++
} else {
tempMap[tempArr.Index(idx).Interface()] = 1
}
}
}
// write the final val of the diffMap to an array and return
numElems := 0
for _, v := range tempMap {
if v == 1 {
numElems++
}
}
out := reflect.MakeSlice(reflect.TypeOf(arrs[0]), numElems, numElems)
i := 0
for key, val := range tempMap {
if val == 1 {
v := reflect.ValueOf(key)
o := out.Index(i)
o.Set(v)
i++
}
}
return out, true
}
func getArg(arg interface{}, kind reflect.Kind) (val reflect.Value, ok bool) {
val = reflect.ValueOf(arg)
if val.Kind() == kind {
ok = true
}
return
}