-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray.go
261 lines (245 loc) · 5.06 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
package item
import (
"github.com/modern-go/reflect2"
"log"
"reflect"
)
// GetArrayIndex get value index with slice, if not found return -1
// 获取数据下标,不存在返回-1
func GetArrayIndex[T comparable](params []T, value T) int {
for index, param := range params {
if param == value {
return index
}
}
return -1
}
// InArray
// 判断数据是否存在 compare
func InArray[T comparable](params []T, value T) bool {
for _, param := range params {
if param == value {
return true
}
}
return false
}
// InArrayCompare
// 判断数据是否存在 ICompare
func InArrayCompare[T ICompare](params []T, value T) bool {
for _, param := range params {
if param.Compare(value) {
return true
}
}
return false
}
// ArrayMap
// 返回输入数组中某个单一列的值的map string interface
func ArrayMap[V any](params []map[string]V, key string) map[string][]map[string]V {
if len(params) < 1 {
return nil
}
res := make(map[string][]map[string]V)
for _, v := range params {
_, ok := v[key]
if !ok {
continue
}
res[key] = append(res[key], v)
}
return res
}
// ArrayMapCompare
// 返回输入数组中某个单一列的值的map interface
func ArrayMapCompare[T comparable](params []map[T]interface{}, key T) map[T]map[T]interface{} {
if len(params) < 1 {
return nil
}
res := make(map[T]map[T]interface{})
for _, v := range params {
_, ok := v[key]
if !ok {
continue
}
res[key] = v
}
return res
}
// ArrayMapCompareValue
// 返回输入数组中某个单一列的值的map
func ArrayMapCompareValue[K comparable, V any](params []map[K]V, key K) map[K]map[K]V {
if len(params) < 1 {
return nil
}
res := make(map[K]map[K]V)
for _, v := range params {
_, ok := v[key]
if !ok {
continue
}
res[key] = v
}
return res
}
func ArrayMapColumn[V any, K comparable](params []V, key string) map[K]V {
if len(params) < 1 {
return nil
}
rt0 := reflect2.TypeOf(params[0])
switch rt0.Kind() {
case reflect.Struct:
_, ok := rt0.Type1().FieldByName(key)
if !ok {
log.Printf("not found key with %s", key)
return nil
}
case reflect.Map:
default:
log.Printf("not supported kind with %s", rt0.Kind())
return nil
}
res := make(map[K]V)
for _, v := range params {
rv := reflect.ValueOf(v)
if rv.Kind() == reflect.Pointer {
continue
}
rv = reflect.Indirect(rv)
switch rv.Kind() {
case reflect.Map:
field := rv.MapIndex(reflect.ValueOf(key))
if !field.Comparable() {
log.Printf("can compare with key:%s type: %s", key, field.Kind())
return nil
}
kv, ok := field.Interface().(K)
if !ok {
log.Printf("can convert with key:%s type: %s", key, field.Kind())
return nil
}
res[kv] = v
case reflect.Struct:
field := rv.FieldByName(key)
if !field.Comparable() {
log.Printf("can compare with key:%s type: %s", key, field.Kind())
return nil
}
kv, ok := field.Interface().(K)
if !ok {
log.Printf("can convert with key:%s type: %s", key, field.Kind())
return nil
}
res[kv] = v
}
}
return res
}
// ArrayColumns
// 返回输入数组中某个单一列的值
func ArrayColumns[K comparable, V any](params []map[K]V, key K) []V {
if len(params) < 1 {
return nil
}
res := make([]V, len(params), len(params))
for i, m := range params {
v, ok := m[key]
if !ok {
// res[i]= *new(V) do not create it
continue
}
res[i] = v
}
return res
}
// ArrayUnique unique with slice
// 对切片进行去重
func ArrayUnique[V comparable](params []V) []V {
mp := map[V]struct{}{}
res := make([]V, len(params))
var index int
for _, v := range params {
if _, ok := mp[v]; ok {
continue
}
mp[v] = struct{}{}
res[index] = v
index++
}
return res[:index]
}
// ArrayDiff diff with slice values
// 取多个切片的差集
func ArrayDiff[V comparable](params ...[]V) []V {
var all []V
mp := map[V]int8{}
for _, v := range params {
all = append(all, v...)
}
for _, v := range all {
if num, ok := mp[v]; ok {
mp[v] = num + 1
continue
}
mp[v] = 1
}
var res []V
for v, num := range mp {
if num == 1 {
res = append(res, v)
}
}
return res
}
// ArraySub diff with slice values
// example: [1,2,3] [2,3,4] array_sub(a,b) => [1]
func ArraySub[V comparable](arr1, arr2 []V) []V {
var res []V
for _, v1 := range arr1 {
found := false
for _, v2 := range arr2 {
if v1 == v2 {
found = true
}
}
if !found {
res = append(res, v1)
}
}
return res
}
// ArrayIntersect Intersect with slice values
// 取多个切片的交集 params max 127
func ArrayIntersect[V comparable](params ...[]V) []V {
var all []V
mp := map[V]int8{}
l := int8(len(params))
for _, v := range params {
v = ArrayUnique[V](v)
all = append(all, v...)
}
for _, v := range all {
if num, ok := mp[v]; ok {
mp[v] = num + 1
continue
}
mp[v] = 1
}
var res []V
for v, num := range mp {
if num == l {
res = append(res, v)
}
}
return res
}
// ArrayValues map transfer to slice
func ArrayValues[K comparable, V any](mp map[K]V) []V {
res := make([]V, len(mp), len(mp))
var i int
for _, v := range mp {
res[i] = v
i++
}
return res
}