-
Notifications
You must be signed in to change notification settings - Fork 1
/
pipe.go
1327 lines (1113 loc) · 33.7 KB
/
pipe.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This file was automatically generated by genny.
// Any changes will be lost if this file is regenerated.
// see https://github.com/cheekybits/genny
// 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.
// Code generated by golang.org/x/tools/cmd/bundle. DO NOT EDIT.
package pipe
import (
"container/ring"
"sync"
"time"
"github.com/cheekybits/genny/generic"
)
// Any is the generic type flowing thru the pipe network.
type Any generic.Type
// ===========================================================================
// Beg of AnyMake creators
// AnyMakeChan returns a new open channel
// (simply a 'chan Any' that is).
// Note: No 'Any-producer' is launched here yet! (as is in all the other functions).
// This is useful to easily create corresponding variables such as:
/*
var myAnyPipelineStartsHere := AnyMakeChan()
// ... lot's of code to design and build Your favourite "myAnyWorkflowPipeline"
// ...
// ... *before* You start pouring data into it, e.g. simply via:
for drop := range water {
myAnyPipelineStartsHere <- drop
}
close(myAnyPipelineStartsHere)
*/
// 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 AnyPipeBuffer) the channel is unbuffered.
//
func AnyMakeChan() (out chan Any) {
return make(chan Any)
}
// End of AnyMake creators
// ===========================================================================
// ===========================================================================
// Beg of AnyChan producers
// AnyChan returns a channel to receive
// all inputs
// before close.
func AnyChan(inp ...Any) (out <-chan Any) {
cha := make(chan Any)
go chanAny(cha, inp...)
return cha
}
func chanAny(out chan<- Any, inp ...Any) {
defer close(out)
for i := range inp {
out <- inp[i]
}
}
// AnyChanSlice returns a channel to receive
// all inputs
// before close.
func AnyChanSlice(inp ...[]Any) (out <-chan Any) {
cha := make(chan Any)
go chanAnySlice(cha, inp...)
return cha
}
func chanAnySlice(out chan<- Any, inp ...[]Any) {
defer close(out)
for i := range inp {
for j := range inp[i] {
out <- inp[i][j]
}
}
}
// AnyChanFuncNok returns a channel to receive
// all results of generator `gen`
// until `!ok`
// before close.
func AnyChanFuncNok(gen func() (Any, bool)) (out <-chan Any) {
cha := make(chan Any)
go chanAnyFuncNok(cha, gen)
return cha
}
func chanAnyFuncNok(out chan<- Any, gen func() (Any, bool)) {
defer close(out)
for {
res, ok := gen() // generate
if !ok {
return
}
out <- res
}
}
// AnyChanFuncErr returns a channel to receive
// all results of generator `gen`
// until `err != nil`
// before close.
func AnyChanFuncErr(gen func() (Any, error)) (out <-chan Any) {
cha := make(chan Any)
go chanAnyFuncErr(cha, gen)
return cha
}
func chanAnyFuncErr(out chan<- Any, gen func() (Any, error)) {
defer close(out)
for {
res, err := gen() // generate
if err != nil {
return
}
out <- res
}
}
// End of AnyChan producers
// ===========================================================================
// ===========================================================================
// Beg of AnyPipe functions
// AnyPipeFunc returns a channel to receive
// every result of action `act` applied to `inp`
// before close.
// Note: it 'could' be PipeAnyMap for functional people,
// but 'map' has a very different meaning in go lang.
func AnyPipeFunc(inp <-chan Any, act func(a Any) Any) (out <-chan Any) {
cha := make(chan Any)
if act == nil { // Make `nil` value useful
act = func(a Any) Any { return a }
}
go pipeAnyFunc(cha, inp, act)
return cha
}
func pipeAnyFunc(out chan<- Any, inp <-chan Any, act func(a Any) Any) {
defer close(out)
for i := range inp {
out <- act(i) // apply action
}
}
// End of PipeAny functions
// ===========================================================================
// ===========================================================================
// Beg of AnyTube closures around AnyPipe
// AnyTubeFunc returns a closure around PipeAnyFunc (_, act).
func AnyTubeFunc(act func(a Any) Any) (tube func(inp <-chan Any) (out <-chan Any)) {
return func(inp <-chan Any) (out <-chan Any) {
return AnyPipeFunc(inp, act)
}
}
// End of AnyTube closures around AnyPipe
// ===========================================================================
// ===========================================================================
// Beg of AnyDone terminators
// AnyDone returns a channel to receive
// one signal before close after `inp` has been drained.
func AnyDone(inp <-chan Any) (done <-chan struct{}) {
sig := make(chan struct{})
go doneAny(sig, inp)
return sig
}
func doneAny(done chan<- struct{}, inp <-chan Any) {
defer close(done)
for i := range inp {
_ = i // Drain inp
}
done <- struct{}{}
}
// AnyDoneSlice returns a channel to receive
// a slice with every Any received on `inp`
// before close.
//
// Note: Unlike AnyDone, DoneAnySlice sends the fully accumulated slice, not just an event, once upon close of inp.
func AnyDoneSlice(inp <-chan Any) (done <-chan []Any) {
sig := make(chan []Any)
go doneAnySlice(sig, inp)
return sig
}
func doneAnySlice(done chan<- []Any, inp <-chan Any) {
defer close(done)
slice := []Any{}
for i := range inp {
slice = append(slice, i)
}
done <- slice
}
// AnyDoneFunc returns a channel to receive
// one signal after `act` has been applied to every `inp`
// before close.
func AnyDoneFunc(inp <-chan Any, act func(a Any)) (done <-chan struct{}) {
sig := make(chan struct{})
if act == nil {
act = func(a Any) { return }
}
go doneAnyFunc(sig, inp, act)
return sig
}
func doneAnyFunc(done chan<- struct{}, inp <-chan Any, act func(a Any)) {
defer close(done)
for i := range inp {
act(i) // apply action
}
done <- struct{}{}
}
// End of AnyDone terminators
// ===========================================================================
// ===========================================================================
// Beg of AnyFini closures
// AnyFini returns a closure around `AnyDone(_)`.
func AnyFini() func(inp <-chan Any) (done <-chan struct{}) {
return func(inp <-chan Any) (done <-chan struct{}) {
return AnyDone(inp)
}
}
// AnyFiniSlice returns a closure around `AnyDoneSlice(_)`.
func AnyFiniSlice() func(inp <-chan Any) (done <-chan []Any) {
return func(inp <-chan Any) (done <-chan []Any) {
return AnyDoneSlice(inp)
}
}
// AnyFiniFunc returns a closure around `AnyDoneFunc(_, act)`.
func AnyFiniFunc(act func(a Any)) func(inp <-chan Any) (done <-chan struct{}) {
return func(inp <-chan Any) (done <-chan struct{}) {
return AnyDoneFunc(inp, act)
}
}
// End of AnyFini closures
// ===========================================================================
// ===========================================================================
// Beg of AnyPair functions
// AnyPair returns a pair of channels to receive every result of inp before close.
// Note: Yes, it is a VERY simple fanout - but sometimes all You need.
func AnyPair(inp <-chan Any) (out1, out2 <-chan Any) {
cha1 := make(chan Any)
cha2 := make(chan Any)
go pairAny(cha1, cha2, inp)
return cha1, cha2
}
/* not used - kept for reference only.
func pairAny(out1, out2 chan<- Any, inp <-chan Any) {
defer close(out1)
defer close(out2)
for i := range inp {
out1 <- i
out2 <- i
}
} */
func pairAny(out1, out2 chan<- Any, inp <-chan Any) {
defer close(out1)
defer close(out2)
for i := range inp {
select { // send first to whomever is ready to receive
case out1 <- i:
out2 <- i
case out2 <- i:
out1 <- i
}
}
}
// End of AnyPair functions
// ===========================================================================
// ===========================================================================
// Beg of AnyFork functions
// AnyFork returns two channels
// either of which is to receive
// every result of inp
// before close.
func AnyFork(inp <-chan Any) (out1, out2 <-chan Any) {
cha1 := make(chan Any)
cha2 := make(chan Any)
go forkAny(cha1, cha2, inp)
return cha1, cha2
}
/* not used - kept for reference only.
func forkAny(out1, out2 chan<- Any, inp <-chan Any) {
defer close(out1)
defer close(out2)
for i := range inp {
out1 <- i
out2 <- i
}
} */
func forkAny(out1, out2 chan<- Any, inp <-chan Any) {
defer close(out1)
defer close(out2)
for i := range inp {
select { // send first to whomever is ready to receive
case out1 <- i:
out2 <- i
case out2 <- i:
out1 <- i
}
}
}
// End of AnyFork functions
// ===========================================================================
// ===========================================================================
// Beg of AnyFanIn2 simple binary Fan-In
// AnyFanIn2 returns a channel to receive all to receive all from both `inp1` and `inp2` before close.
func AnyFanIn2(inp1, inp2 <-chan Any) (out <-chan Any) {
cha := make(chan Any)
go fanIn2Any(cha, inp1, inp2)
return cha
}
/* not used - kept for reference only.
// fanin2Any as seen in Go Concurrency Patterns
func fanin2Any(out chan<- Any, inp1, inp2 <-chan Any) {
for {
select {
case e := <-inp1:
out <- e
case e := <-inp2:
out <- e
}
}
} */
func fanIn2Any(out chan<- Any, inp1, inp2 <-chan Any) {
defer close(out)
var (
closed bool // we found a chan closed
ok bool // did we read successfully?
e Any // what we've read
)
for !closed {
select {
case e, ok = <-inp1:
if ok {
out <- e
} else {
inp1 = inp2 // swap inp2 into inp1
closed = true // break out of the loop
}
case e, ok = <-inp2:
if ok {
out <- e
} else {
closed = true // break out of the loop }
}
}
}
// inp1 might not be closed yet. Drain it.
for e = range inp1 {
out <- e
}
}
// End of AnyFanIn2 simple binary Fan-In
// ===========================================================================
// Beg of AnyPipeBuffered - a buffered channel with capacity `cap` to receive
// AnyPipeBuffer returns a buffered channel with capacity `cap` to receive
// all `inp`
// before close.
func AnyPipeBuffered(inp <-chan Any, cap int) (out <-chan Any) {
cha := make(chan Any, cap)
go pipeAnyBuffer(cha, inp)
return cha
}
func pipeAnyBuffered(out chan<- Any, inp <-chan Any) {
defer close(out)
for i := range inp {
out <- i
}
}
// AnyTubeBuffered returns a closure around PipeAnyBuffer (_, cap).
func AnyTubeBuffered(cap int) (tube func(inp <-chan Any) (out <-chan Any)) {
return func(inp <-chan Any) (out <-chan Any) {
return AnyPipeBuffer(inp, cap)
}
}
// End of AnyPipeBuffered - a buffered channel with capacity `cap` to receive
// ===========================================================================
// Beg of AnyPipeEnter/Leave - Flapdoors observed by a Waiter
// AnyWaiter - as implemented by `*sync.WaitGroup` -
// attends Flapdoors and keeps counting
// who enters and who leaves.
//
// Use DoneAnyWait to learn about
// when the facilities are closed.
//
// Note: You may also use Your provided `*sync.WaitGroup.Wait()`
// to know when to close the facilities.
// Just: DoneAnyWait is more convenient
// as it also closes the primary channel.
//
// Just make sure to have _all_ entrances and exits attended,
// and `Wait()` only *after* You've started flooding the facilities.
type AnyWaiter interface {
Add(delta int)
Done()
Wait()
}
// Note: Name is generic in order to avoid multiple-declaration clashes.
// AnyPipeEnter returns a channel to receive
// all `inp`
// and registers throughput
// as arrival
// on the given `sync.WaitGroup`
// until close.
func AnyPipeEnter(inp <-chan Any, wg AnyWaiter) (out <-chan Any) {
cha := make(chan Any)
go pipeAnyEnter(cha, wg, inp)
return cha
}
// AnyPipeLeave returns a channel to receive
// all `inp`
// and registers throughput
// as departure
// on the given `sync.WaitGroup`
// until close.
func AnyPipeLeave(inp <-chan Any, wg AnyWaiter) (out <-chan Any) {
cha := make(chan Any)
go pipeAnyLeave(cha, wg, inp)
return cha
}
func pipeAnyEnter(out chan<- Any, wg AnyWaiter, inp <-chan Any) {
defer close(out)
for i := range inp {
wg.Add(1)
out <- i
}
}
func pipeAnyLeave(out chan<- Any, wg AnyWaiter, inp <-chan Any) {
defer close(out)
for i := range inp {
out <- i
wg.Done()
}
}
// AnyTubeEnter returns a closure around AnyPipeEnter (_, wg)
// registering throughput
// on the given `sync.WaitGroup`
// as arrival.
func AnyTubeEnter(wg AnyWaiter) (tube func(inp <-chan Any) (out <-chan Any)) {
return func(inp <-chan Any) (out <-chan Any) {
return AnyPipeEnter(inp, wg)
}
}
// AnyTubeLeave returns a closure around AnyPipeLeave (_, wg)
// registering throughput
// on the given `sync.WaitGroup`
// as departure.
func AnyTubeLeave(wg AnyWaiter) (tube func(inp <-chan Any) (out <-chan Any)) {
return func(inp <-chan Any) (out <-chan Any) {
return AnyPipeLeave(inp, wg)
}
}
// AnyDoneWait returns a channel to receive
// one signal
// after wg.Wait() has returned and inp has been closed
// before close.
//
// Note: Use only *after* You've started flooding the facilities.
func AnyDoneWait(inp chan<- Any, wg AnyWaiter) (done <-chan struct{}) {
cha := make(chan struct{})
go doneAnyWait(cha, inp, wg)
return cha
}
func doneAnyWait(done chan<- struct{}, inp chan<- Any, wg AnyWaiter) {
defer close(done)
wg.Wait()
close(inp)
done <- struct{}{} // not really needed - but looks better
}
// AnyFiniWait returns a closure around `DoneAnyWait(_, wg)`.
func AnyFiniWait(wg AnyWaiter) func(inp chan<- Any) (done <-chan struct{}) {
return func(inp chan<- Any) (done <-chan struct{}) {
return AnyDoneWait(inp, wg)
}
}
// End of AnyPipeEnter/Leave - Flapdoors observed by a Waiter
// ===========================================================================
// Beg of AnyPipeDone
// AnyPipeDone returns a channel to receive every `inp` before close and a channel to signal this closing.
func AnyPipeDone(inp <-chan Any) (out <-chan Any, done <-chan struct{}) {
cha := make(chan Any)
doit := make(chan struct{})
go pipeAnyDone(cha, doit, inp)
return cha, doit
}
func pipeAnyDone(out chan<- Any, done chan<- struct{}, inp <-chan Any) {
defer close(out)
defer close(done)
for i := range inp {
out <- i
}
done <- struct{}{}
}
// End of AnyPipeDone
// ===========================================================================
// Beg of AnyPlug - graceful terminator
// AnyPlug returns a channel to receive every `inp` before close and a channel to signal this closing.
// Upon receipt of a stop signal,
// output is immediately closed,
// and for graceful termination
// any remaining input is drained before done is signalled.
func AnyPlug(inp <-chan Any, stop <-chan struct{}) (out <-chan Any, done <-chan struct{}) {
cha := make(chan Any)
doit := make(chan struct{})
go plugAny(cha, doit, inp, stop)
return cha, doit
}
func plugAny(out chan<- Any, done chan<- struct{}, inp <-chan Any, stop <-chan struct{}) {
defer close(done)
var end bool // shall we end?
var ok bool // did we read successfully?
var e Any // what we've read
for !end {
select {
case e, ok = <-inp:
if ok {
out <- e
} else {
end = true
}
case <-stop:
end = true
}
}
close(out)
for range inp {
// drain inp
}
done <- struct{}{}
}
// End of AnyPlug - graceful terminator
// ===========================================================================
// Beg of AnyPlugAfter - graceful terminator
// AnyPlugAfter returns a channel to receive every `inp` before close and a channel to signal this closing.
// Upon receipt of a time signal
// (e.g. from `time.After(...)`),
// output is immediately closed,
// and for graceful termination
// any remaining input is drained before done is signalled.
func AnyPlugAfter(inp <-chan Any, after <-chan time.Time) (out <-chan Any, done <-chan struct{}) {
cha := make(chan Any)
doit := make(chan struct{})
go plugAnyAfter(cha, doit, inp, after)
return cha, doit
}
func plugAnyAfter(out chan<- Any, done chan<- struct{}, inp <-chan Any, after <-chan time.Time) {
defer close(done)
var end bool // shall we end?
var ok bool // did we read successfully?
var e Any // what we've read
for !end {
select {
case e, ok = <-inp:
if ok {
out <- e
} else {
end = true
}
case <-after:
end = true
}
}
close(out)
for range inp {
// drain inp
}
done <- struct{}{}
}
// End of AnyPlugAfter - graceful terminator
// Note: pipeAnyAdjust imports "container/ring" for the expanding buffer.
// ===========================================================================
// Beg of AnyPipeAdjust
// AnyPipeAdjust returns a channel to receive
// all `inp`
// buffered by a AnySendProxy process
// before close.
func AnyPipeAdjust(inp <-chan Any, sizes ...int) (out <-chan Any) {
cap, que := sendAnyProxySizes(sizes...)
cha := make(chan Any, cap)
go pipeAnyAdjust(cha, inp, que)
return cha
}
// AnyTubeAdjust returns a closure around AnyPipeAdjust (_, sizes ...int).
func AnyTubeAdjust(sizes ...int) (tube func(inp <-chan Any) (out <-chan Any)) {
return func(inp <-chan Any) (out <-chan Any) {
return AnyPipeAdjust(inp, sizes...)
}
}
// End of AnyPipeAdjust
// ===========================================================================
// ===========================================================================
// Beg of sendAnyProxy
func sendAnyProxySizes(sizes ...int) (cap, que int) {
// CAP is the minimum capacity of the buffered proxy channel in `AnySendProxy`
const CAP = 10
// QUE is the minimum initially allocated size of the circular queue in `AnySendProxy`
const QUE = 16
cap = CAP
que = QUE
if len(sizes) > 0 && sizes[0] > CAP {
que = sizes[0]
}
if len(sizes) > 1 && sizes[1] > QUE {
que = sizes[1]
}
if len(sizes) > 2 {
panic("AnySendProxy: too many sizes")
}
return
}
// AnySendProxy returns a channel to serve as a sending proxy to 'out'.
// Uses a goroutine to receive values from 'out' and store them
// in an expanding buffer, so that sending to 'out' never blocks.
// Note: the expanding buffer is implemented via "container/ring"
//
// Note: AnySendProxy is kept for the Sieve example
// and other dynamic use to be discovered
// even so it does not fit the pipe tube pattern as AnyPipeAdjust does.
func AnySendProxy(out chan<- Any, sizes ...int) chan<- Any {
cap, que := sendAnyProxySizes(sizes...)
cha := make(chan Any, cap)
go pipeAnyAdjust(out, cha, que)
return cha
}
// pipeAnyAdjust uses an adjusting buffer to receive from 'inp'
// even so 'out' is not ready to receive yet. The buffer may grow
// until 'inp' is closed and then will shrink by every send to 'out'.
// Note: the adjusting buffer is implemented via "container/ring"
func pipeAnyAdjust(out chan<- Any, inp <-chan Any, QUE int) {
defer close(out)
n := QUE // the allocated size of the circular queue
first := ring.New(n)
last := first
var c chan<- Any
var e Any
ok := true
for ok {
c = out
if first == last {
c = nil // buffer empty: disable output
} else {
e = first.Value.(Any)
}
select {
case e, ok = <-inp:
if ok {
last.Value = e
if last.Next() == first {
last.Link(ring.New(n)) // buffer full: expand it
n *= 2
}
last = last.Next()
}
case c <- e:
first = first.Next()
}
}
for first != last {
out <- first.Value.(Any)
first = first.Unlink(1) // first.Next()
}
}
// End of sendAnyProxy
// ===========================================================================
// Beg of AnyFanOut
// AnyFanOut returns a slice (of size = size) of channels
// each of which shall receive any inp before close.
func AnyFanOut(inp <-chan Any, size int) (outS [](<-chan Any)) {
chaS := make([]chan Any, size)
for i := 0; i < size; i++ {
chaS[i] = make(chan Any)
}
go fanAnyOut(inp, chaS...)
outS = make([]<-chan Any, size)
for i := 0; i < size; i++ {
outS[i] = chaS[i] // convert `chan` to `<-chan`
}
return outS
}
// c fanAnyOut(inp <-chan Any, outs ...chan<- Any) {
func fanAnyOut(inp <-chan Any, outs ...chan Any) {
for i := range inp {
for o := range outs {
outs[o] <- i
}
}
for o := range outs {
close(outs[o])
}
}
// End of AnyFanOut
// ===========================================================================
// Beg of AnyStrew - scatter them
// AnyStrew returns a slice (of size = size) of channels
// one of which shall receive each inp before close.
func AnyStrew(inp <-chan Any, size int) (outS [](<-chan Any)) {
chaS := make([]chan Any, size)
for i := 0; i < size; i++ {
chaS[i] = make(chan Any)
}
go strewAny(inp, chaS...)
outS = make([]<-chan Any, size)
for i := 0; i < size; i++ {
outS[i] = chaS[i] // convert `chan` to `<-chan`
}
return outS
}
// c strewAny(inp <-chan Any, outS ...chan<- Any) {
// Note: go does not convert the passed slice `[]chan Any` to `[]chan<- Any` automatically.
// So, we do neither here, as we are lazy (we just call an internal helper function).
func strewAny(inp <-chan Any, outS ...chan Any) {
for i := range inp {
for !trySendAny(i, outS...) {
time.Sleep(time.Millisecond * 10) // wait a little before retry
} // !sent
} // inp
for o := range outS {
close(outS[o])
}
}
func trySendAny(inp Any, outS ...chan Any) bool {
for o := range outS {
select { // try to send
case outS[o] <- inp:
return true
default:
// keep trying
}
} // outS
return false
}
// End of AnyStrew - scatter them
// ===========================================================================
// Beg of AnyPipeSeen/AnyForkSeen - an "I've seen this Any before" filter / forker
// AnyPipeSeen returns a channel to receive
// all `inp`
// not been seen before
// while silently dropping everything seen before
// (internally growing a `sync.Map` to discriminate)
// until close.
// Note: AnyPipeFilterNotSeenYet might be a better name, but is fairly long.
func AnyPipeSeen(inp <-chan Any) (out <-chan Any) {
cha := make(chan Any)
go pipeAnySeenAttr(cha, inp, nil)
return cha
}
// AnyPipeSeenAttr returns a channel to receive
// all `inp`
// whose attribute `attr` has
// not been seen before
// while silently dropping everything seen before
// (internally growing a `sync.Map` to discriminate)
// until close.
// Note: AnyPipeFilterAttrNotSeenYet might be a better name, but is fairly long.
func AnyPipeSeenAttr(inp <-chan Any, attr func(a Any) interface{}) (out <-chan Any) {
cha := make(chan Any)
go pipeAnySeenAttr(cha, inp, attr)
return cha
}
// AnyForkSeen returns two channels, `new` and `old`,
// where `new` is to receive
// all `inp`
// not been seen before
// and `old`
// all `inp`
// seen before
// (internally growing a `sync.Map` to discriminate)
// until close.
func AnyForkSeen(inp <-chan Any) (new, old <-chan Any) {
cha1 := make(chan Any)
cha2 := make(chan Any)
go forkAnySeenAttr(cha1, cha2, inp, nil)
return cha1, cha2
}
// AnyForkSeenAttr returns two channels, `new` and `old`,
// where `new` is to receive
// all `inp`
// whose attribute `attr` has
// not been seen before
// and `old`
// all `inp`
// seen before
// (internally growing a `sync.Map` to discriminate)
// until close.
func AnyForkSeenAttr(inp <-chan Any, attr func(a Any) interface{}) (new, old <-chan Any) {
cha1 := make(chan Any)
cha2 := make(chan Any)
go forkAnySeenAttr(cha1, cha2, inp, attr)
return cha1, cha2
}
func pipeAnySeenAttr(out chan<- Any, inp <-chan Any, attr func(a Any) interface{}) {
defer close(out)
if attr == nil { // Make `nil` value useful
attr = func(a Any) interface{} { return a }
}
seen := sync.Map{}
for i := range inp {
if _, visited := seen.LoadOrStore(attr(i), struct{}{}); visited {
// drop i silently
} else {
out <- i
}
}
}
func forkAnySeenAttr(new, old chan<- Any, inp <-chan Any, attr func(a Any) interface{}) {
defer close(new)
defer close(old)
if attr == nil { // Make `nil` value useful
attr = func(a Any) interface{} { return a }
}
seen := sync.Map{}
for i := range inp {
if _, visited := seen.LoadOrStore(attr(i), struct{}{}); visited {
old <- i
} else {
new <- i
}
}
}
// AnyTubeSeen returns a closure around AnyPipeSeen()
// (silently dropping every Any seen before).
func AnyTubeSeen() (tube func(inp <-chan Any) (out <-chan Any)) {
return func(inp <-chan Any) (out <-chan Any) {
return AnyPipeSeen(inp)
}
}
// AnyTubeSeenAttr returns a closure around AnyPipeSeenAttr()
// (silently dropping every Any
// whose attribute `attr` was
// seen before).
func AnyTubeSeenAttr(attr func(a Any) interface{}) (tube func(inp <-chan Any) (out <-chan Any)) {
return func(inp <-chan Any) (out <-chan Any) {
return AnyPipeSeenAttr(inp, attr)
}
}
// End of AnyPipeSeen/AnyForkSeen - an "I've seen this Any before" filter / forker
// ===========================================================================
// Beg of AnyFanIn
// AnyFanIn returns a channel to receive all inputs arriving
// on variadic inps
// before close.
//
// Ref: https://blog.golang.org/pipelines
// Ref: https://github.com/QuentinPerez/go-stuff/channel/Fan-out-Fan-in/main.go
func AnyFanIn(inps ...<-chan Any) (out <-chan Any) {
cha := make(chan Any)