-
Notifications
You must be signed in to change notification settings - Fork 1
/
slice.go
632 lines (573 loc) · 11.5 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
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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
package gofn
// Equal compares 2 slices with preserving order
func Equal[T comparable](a, b []T) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}
// EqualPred compares 2 slices with preserving order
func EqualPred[T any](a, b []T, equalFunc func(a, b T) bool) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if !equalFunc(a[i], b[i]) {
return false
}
}
return true
}
// EqualPredPtr compares 2 slices with preserving order
func EqualPredPtr[T any](a, b []T, equalFunc func(a, b *T) bool) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if !equalFunc(&a[i], &b[i]) {
return false
}
}
return true
}
// ContentEqual compares 2 slices without caring about order
// NOTE: if you want to compare content of slices of pointers, use ContentEqualPtr
func ContentEqual[T comparable](a, b []T) bool {
if len(a) != len(b) {
return false
}
mapA := make(map[T]*int, len(a))
for i := range a {
k := a[i]
if count, ok := mapA[k]; !ok {
cnt := 1
mapA[k] = &cnt
} else {
*count++
}
}
for i := range b {
k := b[i]
switch count, ok := mapA[k]; {
case !ok:
return false
case *count == 1:
delete(mapA, k)
default:
*count--
}
}
return len(mapA) == 0
}
// ContentEqualPtr compares 2 slices of pointers without caring about order
func ContentEqualPtr[T comparable](a, b []*T) bool {
if len(a) != len(b) {
return false
}
mapA := make(map[T]*int, len(a))
for i := range a {
k := *a[i]
if count, ok := mapA[k]; !ok {
cnt := 1
mapA[k] = &cnt
} else {
*count++
}
}
for i := range b {
k := *b[i]
switch count, ok := mapA[k]; {
case !ok:
return false
case *count == 1:
delete(mapA, k)
default:
*count--
}
}
return len(mapA) == 0
}
// ContentEqualPred compares 2 slices without preserving order
func ContentEqualPred[T any, K comparable](a, b []T, keyFunc func(t T) K) bool {
if len(a) != len(b) {
return false
}
mapA := make(map[K]*int, len(a))
for i := range a {
k := keyFunc(a[i])
if count, ok := mapA[k]; !ok {
cnt := 1
mapA[k] = &cnt
} else {
*count++
}
}
for i := range b {
k := keyFunc(b[i])
switch count, ok := mapA[k]; {
case !ok:
return false
case *count == 1:
delete(mapA, k)
default:
*count--
}
}
return len(mapA) == 0
}
// Concat concatenates slices
func Concat[T any](slices ...[]T) []T {
capacity := 0
for _, s := range slices {
capacity += len(s)
}
result := make([]T, 0, capacity)
for _, s := range slices {
result = append(result, s...)
}
return result
}
// Contain tests if a slice contains an item
func Contain[T comparable](a []T, t T) bool {
for i := range a {
if a[i] == t {
return true
}
}
return false
}
// ContainPred tests if a slice contains an item by predicate
func ContainPred[T any](a []T, pred func(t T) bool) bool {
for i := range a {
if pred(a[i]) {
return true
}
}
return false
}
// ContainPredPtr tests if a slice contains an item by predicate
func ContainPredPtr[T any](a []T, pred func(t *T) bool) bool {
for i := range a {
if pred(&a[i]) {
return true
}
}
return false
}
// ContainAll tests if a slice contains all given values
func ContainAll[T comparable](a []T, b ...T) bool {
lenA, lenB := len(a), len(b)
if lenA == 0 || lenB == 0 {
return false
}
if lenA < 10 && lenB < 3 {
for i := range b {
if !Contain(a, b[i]) {
return false
}
}
return true
}
seen := make(map[T]struct{}, lenA)
for i := range a {
seen[a[i]] = struct{}{}
}
for i := range b {
if _, ok := seen[b[i]]; !ok {
return false
}
}
return true
}
// ContainAny tests if a slice contains any given value
func ContainAny[T comparable](a []T, b ...T) bool {
lenA, lenB := len(a), len(b)
if lenA == 0 || lenB == 0 {
return false
}
if lenA < 10 && lenB < 3 {
for i := range b {
if Contain(a, b[i]) {
return true
}
}
return false
}
seen := make(map[T]struct{}, lenA)
for i := range a {
seen[a[i]] = struct{}{}
}
for i := range b {
if _, ok := seen[b[i]]; ok {
return true
}
}
return false
}
// IsUnique checks a slice for uniqueness
func IsUnique[T comparable](s []T) bool {
length := len(s)
if length <= 1 {
return true
}
seen := make(map[T]struct{}, length)
for i := 0; i < length; i++ {
v := s[i]
if _, ok := seen[v]; ok {
return false
}
seen[v] = struct{}{}
}
return true
}
// IsUniquePred checks a slice for uniqueness using key function
func IsUniquePred[T any, U comparable](s []T, keyFunc func(t T) U) bool {
length := len(s)
if length <= 1 {
return true
}
seen := make(map[U]struct{}, length)
for i := 0; i < length; i++ {
v := keyFunc(s[i])
if _, ok := seen[v]; ok {
return false
}
seen[v] = struct{}{}
}
return true
}
// FindPred finds value in slice by predicate
func FindPred[T any](a []T, pred func(t T) bool) (t T, found bool) {
for i := range a {
if pred(a[i]) {
return a[i], true
}
}
return t, false
}
// FindPredPtr finds value in slice by predicate
func FindPredPtr[T any](a []T, pred func(t *T) bool) (t T, found bool) {
for i := range a {
if pred(&a[i]) {
return a[i], true
}
}
return t, false
}
// FindLastPred finds value in slice from the end by predicate
func FindLastPred[T any](a []T, pred func(t T) bool) (t T, found bool) {
for i := len(a) - 1; i >= 0; i-- {
if pred(a[i]) {
return a[i], true
}
}
return t, false
}
// FindLastPredPtr finds value in slice from the end by predicate
func FindLastPredPtr[T any](a []T, pred func(t *T) bool) (t T, found bool) {
for i := len(a) - 1; i >= 0; i-- {
if pred(&a[i]) {
return a[i], true
}
}
return t, false
}
// IndexOf gets index of item in slice
// Returns -1 if not found
func IndexOf[T comparable](a []T, t T) int {
for i := range a {
if a[i] == t {
return i
}
}
return -1
}
// IndexOfPred gets index of item in slice by predicate
// Returns -1 if not found
func IndexOfPred[T any](a []T, pred func(t T) bool) int {
for i := range a {
if pred(a[i]) {
return i
}
}
return -1
}
// LastIndexOf gets index of item from the end in slice
// Returns -1 if not found
func LastIndexOf[T comparable](a []T, t T) int {
for i := len(a) - 1; i >= 0; i-- {
if a[i] == t {
return i
}
}
return -1
}
// LastIndexOfPred gets index of item from the end in slice
// Returns -1 if not found
func LastIndexOfPred[T any](a []T, pred func(t T) bool) int {
for i := len(a) - 1; i >= 0; i-- {
if pred(a[i]) {
return i
}
}
return -1
}
// RemoveAt removes element at the specified index
func RemoveAt[T any](ps *[]T, i int) {
s := *ps
if i < 0 || i >= len(s) {
panic(ErrIndexOutOfRange)
}
var zeroT T
s[i] = zeroT
*ps = s[:i]
*ps = append(*ps, s[i+1:]...)
}
// FastRemoveAt removes element at the specified index by swapping it with the last item in slice
func FastRemoveAt[T any](ps *[]T, i int) {
s := *ps
length := len(s)
if i < 0 || i >= length {
panic(ErrIndexOutOfRange)
}
s[i] = s[length-1]
var zeroT T
s[length-1] = zeroT
*ps = s[:length-1]
}
// Remove removes element value
func Remove[T comparable](ps *[]T, v T) bool {
i := IndexOf(*ps, v)
if i == -1 {
return false
}
RemoveAt(ps, i)
return true
}
// FastRemove removes element value
func FastRemove[T comparable](ps *[]T, v T) bool {
i := IndexOf(*ps, v)
if i == -1 {
return false
}
FastRemoveAt(ps, i)
return true
}
// RemoveLastOf removes element value
func RemoveLastOf[T comparable](ps *[]T, v T) bool {
i := LastIndexOf(*ps, v)
if i == -1 {
return false
}
RemoveAt(ps, i)
return true
}
// FastRemoveLastOf removes element value
func FastRemoveLastOf[T comparable](ps *[]T, v T) bool {
i := LastIndexOf(*ps, v)
if i == -1 {
return false
}
FastRemoveAt(ps, i)
return true
}
// RemoveAll removes all occurrences of value
func RemoveAll[T comparable](ps *[]T, v T) int {
newIdx := 0
count := 0
s := *ps
length := len(s)
for i := 0; i < length; i++ {
if s[i] != v {
if newIdx != i {
s[newIdx] = s[i]
}
newIdx++
} else {
count++
}
}
*ps = s[:length-count]
return count
}
func Compact[T comparable](s []T) []T {
result := make([]T, 0, len(s))
var zeroT T
for _, v := range s {
if v == zeroT {
continue
}
result = append(result, v)
}
return result
}
// Replace replaces a value in slice with another value
func Replace[T comparable](s []T, value, replacement T) bool {
for i := range s {
if s[i] == value {
s[i] = replacement
return true
}
}
return false
}
// ReplaceAll replaces a value in slice with another value
func ReplaceAll[T comparable](s []T, value, replacement T) int {
count := 0
for i := range s {
if s[i] == value {
s[i] = replacement
count++
}
}
return count
}
// Fill sets slice element values
func Fill[T any](a []T, val T) {
for i := range a {
a[i] = val
}
}
// CountValue counts number of occurrences of an item in the slice
func CountValue[T comparable](a []T, val T) int {
count := 0
for i := range a {
if a[i] == val {
count++
}
}
return count
}
// CountValuePred counts number of occurrences of an item in the slice
func CountValuePred[T any](a []T, pred func(t T) bool) int {
count := 0
for i := range a {
if pred(a[i]) {
count++
}
}
return count
}
// ContainSlice tests if a slice contains a slice
func ContainSlice[T comparable](a, b []T) bool {
return IndexOfSlice(a, b) >= 0
}
// IndexOfSlice gets index of sub-slice in slice
// Returns -1 if not found
func IndexOfSlice[T comparable](a, sub []T) int {
lengthA := len(a)
lengthSub := len(sub)
if lengthSub == 0 || lengthA < lengthSub {
return -1
}
sub1st := sub[0]
for i, max := 0, lengthA-lengthSub; i <= max; i++ {
if a[i] == sub1st {
found := true
for j := 1; j < lengthSub; j++ {
if a[i+j] != sub[j] {
found = false
break
}
}
if found {
return i
}
}
}
return -1
}
// LastIndexOfSlice gets last index of sub-slice in slice
// Returns -1 if not found
func LastIndexOfSlice[T comparable](a []T, sub []T) int {
lengthA := len(a)
lengthSub := len(sub)
if lengthSub == 0 || lengthA < lengthSub {
return -1
}
sub1st := sub[0]
for i := lengthA - lengthSub; i >= 0; i-- {
if a[i] == sub1st {
found := true
for j := 1; j < lengthSub; j++ {
if a[i+j] != sub[j] {
found = false
break
}
}
if found {
return i
}
}
}
return -1
}
func GetFirst[T any](s []T, defaultVal T) T {
if len(s) > 0 {
return s[0]
}
return defaultVal
}
func GetLast[T any](s []T, defaultVal T) T {
if len(s) > 0 {
return s[len(s)-1]
}
return defaultVal
}
// SubSlice gets sub slice from a slice
// Passing negative numbers to get items from the end of the slice
// For example, using start=-1, end=-2 to get the last item of the slice
// end param is exclusive.
func SubSlice[T any](s []T, start, end int) []T {
length := len(s)
if length == 0 {
return []T{}
}
for start < 0 {
start += length
}
if start > length {
start = length
}
for end < 0 {
end += length
}
if end > length {
end = length
}
if start > end {
// NOTE: end is exclusive
return s[end+1 : start+1]
}
return s[start:end]
}
// SliceByRange generates a slice by range
// start is inclusive, end is exclusive
func SliceByRange[T NumberEx](start, end, step T) []T {
if end > start {
if step <= 0 {
return []T{}
}
count := int((end-start)/step) + 1
result := make([]T, 0, count)
for i := start; i < end; i += step {
result = append(result, i)
}
return result
} else {
if step >= 0 {
return []T{}
}
count := int((end-start)/step) + 1
result := make([]T, 0, count)
for i := start; i > end; i += step {
result = append(result, i)
}
return result
}
}