-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathEnumerable2.cs
278 lines (245 loc) · 10.8 KB
/
Enumerable2.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
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
namespace Standard
{
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
/// <summary>
/// Further LINQ extensions
/// </summary>
internal static class Enumerable2
{
// Unnecessary in .Net 4.
//public static IEnumerable<TResult> Zip<TFirst, TSecond, TResult>(this IEnumerable<TFirst> first, IEnumerable<TSecond> second, Func<TFirst, TSecond, TResult> func)
//{
// Verify.IsNotNull(first, "first");
// Verify.IsNotNull(second, "second");
// return _Zip(first, second, func);
//}
//private static IEnumerable<TResult> _Zip<TFirst, TSecond, TResult>(this IEnumerable<TFirst> first, IEnumerable<TSecond> second, Func<TFirst, TSecond, TResult> func)
//{
// IEnumerator<TFirst> ie1 = first.GetEnumerator();
// IEnumerator<TSecond> ie2 = second.GetEnumerator();
// while (ie1.MoveNext() && ie2.MoveNext())
// {
// yield return func(ie1.Current, ie2.Current);
// }
//}
/// <summary>Partition a collection into two, based on whether the items match a predicate.</summary>
/// <typeparam name="T">The type of the enumeration.</typeparam>
/// <param name="collection">The original collection to split.</param>
/// <param name="condition">The condition to use for the split.</param>
/// <param name="rest">A collection of all items in the original collection that do not satisfy the condition.</param>
/// <returns>A collection of all items in the original collection that satisfy the condition.</returns>
/// <remarks>Unlike most extension methods of this nature, this does not perform the operation lazily.</remarks>
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
public static IEnumerable<T> SplitWhere<T>(this IEnumerable<T> collection, Predicate<T> condition, out IEnumerable<T> rest)
{
Verify.IsNotNull(collection, "collection");
Verify.IsNotNull(condition, "condition");
var passList = new List<T>();
var failList = new List<T>();
foreach (T t in collection)
{
if (condition(t))
{
passList.Add(t);
}
else
{
failList.Add(t);
}
}
rest = failList;
return passList;
}
/// <summary>
/// Limit an enumeration to be constrained to a subset after a given index.
/// </summary>
/// <typeparam name="T">The type of items being enumerated.</typeparam>
/// <param name="enumerable">The collection to be enumerated.</param>
/// <param name="startIndex">The index (inclusive) of the first item to be returned.</param>
/// <returns></returns>
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
public static IEnumerable<T> Sublist<T>(this IEnumerable<T> enumerable, int startIndex)
{
return Sublist(enumerable, startIndex, null);
}
/// <summary>
/// Limit an enumeration to be within a set of indices.
/// </summary>
/// <typeparam name="T">The type of items being enumerated.</typeparam>
/// <param name="enumerable">The collection to be enumerated.</param>
/// <param name="startIndex">The index (inclusive) of the first item to be returned.</param>
/// <param name="endIndex">
/// The index (exclusive) of the last item to be returned.
/// If this is null then the full collection after startIndex is returned.
/// If this is greater than the count of the collection after startIndex, then the full collection after startIndex is returned.
/// </param>
/// <returns></returns>
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
public static IEnumerable<T> Sublist<T>(this IEnumerable<T> enumerable, int startIndex, int? endIndex)
{
Verify.IsNotNull(enumerable, "enumerable");
Verify.BoundedInteger(0, startIndex, int.MaxValue, "startIndex");
if (endIndex != null)
{
Verify.BoundedInteger(startIndex, endIndex.Value, int.MaxValue, "endIndex");
}
// If this supports indexing then just use that.
var list = enumerable as IList<T>;
if (list != null)
{
return _SublistList(list, startIndex, endIndex);
}
return _SublistEnum(enumerable, startIndex, endIndex);
}
private static IEnumerable<T> _SublistEnum<T>(this IEnumerable<T> enumerable, int startIndex, int? endIndex)
{
int currentIndex = 0;
IEnumerator<T> enumerator = enumerable.GetEnumerator();
while (currentIndex < startIndex && enumerator.MoveNext())
{
++currentIndex;
}
int trueEndIndex = endIndex ?? int.MaxValue;
while (currentIndex < trueEndIndex && enumerator.MoveNext())
{
yield return enumerator.Current;
++currentIndex;
}
}
private static IEnumerable<T> _SublistList<T>(this IList<T> list, int startIndex, int? endIndex)
{
int trueEndIndex = Math.Min(list.Count, endIndex ?? int.MaxValue);
for (int i = startIndex; i < trueEndIndex; ++i)
{
yield return list[i];
}
}
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
public static bool AreSorted<T>(this IEnumerable<T> enumerable)
{
return _AreSorted(enumerable, null);
}
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
public static bool AreSorted<T>(this IEnumerable<T> enumerable, Comparison<T> comparison)
{
Verify.IsNotNull(enumerable, "enumerable");
if (comparison == null)
{
if (typeof(T).GetInterface(typeof(IComparable<T>).Name) == null)
{
// Not comparable for a sort.
return true;
}
comparison = delegate(T left, T right)
{
if (left == null)
{
if (right == null)
{
return 0;
}
return -((IComparable<T>)right).CompareTo(left);
}
return ((IComparable<T>)left).CompareTo(right);
};
}
return _AreSorted(enumerable, comparison);
}
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
private static bool _AreSorted<T>(IEnumerable<T> enumerable, Comparison<T> comparison)
{
var enumerator = enumerable.GetEnumerator();
if (!enumerator.MoveNext())
{
return true;
}
T last = enumerator.Current;
while (enumerator.MoveNext())
{
if (comparison(last, enumerator.Current) > 0)
{
return false;
}
last = enumerator.Current;
}
return true;
}
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
public static void AddRange<T>(this ICollection<T> collection, params T[] items)
{
Verify.IsNotNull(collection, "collection");
_AddRange(collection, items);
}
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
public static void AddRange<T>(this ICollection<T> collection, IEnumerable<T> items)
{
Verify.IsNotNull(collection, "collection");
_AddRange(collection, items);
}
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
private static void _AddRange<T>(ICollection<T> collection, IEnumerable<T> items)
{
if (items == null)
{
return;
}
foreach (var item in items)
{
collection.Add(item);
}
}
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
public static IEnumerable<T> Reverse<T>(this IList<T> list)
{
Verify.IsNotNull(list, "list");
return _Reverse(list);
}
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
private static IEnumerable<T> _Reverse<T>(IList<T> list)
{
for (int i = list.Count - 1; i >= 0; --i)
{
yield return list[i];
}
}
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
public static IList<T> Shuffle<T>(this IList<T> list)
{
var r = new Random();
return Shuffle(list, () => r.Next(list.Count));
}
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
public static IList<T> Shuffle<T>(this IList<T> list, Func<int> numberGenerator)
{
Verify.IsNotNull(list, "list");
Verify.IsNotNull(numberGenerator, "numberGenerator");
var swapIndices = new int[list.Count];
for (int i = 0; i < list.Count; ++i)
{
int j = numberGenerator();
if (j < 0 || j >= list.Count)
{
throw new ArgumentException("The number generator function generated a number outside the valid range.");
}
swapIndices[i] = j;
}
return _Shuffle(list, swapIndices);
}
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
private static IList<T> _Shuffle<T>(IList<T> list, int[] swapIndices)
{
Assert.AreEqual(list.Count, swapIndices.Length);
for (int i = swapIndices.Length; i > 1; --i)
{
int k = swapIndices[i-1];
T temp = list[k];
list[k] = list[i-1];
list[i-1] = temp;
}
return list;
}
}
}