-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslice.go
More file actions
495 lines (427 loc) · 11.7 KB
/
Copy pathslice.go
File metadata and controls
495 lines (427 loc) · 11.7 KB
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
package jpath
type (
// SlicePlan stores precompiled bounds for slice selection
SlicePlan struct {
Start int
End int
Step int
}
selectorCase uint8
append0Func func(out, arr []any, step int) []any
append1Func func(out, arr []any, bound, step int) []any
append2Func func(out, arr []any, start, end, step int) []any
selectorMakerFunc func(plan *SlicePlan) SelectorFunc
)
const (
selectorCaseArrayAll selectorCase = iota
selectorCaseF00
selectorCaseF10P
selectorCaseF10N
selectorCaseF01P
selectorCaseF01N
selectorCaseF11PP
selectorCaseF11PN
selectorCaseF11NP
selectorCaseF11NN
selectorCaseB00
selectorCaseB10P
selectorCaseB10N
selectorCaseB01P
selectorCaseB01N
selectorCaseB11PP
selectorCaseB11PN
selectorCaseB11NP
selectorCaseB11NN
selectorCaseEmpty
selectorCaseCount
)
var selectorDispatch = [selectorCaseCount]selectorMakerFunc{
selectorCaseArrayAll: MakeSelectorArrayAll,
selectorCaseF00: MakeSelectorF00,
selectorCaseF10P: MakeSelectorF10P,
selectorCaseF10N: MakeSelectorF10N,
selectorCaseF01P: MakeSelectorF01P,
selectorCaseF01N: MakeSelectorF01N,
selectorCaseF11PP: MakeSelectorF11PP,
selectorCaseF11PN: MakeSelectorF11PN,
selectorCaseF11NP: MakeSelectorF11NP,
selectorCaseF11NN: MakeSelectorF11NN,
selectorCaseB00: MakeSelectorB00,
selectorCaseB10P: MakeSelectorB10P,
selectorCaseB10N: MakeSelectorB10N,
selectorCaseB01P: MakeSelectorB01P,
selectorCaseB01N: MakeSelectorB01N,
selectorCaseB11PP: MakeSelectorB11PP,
selectorCaseB11PN: MakeSelectorB11PN,
selectorCaseB11NP: MakeSelectorB11NP,
selectorCaseB11NN: MakeSelectorB11NN,
}
// MakeSelectorArrayAll builds a selector for selectorCaseArrayAll
func MakeSelectorArrayAll(_ *SlicePlan) SelectorFunc {
return func(out []any, node, _ any) []any {
arr, ok := node.([]any)
if !ok {
return out
}
return append(out, arr...)
}
}
// MakeSelectorF00 builds a selector for selectorCaseF00
func MakeSelectorF00(plan *SlicePlan) SelectorFunc {
return makeSelector0(plan.Step, appendSliceF00)
}
// MakeSelectorF10P builds a selector for selectorCaseF10P
func MakeSelectorF10P(plan *SlicePlan) SelectorFunc {
return makeSelector1(plan.Start, plan.Step, appendSliceF10P)
}
// MakeSelectorF10N builds a selector for selectorCaseF10N
func MakeSelectorF10N(plan *SlicePlan) SelectorFunc {
return makeSelector1(plan.Start, plan.Step, appendSliceF10N)
}
// MakeSelectorF01P builds a selector for selectorCaseF01P
func MakeSelectorF01P(plan *SlicePlan) SelectorFunc {
return makeSelector1(plan.End, plan.Step, appendSliceF01P)
}
// MakeSelectorF01N builds a selector for selectorCaseF01N
func MakeSelectorF01N(plan *SlicePlan) SelectorFunc {
return makeSelector1(plan.End, plan.Step, appendSliceF01N)
}
// MakeSelectorF11PP builds a selector for selectorCaseF11PP
func MakeSelectorF11PP(plan *SlicePlan) SelectorFunc {
return makeSelector2(plan.Start, plan.End, plan.Step, appendSliceF11PP)
}
// MakeSelectorF11PN builds a selector for selectorCaseF11PN
func MakeSelectorF11PN(plan *SlicePlan) SelectorFunc {
return makeSelector2(plan.Start, plan.End, plan.Step, appendSliceF11PN)
}
// MakeSelectorF11NP builds a selector for selectorCaseF11NP
func MakeSelectorF11NP(plan *SlicePlan) SelectorFunc {
return makeSelector2(plan.Start, plan.End, plan.Step, appendSliceF11NP)
}
// MakeSelectorF11NN builds a selector for selectorCaseF11NN
func MakeSelectorF11NN(plan *SlicePlan) SelectorFunc {
return makeSelector2(plan.Start, plan.End, plan.Step, appendSliceF11NN)
}
// MakeSelectorB00 builds a selector for selectorCaseB00
func MakeSelectorB00(plan *SlicePlan) SelectorFunc {
return makeSelector0(plan.Step, appendSliceB00)
}
// MakeSelectorB10P builds a selector for selectorCaseB10P
func MakeSelectorB10P(plan *SlicePlan) SelectorFunc {
return makeSelector1(plan.Start, plan.Step, appendSliceB10P)
}
// MakeSelectorB10N builds a selector for selectorCaseB10N
func MakeSelectorB10N(plan *SlicePlan) SelectorFunc {
return makeSelector1(plan.Start, plan.Step, appendSliceB10N)
}
// MakeSelectorB01P builds a selector for selectorCaseB01P
func MakeSelectorB01P(plan *SlicePlan) SelectorFunc {
return makeSelector1(plan.End, plan.Step, appendSliceB01P)
}
// MakeSelectorB01N builds a selector for selectorCaseB01N
func MakeSelectorB01N(plan *SlicePlan) SelectorFunc {
return makeSelector1(plan.End, plan.Step, appendSliceB01N)
}
// MakeSelectorB11PP builds a selector for selectorCaseB11PP
func MakeSelectorB11PP(plan *SlicePlan) SelectorFunc {
return makeSelector2(plan.Start, plan.End, plan.Step, appendSliceB11PP)
}
// MakeSelectorB11PN builds a selector for selectorCaseB11PN
func MakeSelectorB11PN(plan *SlicePlan) SelectorFunc {
return makeSelector2(plan.Start, plan.End, plan.Step, appendSliceB11PN)
}
// MakeSelectorB11NP builds a selector for selectorCaseB11NP
func MakeSelectorB11NP(plan *SlicePlan) SelectorFunc {
return makeSelector2(plan.Start, plan.End, plan.Step, appendSliceB11NP)
}
// MakeSelectorB11NN builds a selector for selectorCaseB11NN
func MakeSelectorB11NN(plan *SlicePlan) SelectorFunc {
return makeSelector2(plan.Start, plan.End, plan.Step, appendSliceB11NN)
}
func selectorCaseFor(s *SliceExpr) selectorCase {
if s.Step == 0 {
return selectorCaseEmpty
}
if s.Step == 1 && !s.HasStart && !s.HasEnd {
return selectorCaseArrayAll
}
if s.Step > 0 {
switch {
case !s.HasStart && !s.HasEnd:
return selectorCaseF00
case s.HasStart && !s.HasEnd:
if s.Start >= 0 {
return selectorCaseF10P
}
return selectorCaseF10N
case !s.HasStart && s.HasEnd:
if s.End >= 0 {
return selectorCaseF01P
}
return selectorCaseF01N
default:
switch {
case s.Start >= 0 && s.End >= 0:
return selectorCaseF11PP
case s.Start >= 0 && s.End < 0:
return selectorCaseF11PN
case s.Start < 0 && s.End >= 0:
return selectorCaseF11NP
default:
return selectorCaseF11NN
}
}
}
switch {
case !s.HasStart && !s.HasEnd:
return selectorCaseB00
case s.HasStart && !s.HasEnd:
if s.Start >= 0 {
return selectorCaseB10P
}
return selectorCaseB10N
case !s.HasStart && s.HasEnd:
if s.End >= 0 {
return selectorCaseB01P
}
return selectorCaseB01N
default:
switch {
case s.Start >= 0 && s.End >= 0:
return selectorCaseB11PP
case s.Start >= 0 && s.End < 0:
return selectorCaseB11PN
case s.Start < 0 && s.End >= 0:
return selectorCaseB11NP
default:
return selectorCaseB11NN
}
}
}
func makeSelector0(step int, appendFunc append0Func) SelectorFunc {
return func(out []any, node, _ any) []any {
arr, ok := node.([]any)
if !ok || len(arr) == 0 {
return out
}
return appendFunc(out, arr, step)
}
}
func makeSelector1(bound, step int, appendFunc append1Func) SelectorFunc {
return func(out []any, node, _ any) []any {
arr, ok := node.([]any)
if !ok || len(arr) == 0 {
return out
}
return appendFunc(out, arr, bound, step)
}
}
func makeSelector2(start, end, step int, appendFunc append2Func) SelectorFunc {
return func(out []any, node, _ any) []any {
arr, ok := node.([]any)
if !ok || len(arr) == 0 {
return out
}
return appendFunc(out, arr, start, end, step)
}
}
func appendSliceF00(out, arr []any, step int) []any {
for idx := 0; idx < len(arr); idx += step {
out = append(out, arr[idx])
}
return out
}
func appendSliceF10P(out, arr []any, start, step int) []any {
start = forwardStartPos(start, len(arr))
for idx := start; idx < len(arr); idx += step {
out = append(out, arr[idx])
}
return out
}
func appendSliceF10N(out, arr []any, start, step int) []any {
start = forwardStartNeg(start, len(arr))
for idx := start; idx < len(arr); idx += step {
out = append(out, arr[idx])
}
return out
}
func appendSliceF01P(out, arr []any, end, step int) []any {
end = forwardEndPos(end, len(arr))
for idx := 0; idx < end; idx += step {
out = append(out, arr[idx])
}
return out
}
func appendSliceF01N(out, arr []any, end, step int) []any {
end = forwardEndNeg(end, len(arr))
for idx := 0; idx < end; idx += step {
out = append(out, arr[idx])
}
return out
}
func appendSliceF11PP(out, arr []any, start, end, step int) []any {
size := len(arr)
start = forwardStartPos(start, size)
end = forwardEndPos(end, size)
if start >= end {
return out
}
for idx := start; idx < end; idx += step {
out = append(out, arr[idx])
}
return out
}
func appendSliceF11PN(out, arr []any, start, end, step int) []any {
size := len(arr)
start = forwardStartPos(start, size)
end = forwardEndNeg(end, size)
if start >= end {
return out
}
for idx := start; idx < end; idx += step {
out = append(out, arr[idx])
}
return out
}
func appendSliceF11NP(out, arr []any, start, end, step int) []any {
size := len(arr)
start = forwardStartNeg(start, size)
end = forwardEndPos(end, size)
if start >= end {
return out
}
for idx := start; idx < end; idx += step {
out = append(out, arr[idx])
}
return out
}
func appendSliceF11NN(out, arr []any, start, end, step int) []any {
size := len(arr)
start = forwardStartNeg(start, size)
end = forwardEndNeg(end, size)
if start >= end {
return out
}
for idx := start; idx < end; idx += step {
out = append(out, arr[idx])
}
return out
}
func appendSliceB00(out, arr []any, step int) []any {
for idx := len(arr) - 1; idx > -1; idx += step {
out = append(out, arr[idx])
}
return out
}
func appendSliceB10P(out, arr []any, start, step int) []any {
start = backwardStartPos(start, len(arr))
for idx := start; idx > -1; idx += step {
out = append(out, arr[idx])
}
return out
}
func appendSliceB10N(out, arr []any, start, step int) []any {
start = backwardStartNeg(start, len(arr))
for idx := start; idx > -1; idx += step {
out = append(out, arr[idx])
}
return out
}
func appendSliceB01P(out, arr []any, end, step int) []any {
end = backwardEndPos(end, len(arr))
for idx := len(arr) - 1; idx > end; idx += step {
out = append(out, arr[idx])
}
return out
}
func appendSliceB01N(out, arr []any, end, step int) []any {
end = backwardEndNeg(end, len(arr))
for idx := len(arr) - 1; idx > end; idx += step {
out = append(out, arr[idx])
}
return out
}
func appendSliceB11PP(out, arr []any, start, end, step int) []any {
start = backwardStartPos(start, len(arr))
end = backwardEndPos(end, len(arr))
for idx := start; idx > end; idx += step {
out = append(out, arr[idx])
}
return out
}
func appendSliceB11PN(out, arr []any, start, end, step int) []any {
start = backwardStartPos(start, len(arr))
end = backwardEndNeg(end, len(arr))
for idx := start; idx > end; idx += step {
out = append(out, arr[idx])
}
return out
}
func appendSliceB11NP(out, arr []any, start, end, step int) []any {
start = backwardStartNeg(start, len(arr))
end = backwardEndPos(end, len(arr))
for idx := start; idx > end; idx += step {
out = append(out, arr[idx])
}
return out
}
func appendSliceB11NN(out, arr []any, start, end, step int) []any {
start = backwardStartNeg(start, len(arr))
end = backwardEndNeg(end, len(arr))
for idx := start; idx > end; idx += step {
out = append(out, arr[idx])
}
return out
}
func forwardStartPos(start, size int) int {
if start > size {
return size
}
return start
}
func forwardStartNeg(start, size int) int {
start += size
if start < 0 {
return 0
}
return start
}
func forwardEndPos(end, size int) int {
if end > size {
return size
}
return end
}
func forwardEndNeg(end, size int) int {
end += size
if end < 0 {
return 0
}
return end
}
func backwardStartPos(start, size int) int {
if start >= size {
return size - 1
}
return start
}
func backwardStartNeg(start, size int) int {
start += size
if start < -1 {
return -1
}
return start
}
func backwardEndPos(end, size int) int {
if end >= size {
return size - 1
}
return end
}
func backwardEndNeg(end, size int) int {
end += size
if end < -1 {
return -1
}
return end
}