forked from ahmetb/go-linq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplinq.go
391 lines (357 loc) · 9.19 KB
/
plinq.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
package linq
// ParallelQuery is the type returned from functions executing in parallel.
// To transform a Query into ParallelQuery, use AsParallel() and use
// AsSequential() to do vice versa.
type ParallelQuery struct {
values []T
ordered bool
err error
}
type parallelBinaryResult struct {
ok bool
err error
index int
}
type parallelValueResult struct {
val T
err error
index int
}
// Results evaluates the query and returns the results as T slice.
// An error occurred in during evaluation of the query will be returned.
//
// Example:
// results, err := From(slice).AsParallel().Select(something).Results()
func (q ParallelQuery) Results() ([]T, error) {
// we need to copy results for isolating user modification on returned
// slice from the current Query instance.
res := make([]T, len(q.values))
_ = copy(res, q.values)
return res, q.err
}
// AsSequential returns a Query from the same source and the query functions
// can be executed in serial for each element of the source sequence.
// This is for undoing AsParallel().
func (q ParallelQuery) AsSequential() Query {
return Query{values: q.values, err: q.err}
}
// copyMeta copies all fields of ParallelQuery except 'values' into a new
// instance. This should be used for retaining options e.g. 'ordered'.
func (q ParallelQuery) copyMeta() ParallelQuery {
return ParallelQuery{err: q.err,
ordered: q.ordered}
}
// copyMetaWithValues copies all fields of ParallelQuery. This should be used
// for retaining options e.g. 'ordered' as well as values.
func (q ParallelQuery) copyMetaWithValues() ParallelQuery {
return ParallelQuery{err: q.err,
ordered: q.ordered,
values: q.values}
}
// AsOrdered makes the parallel queries to preserve original order. By default,
// parallel queries do not preserve the order and process the parallel
// executions in first-come-first-served fashion.
//
// Not applicable for all query methods and comes
// with a performance penalty in some queries, please refer to
// http://msdn.microsoft.com/en-us/library/dd460677(v=vs.110).aspx .
func (q ParallelQuery) AsOrdered() (p ParallelQuery) {
p = q.copyMetaWithValues()
p.ordered = true
return
}
// AsUnordered undoes the effect of AsOrdered() and do not enforce parallel
// query to preserve the original order.
//
// See AsOrdered() for remarks.
func (q ParallelQuery) AsUnordered() (p ParallelQuery) {
p = q.copyMetaWithValues()
p.ordered = false
return
}
// Where filters a sequence of values by running given predicate function
// in parallel for each element.
//
// This function will take elements of the source (or results of previous query)
// as interface[] so it should make type assertion to work on the types.
// Returns a query with elements satisfy the condition.
//
// If any of the parallel executions return with an error, this function
// immediately returns with the error.
//
// If you would like to preserve order from the original sequence, use
// AsOrdered() on the query beforehand.
//
// Example:
// flying, err := From(animals).AsParallel().Where(func (a T) (bool, error){
// return a.(*Animal).IsFlying, nil
// }).Results()
func (q ParallelQuery) Where(f func(T) (bool, error)) (r ParallelQuery) {
r = q.copyMeta()
if r.err != nil {
return r
}
if f == nil {
r.err = ErrNilFunc
return
}
count := len(q.values)
ch := make(chan *parallelBinaryResult)
for i := 0; i < count; i++ {
go func(ind int, f func(T) (bool, error), in T) {
out := parallelBinaryResult{index: ind}
ok, err := f(in)
if err != nil {
out.err = err
} else {
out.ok = ok
}
ch <- &out
}(i, f, q.values[i])
}
tmp := make([]T, count)
take := make([]bool, count)
for j := 0; j < count; j++ {
out := <-ch
if out.err != nil {
r.err = out.err
return
}
if out.ok {
origI := out.index
val := q.values[origI]
if r.ordered {
tmp[origI] = val
take[origI] = true
} else {
r.values = append(r.values, val)
}
}
}
if r.ordered {
// iterate over the flag slice to take marked elements
for i, v := range tmp {
if take[i] {
r.values = append(r.values, v)
}
}
}
return
}
// Select projects each element of a sequence into a new form by running
// the given transform function in parallel for each element.
// Returns a query with the return values of invoking the transform function
// on each element of original source.
//
// Example:
// names, err := From(animals).AsParallel().Select(func (a T) (T, error){
// return a.(*Animal).Name, nil
// }).Results()
func (q ParallelQuery) Select(f func(T) (T, error)) (r ParallelQuery) {
r = q.copyMeta()
if r.err != nil {
return r
}
if f == nil {
r.err = ErrNilFunc
return
}
ch := make(chan *parallelValueResult)
r.values = make([]T, len(q.values))
for i, v := range q.values {
go func(ind int, f func(T) (T, error), in T) {
out := parallelValueResult{index: ind}
val, err := f(in)
if err != nil {
out.err = err
} else {
out.val = val
}
ch <- &out
}(i, f, v)
}
for i := 0; i < len(q.values); i++ {
out := <-ch
if out.err != nil {
r.err = out.err
return
}
r.values[out.index] = out.val
}
return
}
// Any determines whether the query source contains any elements.
// Example:
// anyOver18, err := From(students).AsParallel().Where(func (s T)(bool, error){
// return s.(*Person).Age > 18, nil
// }).Any()
func (q ParallelQuery) Any() (exists bool, err error) {
return len(q.values) > 0, q.err
}
// AnyWith determines whether the query source contains any elements satisfying
// the provided predicate function.
//
// Example:
// anyOver18, err := From(students).AsParallel().AnyWith(func (s T)(bool, error){
// return s.(*Person).Age > 18, nil
// })
func (q ParallelQuery) AnyWith(f func(T) (bool, error)) (exists bool, err error) {
if q.err != nil {
err = q.err
return
}
if f == nil {
err = ErrNilFunc
return
}
ch := make(chan parallelBinaryResult)
for _, v := range q.values {
go func(f func(T) (bool, error), value T) {
out := parallelBinaryResult{}
ok, e := f(value)
out.ok = ok
out.err = e
ch <- out
}(f, v)
}
for i := 0; i < len(q.values); i++ {
out := <-ch
if out.err != nil {
err = out.err
return
}
if out.ok {
exists = true
return
}
}
return
}
// All determines whether all elements of the query source satisfy the provided
// predicate function by executing the function for each element in parallel.
//
// Returns early if one element does not meet the conditions provided.
//
// Example:
// allOver18, err := From(students).AsParallel().All(func (s T)(bool, error){
// return s.(*Person).Age > 18, nil
// })
func (q ParallelQuery) All(f func(T) (bool, error)) (all bool, err error) {
if q.err != nil {
err = q.err
return
}
if f == nil {
err = ErrNilFunc
return
}
ch := make(chan parallelBinaryResult)
for _, v := range q.values {
go func(f func(T) (bool, error), value T) {
ok, e := f(value)
ch <- parallelBinaryResult{ok: ok, err: e}
}(f, v)
}
for i := 0; i < len(q.values); i++ {
out := <-ch
if out.err != nil {
err = out.err
return
}
if !out.ok {
return false, nil
}
}
return true, nil
}
// Single returns the only one element of the original sequence satisfies the
// provided predicate function if exists, otherwise returns ErrNotSingle.
// Predicate function is executed in parallel for each element of the sequence.
// Example:
// admin, err := From(students).AsParallel().Single(func (s T)(bool, error){
// return s.(*Person).Id == 1, nil
// })
// if err == nil {
// // use admin.(*Person)
// }
func (q ParallelQuery) Single(f func(T) (bool, error)) (single T, err error) {
if q.err != nil {
err = q.err
return
}
if f == nil {
err = ErrNilFunc
return
}
ch := make(chan parallelBinaryResult)
for i, v := range q.values {
go func(f func(T) (bool, error), value T, ind int) {
ok, e := f(value)
out := parallelBinaryResult{err: e, ok: ok, index: ind}
ch <- out
}(f, v, i)
}
for i := 0; i < len(q.values); i++ {
out := <-ch
if out.err != nil {
err = out.err
return
}
if out.ok {
if single != nil {
err = ErrNotSingle
return
}
single = q.values[out.index]
}
}
if single == nil {
err = ErrNotSingle
}
return
}
// Count returns number of elements in the sequence.
//
// Example:
// over18, err := From(students).AsParallel().Where(func (s T)(bool, error){
// return s.(*Person).Age > 18, nil
// }).Count()
func (q ParallelQuery) Count() (count int, err error) {
return len(q.values), q.err
}
// CountBy returns number of elements satisfying the provided predicate
// function by running the function for each element of the sequence
// in parallel.
//
// Example:
// over18, err := From(students).AsParallel().CountBy(func (s T)(bool, error){
// return s.(*Person).Age > 18, nil
// })
func (q ParallelQuery) CountBy(f func(T) (bool, error)) (c int, err error) {
if q.err != nil {
err = q.err
return
}
if f == nil {
err = ErrNilFunc
return
}
ch := make(chan parallelBinaryResult)
for _, v := range q.values {
go func(f func(T) (bool, error), value T) {
ok, e := f(value)
ch <- parallelBinaryResult{ok: ok, err: e}
}(f, v)
}
for i := 0; i < len(q.values); i++ {
out := <-ch
if out.err != nil {
err = out.err
return
}
if out.ok {
c++
}
}
return
}