This repository has been archived by the owner on Feb 17, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Extensions.cs
258 lines (202 loc) · 6.82 KB
/
Extensions.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Windows;
using System.Windows.Threading;
namespace Com.Xenthrax.WindowsDataVisualizer
{
internal static class Extensions
{
public static IEnumerable<T> Convert<T>(this IEnumerable enumerable)
{
if (enumerable == null)
throw new ArgumentNullException("enumerable");
foreach (object value in enumerable)
yield return (T)System.Convert.ChangeType(value, typeof(T));
}
public static void AddRange<T>(this ICollection<T> list, params T[] value)
{
if (list == null)
throw new ArgumentNullException("list");
if (value == null)
throw new ArgumentNullException("value");
foreach (T item in value)
list.Add(item);
}
public static void AddRange<T>(this List<T> list, params T[] value)
{
if (list == null)
throw new ArgumentNullException("list");
if (value == null)
throw new ArgumentNullException("value");
list.AddRange((IEnumerable<T>)value);
}
public static void RemoveFirst<T>(this List<T> list)
{
if (list == null)
throw new ArgumentNullException("list");
list.RemoveAt(0);
}
public static void RemoveLast<T>(this List<T> list)
{
if (list == null)
throw new ArgumentNullException("list");
list.RemoveAt(list.Count - 1);
}
public static object Default(this Type type)
{
if (type == null)
throw new ArgumentNullException("type");
return type.IsValueType ? Activator.CreateInstance(type) : null;
}
public static IEnumerable<T> ReturnEnumerable<T>(params T[] items)
{
if (items == null)
throw new ArgumentNullException("items");
return items;
}
#region DeferExecution/DeferExecutionOnce
public static IEnumerable<TResult> DeferExecution<TResult>(this Func<IEnumerable<TResult>> func)
{
if (func == null)
throw new ArgumentNullException("func");
return new DeferExecutionEnumerable<TResult>(func);
}
public static IEnumerable<TResult> DeferExecution<T, TResult>(this Func<T, IEnumerable<TResult>> func, T arg)
{
if (func == null)
throw new ArgumentNullException("func");
return new DeferExecutionEnumerable<TResult>(() => func(arg));
}
public static IEnumerable<TResult> DeferExecution<T1, T2, TResult>(this Func<T1, T2, IEnumerable<TResult>> func, T1 arg1, T2 arg2)
{
if (func == null)
throw new ArgumentNullException("func");
return new DeferExecutionEnumerable<TResult>(() => func(arg1, arg2));
}
public static IEnumerable<TResult> DeferExecution<T1, T2, T3, TResult>(this Func<T1, T2, T3, IEnumerable<TResult>> func, T1 arg1, T2 arg2, T3 arg3)
{
if (func == null)
throw new ArgumentNullException("func");
return new DeferExecutionEnumerable<TResult>(() => func(arg1, arg2, arg3));
}
public static IEnumerable<TResult> DeferExecution<T1, T2, T3, T4, TResult>(this Func<T1, T2, T3, T4, IEnumerable<TResult>> func, T1 arg1, T2 arg2, T3 arg3, T4 arg4)
{
if (func == null)
throw new ArgumentNullException("func");
return new DeferExecutionEnumerable<TResult>(() => func(arg1, arg2, arg3, arg4));
}
public static IEnumerable<TResult> DeferExecutionOnce<TResult>(this Func<IEnumerable<TResult>> func)
{
if (func == null)
throw new ArgumentNullException("func");
return new DeferExecutionEnumerable<TResult>(func).Memoize();
}
public static IEnumerable<TResult> DeferExecutionOnce<T, TResult>(this Func<T, IEnumerable<TResult>> func, T arg)
{
if (func == null)
throw new ArgumentNullException("func");
return new DeferExecutionEnumerable<TResult>(() => func(arg)).Memoize();
}
public static IEnumerable<TResult> DeferExecutionOnce<T1, T2, TResult>(this Func<T1, T2, IEnumerable<TResult>> func, T1 arg1, T2 arg2)
{
if (func == null)
throw new ArgumentNullException("func");
return new DeferExecutionEnumerable<TResult>(() => func(arg1, arg2)).Memoize();
}
public static IEnumerable<TResult> DeferExecutionOnce<T1, T2, T3, TResult>(this Func<T1, T2, T3, IEnumerable<TResult>> func, T1 arg1, T2 arg2, T3 arg3)
{
if (func == null)
throw new ArgumentNullException("func");
return new DeferExecutionEnumerable<TResult>(() => func(arg1, arg2, arg3)).Memoize();
}
public static IEnumerable<TResult> DeferExecutionOnce<T1, T2, T3, T4, TResult>(this Func<T1, T2, T3, T4, IEnumerable<TResult>> func, T1 arg1, T2 arg2, T3 arg3, T4 arg4)
{
if (func == null)
throw new ArgumentNullException("func");
return new DeferExecutionEnumerable<TResult>(() => func(arg1, arg2, arg3, arg4)).Memoize();
}
#endregion
public static IEnumerable<T> AsSerializable<T>(this IEnumerable<T> enumerable)
{
if (enumerable == null)
throw new ArgumentNullException("enumerable");
if (enumerable is SerializableEnumerable<T>)
return enumerable;
else
return new SerializableEnumerable<T>(enumerable);
}
private class DeferExecutionEnumerable<T> : IEnumerable<T>, IEnumerator<T>
{
private Func<IEnumerable<T>> func;
public DeferExecutionEnumerable(Func<IEnumerable<T>> func)
{
this.func = func;
}
// IEnumerable<T>
public IEnumerator<T> GetEnumerator()
{
return new DeferExecutionEnumerable<T>(this.func);
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
// IEnumerator<T>
private IEnumerator<T> enumerator;
public T Current
{
get { return this.enumerator.Current; }
}
public void Dispose()
{
if (this.enumerator != null)
this.enumerator.Dispose();
this.enumerator = null;
}
object System.Collections.IEnumerator.Current
{
get { return this.enumerator.Current; }
}
public bool MoveNext()
{
if (this.enumerator == null)
this.enumerator = this.func().GetEnumerator();
return this.enumerator.MoveNext();
}
public void Reset()
{
if (this.enumerator != null)
this.enumerator.Reset();
}
}
[CollectionDataContract]
private class SerializableEnumerable<T> : IEnumerable<T>
{
private IEnumerable<T> enumerable;
private List<T> list;
private SerializableEnumerable()
{
this.enumerable = this.list = new List<T>();
}
public SerializableEnumerable(IEnumerable<T> enumerable)
{
this.enumerable = enumerable;
}
public IEnumerator<T> GetEnumerator()
{
return this.enumerable.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return this.enumerable.GetEnumerator();
}
private void Add(T value)
{
this.list.Add(value);
}
}
}
}