forked from dotnet/roslyn-analyzers
/
IEnumerableExtensions.cs
227 lines (192 loc) · 7.68 KB
/
IEnumerableExtensions.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
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
namespace Analyzer.Utilities.Extensions
{
internal static class IEnumerableExtensions
{
public static IEnumerable<T> Concat<T>(this IEnumerable<T> source, T value)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
foreach (T v in source)
{
yield return v;
}
yield return value;
}
public static ISet<T> ToSet<T>(this IEnumerable<T> source, IEqualityComparer<T> comparer)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
return new HashSet<T>(source, comparer);
}
public static ISet<T> ToSet<T>(this IEnumerable<T> source)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
return source as ISet<T> ?? new HashSet<T>(source);
}
public static IEnumerable<T> OrderBy<T>(this IEnumerable<T> source, IComparer<T> comparer)
{
return source.OrderBy(t => t, comparer);
}
public static IEnumerable<T> OrderBy<T>(this IEnumerable<T> source, Comparison<T> compare)
{
return source.OrderBy(new ComparisonComparer<T>(compare));
}
public static IEnumerable<T> Order<T>(this IEnumerable<T> source) where T : IComparable<T>
{
return source.OrderBy((t1, t2) => t1.CompareTo(t2));
}
private static readonly Func<object?, bool> s_notNullTest = x => x != null;
public static IEnumerable<T> WhereNotNull<T>(this IEnumerable<T?> source) where T : class
{
if (source == null)
{
return ImmutableArray<T>.Empty;
}
return source.Where((Func<T?, bool>)s_notNullTest)!;
}
public static ImmutableArray<TSource> WhereAsArray<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> selector)
{
var builder = ImmutableArray.CreateBuilder<TSource>();
bool any = false;
foreach (var element in source)
{
if (selector(element))
{
any = true;
builder.Add(element);
}
}
if (any)
{
return builder.ToImmutable();
}
else
{
return ImmutableArray<TSource>.Empty;
}
}
public static void Dispose<T>(this IEnumerable<T?> collection)
where T : class, IDisposable
{
foreach (var item in collection)
{
item?.Dispose();
}
}
/// <summary>
/// Determines whether a sequence contains, exactly, <paramref name="count"/> elements.
/// </summary>
/// <typeparam name="TSource">The type of the elements of source.</typeparam>
/// <param name="source">The <see cref="IEnumerable{TSource}"/> to check for cardinality.</param>
/// <param name="count">The number of elements to ensure exists.</param>
/// <returns><see langword="true" /> the source sequence contains, exactly, <paramref name="count"/> elements; otherwise, <see langword="false" />.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
public static bool HasExactly<TSource>(this IEnumerable<TSource> source, int count)
{
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
if (source is ICollection<TSource> collectionoft)
{
return collectionoft.Count == count;
}
if (source is ICollection collection)
{
return collection.Count == count;
}
using var enumerator = source.GetEnumerator();
while (count-- > 0)
{
if (!enumerator.MoveNext())
{
return false;
}
}
return !enumerator.MoveNext();
}
/// <summary>
/// Determines whether a sequence contains more than <paramref name="count"/> elements.
/// </summary>
/// <typeparam name="TSource">The type of the elements of <paramref name="source"/>.</typeparam>
/// <param name="source">The <see cref="IEnumerable{TSource}"/> to check for cardinality.</param>
/// <param name="count">The number of elements to ensure exists.</param>
/// <returns><see langword="true" /> the source sequence contains more than <paramref name="count"/> elements; otherwise, <see langword="false" />.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
public static bool HasMoreThan<TSource>(this IEnumerable<TSource> source, int count)
{
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
if (source is ICollection<TSource> collectionoft)
{
return collectionoft.Count > count;
}
if (source is ICollection collection)
{
return collection.Count > count;
}
using var enumerator = source.GetEnumerator();
while (count-- > 0)
{
if (!enumerator.MoveNext())
{
return false;
}
}
return enumerator.MoveNext();
}
/// <summary>
/// Determines whether a sequence contains fewer than <paramref name="count"/> elements.
/// </summary>
/// <typeparam name="TSource">The type of the elements of <paramref name="source"/>.</typeparam>
/// <param name="source">The <see cref="IEnumerable{TSource}"/> to check for cardinality.</param>
/// <param name="count">The number of elements to ensure exists.</param>
/// <returns><see langword="true" /> the source sequence contains less than <paramref name="count"/> elements; otherwise, <see langword="false" />.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
public static bool HasFewerThan<TSource>(this IEnumerable<TSource> source, int count)
{
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
if (source is ICollection<TSource> collectionoft)
{
return collectionoft.Count < count;
}
if (source is ICollection collection)
{
return collection.Count < count;
}
using var enumerator = source.GetEnumerator();
while (count > 0 && enumerator.MoveNext()) { count--; }
return count > 0;
}
private class ComparisonComparer<T> : Comparer<T>
{
private readonly Comparison<T> _compare;
public ComparisonComparer(Comparison<T> compare)
{
_compare = compare;
}
public override int Compare(T x, T y)
{
return _compare(x, y);
}
}
}
}