-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslice.go
78 lines (66 loc) · 1.39 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
// Copyright 2020 VMware, Inc.
// SPDX-License-Identifier: BSD-2-Clause
package slice
// Slice function
func Slice(a []int, pstart *int, pend *int, pstep *int) (res []int) {
if pstart == nil && pend == nil && pstep == nil {
return a
}
step := defaultInt(pstep, 1)
if step == 0 {
return []int{}
}
lower, upper := bounds(pstart, pend, step, len(a))
if step > 0 {
for i := lower; i < upper; i += step {
res = append(res, a[i])
}
} else {
for i := upper; lower < i; i += step {
res = append(res, a[i])
}
}
return
}
func bounds(pstart, pend *int, step, length int) (lower, upper int) {
normalize := normalizer(length)
if step >= 0 {
start := normalize(defaultInt(pstart, 0))
end := normalize(defaultInt(pend, length))
lower = minInt(maxInt(start, 0), length)
upper = minInt(maxInt(end, 0), length)
} else {
start := normalize(defaultInt(pstart, length-1))
end := normalize(defaultInt(pend, -length-1))
upper = minInt(maxInt(start, -1), length-1)
lower = minInt(maxInt(end, -1), length-1)
}
return
}
func defaultInt(v *int, def int) int {
if v != nil {
return *v
}
return def
}
type normalizeFunc func(int) int
func normalizer(length int) normalizeFunc {
return func(v int) int {
if v >= 0 {
return v
}
return length + v
}
}
func maxInt(x, y int) int {
if x > y {
return x
}
return y
}
func minInt(x, y int) int {
if x < y {
return x
}
return y
}