-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathExtensions.cs
More file actions
381 lines (342 loc) · 11.2 KB
/
Extensions.cs
File metadata and controls
381 lines (342 loc) · 11.2 KB
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Resources;
using IllidanS4.SharpUtils.Reflection.Linq;
using IllidanS4.SharpUtils.Unsafe;
namespace IllidanS4.SharpUtils
{
/// <summary>
/// This class contains various useful methods and extension methods.
/// </summary>
public static class Extensions
{
/// <summary>
/// Enumerates an array, returning the (index, value) pairs.
/// </summary>
/// <param name="array">The array to enumerate.</param>
/// <returns>An enumerable of the pairs.</returns>
public static IEnumerable<KeyValuePair<int, T>> PairEnumerate<T>(this T[] array)
{
for(int i = 0; i < array.Length; i++)
{
yield return new KeyValuePair<int, T>(i, array[i]);
}
}
/// <summary>
/// Creates an enumerable of one element.
/// </summary>
/// <param name="elem">The one element in this enumerable.</param>
/// <returns>The enumerable.</returns>
public static IEnumerable<TElement> Once<TElement>(TElement elem)
{
yield return elem;
}
/// <summary>
/// Enumerates a list, returning the (index, value) pairs.
/// </summary>
/// <param name="list">The list to enumerate.</param>
/// <returns>An enumerable of the pairs.</returns>
public static IEnumerable<KeyValuePair<int, T>> PairEnumerate<T>(this IList<T> list)
{
for(int i = 0; i < list.Count; i++)
{
yield return new KeyValuePair<int, T>(i, list[i]);
}
}
/// <summary>
/// Fills an array with a specific value.
/// </summary>
/// <param name="arr">The array to populate.</param>
/// <param name="value">The value to set each array element to.</param>
/// <returns>The same array as passed in <paramref name="arr"/>.</returns>
public static T[] Populate<T>(this T[] arr, T value)
{
int l = arr.Length;
for(int i = 0; i < l; i++)
{
arr[i] = value;
}
return arr;
}
/// <summary>
/// Processes each element in a list with a specifiec function and sets it to the return value.
/// </summary>
/// <param name="list">The list to process.</param>
/// <param name="changeFunc">The processing function.</param>
public static void Process<T>(this IList<T> list, Func<T,T> changeFunc)
{
for(int i = 0; i < list.Count; i++)
{
list[i] = changeFunc(list[i]);
}
}
/// <summary>
/// Processes each element in a list with a specifiec function and sets it to the return value.
/// </summary>
/// <param name="list">The list to process.</param>
/// <param name="changeFunc">The processing function.</param>
public static void Process<T>(this IList<T> list, Func<int,T,T> changeFunc)
{
for(int i = 0; i < list.Count; i++)
{
list[i] = changeFunc(i, list[i]);
}
}
/// <summary>
/// Processes each element in a dictionary with a specifiec function and sets it to the return value.
/// </summary>
/// <param name="dictionary">The dictionary to process.</param>
/// <param name="changeFunc">The processing function.</param>
public static void Process<TKey,TValue>(this IDictionary<TKey,TValue> dictionary, Func<TKey,TValue,TValue> changeFunc)
{
foreach(TKey key in dictionary.Keys)
{
dictionary[key] = changeFunc(key, dictionary[key]);
}
}
/// <summary>
/// Automatically enumerates an enumerator and runs an action for each returned object.
/// </summary>
/// <param name="enumerator">The enumerator to enumerate.</param>
/// <param name="each">The action to call for each object returned by the enumerator.</param>
public static void ForEach(this IEnumerator enumerator, Action<object> each)
{
while(enumerator.MoveNext())
{
each(enumerator.Current);
}
}
/// <summary>
/// Automatically enumerates an enumerator and runs an action for each returned object.
/// </summary>
/// <param name="enumerator">The enumerator to enumerate.</param>
/// <param name="each">The action to call for each object returned by the enumerator.</param>
public static void ForEach<T>(this IEnumerator<T> enumerator, Action<T> each)
{
while(enumerator.MoveNext())
{
each(enumerator.Current);
}
}
/// <summary>
/// Performs a cast on an enumerable object, returning its interface representation without changing its value.
/// </summary>
/// <param name="enumerable">The enumerable object.</param>
/// <returns>The same object.</returns>
public static IEnumerable<T> ToIEnumerable<T>(this IEnumerable<T> enumerable)
{
return enumerable;
}
/// <summary>
/// Wraps an enumerator in an enumerable object.
/// </summary>
/// <param name="enumerator">The enumerator to wrap.</param>
/// <returns>The object that can be enumerated.</returns>
[Obsolete("The resulting object doesn't fulfill the IEnumerable contract correctly. Do not use this for iterating an enumerator.")]
public static IEnumerable ToIEnumerable(this IEnumerator enumerator)
{
return new Enumerator(enumerator);
}
/// <summary>
/// Wraps an enumerator in an enumerable object.
/// </summary>
/// <param name="enumerator">The enumerator to wrap.</param>
/// <returns>The object that can be enumerated.</returns>
[Obsolete("The resulting object doesn't fulfill the IEnumerable contract correctly. Do not use this for iterating an enumerator.")]
public static IEnumerable<T> ToIEnumerable<T>(this IEnumerator<T> enumerator)
{
return new Enumerator<T>(enumerator);
}
class Enumerator : IEnumerable
{
readonly IEnumerator enumerator;
public Enumerator(IEnumerator ienum)
{
enumerator = ienum;
}
public IEnumerator GetEnumerator()
{
return enumerator;
}
}
class Enumerator<T> : IEnumerable<T>
{
readonly IEnumerator<T> enumerator;
public Enumerator(IEnumerator<T> ienum)
{
enumerator = ienum;
}
IEnumerator IEnumerable.GetEnumerator()
{
return enumerator;
}
public IEnumerator<T> GetEnumerator()
{
return enumerator;
}
}
/// <summary>
/// Converts a vararg iterator to an enumerable.
/// </summary>
/// <param name="arglist">The iterator to convert.</param>
/// <returns>The enumerable object.</returns>
public static IEnumerable ToIEnumerable(this ArgIterator arglist)
{
return new ArgListEnumerable(arglist);
}
class ArgListEnumerable : IEnumerable
{
object Arglist;
readonly object Initial;
public ArgListEnumerable(ArgIterator arglist)
{
Arglist = UnsafeTools.Box(arglist);
Initial = UnsafeTools.Box(arglist);
}
public IEnumerator GetEnumerator()
{
var enumerator = new ArgListEnumerator(Arglist);
Arglist = Initial;
return enumerator;
}
class ArgListEnumerator : IEnumerator
{
object Arglist;
object Initial;
public ArgListEnumerator(object arglist)
{
Arglist = arglist;
Initial = arglist;
}
object current;
int state = -1;
public object Current
{
get{
if(state == -1)
{
throw new InvalidOperationException(GetResourceString("InvalidOperation_EnumNotStarted"));
}else if(state == 1)
{
throw new InvalidOperationException(GetResourceString("InvalidOperation_EnumEnded"));
}
return current;
}
}
public void Reset()
{
Arglist = Initial;
}
public bool MoveNext()
{
ArgIterator arglist = (ArgIterator)Arglist;
state = 0;
if(arglist.GetRemainingCount() == 0)
{
state = 1;
return false;
}
current = TypedReference.ToObject(arglist.GetNextArg());
Arglist = UnsafeTools.Box(arglist);
return true;
}
}
}
/// <summary>
/// Creates an enumerable object from a varargs argument.
/// </summary>
/// <param name="arglist">The handle to the arglist.</param>
/// <returns>The enumerable object.</returns>
public static IEnumerable Enumerate(this RuntimeArgumentHandle arglist)
{
return new ArgIterator(arglist).ToIEnumerable();
}
/// <summary>
/// Constructs an enumerable object from a lazily initialized collection.
/// </summary>
/// <param name="lazyCollection">The lazy collection.</param>
/// <returns>The enumerable object. The collection will not be initialized until the enumeration begins.</returns>
public static IEnumerable ToIEnumerable(this Lazy<ICollection> lazyCollection)
{
foreach(var obj in lazyCollection.Value) yield return obj;
}
/// <summary>
/// Constructs an enumerable object from a lazily initialized collection.
/// </summary>
/// <param name="lazyCollection">The lazy collection.</param>
/// <returns>The enumerable object. The collection will not be initialized until the enumeration begins.</returns>
public static IEnumerable<T> ToIEnumerable<T>(this Lazy<ICollection<T>> lazyCollection)
{
foreach(var obj in lazyCollection.Value) yield return obj;
}
/// <summary>
/// Performs a direct cast on the <typeparamref name="TFrom"/> argument to the same type, represented by <typeparamref name="TTo"/>.
/// </summary>
/// <param name="arg">The argument to cast.</param>
/// <returns>The same value, expressed using <typeparamref name="TTo"/>.</returns>
/// <exception cref="System.InvalidCastException">This exception is thrown when <typeparamref name="TFrom"/> and <typeparamref name="TTo"/> do not represent the same type.</exception>
public static TTo FastCast<TFrom,TTo>(TFrom arg)
{
return __refvalue(__makeref(arg), TTo);
}
static ResourceManager clr_resources;
static Extensions()
{
clr_resources = new ResourceManager("mscorlib", Assembly.Load("mscorlib.dll"));
}
/// <summary>
/// Returns a mscorlib.dll resource string from its key.
/// </summary>
/// <param name="key">The resource key.</param>
/// <returns>The resource text.</returns>
public static string GetResourceString(string key)
{
return clr_resources.GetString(key);
}
/// <summary>
/// Swaps the values of two variables.
/// </summary>
/// <param name="a">The first variable.</param>
/// <param name="b">The second variable.</param>
public static void Swap<T>(ref T a, ref T b)
{
T t = a;
a = b;
b = t;
}
/// <summary>
/// Combines two enumerables, producing a pair enumerable.
/// </summary>
/// <param name="larg">The left enumerable.</param>
/// <param name="rarg">The right.</param>
/// <returns>The pair enumerable.</returns>
public static IEnumerable<Tuple<object,object>> Combine(IEnumerable larg, IEnumerable rarg)
{
IEnumerator enum1 = larg.GetEnumerator();
IEnumerator enum2 = rarg.GetEnumerator();
while(enum1.MoveNext() && enum2.MoveNext())
{
yield return new Tuple<object,object>(enum1.Current, enum2.Current);
}
}
/// <summary>
/// Combines two enumerables, producing a pair enumerable.
/// </summary>
/// <param name="larg">The left enumerable.</param>
/// <param name="rarg">The right.</param>
/// <returns>The pair enumerable.</returns>
public static IEnumerable<Tuple<T1,T2>> Combine<T1,T2>(IEnumerable<T1> larg, IEnumerable<T2> rarg)
{
IEnumerator<T1> enum1 = larg.GetEnumerator();
IEnumerator<T2> enum2 = rarg.GetEnumerator();
while(enum1.MoveNext() && enum2.MoveNext())
{
yield return new Tuple<T1,T2>(enum1.Current, enum2.Current);
}
}
}
}