-
Notifications
You must be signed in to change notification settings - Fork 9
/
array.go
578 lines (538 loc) · 12.9 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
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
package php
import (
"math"
"reflect"
"sort"
)
// ArrayUnique removes duplicate values from an array,
// if the input is not a slice or empty then return the original input
//
// you can use type assertion to convert the result to the type of input
func ArrayUnique(array interface{}) interface{} {
type empty struct{}
if array == nil {
return nil
}
// if it's not a slice then return the original input
kind := reflect.TypeOf(array).Kind()
if kind != reflect.Slice && kind != reflect.Array {
return array
}
s := reflect.ValueOf(array)
if s.Len() == 0 {
return array
}
set := make(map[interface{}]empty)
for i := 0; i < s.Len(); i++ {
set[s.Index(i).Interface()] = empty{}
}
switch s.Index(0).Kind() {
case reflect.Bool:
result := make([]bool, 0, s.Len())
for k := range set {
result = append(result, k.(bool))
}
return result
case reflect.Int:
result := make([]int, 0, s.Len())
for k := range set {
result = append(result, k.(int))
}
return result
case reflect.Int8:
result := make([]int8, 0, s.Len())
for k := range set {
result = append(result, k.(int8))
}
return result
case reflect.Int16:
result := make([]int16, 0, s.Len())
for k := range set {
result = append(result, k.(int16))
}
return result
case reflect.Int32:
result := make([]int32, 0, s.Len())
for k := range set {
result = append(result, k.(int32))
}
return result
case reflect.Int64:
result := make([]int64, 0, s.Len())
for k := range set {
result = append(result, k.(int64))
}
return result
case reflect.Uint:
result := make([]uint, 0, s.Len())
for k := range set {
result = append(result, k.(uint))
}
return result
case reflect.Uint8:
result := make([]uint8, 0, s.Len())
for k := range set {
result = append(result, k.(uint8))
}
return result
case reflect.Uint16:
result := make([]uint16, 0, s.Len())
for k := range set {
result = append(result, k.(uint16))
}
return result
case reflect.Uint32:
result := make([]uint32, 0, s.Len())
for k := range set {
result = append(result, k.(uint32))
}
return result
case reflect.Uint64:
result := make([]uint64, 0, s.Len())
for k := range set {
result = append(result, k.(uint64))
}
return result
case reflect.Float32:
result := make([]float32, 0, s.Len())
for k := range set {
result = append(result, k.(float32))
}
return result
case reflect.Float64:
result := make([]float64, 0, s.Len())
for k := range set {
result = append(result, k.(float64))
}
return result
case reflect.Complex64:
result := make([]complex64, 0, s.Len())
for k := range set {
result = append(result, k.(complex64))
}
return result
case reflect.Complex128:
result := make([]complex128, 0, s.Len())
for k := range set {
result = append(result, k.(complex128))
}
return result
case reflect.String:
result := make([]string, 0, s.Len())
for k := range set {
result = append(result, k.(string))
}
return result
default:
result := make([]interface{}, 0, s.Len())
for k := range set {
result = append(result, k)
}
return result
}
}
// InArray checks if a value exists in an array or map
//
// needle is the element to search, haystack is the slice or map to be search
func InArray(needle interface{}, haystack interface{}) bool {
val := reflect.ValueOf(haystack)
switch val.Kind() {
case reflect.Slice, reflect.Array:
for i := 0; i < val.Len(); i++ {
if reflect.DeepEqual(needle, val.Index(i).Interface()) {
return true
}
}
case reflect.Map:
for _, k := range val.MapKeys() {
if reflect.DeepEqual(needle, val.MapIndex(k).Interface()) {
return true
}
}
}
return false
}
// ArrayChunk splits an array into chunks, returns nil if size < 1
func ArrayChunk(array []interface{}, size int) [][]interface{} {
if size < 1 {
return nil
}
length := len(array)
chunkNum := int(math.Ceil(float64(length) / float64(size)))
var chunks [][]interface{}
for i, end := 0, 0; chunkNum > 0; chunkNum-- {
end = (i + 1) * size
if end > length {
end = length
}
chunks = append(chunks, array[i*size:end])
i++
}
return chunks
}
// ArrayColumn returns the values from a single column in the input array
func ArrayColumn(input []map[string]interface{}, columnKey string) []interface{} {
columns := make([]interface{}, 0, len(input))
for _, val := range input {
if v, ok := val[columnKey]; ok {
columns = append(columns, v)
}
}
return columns
}
// ArrayCombine creates an array by using one array for keys and another for its values
func ArrayCombine(keys, values []interface{}) map[interface{}]interface{} {
if len(keys) != len(values) {
return nil
}
m := make(map[interface{}]interface{}, len(keys))
for i, v := range keys {
m[v] = values[i]
}
return m
}
// ArrayDiff computes the difference of arrays
func ArrayDiff(array1, array2 []interface{}) []interface{} {
var res []interface{}
for _, v := range array1 {
if !InArray(v, array2) {
res = append(res, v)
}
}
return res
}
// ArrayIntersect computes the intersection of arrays
func ArrayIntersect(array1, array2 []interface{}) []interface{} {
var res []interface{}
for _, v := range array1 {
if InArray(v, array2) {
res = append(res, v)
}
}
return res
}
// ArrayFlip exchanges all keys with their associated values in an array
func ArrayFlip(input interface{}) interface{} {
if input == nil {
return nil
}
val := reflect.ValueOf(input)
if val.Len() == 0 {
return nil
}
res := make(map[interface{}]interface{}, val.Len())
switch val.Kind() {
case reflect.Slice, reflect.Array:
for i := 0; i < val.Len(); i++ {
res[val.Index(i).Interface()] = i
}
return res
case reflect.Map:
for _, k := range val.MapKeys() {
res[val.MapIndex(k).Interface()] = k.Interface()
}
return res
}
return nil
}
// ArrayKeys returns all the keys or a subset of the keys of an array
func ArrayKeys(input interface{}) interface{} {
if input == nil {
return nil
}
val := reflect.ValueOf(input)
if val.Len() == 0 {
return nil
}
switch val.Kind() {
case reflect.Slice, reflect.Array:
var res []int
for i := 0; i < val.Len(); i++ {
res = append(res, i)
}
return res
case reflect.Map:
var res []string
for _, k := range val.MapKeys() {
res = append(res, k.String())
}
sort.SliceStable(res, func(i, j int) bool {
return res[i] < res[j]
})
return res
}
return nil
}
// ArrayKeyExists is alias of KeyExists()
func ArrayKeyExists(k interface{}, m map[interface{}]interface{}) bool {
return KeyExists(k, m)
}
// KeyExists checks if the given key or index exists in the array
func KeyExists(k interface{}, m map[interface{}]interface{}) bool {
_, ok := m[k]
return ok
}
// Count counts all elements in an array or map
func Count(v interface{}) int {
if v == nil {
return 0
}
return reflect.ValueOf(v).Len()
}
// ArrayFilter filters elements of an array using a callback function
func ArrayFilter(input interface{}, callback func(interface{}) bool) interface{} {
if input == nil {
return nil
}
val := reflect.ValueOf(input)
if val.Len() == 0 {
return nil
}
if callback == nil {
callback = func(v interface{}) bool {
return v != nil
}
}
switch val.Kind() {
case reflect.Slice, reflect.Array:
var res []interface{}
for i := 0; i < val.Len(); i++ {
v := val.Index(i).Interface()
if callback(v) {
res = append(res, v)
}
}
return res
case reflect.Map:
res := make(map[interface{}]interface{})
for _, k := range val.MapKeys() {
v := val.MapIndex(k).Interface()
if callback(v) {
res[k.Interface()] = v
}
}
return res
}
return input
}
// ArrayPad pads array to the specified length with a value
func ArrayPad(array []interface{}, size int, value interface{}) []interface{} {
if size == 0 || (size > 0 && size < len(array)) || (size < 0 && size > -len(array)) {
return array
}
n := size
if size < 0 {
n = -size
}
n -= len(array)
tmp := make([]interface{}, n)
for i := 0; i < n; i++ {
tmp[i] = value
}
if size > 0 {
return append(array, tmp...)
}
return append(tmp, array...)
}
// ArrayPop pops the element off the end of array
func ArrayPop(s *[]interface{}) interface{} {
if s == nil || len(*s) == 0 {
return nil
}
ep := len(*s) - 1
e := (*s)[ep]
*s = (*s)[:ep]
return e
}
// ArrayPush pushes one or more elements onto the end of array,
// returns the new number of elements in the array
func ArrayPush(s *[]interface{}, elements ...interface{}) int {
if s == nil {
return 0
}
*s = append(*s, elements...)
return len(*s)
}
// ArrayShift shifts an element off the beginning of array
func ArrayShift(s *[]interface{}) interface{} {
if s == nil || len(*s) == 0 {
return nil
}
f := (*s)[0]
*s = (*s)[1:]
return f
}
// ArrayUnshift prepends one or more elements to the beginning of a array,
// returns the new number of elements in the array.
func ArrayUnshift(s *[]interface{}, elements ...interface{}) int {
if s == nil {
return 0
}
*s = append(elements, *s...)
return len(*s)
}
// ArrayReverse returns an array with elements in reverse order
func ArrayReverse(s []interface{}) []interface{} {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
return s
}
// ArraySlice extracts a slice of the array
func ArraySlice(array []interface{}, offset, length uint) []interface{} {
if offset > uint(len(array)) {
return nil
}
end := offset + length
if end < uint(len(array)) {
return array[offset:end]
}
return array[offset:]
}
// ArraySum returns the sum of values in an array
func ArraySum(array interface{}) interface{} {
if array == nil {
return nil
}
kind := reflect.TypeOf(array).Kind()
if kind != reflect.Slice && kind != reflect.Array {
return nil
}
s := reflect.ValueOf(array)
if s.Len() == 0 {
return 0
}
switch s.Index(0).Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
var sum int64
for i := 0; i < s.Len(); i++ {
sum += s.Index(i).Int()
}
return sum
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
var sum uint64
for i := 0; i < s.Len(); i++ {
sum += s.Index(i).Uint()
}
return sum
case reflect.Float32, reflect.Float64:
var sum float64
for i := 0; i < s.Len(); i++ {
sum += s.Index(i).Float()
}
return sum
case reflect.String:
var sum string
for i := 0; i < s.Len(); i++ {
sum += s.Index(i).String()
}
return sum
}
return nil
}
// Sort sorts an array (lowest to highest)
func Sort(array interface{}) interface{} {
if array == nil {
return nil
}
kind := reflect.TypeOf(array).Kind()
if kind != reflect.Slice && kind != reflect.Array {
return nil
}
s := reflect.ValueOf(array)
if s.Len() == 0 {
return array
}
switch s.Index(0).Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
var res = make([]int64, s.Len())
for i := 0; i < s.Len(); i++ {
res[i] = s.Index(i).Int()
}
sort.Slice(res, func(i int, j int) bool {
return res[i] < res[j]
})
return res
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
var res = make([]uint64, s.Len())
for i := 0; i < s.Len(); i++ {
res[i] = s.Index(i).Uint()
}
sort.Slice(res, func(i int, j int) bool {
return res[i] < res[j]
})
return res
case reflect.Float32, reflect.Float64:
var res = make([]float64, s.Len())
for i := 0; i < s.Len(); i++ {
res[i] = s.Index(i).Float()
}
sort.Slice(res, func(i int, j int) bool {
return res[i] < res[j]
})
return res
case reflect.String:
var res = make([]string, s.Len())
for i := 0; i < s.Len(); i++ {
res[i] = s.Index(i).String()
}
sort.Slice(res, func(i int, j int) bool {
return res[i] < res[j]
})
return res
}
return array
}
// Rsort sorts an array in reverse order (highest to lowest)
func Rsort(array interface{}) interface{} {
if array == nil {
return nil
}
kind := reflect.TypeOf(array).Kind()
if kind != reflect.Slice && kind != reflect.Array {
return nil
}
s := reflect.ValueOf(array)
if s.Len() == 0 {
return array
}
switch s.Index(0).Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
var res = make([]int64, s.Len())
for i := 0; i < s.Len(); i++ {
res[i] = s.Index(i).Int()
}
sort.Slice(res, func(i int, j int) bool {
return res[i] > res[j]
})
return res
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
var res = make([]uint64, s.Len())
for i := 0; i < s.Len(); i++ {
res[i] = s.Index(i).Uint()
}
sort.Slice(res, func(i int, j int) bool {
return res[i] > res[j]
})
return res
case reflect.Float32, reflect.Float64:
var res = make([]float64, s.Len())
for i := 0; i < s.Len(); i++ {
res[i] = s.Index(i).Float()
}
sort.Slice(res, func(i int, j int) bool {
return res[i] > res[j]
})
return res
case reflect.String:
var res = make([]string, s.Len())
for i := 0; i < s.Len(); i++ {
res[i] = s.Index(i).String()
}
sort.Slice(res, func(i int, j int) bool {
return res[i] > res[j]
})
return res
}
return array
}