-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathslice_go1.18.go
150 lines (131 loc) · 2.77 KB
/
slice_go1.18.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
//go:build go1.18
package com
import (
"math/rand"
"sort"
"strings"
)
type Number interface {
~uint8 | ~int8 | ~uint16 | ~int16 | ~uint32 | ~int32 | ~uint | ~int | ~uint64 | ~int64 | ~float32 | ~float64
}
type Scalar interface {
Number | ~bool | ~string
}
func SliceExtractCallback[T Scalar](parts []string, cb func(string) T, recv ...*T) {
recvEndIndex := len(recv) - 1
if recvEndIndex < 0 {
return
}
for index, value := range parts {
if index > recvEndIndex {
break
}
*recv[index] = cb(value)
}
}
func ExtractSlicex[T any](parts []T, recv ...*T) {
recvEndIndex := len(recv) - 1
if recvEndIndex < 0 {
return
}
for index, value := range parts {
if index > recvEndIndex {
break
}
*recv[index] = value
}
}
type reverseSortIndex[T any] []T
func (s reverseSortIndex[T]) Len() int { return len(s) }
func (s reverseSortIndex[T]) Less(i, j int) bool {
return j < i
}
func (s reverseSortIndex[T]) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func ReverseSortIndex[T any](values []T) []T {
sort.Sort(reverseSortIndex[T](values))
return values
}
func ChunkSlicex[T any](slice []T, size int) (chunkslice [][]T) {
length := len(slice)
if size >= length {
chunkslice = append(chunkslice, slice)
return
}
end := size
for i := 0; i < length; i += size {
if end < length {
chunkslice = append(chunkslice, slice[i:end])
} else {
chunkslice = append(chunkslice, slice[i:])
}
end += size
}
return
}
func InSlicex[T comparable](v T, sl []T) bool {
for _, vv := range sl {
if vv == v {
return true
}
}
return false
}
func IntersectSlicex[T comparable](slice1, slice2 []T) (inslice []T) {
for _, v := range slice1 {
if InSlicex(v, slice2) {
inslice = append(inslice, v)
}
}
return
}
func DiffSlicex[T comparable](slice1, slice2 []T) (diffslice []T) {
for _, v := range slice1 {
if !InSlicex(v, slice2) {
diffslice = append(diffslice, v)
}
}
return
}
func MergeSlicex[T any](slice1, slice2 []T) (c []T) {
c = append(slice1, slice2...)
return
}
func ReduceSlicex[T any](slice []T, a func(T) T) (dslice []T) {
for _, v := range slice {
dslice = append(dslice, a(v))
}
return
}
func RandSlicex[T any](a []T) (b T) {
randnum := rand.Intn(len(a))
b = a[randnum]
return
}
func JoinNumbers[T Number](slice []T, sep string) string {
sb := strings.Builder{}
for i, v := range slice {
if i > 0 {
sb.WriteString(sep)
}
sb.WriteString(Str(v))
}
return sb.String()
}
func MapKeys[K Scalar, V any](m map[K]V) []K {
r := make([]K, 0, len(m))
for k := range m {
r = append(r, k)
}
return r
}
func SplitSliceByGroup[K Scalar, V any](rows []V, groupValue func(V) K) map[K][]V {
r := map[K][]V{}
for _, row := range rows {
k := groupValue(row)
if _, ok := r[k]; !ok {
r[k] = []V{}
}
r[k] = append(r[k], row)
}
return r
}