-
Notifications
You must be signed in to change notification settings - Fork 9
/
array.go
339 lines (299 loc) · 6.76 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
package php
import (
"math"
"reflect"
"sort"
"golang.org/x/exp/constraints"
)
type empty struct{}
// ArrayUnique removes duplicate values from an array,
// if the input is empty then return the original input
func ArrayUnique[T comparable](arr []T) []T {
set := make(map[T]empty)
for _, v := range arr {
set[v] = empty{}
}
result := make([]T, 0)
for k := range set {
result = append(result, k)
}
return result
}
// InArray checks if a value exists in an array
//
// needle is the element to search, haystack is the slice to be searched
func InArray[T comparable](needle T, haystack []T) bool {
for _, v := range haystack {
if needle == v {
return true
}
}
return false
}
// ArrayChunk splits an array into chunks, returns nil if size < 1
func ArrayChunk[T comparable](array []T, size int) [][]T {
if size < 1 {
return nil
}
length := len(array)
chunkNum := int(math.Ceil(float64(length) / float64(size)))
var chunks [][]T
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[T comparable](input []map[string]T, columnKey string) []T {
columns := make([]T, 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[K, V comparable](keys []K, values []V) map[K]V {
if len(keys) != len(values) {
return nil
}
m := make(map[K]V, len(keys))
for i, v := range keys {
m[v] = values[i]
}
return m
}
// ArrayDiff computes the difference of arrays
func ArrayDiff[T comparable](array1, array2 []T) []T {
var res []T
for _, v := range array1 {
if !InArray(v, array2) {
res = append(res, v)
}
}
return res
}
// ArrayIntersect computes the intersection of arrays
func ArrayIntersect[T comparable](array1, array2 []T) []T {
var res []T
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 any) any {
if input == nil {
return nil
}
val := reflect.ValueOf(input)
if val.Len() == 0 {
return nil
}
res := make(map[any]any, 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 any) any {
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, V comparable](k K, m map[K]V) bool {
return KeyExists(k, m)
}
// KeyExists checks if the given key or index exists in the array
func KeyExists[K, V comparable](k K, m map[K]V) bool {
_, ok := m[k]
return ok
}
// Count counts all elements in an array or map
func Count(v any) int {
if v == nil {
return 0
}
return reflect.ValueOf(v).Len()
}
// ArrayFilter filters elements of an array using a callback function
func ArrayFilter(input any, callback func(any) bool) any {
if input == nil {
return nil
}
val := reflect.ValueOf(input)
if val.Len() == 0 {
return nil
}
if callback == nil {
callback = func(v any) bool {
return v != nil
}
}
switch val.Kind() {
case reflect.Slice, reflect.Array:
var res []any
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[any]any)
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[T comparable](array []T, size int, value T) []T {
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([]T, 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[T comparable](s *[]T) T {
var t T
if s == nil || len(*s) == 0 {
return t
}
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[T comparable](s *[]T, elements ...T) int {
if s == nil {
return 0
}
*s = append(*s, elements...)
return len(*s)
}
// ArrayShift shifts an element off the beginning of array
func ArrayShift[T comparable](s *[]T) T {
var t T
if s == nil || len(*s) == 0 {
return t
}
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[T comparable](s *[]T, elements ...T) int {
if s == nil {
return 0
}
*s = append(elements, *s...)
return len(*s)
}
// ArrayReverse returns an array with elements in reverse order
func ArrayReverse[T comparable](s []T) []T {
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[T comparable](array []T, offset, length uint) []T {
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[T constraints.Ordered](array []T) T {
var sum T
for _, v := range array {
sum += v
}
return sum
}
// Sort sorts an array (lowest to highest)
func Sort[T constraints.Ordered](array []T) []T {
if len(array) == 0 {
return array
}
sort.Slice(array, func(i int, j int) bool {
return array[i] < array[j]
})
return array
}
// Rsort sorts an array in reverse order (highest to lowest)
func Rsort[T constraints.Ordered](array []T) []T {
if len(array) == 0 {
return array
}
sort.Slice(array, func(i int, j int) bool {
return array[i] > array[j]
})
return array
}