forked from TerrariaPrismTeam/Prism
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinqExt.cs
316 lines (258 loc) · 9.84 KB
/
LinqExt.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
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
// one of these is defined for Prism.csproj
// otherwise, it must be Prism.Injector.csproj
#if WINDOWS || UNIX || LINUX || OSX
//|| MAIN_PROJ || DEV_BUILD
namespace Prism.Util
#else
namespace Prism.Injector
#endif
{
#pragma warning disable RECS0001
public static partial class Empty<T>
{
public static T[] Array = new T[0];
}
public static partial class MiscExtensions
{
public static T Identity<T>(T x)
{
return x;
}
}
#pragma warning restore RECS0001
public static class LinqExt
{
public static bool IsEmpty <T>(this IEnumerable<T> coll)
{
foreach (var t in coll)
return false;
return true;
}
public static bool IsSingleton<T>(this IEnumerable<T> coll)
{
bool foundFirst = false;
foreach (var t in coll)
{
if (foundFirst)
return false;
foundFirst = true;
}
return foundFirst;
}
public static string SafeToString(this object v, string defValue = null)
{
if (ReferenceEquals(v, null))
return defValue;
return v.ToString();
}
public static IEnumerable<T> Flatten <T>(this IEnumerable<IEnumerable<T>> coll)
{
if (coll.IsEmpty())
return Empty<T>.Array;
return coll.Aggregate((a, b) => a.SafeConcat(b));
}
public static IEnumerable<T> SafeConcat<T>(this IEnumerable<T> coll, IEnumerable<T> other)
{
if (coll == null && other == null)
return Empty<T>.Array;
if (coll == null)
return other;
if (other == null)
return coll;
return coll.Concat(other);
}
public static IEnumerable<TOut> SafeSelect<TIn, TOut>(this IEnumerable<TIn> coll, Func<TIn, TOut> fn)
{
if (fn == null)
throw new ArgumentNullException("fn");
if (coll == null)
yield break;
foreach (var i in coll)
yield return fn(i);
yield break;
}
public static Dictionary<TKey, TValue> ToDictionary<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> dict)
{
var ret = new Dictionary<TKey, TValue>();
foreach (var kvp in dict)
ret.Add(kvp.Key, kvp.Value);
return ret;
}
public static bool All(this IEnumerable<bool> coll)
{
foreach (var b in coll)
if (!b)
return false;
return true;
}
public static bool Any(this IEnumerable<bool> coll)
{
foreach (var b in coll)
if (b)
return true;
return false;
}
public static T[] Subarray<T>(this T[] arr, int s, int l)
{
if (s == 0 && l == arr.Length)
return arr;
if (s < 0 || s + l >= arr.Length)
throw new ArgumentOutOfRangeException();
var ret = new T[l];
Array.Copy(arr, s, ret, 0, l);
return ret;
}
public static void Resize2D<T>(ref T[,] array, int x, int y)
{
int ox = array.GetLength(0), oy = array.GetLength(1);
var orig = array;
array = new T[x, y];
int mx = Math.Min(ox, x), my = Math.Min(oy, y);
for (int i = 0; i < my; i++)
Array.Copy(orig, i * ox, array, i * x, mx);
}
public static void Add<T>(ref T[] array, T toAdd)
{
Array.Resize(ref array, array.Length + 1);
array[array.Length - 1] = toAdd;
}
public static void AddArray<T>(ref T[] array, T[] toAdd)
{
var ol = array.Length;
Array.Resize(ref array, array.Length + toAdd.Length);
for (int i = 0; i < toAdd.Length; i++)
array[i + ol] = toAdd[i];
}
public static IEnumerable<TOut> SelectIndex<TIn, TOut>(this TIn[] arr, Func<int, TIn, TOut> selector)
{
if (selector == null)
throw new ArgumentNullException("selector");
for (int i = 0; i < arr.Length; i++)
yield return selector(i, arr[i]);
yield break;
}
public static IEnumerable<TOut> SelectIndex<TIn, TOut>(this IList<TIn> list, Func<int, TIn, TOut> selector)
{
if (selector == null)
throw new ArgumentNullException("selector");
for (int i = 0; i < list.Count; i++)
yield return selector(i, list[i]);
yield break;
}
public static IEnumerable<TOut> SelectIndex<TIn, TOut>(this IEnumerable<TIn> coll, Func<int, TIn, TOut> selector)
{
if (selector == null)
throw new ArgumentNullException("selector");
int i = 0;
foreach (var e in coll)
{
yield return selector(i, e);
i++;
}
yield break;
}
public static string Join(this IEnumerable<string> coll, Func<int, string> sepatator)
{
var sb = new StringBuilder();
int i = 0;
foreach (var t in coll)
sb.Append(sepatator(i++)).Append(t);
return sb.ToString().SafeToString();
}
public static T Join<T>(this IEnumerable<T> coll, Func<int, T> separator, Func<T, T, T> accumulator)
{
if (separator == null)
throw new ArgumentNullException("separator");
if (accumulator == null)
throw new ArgumentNullException("accumulator");
if (coll.IsEmpty())
throw new ArgumentException("Collection is empty.", "coll");
T r;
int i = 0;
using (var etor = coll.GetEnumerator())
{
etor.Reset();
etor.MoveNext(); // should be true because the collection isn't empty
r = etor.Current;
while (etor.MoveNext())
r = accumulator(accumulator(r, separator(i++)), etor.Current);
// r += separator(i++) + current
}
return r;
}
public static T SafeJoin<T>(this IEnumerable<T> coll, Func<int, T> separator, Func<T, T, T> accumulator)
{
if (coll.IsEmpty())
return default(T);
return coll.Join(separator, accumulator);
}
public static IEnumerable<T> Join<T>(this IEnumerable<IEnumerable<T>> coll, Func<int, IEnumerable<T>> separator)
{
if (separator == null)
throw new ArgumentNullException("separator");
IEnumerable<T> r = Empty<T>.Array;
int i = 0;
foreach (var e in coll)
// yield! separator(i++)
// yield! e
r = r.Concat(separator(i++)).Concat(e);
return r;
}
static bool Equality<T>(T a, T b)
{
if (ReferenceEquals(a, b))
return true;
if (ReferenceEquals(a, null) ||ReferenceEquals(b, null))
return false;
if (a is IEquatable<T>)
return ((IEquatable<T>)a).Equals(b);
if (b is IEquatable<T>)
return ((IEquatable<T>)b).Equals(a);
return a.Equals(b);
}
[DebuggerStepThrough]
public static bool Equals<T>(this IList<T> a, IList<T> b)
{
if (a.Count != b.Count)
return false;
for (int i = 0; i < a.Count; i++)
if (!Equality(a[i], b[i]))
return false;
return true;
}
/// <summary>
/// Gets whether an <see cref="IEnumerable{T}"/> is in chronological order by supplying the adjacent items for each item.
/// </summary>
/// <typeparam name="T">Item type.</typeparam>
/// <param name="coll">This collection.</param>
/// <param name="getPrevious">
/// Passes an item in the collection and returns either the previous item or null to ignore the check.
/// <para/>Note: pass null for the function itself to skip it. However, if both the getPrevious and getNext functions are null, an <see cref="ArgumentException"/> will be thrown.
/// </param>
/// <param name="getNext">
/// Passes an item in the collection and returns either the next item or null to ignore the check.
/// <para/>Note: pass null for the function itself to skip it. However, if both the getPrevious and getNext functions are null, an <see cref="ArgumentException"/> will be thrown.
/// </param>
/// <returns>Whether the collection is chronological.</returns>
[DebuggerStepThrough]
public static bool Chronological<T>(this IEnumerable<T> coll, Func<T, T> getPrevious, Func<T, T> getNext)
{
var arr = coll.ToArray();
if (getPrevious == null && getNext == null)
throw new ArgumentException("At least one of the two adjacent item functions must be defined.");
for (int i = 0; i < arr.Length; i++)
{
var prev = getPrevious == null || i == 0 || ReferenceEquals(getPrevious(arr[i]), null) || getPrevious(arr[i]).Equals(arr[i - 1]);
var next = getNext == null || i == arr.Length - 1 || ReferenceEquals(getNext(arr[i]), null) || getNext(arr[i]).Equals(arr[i + 1]);
if (!(prev && next))
return false;
}
return true;
}
}
}