forked from digisan/go-generics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iter.go
277 lines (256 loc) · 5 KB
/
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
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
package gogenerics
// Iter : for i := range Iter(end) / (start, end) / (start, step, end)
func Iter[T Integer](params ...T) <-chan T {
start, end, step := *new(T), *new(T), *new(T)
switch len(params) {
case 1:
end = params[0]
step = IF[T](end >= 0, 1, -1)
case 2:
start, end = params[0], params[1]
step = IF[T](end >= start, 1, -1)
case 3:
start, step, end = params[0], params[1], params[2]
if start > end {
if step > 0 {
panic("step error, must be NEGATIVE for bigger start")
}
}
if start < end {
if step < 0 {
panic("step error, must be POSITIVE for smaller start")
}
}
default:
panic("params' count only can be 1, 2 or 3")
}
ch := make(chan T)
if start > end {
go func() {
defer close(ch)
for i := start; i > end; i += step {
ch <- i
}
}()
} else {
go func() {
defer close(ch)
for i := start; i < end; i += step {
ch <- i
}
}()
}
return ch
}
// IterToSlc : for i := range Iter(end) / (start, end) / (start, step, end)
func IterToSlc[T Integer](params ...T) (slc []T) {
// if len(params) == 1 {
// for i := range make([]struct{}, params[0]) {
// slc = append(slc, i)
// }
// return
// }
for i := range Iter(params...) {
slc = append(slc, i)
}
return
}
////////////////////////////////////////////////////////////////////////
type Pair[T any] struct {
A T
B T
First bool
Last bool
ValidA bool
ValidB bool
}
// 1,2,3,4 => (1,2), (2,3), (3,4), (4, junk)
func IterPair[T any](data []T) <-chan Pair[T] {
ch := make(chan Pair[T])
go func() {
defer close(ch)
switch {
case len(data) >= 2:
for i, p := range data[1:] {
ch <- Pair[T]{
A: data[i],
B: p,
First: IF(i == 0, true, false),
Last: false,
ValidA: true,
ValidB: true,
}
}
ch <- Pair[T]{
A: data[len(data)-1],
B: *new(T),
First: false,
Last: true,
ValidA: true,
ValidB: false,
}
case len(data) == 1:
ch <- Pair[T]{
A: data[0],
B: *new(T),
First: true,
Last: true,
ValidA: true,
ValidB: false,
}
default:
ch <- Pair[T]{
A: *new(T),
B: *new(T),
First: false,
Last: false,
ValidA: false,
ValidB: false,
}
}
}()
return ch
}
type Triple[T any] struct {
A T
B T
C T
First bool
Last bool
ValidA bool
ValidB bool
ValidC bool
}
// 1,2,3,4 => (1,2,3), (2,3,4), (3,4,junk), (4,junk,junk)
func IterTriple[T any](data []T) <-chan Triple[T] {
ch := make(chan Triple[T])
go func() {
defer close(ch)
switch {
case len(data) >= 3:
for i, p := range data[2:] {
ch <- Triple[T]{
A: data[i],
ValidA: true,
B: data[i+1],
ValidB: true,
C: p,
ValidC: true,
First: IF(i == 0, true, false),
Last: false,
}
}
ch <- Triple[T]{
A: data[len(data)-2],
ValidA: true,
B: data[len(data)-1],
ValidB: true,
C: *new(T),
ValidC: false,
First: false,
Last: false,
}
ch <- Triple[T]{
A: data[len(data)-1],
ValidA: true,
B: *new(T),
ValidB: false,
C: *new(T),
ValidC: false,
First: false,
Last: true,
}
case len(data) == 2:
ch <- Triple[T]{
A: data[0],
ValidA: true,
B: data[1],
ValidB: true,
C: *new(T),
ValidC: false,
First: true,
Last: false,
}
ch <- Triple[T]{
A: data[1],
ValidA: true,
B: *new(T),
ValidB: false,
C: *new(T),
ValidC: false,
First: false,
Last: true,
}
case len(data) == 1:
ch <- Triple[T]{
A: data[0],
ValidA: true,
B: *new(T),
ValidB: false,
C: *new(T),
ValidC: false,
First: true,
Last: true,
}
default:
ch <- Triple[T]{
A: *new(T),
ValidA: false,
B: *new(T),
ValidB: false,
C: *new(T),
ValidC: false,
First: false,
Last: false,
}
}
}()
return ch
}
type Cache[T any] struct {
Elem T
Cache []T
First bool
Last bool
}
func IterCache[T any](data []T, nPrev, nNext int, junk T) <-chan Cache[T] {
ch := make(chan Cache[T])
go func() {
defer close(ch)
for i, e := range data {
var (
cache []T
idxS int
idxE int
nHeadJunk = 0
nTailJunk = 0
headJunk []T
tailJunk []T
)
if i-nPrev < 0 {
nHeadJunk = nPrev - i
for i := 0; i < nHeadJunk; i++ {
headJunk = append(headJunk, junk)
}
}
if i+nNext >= len(data) {
nTailJunk = i + nNext - len(data) + 1
for i := 0; i < nTailJunk; i++ {
tailJunk = append(tailJunk, junk)
}
}
idxS = IF(i-nPrev >= 0, i-nPrev, 0)
idxE = IF(i+nNext < len(data), i+nNext+1, len(data))
cache = append(cache, headJunk...)
cache = append(cache, data[idxS:idxE]...)
cache = append(cache, tailJunk...)
ch <- Cache[T]{
Elem: e,
Cache: cache,
First: IF(i == 0, true, false),
Last: IF(i == len(data)-1, true, false),
}
}
}()
return ch
}