-
Notifications
You must be signed in to change notification settings - Fork 3
/
EnumerableExtensions.cs
225 lines (210 loc) · 12.7 KB
/
EnumerableExtensions.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
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
using System;
using System.Collections.Generic;
using System.Linq;
using Cuemon.Collections.Generic;
namespace Cuemon.Extensions.Collections.Generic
{
/// <summary>
/// Extension methods for the <see cref="IEnumerable{T}"/> interface.
/// </summary>
public static class EnumerableExtensions
{
/// <summary>
/// Returns a chunked <see cref="IEnumerable{T}"/> sequence with a maximum of the specified <paramref name="size"/>. Default is 128.
/// </summary>
/// <typeparam name="T">The type of the elements of <paramref name="source" />.</typeparam>
/// <param name="source">An <see cref="IEnumerable{T}" /> to extend.</param>
/// <param name="size">The amount of elements to process at a time.</param>
/// <returns>An <see cref="IEnumerable{T}" /> that contains no more than the specified <paramref name="size" /> of elements from the <paramref name="source" /> sequence.</returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="source"/> is null.
/// </exception>
/// <exception cref="ArgumentException">
/// <paramref name="size"/> is less or equal to 0.
/// </exception>
/// <remarks>The original <paramref name="source"/> is reduced equivalent to the number of elements in the returned sequence.</remarks>
public static PartitionerEnumerable<T> Chunk<T>(this IEnumerable<T> source, int size = 128)
{
Validator.ThrowIfNull(source);
Validator.ThrowIfLowerThanOrEqual(size, 0, nameof(size));
return new PartitionerEnumerable<T>(source, size);
}
/// <summary>
/// Shuffles the specified <paramref name="source"/> like a deck of cards.
/// </summary>
/// <param name="source">An <see cref="IEnumerable{T}" /> to extend.</param>
/// <returns>A sequence of <typeparamref name="T"/> with the shuffled <paramref name="source"/>.</returns>
/// <remarks>Fisher–Yates shuffle: https://en.wikipedia.org/wiki/Fisher–Yates_shuffle</remarks>
public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source)
{
return source.Shuffle(Generate.RandomNumber);
}
/// <summary>
/// Shuffles the specified <paramref name="source"/> like a deck of cards.
/// </summary>
/// <param name="source">An <see cref="IEnumerable{T}" /> to extend.</param>
/// <param name="randomizer">The function delegate that will handle the randomization of <paramref name="source"/>.</param>
/// <returns>A sequence of <typeparamref name="T"/> with the shuffled <paramref name="source"/>.</returns>
/// <remarks>Fisher–Yates shuffle: https://en.wikipedia.org/wiki/Fisher–Yates_shuffle</remarks>
public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source, Func<int, int, int> randomizer)
{
Validator.ThrowIfNull(source);
Validator.ThrowIfNull(randomizer);
var buffer = source.ToArray();
var length = buffer.Length;
while (length > 0)
{
length--;
var random = randomizer(0, length + 1);
var shuffled = buffer[random];
yield return shuffled;
buffer[random] = buffer[length];
}
}
/// <summary>
/// Returns ascending sorted elements from a sequence by using the default comparer to compare values.
/// </summary>
/// <typeparam name="T">The type of the elements of <paramref name="source"/>.</typeparam>
/// <param name="source">An <see cref="IEnumerable{T}" /> to extend.</param>
/// <returns>An <see cref="IEnumerable{T}"/> that contains ascending sorted elements from the source sequence.</returns>
public static IEnumerable<T> OrderAscending<T>(this IEnumerable<T> source)
{
return source.OrderAscending(Comparer<T>.Default);
}
/// <summary>
/// Returns ascending sorted elements from a sequence by using a specified <see cref="IComparer{T}"/> to compare values.
/// </summary>
/// <typeparam name="T">The type of the elements of <paramref name="source"/>.</typeparam>
/// <param name="source">An <see cref="IEnumerable{T}" /> to extend.</param>
/// <param name="comparer">An <see cref="IComparer{T}"/> to compare values.</param>
/// <returns>An <see cref="IEnumerable{T}"/> that contains ascending sorted elements from the source sequence.</returns>
public static IEnumerable<T> OrderAscending<T>(this IEnumerable<T> source, IComparer<T> comparer)
{
Validator.ThrowIfNull(source);
Validator.ThrowIfNull(comparer);
return source.OrderBy(t => t, comparer);
}
/// <summary>
/// Returns descending sorted elements from a sequence by using the default comparer to compare values.
/// </summary>
/// <typeparam name="T">The type of the elements of <paramref name="source"/>.</typeparam>
/// <param name="source">An <see cref="IEnumerable{T}" /> to extend.</param>
/// <returns>An <see cref="IEnumerable{T}"/> that contains descending sorted elements from the source sequence.</returns>
public static IEnumerable<T> OrderDescending<T>(this IEnumerable<T> source)
{
return source.OrderDescending(Comparer<T>.Default);
}
/// <summary>
/// Returns descending sorted elements from a sequence by using a specified <see cref="IComparer{T}"/> to compare values.
/// </summary>
/// <typeparam name="T">The type of the elements of <paramref name="source"/>.</typeparam>
/// <param name="source">An <see cref="IEnumerable{T}" /> to extend.</param>
/// <param name="comparer">An <see cref="IComparer{T}"/> to compare values.</param>
/// <returns>An <see cref="IEnumerable{T}"/> that contains descending sorted elements from the source sequence.</returns>
public static IEnumerable<T> OrderDescending<T>(this IEnumerable<T> source, IComparer<T> comparer)
{
Validator.ThrowIfNull(source);
Validator.ThrowIfNull(comparer);
return source.OrderByDescending(t => t, comparer);
}
/// <summary>
/// Returns a random element of a sequence of elements, or a default value if no element is found.
/// </summary>
/// <typeparam name="T">The type of the elements of <paramref name="source"/>.</typeparam>
/// <param name="source">An <see cref="IEnumerable{T}" /> to extend.</param>
/// <returns><c>default</c> if <paramref name="source"/> is empty; otherwise, a random element of <paramref name="source"/>.</returns>
public static T RandomOrDefault<T>(this IEnumerable<T> source)
{
Validator.ThrowIfNull(source);
var collection = source as ICollection<T> ?? new List<T>(source);
return collection.Count == 0 ? default : collection.ElementAt(Generate.RandomNumber(collection.Count));
}
/// <summary>
/// Returns an <see cref="IEnumerable{T}"/> sequence with the specified <paramref name="value"/> as the only element.
/// </summary>
/// <typeparam name="T">The type of the element of <see cref="IEnumerable{T}"/>.</typeparam>
/// <param name="value">The <typeparamref name="T"/> to extend.</param>
/// <returns>An <see cref="IEnumerable{T}"/> sequence with the specified <paramref name="value"/> as the only element.</returns>
public static IEnumerable<T> Yield<T>(this T value)
{
return Arguments.Yield(value);
}
/// <summary>
/// Creates a <see cref="Dictionary{TKey,TValue}"/> from the specified <paramref name="source"/> sequence.
/// </summary>
/// <typeparam name="TKey">The type of keys in the <paramref name="source"/>.</typeparam>
/// <typeparam name="TValue">The type of values in the <paramref name="source"/>.</typeparam>
/// <param name="source">An <see cref="IEnumerable{T}" /> to extend.</param>
/// <returns>A <see cref="Dictionary{TKey,TValue}"/> that is equivalent to the specified <paramref name="source"/> sequence.</returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="source"/> is null.
/// </exception>
/// <exception cref="ArgumentException">
/// <paramref name="source"/> contains at least one <see cref="KeyValuePair{TKey,TValue}"/> that produces duplicate keys for two elements.
/// </exception>
public static IDictionary<TKey, TValue> ToDictionary<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> source)
{
return ToDictionary(source, EqualityComparer<TKey>.Default);
}
/// <summary>
/// Creates a <see cref="Dictionary{TKey,TValue}"/> from the specified <paramref name="source"/> sequence.
/// </summary>
/// <typeparam name="TKey">The type of keys in the <paramref name="source"/>.</typeparam>
/// <typeparam name="TValue">The type of values in the <paramref name="source"/>.</typeparam>
/// <param name="source">An <see cref="IEnumerable{T}" /> to extend.</param>
/// <param name="comparer">The <see cref="IEqualityComparer{T}"/> implementation to use when comparing keys.</param>
/// <returns>A <see cref="Dictionary{TKey,TValue}"/> that is equivalent to the specified <paramref name="source"/> sequence.</returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="source"/> is null - or - <paramref name="comparer"/> is null.
/// </exception>
/// <exception cref="ArgumentException">
/// <paramref name="source"/> contains at least one <see cref="KeyValuePair{TKey,TValue}"/> that produces duplicate keys for two elements.
/// </exception>
public static IDictionary<TKey, TValue> ToDictionary<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> source, IEqualityComparer<TKey> comparer)
{
Validator.ThrowIfNull(source);
Validator.ThrowIfNull(comparer);
var result = new Dictionary<TKey, TValue>(comparer);
foreach (var item in source)
{
result.Add(item.Key, item.Value);
}
return result;
}
/// <summary>
/// Extends the specified <paramref name="source"/> to support iterating in partitions.
/// </summary>
/// <typeparam name="T">The type of elements in the <paramref name="source"/>.</typeparam>
/// <param name="source">An <see cref="IEnumerable{T}" /> to extend.</param>
/// <param name="partitionSize">The size of the partitions.</param>
/// <returns>An instance of <see cref="PartitionerEnumerable{T}"/>.</returns>
public static PartitionerEnumerable<T> ToPartitioner<T>(this IEnumerable<T> source, int partitionSize = 128)
{
return new PartitionerEnumerable<T>(source, partitionSize);
}
/// <summary>
/// Converts the specified <paramref name="source"/> to a generic and read-only pagination sequence.
/// </summary>
/// <typeparam name="T">The type of elements in the collection.</typeparam>
/// <param name="source">An <see cref="IEnumerable{T}" /> to extend.</param>
/// <param name="totalElementCounter">The total element counter.</param>
/// <param name="setup">The <see cref="PaginationOptions"/> which may be configured.</param>
/// <returns>An instance of <see cref="PaginationEnumerable{T}"/>.</returns>
public static PaginationEnumerable<T> ToPagination<T>(this IEnumerable<T> source, Func<int> totalElementCounter, Action<PaginationOptions> setup = null)
{
return new PaginationEnumerable<T>(source, totalElementCounter, setup);
}
/// <summary>
/// Converts the specified <paramref name="source"/> to an eagerly materialized generic and read-only pagination list.
/// </summary>
/// <typeparam name="T">The type of elements in the collection.</typeparam>
/// <param name="source">An <see cref="IEnumerable{T}" /> to extend.</param>
/// <param name="totalElementCounter">The total element counter.</param>
/// <param name="setup">The <see cref="PaginationOptions"/> which may be configured.</param>
/// <returns>An instance of <see cref="PaginationList{T}"/>.</returns>
public static PaginationList<T> ToPaginationList<T>(this IEnumerable<T> source, Func<int> totalElementCounter, Action<PaginationOptions> setup = null)
{
return new PaginationList<T>(source, totalElementCounter, setup);
}
}
}