-
Notifications
You must be signed in to change notification settings - Fork 1
/
slice_iter.go
98 lines (90 loc) · 2.58 KB
/
slice_iter.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
package gofn
// ForEach iterates over slice items.
// For more advanced requirements, see Iter.
func ForEach[T any, S ~[]T](s S, pred func(i int, t T)) {
for i := range s {
pred(i, s[i])
}
}
// ForEachPtr iterates over pointers to slice items.
// You can use this to achieve more performance if you have a slice of big structs.
// For more advanced requirements, see IterPtr.
func ForEachPtr[T any, S ~[]T](s S, pred func(i int, t *T)) {
for i := range s {
pred(i, &s[i])
}
}
// ForEachReverse iterates over slice items from the end.
// For more advanced requirements, see IterReverse.
func ForEachReverse[T any, S ~[]T](s S, pred func(i int, t T)) {
for i := len(s) - 1; i >= 0; i-- {
pred(i, s[i])
}
}
// ForEachPtrReverse iterates over pointers to slice items from the end.
// For more advanced requirements, see IterPtrReverse.
func ForEachPtrReverse[T any, S ~[]T](s S, pred func(i int, t *T)) {
for i := len(s) - 1; i >= 0; i-- {
pred(i, &s[i])
}
}
// Iter iterates over items from multiple slices with ability to stop.
// When the `iterFunc` function returns false, the iteration stops.
func Iter[T any, S ~[]T](iterFunc func(index int, v T) bool, slices ...S) {
global := 0
for _, s := range slices {
for _, v := range s {
if !iterFunc(global, v) {
return
}
global++
}
}
}
// IterPtr iterates over pointers to items from multiple slices with ability to stop.
// When the `iterFunc` function returns false, the iterating stops.
func IterPtr[T any, S ~[]T](iterFunc func(index int, v *T) bool, slices ...S) {
global := 0
for _, s := range slices {
for j := range s {
if !iterFunc(global, &s[j]) {
return
}
global++
}
}
}
// IterReverse iterates over items from multiple slices from the end with ability to stop.
// When the `iterFunc` function returns false, the iteration stops.
func IterReverse[T any, S ~[]T](iterFunc func(index int, v T) bool, slices ...S) {
global := -1
for _, s := range slices {
global += len(s)
}
for i := len(slices) - 1; i >= 0; i-- {
s := slices[i]
for j := len(s) - 1; j >= 0; j-- {
if !iterFunc(global, s[j]) {
return
}
global--
}
}
}
// IterPtrReverse iterates over pointers to items from multiple slices with ability to stop.
// When the `iterFunc` function returns false, the iteration stops.
func IterPtrReverse[T any, S ~[]T](iterFunc func(index int, v *T) bool, slices ...S) {
global := -1
for _, s := range slices {
global += len(s)
}
for i := len(slices) - 1; i >= 0; i-- {
s := slices[i]
for j := len(s) - 1; j >= 0; j-- {
if !iterFunc(global, &s[j]) {
return
}
global--
}
}
}