/
ParallelFactory.ForEachResult.cs
142 lines (135 loc) · 12.2 KB
/
ParallelFactory.ForEachResult.cs
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
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Cuemon.Threading
{
public static partial class ParallelFactory
{
/// <summary>
/// Executes a parallel foreach loop where the return value of the function delegate <paramref name="worker" /> is stored in the same sequential order as <paramref name="source" />.
/// </summary>
/// <typeparam name="TSource">The type of the data in the source.</typeparam>
/// <typeparam name="TResult">The type of the return value of the function delegate <paramref name="worker"/>.</typeparam>
/// <param name="source">The sequence to iterate over parallel.</param>
/// <param name="worker">The delegate that is invoked once per iteration.</param>
/// <param name="setup">The <see cref="AsyncTaskFactoryOptions"/> which may be configured.</param>
/// <returns>A <see cref="Task" /> that represents the asynchronous operation.
/// The task result contains an <see cref="IReadOnlyCollection{TResult}" /> where the return value of the function delegate <paramref name="worker" /> is stored in the same sequential order as <paramref name="source" />.</returns>
public static IReadOnlyCollection<TResult> ForEachResult<TSource, TResult>(IEnumerable<TSource> source, Func<TSource, TResult> worker, Action<AsyncTaskFactoryOptions> setup = null)
{
Validator.ThrowIfNull(source);
Validator.ThrowIfNull(worker);
return ForEachResultCore(source, FuncFactory.Create(worker, default), setup);
}
/// <summary>
/// Executes a parallel foreach loop where the return value of the function delegate <paramref name="worker" /> is stored in the same sequential order as <paramref name="source" />.
/// </summary>
/// <typeparam name="TSource">The type of the data in the source.</typeparam>
/// <typeparam name="T">The type of the parameter of the function delegate <paramref name="worker" />.</typeparam>
/// <typeparam name="TResult">The type of the return value of the function delegate <paramref name="worker"/>.</typeparam>
/// <param name="source">The sequence to iterate over parallel.</param>
/// <param name="worker">The delegate that is invoked once per iteration.</param>
/// <param name="arg">The parameter of the function delegate <paramref name="worker" />.</param>
/// <param name="setup">The <see cref="AsyncTaskFactoryOptions"/> which may be configured.</param>
/// <returns>An <see cref="IReadOnlyCollection{TResult}" /> where the return value of the function delegate <paramref name="worker" /> is stored in the same sequential order as <paramref name="source" />.</returns>
public static IReadOnlyCollection<TResult> ForEachResult<TSource, T, TResult>(IEnumerable<TSource> source, Func<TSource, T, TResult> worker, T arg, Action<AsyncTaskFactoryOptions> setup = null)
{
Validator.ThrowIfNull(source);
Validator.ThrowIfNull(worker);
return ForEachResultCore(source, FuncFactory.Create(worker, default, arg), setup);
}
/// <summary>
/// Executes a parallel foreach loop where the return value of the function delegate <paramref name="worker" /> is stored in the same sequential order as <paramref name="source" />.
/// </summary>
/// <typeparam name="TSource">The type of the data in the source.</typeparam>
/// <typeparam name="T1">The type of the first parameter of the function delegate <paramref name="worker" />.</typeparam>
/// <typeparam name="T2">The type of the second parameter of the function delegate <paramref name="worker" />.</typeparam>
/// <typeparam name="TResult">The type of the return value of the function delegate <paramref name="worker"/>.</typeparam>
/// <param name="source">The sequence to iterate over parallel.</param>
/// <param name="worker">The delegate that is invoked once per iteration.</param>
/// <param name="arg1">The first parameter of the function delegate <paramref name="worker" />.</param>
/// <param name="arg2">The second parameter of the function delegate <paramref name="worker" />.</param>
/// <param name="setup">The <see cref="AsyncTaskFactoryOptions"/> which may be configured.</param>
/// <returns>An <see cref="IReadOnlyCollection{TResult}" /> where the return value of the function delegate <paramref name="worker" /> is stored in the same sequential order as <paramref name="source" />.</returns>
public static IReadOnlyCollection<TResult> ForEachResult<TSource, T1, T2, TResult>(IEnumerable<TSource> source, Func<TSource, T1, T2, TResult> worker, T1 arg1, T2 arg2, Action<AsyncTaskFactoryOptions> setup = null)
{
Validator.ThrowIfNull(source);
Validator.ThrowIfNull(worker);
return ForEachResultCore(source, FuncFactory.Create(worker, default, arg1, arg2), setup);
}
/// <summary>
/// Executes a parallel foreach loop where the return value of the function delegate <paramref name="worker" /> is stored in the same sequential order as <paramref name="source" />.
/// </summary>
/// <typeparam name="TSource">The type of the data in the source.</typeparam>
/// <typeparam name="T1">The type of the first parameter of the function delegate <paramref name="worker" />.</typeparam>
/// <typeparam name="T2">The type of the second parameter of the function delegate <paramref name="worker" />.</typeparam>
/// <typeparam name="T3">The type of the third parameter of the function delegate <paramref name="worker" />.</typeparam>
/// <typeparam name="TResult">The type of the return value of the function delegate <paramref name="worker"/>.</typeparam>
/// <param name="source">The sequence to iterate over parallel.</param>
/// <param name="worker">The delegate that is invoked once per iteration.</param>
/// <param name="arg1">The first parameter of the function delegate <paramref name="worker" />.</param>
/// <param name="arg2">The second parameter of the function delegate <paramref name="worker" />.</param>
/// <param name="arg3">The third parameter of the function delegate <paramref name="worker" />.</param>
/// <param name="setup">The <see cref="AsyncTaskFactoryOptions"/> which may be configured.</param>
/// <returns>An <see cref="IReadOnlyCollection{TResult}" /> where the return value of the function delegate <paramref name="worker" /> is stored in the same sequential order as <paramref name="source" />.</returns>
public static IReadOnlyCollection<TResult> ForEachResult<TSource, T1, T2, T3, TResult>(IEnumerable<TSource> source, Func<TSource, T1, T2, T3, TResult> worker, T1 arg1, T2 arg2, T3 arg3, Action<AsyncTaskFactoryOptions> setup = null)
{
Validator.ThrowIfNull(source);
Validator.ThrowIfNull(worker);
return ForEachResultCore(source, FuncFactory.Create(worker, default, arg1, arg2, arg3), setup);
}
/// <summary>
/// Executes a parallel foreach loop where the return value of the function delegate <paramref name="worker" /> is stored in the same sequential order as <paramref name="source" />.
/// </summary>
/// <typeparam name="TSource">The type of the data in the source.</typeparam>
/// <typeparam name="T1">The type of the first parameter of the function delegate <paramref name="worker" />.</typeparam>
/// <typeparam name="T2">The type of the second parameter of the function delegate <paramref name="worker" />.</typeparam>
/// <typeparam name="T3">The type of the third parameter of the function delegate <paramref name="worker" />.</typeparam>
/// <typeparam name="T4">The type of the fourth parameter of the function delegate <paramref name="worker" />.</typeparam>
/// <typeparam name="TResult">The type of the return value of the function delegate <paramref name="worker"/>.</typeparam>
/// <param name="source">The sequence to iterate over parallel.</param>
/// <param name="worker">The delegate that is invoked once per iteration.</param>
/// <param name="arg1">The first parameter of the function delegate <paramref name="worker" />.</param>
/// <param name="arg2">The second parameter of the function delegate <paramref name="worker" />.</param>
/// <param name="arg3">The third parameter of the function delegate <paramref name="worker" />.</param>
/// <param name="arg4">The fourth parameter of the function delegate <paramref name="worker" />.</param>
/// <param name="setup">The <see cref="AsyncTaskFactoryOptions"/> which may be configured.</param>
/// <returns>An <see cref="IReadOnlyCollection{TResult}" /> where the return value of the function delegate <paramref name="worker" /> is stored in the same sequential order as <paramref name="source" />.</returns>
public static IReadOnlyCollection<TResult> ForEachResult<TSource, T1, T2, T3, T4, TResult>(IEnumerable<TSource> source, Func<TSource, T1, T2, T3, T4, TResult> worker, T1 arg1, T2 arg2, T3 arg3, T4 arg4, Action<AsyncTaskFactoryOptions> setup = null)
{
Validator.ThrowIfNull(source);
Validator.ThrowIfNull(worker);
return ForEachResultCore(source, FuncFactory.Create(worker, default, arg1, arg2, arg3, arg4), setup);
}
/// <summary>
/// Executes a parallel foreach loop
/// </summary>
/// <typeparam name="TSource">The type of the data in the source.</typeparam>
/// <typeparam name="T1">The type of the first parameter of the function delegate <paramref name="worker" />.</typeparam>
/// <typeparam name="T2">The type of the second parameter of the function delegate <paramref name="worker" />.</typeparam>
/// <typeparam name="T3">The type of the third parameter of the function delegate <paramref name="worker" />.</typeparam>
/// <typeparam name="T4">The type of the fourth parameter of the function delegate <paramref name="worker" />.</typeparam>
/// <typeparam name="T5">The type of the fifth parameter of the function delegate <paramref name="worker" />.</typeparam>
/// <typeparam name="TResult">The type of the return value of the function delegate <paramref name="worker"/>.</typeparam>
/// <param name="source">The sequence to iterate over parallel.</param>
/// <param name="worker">The delegate that is invoked once per iteration.</param>
/// <param name="arg1">The first parameter of the function delegate <paramref name="worker" />.</param>
/// <param name="arg2">The second parameter of the function delegate <paramref name="worker" />.</param>
/// <param name="arg3">The third parameter of the function delegate <paramref name="worker" />.</param>
/// <param name="arg4">The fourth parameter of the function delegate <paramref name="worker" />.</param>
/// <param name="arg5">The fifth parameter of the function delegate <paramref name="worker" />.</param>
/// <param name="setup">The <see cref="AsyncTaskFactoryOptions" /> which may be configured.</param>
/// <returns>An <see cref="IReadOnlyCollection{TResult}" /> where the return value of the function delegate <paramref name="worker" /> is stored in the same sequential order as <paramref name="source" />.</returns>
public static IReadOnlyCollection<TResult> ForEachResult<TSource, T1, T2, T3, T4, T5, TResult>(IEnumerable<TSource> source, Func<TSource, T1, T2, T3, T4, T5, TResult> worker, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, Action<AsyncTaskFactoryOptions> setup = null)
{
Validator.ThrowIfNull(source);
Validator.ThrowIfNull(worker);
return ForEachResultCore(source, FuncFactory.Create(worker, default, arg1, arg2, arg3, arg4, arg5), setup);
}
private static IReadOnlyCollection<TResult> ForEachResultCore<TSource, TWorker, TResult>(IEnumerable<TSource> source, FuncFactory<TWorker, TResult> workerFactory, Action<AsyncTaskFactoryOptions> setup)
where TWorker : Template<TSource>
{
return new FuncForEachSynchronousLoop<TSource, TResult>(source, setup).GetResult(workerFactory);
}
}
}