forked from arthurkushman/pgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
array.go
338 lines (261 loc) · 7.17 KB
/
array.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
package pgo
import (
"fmt"
"reflect"
"sort"
)
// InArray checks if a value exists in an array
func InArray(needle interface{}, haystack interface{}) bool {
return search(needle, haystack)
}
func search(needle interface{}, haystack interface{}) bool {
switch reflect.TypeOf(haystack).Kind() {
case reflect.Slice:
s := reflect.ValueOf(haystack)
l := s.Len()
for i := 0; i < l; i++ {
if needle == s.Index(i).Interface() {
return true
}
}
}
return false
}
// ArrayChunk split an array into chunks
func ArrayChunk(array interface{}, size int) []interface{} {
var chunks []interface{}
s := reflect.ValueOf(array)
l := s.Len()
var subChunk []interface{}
for i := 0; i < l; i++ {
subChunk = append(subChunk, s.Index(i).Interface())
if (i+1)%size == 0 || i+1 == l {
chunks = append(chunks, subChunk)
subChunk = make([]interface{}, 0)
}
}
return chunks
}
// ArrayCombine creates an array by using one array for keys and another for its values
// returns map[key]value if both slices are equal and nil otherwise
func ArrayCombine(keys interface{}, values interface{}) map[interface{}]interface{} {
s := reflect.ValueOf(keys)
l := s.Len()
ss := reflect.ValueOf(values)
ssLen := ss.Len()
if l != ssLen {
return nil
}
resultMap := make(map[interface{}]interface{})
for i := 0; i < l; i++ {
resultMap[s.Index(i).Interface()] = ss.Index(i).Interface()
}
return resultMap
}
// ArrayCountValues counts all the values of an array/slice
func ArrayCountValues(array interface{}) map[interface{}]int {
res := make(map[interface{}]int)
s := reflect.ValueOf(array)
l := s.Len()
for i := 0; i < l; i++ {
res[s.Index(i).Interface()]++
}
return res
}
// ArrayMap applies the callback to the elements of the given arrays
func ArrayMap(array interface{}, callback interface{}) []interface{} {
s := reflect.ValueOf(array)
l := s.Len()
funcValue := reflect.ValueOf(callback)
var result []interface{}
for i := 0; i < l; i++ {
result = append(result, funcValue.Call([]reflect.Value{s.Index(i)})[0].Interface())
}
return result
}
// ArrayFilter filters elements of an array using a callback function
func ArrayFilter(array interface{}, callback interface{}) []interface{} {
s := reflect.ValueOf(array)
l := s.Len()
funcValue := reflect.ValueOf(callback)
var result []interface{}
for i := 0; i < l; i++ {
if funcValue.Call([]reflect.Value{s.Index(i)})[0].Bool() {
result = append(result, s.Index(i).Interface())
}
}
return result
}
// ArrayDiff compares array1 against one or more other arrays
// returns the values in array1 that are not present in any of the other arrays
func ArrayDiff(arrays ...interface{}) []interface{} {
s := reflect.ValueOf(arrays[0])
l := s.Len()
var result []interface{}
isFound := false
for i := 0; i < l; i++ {
needle := s.Index(i).Interface()
for _, v := range arrays[1:] {
switch reflect.TypeOf(v).Kind() {
case reflect.Slice:
ss := reflect.ValueOf(v)
sLen := ss.Len()
for j := 0; j < sLen; j++ {
if needle == ss.Index(j).Interface() {
isFound = true
}
}
}
}
if !isFound {
result = append(result, needle)
}
isFound = false
}
return result
}
// ArrayUdiff computes the difference of arrays by using a callback function for data comparison
func ArrayUdiff(uf func(interface{}, interface{}) int, arrays ...interface{}) []interface{} {
var result []interface{}
first := reflect.ValueOf(arrays[0])
firstLen := first.Len()
elementType := reflect.TypeOf(arrays[0]).Elem()
originFirst := reflect.MakeSlice(reflect.SliceOf(elementType), firstLen, firstLen)
reflect.Copy(originFirst, first)
for _, ar := range arrays {
sort.Slice(ar, func(i, j int) bool {
first := reflect.ValueOf(ar)
return 0 > uf(first.Index(i).Interface(), first.Index(j).Interface())
})
}
tempResult := make(map[interface{}]interface{})
for i := 0; i < firstLen; i++ {
needle := first.Index(i).Interface()
isFound := false
for _, v := range arrays[1:] {
second := reflect.ValueOf(v)
secondLen := second.Len()
for j := 0; j < secondLen; j++ {
valSecond := second.Index(j).Interface()
compareResult := uf(needle, valSecond)
if compareResult < 0 {
break
}
if compareResult == 0 {
isFound = true
break
}
}
if isFound {
break
}
}
if !isFound {
tempResult[needle] = needle
}
}
originFirstLen := originFirst.Len()
for i := 0; i < originFirstLen && len(tempResult) > 0; i++ {
needle := originFirst.Index(i).Interface()
val, ok := tempResult[needle]
if ok {
result = append(result, val)
delete(tempResult, needle)
}
}
return result
}
// ArraySum calculate the sum of values in an array
func ArraySum(array interface{}) (float64, error) {
s := reflect.ValueOf(array)
l := s.Len()
var amount float64
for i := 0; i < l; i++ {
v, err := getFloat(s.Index(i).Interface())
if err != nil {
return v, err
}
amount += v
}
return amount, nil
}
func getFloat(unk interface{}) (float64, error) {
var floatType = reflect.TypeOf(float64(0))
v := reflect.ValueOf(unk)
v = reflect.Indirect(v)
if !v.Type().ConvertibleTo(floatType) {
return 0, fmt.Errorf("cannot convert %v to float64", v.Type())
}
fv := v.Convert(floatType)
return fv.Float(), nil
}
// ArrayIntersect computes the intersection of arrays
func ArrayIntersect(arrays ...interface{}) []interface{} {
s := reflect.ValueOf(arrays[0])
l := s.Len()
var result []interface{}
isFound := false
intersected := make(map[interface{}]bool)
for i := 0; i < l; i++ {
needle := s.Index(i).Interface()
for _, v := range arrays[1:] {
switch reflect.TypeOf(v).Kind() {
case reflect.Slice:
ss := reflect.ValueOf(v)
sLen := ss.Len()
for j := 0; j < sLen; j++ {
if needle == ss.Index(j).Interface() && !intersected[needle] {
isFound = true
intersected[needle] = true // del op is more expensive for slices
goto out // it is stupid to iterate O(n^2) if found
}
}
}
}
out:
if isFound {
result = append(result, needle)
}
isFound = false
}
return result
}
// Range creates int slice of min to max range
// If a step value is given, it will be used as the increment between elements in the sequence.
// step should be given as a positive number.
// If not specified, step will default to 1.
func Range(min, max int, step ...interface{}) []int {
var slice []int
var argsLen = len(step)
var stepp = 1
if argsLen > 0 && step[0] != nil {
st, ok := step[0].(int)
if !ok || st > 1 {
stepp = st
}
}
for i := min; i <= max; i += stepp {
slice = append(slice, i)
}
return slice
}
// EqualSlices compares two slices and returns true if they are equal, false otherwise
// in case of passing wrong (non-slice) arguments error will be returned
func EqualSlices(a, b interface{}) (bool, error) {
if reflect.TypeOf(a).Kind() != reflect.Slice || reflect.TypeOf(b).Kind() != reflect.Slice {
return false, fmt.Errorf("only slice arguments allowed")
}
sa := reflect.ValueOf(a)
la := sa.Len()
sb := reflect.ValueOf(b)
lb := sb.Len()
if la != lb {
return false, nil
}
for i := 0; i < la; i++ {
if sa.Index(i).Interface() != sb.Index(i).Interface() {
return false, nil
}
}
return true, nil
}