-
Notifications
You must be signed in to change notification settings - Fork 1
/
chain.go
298 lines (228 loc) · 6.95 KB
/
chain.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
package ranges
type baseChain[T any] struct {
r InputRange[InputRange[T]]
isPrimed bool
}
func (bc *baseChain[T]) prime() {
if !bc.isPrimed {
for !bc.r.Empty() && bc.r.Front().Empty() {
bc.r.PopFront()
}
bc.isPrimed = true
}
}
func (bc *baseChain[T]) Empty() bool {
bc.prime()
return bc.r.Empty()
}
func (bc *baseChain[T]) Front() T {
bc.prime()
return bc.r.Front().Front()
}
// flattenResult implments Flatten
type flattenResult[T any] struct {
baseChain[T]
}
func (fr *flattenResult[T]) PopFront() {
fr.prime()
fr.r.Front().PopFront()
fr.isPrimed = false
}
// frontTransversalResult implments FrontTransversal
type frontTransversalResult[T any] struct {
baseChain[T]
}
func (ftr *frontTransversalResult[T]) PopFront() {
ftr.prime()
ftr.r.PopFront()
ftr.isPrimed = false
}
type baseForwardChain[T any] struct {
r ForwardRange[ForwardRange[T]]
isPrimed bool
}
func (bc *baseForwardChain[T]) prime() {
if !bc.isPrimed {
for !bc.r.Empty() && bc.r.Front().Empty() {
bc.r.PopFront()
}
bc.isPrimed = true
}
}
func (bc *baseForwardChain[T]) Empty() bool {
bc.prime()
return bc.r.Empty()
}
func (bc *baseForwardChain[T]) Front() T {
bc.prime()
return bc.r.Front().Front()
}
func (bc *baseForwardChain[T]) saveOuter() ForwardRange[ForwardRange[T]] {
newList := make([]ForwardRange[T], 0)
r := bc.r.Save()
for !r.Empty() {
newList = append(newList, r.Front().Save())
r.PopFront()
}
return SliceRange(newList)
}
// flattenForwardResult implements FlattenF
type flattenForwardResult[T any] struct {
baseForwardChain[T]
}
func (fr *flattenForwardResult[T]) PopFront() {
fr.prime()
fr.r.Front().PopFront()
fr.isPrimed = false
}
func (fr *flattenForwardResult[T]) Save() ForwardRange[T] {
return &flattenForwardResult[T]{baseForwardChain[T]{fr.saveOuter(), fr.isPrimed}}
}
// frontTransversalForwardResult implments FrontTraveralF
type frontTransversalForwardResult[T any] struct {
baseForwardChain[T]
}
func (ftr *frontTransversalForwardResult[T]) PopFront() {
ftr.prime()
ftr.r.PopFront()
ftr.isPrimed = false
}
func (ftr *frontTransversalForwardResult[T]) Save() ForwardRange[T] {
return &frontTransversalForwardResult[T]{baseForwardChain[T]{ftr.saveOuter(), ftr.isPrimed}}
}
type baseBidrectionalChain[T any] struct {
r BidirectionalRange[BidirectionalRange[T]]
isPrimed bool
}
func (bc *baseBidrectionalChain[T]) prime() {
if !bc.isPrimed {
for !bc.r.Empty() && bc.r.Front().Empty() {
bc.r.PopFront()
}
for !bc.r.Empty() && bc.r.Back().Empty() {
bc.r.PopBack()
}
bc.isPrimed = true
}
}
func (bc *baseBidrectionalChain[T]) Empty() bool {
bc.prime()
return bc.r.Empty()
}
func (bc *baseBidrectionalChain[T]) Front() T {
bc.prime()
return bc.r.Front().Front()
}
func (bc *baseBidrectionalChain[T]) saveOuter() BidirectionalRange[BidirectionalRange[T]] {
newList := make([]BidirectionalRange[T], 0)
r := bc.r.Save()
for !r.Empty() {
newList = append(newList, r.Front().SaveB())
r.PopFront()
}
return SliceRange(newList)
}
// flattenBidirectionalResult implements FlattenB
type flattenBidirectionalResult[T any] struct {
baseBidrectionalChain[T]
}
func (fr *flattenBidirectionalResult[T]) PopFront() {
fr.prime()
fr.r.Front().PopFront()
fr.isPrimed = false
}
func (fr *flattenBidirectionalResult[T]) Back() T {
fr.prime()
return fr.r.Back().Back()
}
func (fr *flattenBidirectionalResult[T]) PopBack() {
fr.prime()
fr.r.Back().PopBack()
fr.isPrimed = false
}
func (fr *flattenBidirectionalResult[T]) Save() ForwardRange[T] {
return fr.SaveB()
}
func (fr *flattenBidirectionalResult[T]) SaveB() BidirectionalRange[T] {
return &flattenBidirectionalResult[T]{baseBidrectionalChain[T]{fr.saveOuter(), fr.isPrimed}}
}
// flattenRandomAccessResult implements FlattenR
type flattenRandomAccessResult[T any] struct {
flattenBidirectionalResult[T]
}
// frontTransversalBidirectionalResult implments FrontTraveralB
type frontTransversalBidirectionalResult[T any] struct {
baseBidrectionalChain[T]
}
func (fr *frontTransversalBidirectionalResult[T]) PopFront() {
fr.prime()
fr.r.PopFront()
fr.isPrimed = false
}
func (fr *frontTransversalBidirectionalResult[T]) Back() T {
fr.prime()
return fr.r.Back().Front()
}
func (fr *frontTransversalBidirectionalResult[T]) PopBack() {
fr.prime()
fr.r.PopBack()
fr.isPrimed = false
}
func (fr *frontTransversalBidirectionalResult[T]) Save() ForwardRange[T] {
return fr.SaveB()
}
func (fr *frontTransversalBidirectionalResult[T]) SaveB() BidirectionalRange[T] {
return &frontTransversalBidirectionalResult[T]{baseBidrectionalChain[T]{fr.saveOuter(), fr.isPrimed}}
}
// Flatten combines a range of ranges into one straight range
func Flatten[T any](r InputRange[InputRange[T]]) InputRange[T] {
return &flattenResult[T]{baseChain[T]{r, false}}
}
// FlattenF is `Flatten` where the range can be saved.
func FlattenF[T any](r ForwardRange[ForwardRange[T]]) ForwardRange[T] {
return &flattenForwardResult[T]{baseForwardChain[T]{r, false}}
}
// FlattenB is `FlattenF` that can be shrunk from the back.
func FlattenB[T any](r BidirectionalRange[BidirectionalRange[T]]) BidirectionalRange[T] {
return &flattenBidirectionalResult[T]{baseBidrectionalChain[T]{r, false}}
}
// FlattenS is `FlattenF` accepting a slice.
func FlattenS[T any](r []ForwardRange[T]) ForwardRange[T] {
return FlattenF(SliceRange(r))
}
// FlattenSB is `FlattenS` for bidirectional ranges.
func FlattenSB[T any](r []BidirectionalRange[T]) BidirectionalRange[T] {
return FlattenB(SliceRange(r))
}
// FlattenSS is `FlattenB` accepting a slice of slices.
func FlattenSS[T any](r [][]T) BidirectionalRange[T] {
return FlattenB(MapS(r, func(s []T) BidirectionalRange[T] { return SliceRange(s) }))
}
// FrontTransversal yields the first value in each range, skipping empty ranges.
func FrontTransversal[T any](r InputRange[InputRange[T]]) InputRange[T] {
return &frontTransversalResult[T]{baseChain[T]{r, false}}
}
// FrontTransversalF is `FrontTransversal` where the range can be saved.
func FrontTransversalF[T any](r ForwardRange[ForwardRange[T]]) ForwardRange[T] {
return &frontTransversalForwardResult[T]{baseForwardChain[T]{r, false}}
}
// FrontTransversalB is `FrontTransversalF` that can be shrunk from the back.
func FrontTransversalB[T any](r BidirectionalRange[BidirectionalRange[T]]) BidirectionalRange[T] {
return &frontTransversalBidirectionalResult[T]{baseBidrectionalChain[T]{r, false}}
}
// Chain produces the results of all the ranges together in a sequence.
func Chain[T any](ranges ...InputRange[T]) InputRange[T] {
return Flatten[T](SliceRange(ranges))
}
// ChainF is `Chain` where the range can be saved.
func ChainF[T any](ranges ...ForwardRange[T]) ForwardRange[T] {
return FlattenF(SliceRange(ranges))
}
// ChainB is `ChainF` that can be shrunk from the back.
func ChainB[T any](ranges ...BidirectionalRange[T]) BidirectionalRange[T] {
return FlattenB(SliceRange(ranges))
}
// ChainS is `ChainB` accepting many slices.
func ChainS[T any](ranges ...[]T) BidirectionalRange[T] {
return FlattenSS(ranges)
}