-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslices.go
326 lines (277 loc) · 6.84 KB
/
slices.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
package core
import (
"crypto/rand"
"math/big"
"sort"
)
// SliceMinus returns a new slice containing only the
// elements of one slice not present on the second
func SliceMinus[T comparable](a []T, b []T) []T {
return SliceMinusFn(a, b, func(va, vb T) bool {
return va == vb
})
}
// SliceMinusFn returns a new slice containing only elements
// of slice A that aren't on slice B according to the callback
// eq
func SliceMinusFn[T any](a, b []T, eq func(T, T) bool) []T {
fn := func(_ []T, v T) (T, bool) {
if SliceContainsFn(b, v, eq) {
return v, false // skip
}
return v, true // keep
}
return SliceCopyFn(a, fn)
}
// SliceContains tells if a slice contains a given element
func SliceContains[T comparable](a []T, v T) bool {
return SliceContainsFn(a, v, func(va, vb T) bool {
return va == vb
})
}
// SliceContainsFn tells if a slice contains a given element
// according to the callback eq
func SliceContainsFn[T any](a []T, v T, eq func(T, T) bool) bool {
for _, va := range a {
if eq(va, v) {
return true
}
}
return false
}
// SliceEqual tells if two slices are equal.
func SliceEqual[T comparable](a, b []T) bool {
if len(a) != len(b) {
return false
}
return SliceEqualFn(a, b, func(va, vb T) bool {
return va == vb
})
}
// SliceEqualFn tells if two slices are equal using a comparing helper.
func SliceEqualFn[T any](a, b []T, eq func(va, vb T) bool) bool {
if len(a) != len(b) || eq == nil {
return false
}
for i := range a {
if !eq(a[i], b[i]) {
return false
}
}
return true
}
// SliceUnique returns a new slice containing only
// unique elements
func SliceUnique[T comparable](a []T) []T {
keys := make(map[T]bool, len(a))
// keep only new elements
fn := func(_ []T, entry T) (T, bool) {
var keep bool
if _, known := keys[entry]; !known {
keys[entry] = true
keep = true
}
return entry, keep
}
return SliceCopyFn(a, fn)
}
// SliceUniqueFn returns a new slice containing only
// unique elements according to the callback eq
func SliceUniqueFn[T any](a []T, eq func(T, T) bool) []T {
// keep only elements not present on the partial
// result already
fn := func(partial []T, entry T) (T, bool) {
var keep bool
if !SliceContainsFn(partial, entry, eq) {
keep = true
}
return entry, keep
}
return SliceCopyFn(a, fn)
}
// SliceUniquify returns the same slice, reduced to
// only contain unique elements
func SliceUniquify[T comparable](ptr *[]T) []T {
if ptr == nil {
return []T{}
}
keys := make(map[T]bool, len(*ptr))
// keep only new elements
fn := func(_ []T, entry T) (T, bool) {
var keep bool
if _, known := keys[entry]; !known {
keys[entry] = true
keep = true
}
return entry, keep
}
*ptr = SliceReplaceFn(*ptr, fn)
return *ptr
}
// SliceUniquifyFn returns the same slice, reduced to
// only contain unique elements according to the callback eq
func SliceUniquifyFn[T any](ptr *[]T, eq func(T, T) bool) []T {
if ptr == nil {
return []T{}
}
// keep only elements not present on the partial
// result already
fn := func(partial []T, entry T) (T, bool) {
var keep bool
if !SliceContainsFn(partial, entry, eq) {
keep = true
}
return entry, keep
}
*ptr = SliceReplaceFn(*ptr, fn)
return *ptr
}
// SliceReplaceFn replaces or skips entries in a slice
func SliceReplaceFn[T any](s []T,
fn func(partial []T, before T) (after T, replace bool),
) []T {
//
if fn == nil {
// NO-OP
return s
}
j := 0
for _, v := range s {
if w, ok := fn(s[:j], v); ok {
s[j] = w
j++
}
}
return s[:j]
}
// SliceCopyFn makes a copy of a slice, optionally modifying in-flight
// the items using a function. If no function is provided,
// the destination will be a shallow copy of the source slice.
func SliceCopyFn[T any](s []T,
fn func(partial []T, before T) (after T, include bool),
) []T {
//
if fn == nil {
return SliceCopy(s)
}
result := make([]T, 0, len(s))
for _, v := range s {
if w, ok := fn(result, v); ok {
result = append(result, w)
}
}
return result
}
// SliceCopy makes a shallow copy of a given slice
func SliceCopy[T any](s []T) []T {
l := len(s)
result := make([]T, l)
if l > 0 {
copy(result, s)
}
return result
}
// SliceMap takes a []T1 and uses a function to produce a []T2
// by processing each item on the source slice.
func SliceMap[T1 any, T2 any](a []T1,
fn func(partial []T2, v T1) (newEntries []T2)) []T2 {
//
var result []T2
if fn != nil {
for _, v := range a {
result = append(result, fn(result, v)...)
}
}
return result
}
// SliceRandom returns a random element from a slice
// if the slice is empty it will return the zero value
// of the slice type and false
func SliceRandom[T any](a []T) (T, bool) {
var result T
switch len(a) {
case 0:
return result, false
case 1:
result = a[0]
default:
id, _ := rand.Int(rand.Reader, big.NewInt(int64(len(a))))
result = a[id.Uint64()]
}
return result, true
}
// SliceSortFn sorts the slice x in ascending order as a less function.
// This sort is not guaranteed to be stable.
// less(a, b) should true when a < b
func SliceSortFn[T any](x []T, less func(a, b T) bool) {
if less != nil && len(x) > 0 {
doSliceSort(x, less)
}
}
// SliceSort sorts the slice x in ascending order as determined by the cmp
// function. This sort is not guaranteed to be stable.
// cmp(a, b) should return a negative number when a < b, a positive number when
// a > b and zero when a == b.
func SliceSort[T any](x []T, cmp func(a, b T) int) {
if cmp != nil && len(x) > 0 {
doSliceSort(x, func(a, b T) bool {
return cmp(a, b) < 0
})
}
}
// SliceSortOrdered sorts the slice x of an [Ordered] type in ascending order.
func SliceSortOrdered[T Ordered](x []T) {
if len(x) > 0 {
doSliceSort(x, func(a, b T) bool {
return a < b
})
}
}
func doSliceSort[T any](x []T, less func(a, b T) bool) {
s := sortable[T]{
x: x,
less: less,
}
sort.Sort(s)
}
var _ sort.Interface = sortable[any]{}
type sortable[T any] struct {
x []T
less func(a, b T) bool
}
func (s sortable[T]) Len() int {
return len(s.x)
}
func (s sortable[T]) Less(i, j int) bool {
// this is only accessible from sort.Sort() so
// we can trust the indexes
return s.less(s.x[i], s.x[j])
}
func (s sortable[T]) Swap(i, j int) {
// this is only accessible from sort.Sort() so
// we can trust the indexes
s.x[j], s.x[i] = s.x[i], s.x[j]
}
// SliceReverse modifies a slice reversing the order of its
// elements.
func SliceReverse[T any](x []T) {
l := len(x)
if l > 1 {
for i, j := 0, l-1; i < j; i, j = i+1, j-1 {
x[i], x[j] = x[j], x[i]
}
}
}
// SliceReversed returns a copy of the slice, in reverse order.
func SliceReversed[T any](a []T) []T {
b := SliceCopy(a)
SliceReverse(b)
return b
}
// SliceReversedFn returns a modified copy of the slice, in reverse order.
func SliceReversedFn[T any](a []T,
fn func(partial []T, before T) (after T, include bool)) []T {
b := SliceCopyFn(a, fn)
SliceReverse(b)
return b
}