blob: fd0ce922a1260a7bdfe08fbbdb22d51e53b1437c (
plain)
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
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace AggregateConfigBuildTask
{
/// <summary>
/// Contains extension methods for <see cref="IEnumerable{T}"/>, providing additional
/// functionality available in future .NET versions.
/// </summary>
public static class ListExtensions
{
/// <summary>
/// Splits a list into chunks of the specified size.
/// </summary>
/// <param name="source">The list to split into chunks.</param>
/// <param name="chunkSize">The size of each chunk.</param>
/// <returns>An IEnumerable of string arrays, each containing a chunk of the original list.</returns>
/// <exception cref="ArgumentOutOfRangeException">When chunk size is 0 or less</exception>
/// <exception cref="ArgumentNullException"><paramref name="source"/> is <c>null</c>.</exception>
public static IEnumerable<IEnumerable<string>> Chunk(this ICollection<string> source, int chunkSize)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
if (chunkSize <= 0)
{
throw new ArgumentOutOfRangeException(nameof(chunkSize), "Chunk size must be greater than 0.");
}
return FetchChunks();
IEnumerable<IEnumerable<string>> FetchChunks()
{
var list = source as List<string> ?? source.ToList();
for (int i = 0; i < list.Count; i += chunkSize)
{
yield return list.GetRange(i, Math.Min(chunkSize, list.Count - i));
}
}
}
/// <summary>
/// Asynchronously processes each element of a collection in parallel, with a limit on the number of concurrent tasks.
/// </summary>
/// <typeparam name="TSource">The type of elements in the source collection.</typeparam>
/// <param name="source">The collection of elements to process.</param>
/// <param name="degreeOfParallelism">The maximum number of tasks to run concurrently.</param>
/// <param name="body">The asynchronous delegate to execute for each element in the source collection.</param>
/// <returns>A task that represents the asynchronous processing of the collection.</returns>
/// <exception cref="ArgumentNullException">Thrown if the source or body is null.</exception>
/// <exception cref="ArgumentOutOfRangeException">Thrown if the degree of parallelism is less than 1.</exception>
public static async Task ForEachAsync<TSource>(
this IEnumerable<TSource> source,
int degreeOfParallelism,
Func<TSource, Task> body)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (body == null) throw new ArgumentNullException(nameof(body));
if (degreeOfParallelism < 1) throw new ArgumentOutOfRangeException(nameof(degreeOfParallelism), "Degree of parallelism must be at least 1.");
using (var semaphore = new SemaphoreSlim(degreeOfParallelism))
{
var tasks = source.Select(async item =>
{
await semaphore.WaitAsync().ConfigureAwait(false);
try
{
await body(item).ConfigureAwait(false);
}
finally
{
semaphore.Release();
}
});
await Task.WhenAll(tasks).ConfigureAwait(false);
}
}
}
}
|