-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathCollectionExtensions.cs
More file actions
316 lines (262 loc) · 11.5 KB
/
Copy pathCollectionExtensions.cs
File metadata and controls
316 lines (262 loc) · 11.5 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
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using System.Collections;
using System.Collections.Generic;
namespace UnityEditor.PackageManager.UI.Internal
{
internal static class CollectionExtensions
{
public static bool AnyMatches<T>(this IEnumerable<T> collection, Func<T, bool> condition)
{
if (collection == null)
throw new ArgumentNullException(nameof(collection));
if (condition == null)
throw new ArgumentNullException(nameof(condition));
foreach (var item in collection)
if (condition(item))
return true;
return false;
}
public static bool ContainsMatches<T>(this IEnumerable<T> collection, T itemToCheck, IEqualityComparer<T> comparer = null)
{
if (collection == null)
throw new ArgumentNullException(nameof(collection));
if (itemToCheck == null)
throw new ArgumentNullException(nameof(itemToCheck));
comparer ??= EqualityComparer<T>.Default;
foreach (var item in collection)
if (comparer.Equals(itemToCheck, item))
return true;
return false;
}
public static bool AllMatches<T>(this IEnumerable<T> collection, Func<T, bool> condition)
{
if (collection == null)
throw new ArgumentNullException(nameof(collection));
if (condition == null)
throw new ArgumentNullException(nameof(condition));
foreach (var item in collection)
if (!condition(item))
return false;
return true;
}
public static int CountMatches<T>(this IEnumerable<T> collection, Func<T, bool> condition)
{
if (collection == null)
throw new ArgumentNullException(nameof(collection));
if (condition == null)
throw new ArgumentNullException(nameof(condition));
var count = 0;
foreach (var item in collection)
if (condition(item))
++count;
return count;
}
public static T FirstMatch<T>(this IEnumerable<T> collection, Func<T, bool> condition) where T : class
{
if (collection == null)
throw new ArgumentNullException(nameof(collection));
if (condition == null)
throw new ArgumentNullException(nameof(condition));
foreach (var item in collection)
if (condition(item))
return item;
return null;
}
public static T LastMatch<T>(this IList<T> collection, Func<T, bool> condition) where T : class
{
if (collection == null)
throw new ArgumentNullException(nameof(collection));
if (condition == null)
throw new ArgumentNullException(nameof(condition));
for (var i = collection.Count - 1; i >= 0; i--)
{
var item = collection[i];
if (condition(item))
return item;
}
return null;
}
public static IEnumerable<T> Filter<T>(this IEnumerable<T> collection, Func<T, bool> condition)
{
if (collection == null)
throw new ArgumentNullException(nameof(collection));
if (condition == null)
throw new ArgumentNullException(nameof(condition));
foreach (var item in collection)
if (condition(item))
yield return item;
}
public static IEnumerable<TResult> FilterByType<TResult>(this IEnumerable collection)
{
if (collection == null)
throw new ArgumentNullException(nameof(collection));
foreach (var item in collection)
if (item is TResult result)
yield return result;
}
public static IEnumerable<T> EnumerateDistinct<T>(this IEnumerable<T> collection)
{
if (collection == null)
throw new ArgumentNullException(nameof(collection));
var seen = new HashSet<T>();
foreach (var item in collection)
if (seen.Add(item))
yield return item;
}
public static IEnumerable<TResult> SelectAsEnumerable<T, TResult>(this IEnumerable<T> collection, Func<T, TResult> selector)
{
if (collection == null)
throw new ArgumentNullException(nameof(collection));
if (selector == null)
throw new ArgumentNullException(nameof(selector));
foreach (var item in collection)
yield return selector(item);
}
public static IEnumerable<string> SelectNonEmpty<T>(this IEnumerable<T> collection, Func<T, string> selector)
{
if (collection == null)
throw new ArgumentNullException(nameof(collection));
if (selector == null)
throw new ArgumentNullException(nameof(selector));
foreach (var item in collection)
{
var str = selector(item);
if (!string.IsNullOrEmpty(str))
yield return str;
}
}
public static TResult[] SelectToNewArray<T, TResult>(this IReadOnlyCollection<T> collection, Func<T, TResult> selector)
{
if (collection == null)
throw new ArgumentNullException(nameof(collection));
if (selector == null)
throw new ArgumentNullException(nameof(selector));
if (collection.Count == 0)
return Array.Empty<TResult>();
var result = new TResult[collection.Count];
var index = 0;
foreach (var item in collection)
result[index++] = selector(item);
return result;
}
public static HashSet<TResult> SelectToNewHashSet<T, TResult>(this IEnumerable<T> collection, Func<T, TResult> selector)
{
if (collection == null)
throw new ArgumentNullException(nameof(collection));
if (selector == null)
throw new ArgumentNullException(nameof(selector));
var result = new HashSet<TResult>();
foreach (var item in collection)
result.Add(selector(item));
return result;
}
public static Dictionary<TKey, T> ToNewDictionary<T, TKey>(this IEnumerable<T> collection, Func<T, TKey> keySelector)
{
Dictionary<TKey, T> outputDictionary = null;
ToDictionary(collection, keySelector, ref outputDictionary);
return outputDictionary;
}
public static void ToDictionary<T, TKey>(this IEnumerable<T> collection, Func<T, TKey> keySelector, ref Dictionary<TKey, T> outputDictionary)
{
if (collection == null)
throw new ArgumentNullException(nameof(collection));
if (keySelector == null)
throw new ArgumentNullException(nameof(keySelector));
if (outputDictionary == null)
outputDictionary = new Dictionary<TKey, T>();
else
outputDictionary.Clear();
foreach (var item in collection)
outputDictionary.Add(keySelector(item), item);
}
public static void ToHashSet<T>(this IEnumerable<T> collection, ref HashSet<T> outputHashSet)
{
if (collection == null)
throw new ArgumentNullException(nameof(collection));
if (outputHashSet == null)
outputHashSet = new HashSet<T>();
else
outputHashSet.Clear();
foreach (var item in collection)
outputHashSet.Add(item);
}
public static T[] ToNewArray<T>(this IEnumerable<T> collection, int initialCapacity)
{
if (collection == null)
throw new ArgumentNullException(nameof(collection));
var result = new T[initialCapacity];
var count = 0;
foreach (var item in collection)
{
// We resize the array to at least 4 to handle the case where initialCapacity is 0 (which should be allowed
// as sometimes the input collection is empty). We use 4 as that's a number used as the default by many implementations.
if (count == result.Length)
Array.Resize(ref result, Math.Max(result.Length * 2, 4));
result[count++] = item;
}
if (count != result.Length)
Array.Resize(ref result, count);
return result;
}
public static T[] ToNewArray<T>(this IReadOnlyCollection<T> collection)
{
T[] outputArray = null;
ToArray(collection, ref outputArray);
return outputArray;
}
public static void ToArray<T>(this IReadOnlyCollection<T> collection, ref T[] outputArray)
{
if (collection == null)
throw new ArgumentNullException(nameof(collection));
if (outputArray == null)
outputArray = new T[collection.Count];
else if (outputArray.Length != collection.Count)
Array.Resize(ref outputArray, collection.Count);
var index = 0;
using var enumerator = collection.GetEnumerator();
while (enumerator.MoveNext() && index < collection.Count)
outputArray[index++] = enumerator.Current;
}
public static IEnumerable<T> Join<T>(this IEnumerable<T> collection, params IEnumerable<T>[] collectionsToJoin)
{
if (collection == null)
throw new ArgumentNullException(nameof(collection));
if (collectionsToJoin == null)
throw new ArgumentNullException(nameof(collectionsToJoin));
foreach (var item in collection)
yield return item;
foreach (var list in collectionsToJoin)
if (list != null)
foreach (var item in list)
yield return item;
}
public static IEnumerable<T> Join<T>(this IEnumerable<T> collection, params T[] collectionToJoin)
{
if (collection == null)
throw new ArgumentNullException(nameof(collection));
if (collectionToJoin == null)
throw new ArgumentNullException(nameof(collectionToJoin));
foreach (var item in collection)
yield return item;
foreach (var item in collectionToJoin)
yield return item;
}
public static bool IsSequenceEqual<T>(this IReadOnlyList<T> first, IReadOnlyList<T> second, IEqualityComparer<T> comparer = null)
{
if (ReferenceEquals(first, second))
return true;
first ??= Array.Empty<T>();
second ??= Array.Empty<T>();
if (first.Count != second.Count)
return false;
comparer ??= EqualityComparer<T>.Default;
for (var i = 0; i < first.Count; i++)
if (!comparer.Equals(first[i], second[i]))
return false;
return true;
}
}
}