-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit.go
More file actions
231 lines (214 loc) · 5.06 KB
/
Copy pathsplit.go
File metadata and controls
231 lines (214 loc) · 5.06 KB
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
package pipe
import (
"sync"
)
// Split takes a number of output channels and input channel, and forwards the input
// messages to all output channels.
// There is no guarantee that the message will be sent to the output channels in the
// sequence in which they are provided.
// If input channel is closed then all output channels are closed.
// Creates new channels with the same capacity as input.
//
// # Strategies
//
// - Processing: Parallel
// - Closing: Single
// - Capacity: Same
//
// # Usages
//
// // input := make(chan int, 4) with random values [1, 2, 3]
//
// outs := Split(2, input)
//
// // The gaps demonstrate uneven recording in the channels
// // outs[0]: [2, 1, 3 ]
// // outs[1]: [ 1, 3, 2]
func Split[T any](n int, in <-chan T) []<-chan T {
outs := make([]chan T, n)
for i := 0; i < n; i++ {
outs[i] = make(chan T, cap(in))
}
wg := sync.WaitGroup{}
go func() {
for {
if in, ok := <-in; ok {
for i := 0; i < n; i++ {
i := i
wg.Add(1)
go func() {
outs[i] <- in
wg.Done()
}()
}
} else {
wg.Wait()
for i := 0; i < n; i++ {
close(outs[i])
}
break
}
}
}()
outsR := make([]<-chan T, n)
for i := 0; i < n; i++ {
outsR[i] = outs[i]
}
return outsR
}
// Split2 - alias for [Split]
func Split2[T any](in <-chan T) (out1, out2 <-chan T) {
outs := Split(2, in)
return outs[0], outs[1]
}
// Split3 - alias for [Split]
func Split3[T any](in <-chan T) (out1, out2, out3 <-chan T) {
outs := Split(3, in)
return outs[0], outs[1], outs[2]
}
// SplitSync takes a number of output channels and input channel, and forwards the input
// messages to all output channels.
// There is no guarantee that the message will be sent to the output channels in the
// sequence in which they are provided.
// If input channel is closed then all output channels are closed.
// Creates new channels with the same capacity as input.
//
// # Strategies
//
// - Processing: Sync
// - Closing: Single
// - Capacity: Same
//
// # Usages
//
// // input := make(chan int, 4) with random values [1, 2, 3]
//
// outs := SplitSync(2, input)
//
// // The gaps demonstrate uneven recording in the channels
// // outs[0]: [1, 2, 3 ]
// // outs[1]: [ 1, 2, 3]
func SplitSync[T any](n int, in <-chan T) []<-chan T {
outs := make([]chan T, n)
for i := 0; i < n; i++ {
outs[i] = make(chan T, cap(in))
}
queues := make([]chan func() <-chan T, n)
for i := 0; i < n; i++ {
queues[i] = make(chan func() <-chan T, cap(in))
}
wg := sync.WaitGroup{}
go func() {
for {
if in, ok := <-in; ok {
for i := 0; i < n; i++ {
i := i
wg.Add(1)
queues[i] <- func() <-chan T {
out := make(chan T)
go func() {
out <- in
close(out)
wg.Done()
}()
return out
}
}
} else {
wg.Wait()
for i := 0; i < n; i++ {
close(queues[i])
}
break
}
}
}()
for i := 0; i < n; i++ {
i := i
go func() {
for {
if handler, ok := <-queues[i]; ok {
if data, ok := <-handler(); ok {
outs[i] <- data
}
} else {
close(outs[i])
break
}
}
}()
}
outsR := make([]<-chan T, n)
for i := 0; i < n; i++ {
outsR[i] = outs[i]
}
return outsR
}
// SplitSync2 - alias for [SplitSync]
func SplitSync2[T any](in <-chan T) (out1, out2 <-chan T) {
outs := SplitSync(2, in)
return outs[0], outs[1]
}
// SplitSync3 - alias for [SplitSync]
func SplitSync3[T any](in <-chan T) (out1, out2, out3 <-chan T) {
outs := SplitSync(3, in)
return outs[0], outs[1], outs[2]
}
// SplitSequential takes a number of output channels and input channel, and forwards the input
// messages to all output channels.
// The message will be sent to the output channels in the following sequence.
// If input channel is closed then all output channels are closed.
// Creates new channels with the same capacity as input.
//
// Be aware, if one of the output channels is blocked, then all other output channels will wait.
//
// # Strategies
//
// - Processing: Sequential
// - Closing: Single
// - Capacity: Same
//
// # Usages
//
// // input := make(chan int, 4) with random values [1, 2, 3]
//
// outs := SplitSequential(2, input)
//
// // The gaps demonstrate uneven recording in the channels
// // outs[0]: [1, 2, 3 ]
// // outs[1]: [ 1, 2, 3]
func SplitSequential[T any](n int, in <-chan T) []<-chan T {
outs := make([]chan T, n)
for i := 0; i < n; i++ {
outs[i] = make(chan T, cap(in))
}
go func() {
for {
if in, ok := <-in; ok {
for i := 0; i < n; i++ {
outs[i] <- in
}
} else {
for i := 0; i < n; i++ {
close(outs[i])
}
break
}
}
}()
outsR := make([]<-chan T, n)
for i := 0; i < n; i++ {
outsR[i] = outs[i]
}
return outsR
}
// SplitSequential2 - alias for [SplitSequential]
func SplitSequential2[T any](in <-chan T) (out1, out2 <-chan T) {
outs := SplitSequential(2, in)
return outs[0], outs[1]
}
// SplitSequential3 - alias for [SplitSequential]
func SplitSequential3[T any](in <-chan T) (out1, out2, out3 <-chan T) {
outs := SplitSequential(3, in)
return outs[0], outs[1], outs[2]
}