-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathslice_algo.go
439 lines (390 loc) · 10.7 KB
/
slice_algo.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
package gofn
// Compact excludes all zero items in a slice
func Compact[T comparable, S ~[]T](s S) S {
result := make(S, 0, len(s))
var zeroT T
for _, v := range s {
if v == zeroT {
continue
}
result = append(result, v)
}
return result
}
// Drop returns a copied slice with dropping items in the list
func Drop[T comparable, S ~[]T](a S, values ...T) S {
return FilterNIN(a, values...)
}
// Reverse reverses slice content, this modifies the slice
func Reverse[T any, S ~[]T](s S) S {
if len(s) == 0 {
return s
}
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
}
// ReverseCopy returns a new slice which has content in reversed order
func ReverseCopy[T any, S ~[]T](s S) S {
result := make(S, len(s))
for i, j := 0, len(s)-1; j >= 0; j-- {
result[i] = s[j]
i++
}
return result
}
// Shuffle items of a slice and returns a new slice
func Shuffle[T any, S ~[]T](s S, randFuncs ...func(n int) int) S {
if len(s) <= 1 {
return append(S{}, s...)
}
maker := NewRandChoiceMaker(s, randFuncs...)
result := make(S, 0, len(s))
for {
item, valid := maker.Next()
if !valid {
break
}
result = append(result, item)
}
return result
}
// Chunk splits slice content into chunks by chunk size
func Chunk[T any, S ~[]T](s S, chunkSize int) []S {
if chunkSize <= 0 {
return []S{}
}
chunks := make([]S, 0, len(s)/chunkSize+1)
for {
if len(s) == 0 {
break
}
if len(s) < chunkSize {
chunkSize = len(s)
}
chunks = append(chunks, s[0:chunkSize])
s = s[chunkSize:]
}
return chunks
}
// ChunkByPieces splits slice content into chunks by number of pieces
func ChunkByPieces[T any, S ~[]T](s S, chunkCount int) []S {
if chunkCount <= 0 {
return []S{}
}
chunkSize := len(s) / chunkCount
if chunkSize*chunkCount < len(s) {
chunkSize++
}
return Chunk(s, chunkSize)
}
// Reduce reduces a slice to a value
func Reduce[T any, S ~[]T](s S, reduceFunc func(accumulator, currentValue T) T) T {
length := len(s)
if length == 0 {
var zeroT T
return zeroT
}
accumulator := s[0]
for i := 1; i < length; i++ {
accumulator = reduceFunc(accumulator, s[i])
}
return accumulator
}
// ReduceEx reduces a slice to a value with custom initial value
func ReduceEx[T any, U any, S ~[]T](
s S,
reduceFunc func(accumulator U, currentValue T, currentIndex int) U,
initVal U,
) U {
accumulator := initVal
for i, v := range s {
accumulator = reduceFunc(accumulator, v, i)
}
return accumulator
}
// ReduceReverse reduces a slice to a value
func ReduceReverse[T any, S ~[]T](s S, reduceFunc func(accumulator, currentValue T) T) T {
length := len(s)
if length == 0 {
var zeroT T
return zeroT
}
accumulator := s[length-1]
for i := length - 2; i >= 0; i-- { //nolint:mnd
accumulator = reduceFunc(accumulator, s[i])
}
return accumulator
}
// ReduceReverseEx reduces a slice to a value with custom initial value
func ReduceReverseEx[T any, U any, S ~[]T](
s S,
reduceFunc func(accumulator U, currentValue T, currentIndex int) U,
initVal U,
) U {
accumulator := initVal
for i := len(s) - 1; i >= 0; i-- {
accumulator = reduceFunc(accumulator, s[i], i)
}
return accumulator
}
// Partition splits slice items into 2 lists.
func Partition[T any, S ~[]T](s S, partitionFunc func(T, int) bool) (S, S) {
partition0, partitionRemaining := S{}, S{}
for i, v := range s {
if partitionFunc(v, i) {
partition0 = append(partition0, v)
} else {
partitionRemaining = append(partitionRemaining, v)
}
}
return partition0, partitionRemaining
}
// PartitionN splits slice items into N lists.
// partitionFunc should return index of the partition to put the corresponding item into.
func PartitionN[T any, S ~[]T](s S, numPartitions uint, partitionFunc func(T, int) int) []S {
if numPartitions <= 0 {
return []S{}
}
partitions := make([]S, numPartitions)
for i := range partitions {
partitions[i] = S{}
}
lastIndex := int(numPartitions) - 1 //nolint:gosec
for i, v := range s {
pIndex := partitionFunc(v, i)
if pIndex < 0 || pIndex > lastIndex {
pIndex = lastIndex
}
partitions[pIndex] = append(partitions[pIndex], v)
}
return partitions
}
// Union returns all unique values from multiple slices
func Union[T comparable, S ~[]T](a, b S) S {
lenA, lenB := len(a), len(b)
if lenA == 0 {
return ToSet(b)
}
if lenB == 0 {
return ToSet(a)
}
seen := make(map[T]struct{}, lenA+lenB)
result := make(S, 0, lenA+lenB)
for i := 0; i < lenA; i++ {
v := a[i]
if _, ok := seen[v]; ok {
continue
}
seen[v] = struct{}{}
result = append(result, v)
}
for i := 0; i < lenB; i++ {
v := b[i]
if _, ok := seen[v]; ok {
continue
}
seen[v] = struct{}{}
result = append(result, v)
}
return result
}
// UnionBy returns all unique values from multiple slices with key function
func UnionBy[T any, K comparable, S ~[]T](a, b S, keyFunc func(t T) K) S {
lenA, lenB := len(a), len(b)
if lenA == 0 {
return ToSetBy(b, keyFunc)
}
if lenB == 0 {
return ToSetBy(a, keyFunc)
}
seen := make(map[K]struct{}, lenA+lenB)
result := make(S, 0, lenA+lenB)
for i := 0; i < lenA; i++ {
v := a[i]
k := keyFunc(v)
if _, ok := seen[k]; ok {
continue
}
seen[k] = struct{}{}
result = append(result, v)
}
for i := 0; i < lenB; i++ {
v := b[i]
k := keyFunc(v)
if _, ok := seen[k]; ok {
continue
}
seen[k] = struct{}{}
result = append(result, v)
}
return result
}
// Deprecated: use UnionBy instead
func UnionPred[T any, K comparable, S ~[]T](a, b S, keyFunc func(t T) K) S {
return UnionBy(a, b, keyFunc)
}
// Intersection returns all unique shared values from multiple slices
func Intersection[T comparable, S ~[]T](a, b S) S {
lenA, lenB := len(a), len(b)
if lenA == 0 || lenB == 0 {
return S{}
}
seen := make(map[T]*bool, lenA)
result := make(S, 0, Min(lenA, lenB))
for i := 0; i < lenA; i++ {
active := true
seen[a[i]] = &active
}
for i := 0; i < lenB; i++ {
v := b[i]
if active, ok := seen[v]; ok && *active {
result = append(result, v)
*active = false
}
}
return result
}
// IntersectionBy returns all unique shared values from multiple slices with key function
func IntersectionBy[T any, K comparable, S ~[]T](a, b S, keyFunc func(t T) K) S {
lenA, lenB := len(a), len(b)
if lenA == 0 || lenB == 0 {
return S{}
}
seen := make(map[K]*bool, lenA)
result := make(S, 0, Min(lenA, lenB))
for i := 0; i < lenA; i++ {
active := true
seen[keyFunc(a[i])] = &active
}
for i := 0; i < lenB; i++ {
v := b[i]
k := keyFunc(v)
if active, ok := seen[k]; ok && *active {
result = append(result, v)
*active = false
}
}
return result
}
// Deprecated: use IntersectionBy instead
func IntersectionPred[T any, K comparable, S ~[]T](a, b S, keyFunc func(t T) K) S {
return IntersectionBy(a, b, keyFunc)
}
// Difference calculates the differences between two slices.
// The first result list contains all the values exist in the left list, but the right.
// The second result list contains all the values exist in the right list, but the left.
// NOTE: this function does not return unique values.
func Difference[T comparable, S ~[]T](a, b S) (S, S) {
lenA, lenB := len(a), len(b)
if lenA == 0 || lenB == 0 {
return append(S{}, a...), append(S{}, b...)
}
leftDiff, rightDiff := S{}, S{}
leftMap, rightMap := MapSliceToMapKeys(a, struct{}{}), MapSliceToMapKeys(b, struct{}{})
for _, v := range a {
if _, ok := rightMap[v]; !ok {
leftDiff = append(leftDiff, v)
}
}
for _, v := range b {
if _, ok := leftMap[v]; !ok {
rightDiff = append(rightDiff, v)
}
}
return leftDiff, rightDiff
}
// DifferenceBy calculates the differences between two slices using special key function.
// NOTE: this function does not return unique values.
func DifferenceBy[T any, K comparable, S ~[]T](a, b S, keyFunc func(t T) K) (S, S) {
lenA, lenB := len(a), len(b)
if lenA == 0 || lenB == 0 {
return append(S{}, a...), append(S{}, b...)
}
leftDiff, rightDiff := S{}, S{}
leftMap, rightMap := make(map[K]struct{}, lenA), make(map[K]struct{}, lenB)
for _, v := range a {
leftMap[keyFunc(v)] = struct{}{}
}
for _, v := range b {
rightMap[keyFunc(v)] = struct{}{}
}
for _, v := range a {
if _, ok := rightMap[keyFunc(v)]; !ok {
leftDiff = append(leftDiff, v)
}
}
for _, v := range b {
if _, ok := leftMap[keyFunc(v)]; !ok {
rightDiff = append(rightDiff, v)
}
}
return leftDiff, rightDiff
}
// Deprecated: use DifferenceBy instead
func DifferencePred[T any, K comparable, S ~[]T](a, b S, keyFunc func(t T) K) (S, S) {
return DifferenceBy(a, b, keyFunc)
}
// Flatten flattens 2-dimensional slices.
// E.g. Flatten([1,2,3], [3,4,5]) -> [1,2,3,3,4,5].
func Flatten[T any, S ~[]T](s ...S) S {
result := make(S, 0, len(s)*5) //nolint:mnd
for _, innerSlice := range s {
result = append(result, innerSlice...)
}
return result
}
// Flatten3 flattens 3-dimensional slices
func Flatten3[T any, S ~[]T, SS ~[]S](s ...SS) S {
result := make(S, 0, len(s)*30) //nolint:mnd
for _, innerSlice := range s {
for _, mostInnerSlice := range innerSlice {
result = append(result, mostInnerSlice...)
}
}
return result
}
// Zip combines values from 2 slices by each position
func Zip[T1, T2 any, S1 ~[]T1, S2 ~[]T2](slice1 S1, slice2 S2) []*Tuple2[T1, T2] {
minLen := len(slice1)
if minLen > len(slice2) {
minLen = len(slice2)
}
result := make([]*Tuple2[T1, T2], minLen)
for i := 0; i < minLen; i++ {
result[i] = &Tuple2[T1, T2]{slice1[i], slice2[i]}
}
return result
}
// Zip3 combines values from 3 slices by each position
func Zip3[T1, T2, T3 any, S1 ~[]T1, S2 ~[]T2, S3 ~[]T3](slice1 S1, slice2 S2, slice3 S3) []*Tuple3[T1, T2, T3] {
minLen := Min(len(slice1), len(slice2), len(slice3))
result := make([]*Tuple3[T1, T2, T3], minLen)
for i := 0; i < minLen; i++ {
result[i] = &Tuple3[T1, T2, T3]{slice1[i], slice2[i], slice3[i]}
}
return result
}
// Zip4 combines values from 4 slices by each position
func Zip4[T1, T2, T3, T4 any, S1 ~[]T1, S2 ~[]T2, S3 ~[]T3, S4 ~[]T4](
slice1 S1, slice2 S2, slice3 S3, slice4 S4,
) []*Tuple4[T1, T2, T3, T4] {
minLen := Min(len(slice1), len(slice2), len(slice3), len(slice4))
result := make([]*Tuple4[T1, T2, T3, T4], minLen)
for i := 0; i < minLen; i++ {
result[i] = &Tuple4[T1, T2, T3, T4]{slice1[i], slice2[i], slice3[i], slice4[i]}
}
return result
}
// Zip5 combines values from 4 slices by each position
func Zip5[T1, T2, T3, T4, T5 any, S1 ~[]T1, S2 ~[]T2, S3 ~[]T3, S4 ~[]T4, S5 ~[]T5](
slice1 S1, slice2 S2, slice3 S3, slice4 S4, slice5 S5,
) []*Tuple5[T1, T2, T3, T4, T5] {
minLen := Min(len(slice1), len(slice2), len(slice3), len(slice4), len(slice5))
result := make([]*Tuple5[T1, T2, T3, T4, T5], minLen)
for i := 0; i < minLen; i++ {
result[i] = &Tuple5[T1, T2, T3, T4, T5]{slice1[i], slice2[i], slice3[i], slice4[i], slice5[i]}
}
return result
}