-
Notifications
You must be signed in to change notification settings - Fork 2
/
ChanUInt8.dot.go
363 lines (318 loc) · 9.46 KB
/
ChanUInt8.dot.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
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
// Copyright 2017 Andreas Pannewitz. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package IsInteger
// This file was generated with dotgo
// DO NOT EDIT - Improve the pattern!
// MakeUInt8Chan returns a new open channel
// (simply a 'chan uint8' that is).
//
// Note: No 'UInt8-producer' is launched here yet! (as is in all the other functions).
//
// This is useful to easily create corresponding variables such as
//
// var myUInt8PipelineStartsHere := MakeUInt8Chan()
// // ... lot's of code to design and build Your favourite "myUInt8WorkflowPipeline"
// // ...
// // ... *before* You start pouring data into it, e.g. simply via:
// for drop := range water {
// myUInt8PipelineStartsHere <- drop
// }
// close(myUInt8PipelineStartsHere)
//
// Hint: especially helpful, if Your piping library operates on some hidden (non-exported) type
// (or on a type imported from elsewhere - and You don't want/need or should(!) have to care.)
//
// Note: as always (except for PipeUInt8Buffer) the channel is unbuffered.
//
func MakeUInt8Chan() (out chan uint8) {
return make(chan uint8)
}
func sendUInt8(out chan<- uint8, inp ...uint8) {
defer close(out)
for i := range inp {
out <- inp[i]
}
}
// ChanUInt8 returns a channel to receive all inputs before close.
func ChanUInt8(inp ...uint8) (out <-chan uint8) {
cha := make(chan uint8)
go sendUInt8(cha, inp...)
return cha
}
func sendUInt8Slice(out chan<- uint8, inp ...[]uint8) {
defer close(out)
for i := range inp {
for j := range inp[i] {
out <- inp[i][j]
}
}
}
// ChanUInt8Slice returns a channel to receive all inputs before close.
func ChanUInt8Slice(inp ...[]uint8) (out <-chan uint8) {
cha := make(chan uint8)
go sendUInt8Slice(cha, inp...)
return cha
}
func chanUInt8FuncNok(out chan<- uint8, act func() (uint8, bool)) {
defer close(out)
for {
res, ok := act() // Apply action
if !ok {
return
}
out <- res
}
}
// ChanUInt8FuncNok returns a channel to receive all results of act until nok before close.
func ChanUInt8FuncNok(act func() (uint8, bool)) (out <-chan uint8) {
cha := make(chan uint8)
go chanUInt8FuncNok(cha, act)
return cha
}
func chanUInt8FuncErr(out chan<- uint8, act func() (uint8, error)) {
defer close(out)
for {
res, err := act() // Apply action
if err != nil {
return
}
out <- res
}
}
// ChanUInt8FuncErr returns a channel to receive all results of act until err != nil before close.
func ChanUInt8FuncErr(act func() (uint8, error)) (out <-chan uint8) {
cha := make(chan uint8)
go chanUInt8FuncErr(cha, act)
return cha
}
func joinUInt8(done chan<- struct{}, out chan<- uint8, inp ...uint8) {
defer close(done)
for i := range inp {
out <- inp[i]
}
done <- struct{}{}
}
// JoinUInt8 sends inputs on the given out channel and returns a done channel to receive one signal when inp has been drained
func JoinUInt8(out chan<- uint8, inp ...uint8) (done <-chan struct{}) {
cha := make(chan struct{})
go joinUInt8(cha, out, inp...)
return cha
}
func joinUInt8Slice(done chan<- struct{}, out chan<- uint8, inp ...[]uint8) {
defer close(done)
for i := range inp {
for j := range inp[i] {
out <- inp[i][j]
}
}
done <- struct{}{}
}
// JoinUInt8Slice sends inputs on the given out channel and returns a done channel to receive one signal when inp has been drained
func JoinUInt8Slice(out chan<- uint8, inp ...[]uint8) (done <-chan struct{}) {
cha := make(chan struct{})
go joinUInt8Slice(cha, out, inp...)
return cha
}
func joinUInt8Chan(done chan<- struct{}, out chan<- uint8, inp <-chan uint8) {
defer close(done)
for i := range inp {
out <- i
}
done <- struct{}{}
}
// JoinUInt8Chan sends inputs on the given out channel and returns a done channel to receive one signal when inp has been drained
func JoinUInt8Chan(out chan<- uint8, inp <-chan uint8) (done <-chan struct{}) {
cha := make(chan struct{})
go joinUInt8Chan(cha, out, inp)
return cha
}
func doitUInt8(done chan<- struct{}, inp <-chan uint8) {
defer close(done)
for i := range inp {
_ = i // Drain inp
}
done <- struct{}{}
}
// DoneUInt8 returns a channel to receive one signal before close after inp has been drained.
func DoneUInt8(inp <-chan uint8) (done <-chan struct{}) {
cha := make(chan struct{})
go doitUInt8(cha, inp)
return cha
}
func doitUInt8Slice(done chan<- ([]uint8), inp <-chan uint8) {
defer close(done)
UInt8S := []uint8{}
for i := range inp {
UInt8S = append(UInt8S, i)
}
done <- UInt8S
}
// DoneUInt8Slice returns a channel which will receive a slice
// of all the UInt8s received on inp channel before close.
// Unlike DoneUInt8, a full slice is sent once, not just an event.
func DoneUInt8Slice(inp <-chan uint8) (done <-chan ([]uint8)) {
cha := make(chan ([]uint8))
go doitUInt8Slice(cha, inp)
return cha
}
func doitUInt8Func(done chan<- struct{}, inp <-chan uint8, act func(a uint8)) {
defer close(done)
for i := range inp {
act(i) // Apply action
}
done <- struct{}{}
}
// DoneUInt8Func returns a channel to receive one signal before close after act has been applied to all inp.
func DoneUInt8Func(inp <-chan uint8, act func(a uint8)) (out <-chan struct{}) {
cha := make(chan struct{})
if act == nil {
act = func(a uint8) { return }
}
go doitUInt8Func(cha, inp, act)
return cha
}
func pipeUInt8Buffer(out chan<- uint8, inp <-chan uint8) {
defer close(out)
for i := range inp {
out <- i
}
}
// PipeUInt8Buffer returns a buffered channel with capacity cap to receive all inp before close.
func PipeUInt8Buffer(inp <-chan uint8, cap int) (out <-chan uint8) {
cha := make(chan uint8, cap)
go pipeUInt8Buffer(cha, inp)
return cha
}
func pipeUInt8Func(out chan<- uint8, inp <-chan uint8, act func(a uint8) uint8) {
defer close(out)
for i := range inp {
out <- act(i)
}
}
// PipeUInt8Func returns a channel to receive every result of act applied to inp before close.
// Note: it 'could' be PipeUInt8Map for functional people,
// but 'map' has a very different meaning in go lang.
func PipeUInt8Func(inp <-chan uint8, act func(a uint8) uint8) (out <-chan uint8) {
cha := make(chan uint8)
if act == nil {
act = func(a uint8) uint8 { return a }
}
go pipeUInt8Func(cha, inp, act)
return cha
}
func pipeUInt8Fork(out1, out2 chan<- uint8, inp <-chan uint8) {
defer close(out1)
defer close(out2)
for i := range inp {
out1 <- i
out2 <- i
}
}
// PipeUInt8Fork returns two channels to receive every result of inp before close.
// Note: Yes, it is a VERY simple fanout - but sometimes all You need.
func PipeUInt8Fork(inp <-chan uint8) (out1, out2 <-chan uint8) {
cha1 := make(chan uint8)
cha2 := make(chan uint8)
go pipeUInt8Fork(cha1, cha2, inp)
return cha1, cha2
}
// UInt8Tube is the signature for a pipe function.
type UInt8Tube func(inp <-chan uint8, out <-chan uint8)
// UInt8Daisy returns a channel to receive all inp after having passed thru tube.
func UInt8Daisy(inp <-chan uint8, tube UInt8Tube) (out <-chan uint8) {
cha := make(chan uint8)
go tube(inp, cha)
return cha
}
// UInt8DaisyChain returns a channel to receive all inp after having passed thru all tubes.
func UInt8DaisyChain(inp <-chan uint8, tubes ...UInt8Tube) (out <-chan uint8) {
cha := inp
for i := range tubes {
cha = UInt8Daisy(cha, tubes[i])
}
return cha
}
/*
func sendOneInto(snd chan<- int) {
defer close(snd)
snd <- 1 // send a 1
}
func sendTwoInto(snd chan<- int) {
defer close(snd)
snd <- 1 // send a 1
snd <- 2 // send a 2
}
var fun = func(left chan<- int, right <-chan int) { left <- 1 + <-right }
func main() {
leftmost := make(chan int)
right := daisyChain(leftmost, fun, 10000) // the chain - right to left!
go sendTwoInto(right)
fmt.Println(<-leftmost)
}
*/
// MergeUInt8 returns a channel to receive all inputs sorted and free of duplicates.
// Each input channel needs to be ascending; sorted and free of duplicates.
// Note: If no inputs are given, a closed UInt8channel is returned.
func MergeUInt8(inps ...<-chan uint8) (out <-chan uint8) {
if len(inps) < 1 { // none: return a closed channel
cha := make(chan uint8)
defer close(cha)
return cha
} else if len(inps) < 2 { // just one: return it
return inps[0]
} else { // tail recurse
return mergeUInt82(inps[0], MergeUInt8(inps[1:]...))
}
}
// mergeUInt82 takes two (eager) channels of comparable types,
// each of which needs to be sorted and free of duplicates,
// and merges them into a returned channel, which will be sorted and free of duplicates
func mergeUInt82(i1, i2 <-chan uint8) (out <-chan uint8) {
cha := make(chan uint8)
go func(out chan<- uint8, i1, i2 <-chan uint8) {
defer close(out)
var (
clos1, clos2 bool // we found the chan closed
buff1, buff2 bool // we've read 'from', but not sent (yet)
ok bool // did we read sucessfully?
from1, from2 uint8 // what we've read
)
for !clos1 || !clos2 {
if !clos1 && !buff1 {
if from1, ok = <-i1; ok {
buff1 = true
} else {
clos1 = true
}
}
if !clos2 && !buff2 {
if from2, ok = <-i2; ok {
buff2 = true
} else {
clos2 = true
}
}
if clos1 && !buff1 {
from1 = from2
}
if clos2 && !buff2 {
from2 = from1
}
if from1 < from2 {
out <- from1
buff1 = false
} else if from2 < from1 {
out <- from2
buff2 = false
} else {
out <- from1 // == from2
buff1 = false
buff2 = false
}
}
}(cha, i1, i2)
return cha
}
// Note: merge2 is not my own. Just: I forgot where found it - please accept my apologies.
// I'd love to learn about it's origin/author, so I can give credit. Any hint is highly appreciated!