forked from gitextensions/gitextensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinqExtensions.cs
188 lines (164 loc) · 5.99 KB
/
LinqExtensions.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
using System.Collections.Generic;
namespace System.Linq
{
public static class LinqExtensions
{
//
// Summary:
// Creates a System.Collections.Generic.Dictionary<TKey,List<TValue>> from an System.Collections.Generic.IEnumerable<T>
// according to a specified key selector function.
//
// Parameters:
// source:
// An System.Collections.Generic.IEnumerable<T> to create a System.Collections.Generic.Dictionary<TKey,List<TValue>>
// from.
//
// keySelector:
// A function to extract a key from each element.
//
// Type parameters:
// TSource:
// The type of the elements of source.
//
// TKey:
// The type of the key returned by keySelector.
//
// Returns:
// A System.Collections.Generic.Dictionary<TKey,List<TValue>> that contains keys and
// values.
//
// Exceptions:
// System.ArgumentNullException:
// source or keySelector is null. -or- keySelector produces a key that is null.
public static Dictionary<TKey, List<TSource>> ToDictionaryOfList<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
{
if (keySelector == null)
throw new ArgumentNullException("Argument keySelector can not be null");
Dictionary<TKey, List<TSource>> result = new Dictionary<TKey, List<TSource>>();
foreach (TSource sourceElement in source)
{
TKey key = keySelector(sourceElement);
if (key == null)
{
var ex = new ArgumentNullException("KeySelector produced a key that is null. See exception data for source.");
ex.Data.Add("source", sourceElement);
throw ex;
}
List<TSource> list;
if (!result.TryGetValue(key, out list))
{
list = new List<TSource>();
result[key] = list;
}
list.Add(sourceElement);
}
return result;
}
public static string Join(this IEnumerable<string> source, string separator)
{
return string.Join(separator, source.ToArray());
}
//
// Summary:
// Sorts the elements of a sequence in ascending order by using a specified
// comparer.
//
// Parameters:
// source:
// A sequence of values to order.
//
// keySelector:
// A function to extract a key from an element.
//
// comparer:
// A function to compare keys.
//
// Type parameters:
// TSource:
// The type of the elements of source.
//
// TKey:
// The type of the key returned by keySelector.
//
// Returns:
// An System.Linq.IOrderedEnumerable<TElement> whose elements are sorted according
// to a key.
//
// Exceptions:
// System.ArgumentNullException:
// source or keySelector is null.
public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TKey, TKey, int> comparer)
{
FuncComparer<TKey> fc = new FuncComparer<TKey>(comparer);
return source.OrderBy(keySelector, fc);
}
private class FuncComparer<T> : IComparer<T>
{
private Func<T, T, int> comparer;
public FuncComparer(Func<T, T, int> comparer)
{
this.comparer = comparer;
}
public int Compare(T x, T y)
{
return comparer(x, y);
}
}
//
// Summary:
// Transforms each element of an array into a new form by incorporating the
// element's index.
//
// Parameters:
// source:
// A sequence of values to invoke a transform function on.
//
// transformer:
// A transform function to apply to each source element; the second parameter
// of the function represents the index of the source element.
//
// Type parameters:
// TSource:
// The type of the elements of source.
//
// Exceptions:
// System.ArgumentNullException:
// source or selector is null.
public static void Select<TSource>(this TSource[] source, Func<TSource, int, TSource> transformer)
{
if (source == null)
throw new ArgumentNullException("source");
if (transformer == null)
throw new ArgumentNullException("transformer");
for (int i = 0; i < source.Length; i++)
source[i] = transformer(source[i], i);
}
//
// Summary:
// Transforms each element of an array into a new form.
//
// Parameters:
// source:
// A sequence of values to invoke a transform function on.
//
// transformer:
// A transform function to apply to each element.
//
// Type parameters:
// TSource:
// The type of the elements of source.
//
// Exceptions:
// System.ArgumentNullException:
// source or selector is null.
public static void Transform<TSource>(this TSource[] source, Func<TSource, TSource> transformer)
{
if (source == null)
throw new ArgumentNullException("source");
if (transformer == null)
throw new ArgumentNullException("transformer");
for (int i = 0; i < source.Length; i++)
source[i] = transformer(source[i]);
}
}
}