forked from jakubgarfield/Bonobo-Git-Server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnonymousComparer.cs
190 lines (151 loc) · 8.78 KB
/
AnonymousComparer.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
/*--------------------------------------------------------------------------
* AnonymousComparer - lambda compare selector for Linq
* ver 1.3.0.0 (Oct. 18th, 2010)
*
* created and maintained by neuecc <ils@neue.cc>
* licensed under Microsoft Public License(Ms-PL)
* http://neue.cc/
* http://linqcomparer.codeplex.com/
*--------------------------------------------------------------------------*/
using System.Collections.Generic;
namespace System.Linq
{
public static class AnonymousComparer
{
#region IComparer<T>
/// <summary>Example:AnonymousComparer.Create<int>((x, y) => y - x)</summary>
public static IComparer<T> Create<T>(Func<T, T, int> compare)
{
if (compare == null) throw new ArgumentNullException("compare");
return new Comparer<T>(compare);
}
private class Comparer<T> : IComparer<T>
{
private readonly Func<T, T, int> compare;
public Comparer(Func<T, T, int> compare)
{
this.compare = compare;
}
public int Compare(T x, T y)
{
return compare(x, y);
}
}
#endregion
#region IEqualityComparer<T>
/// <summary>Example:AnonymousComparer.Create((MyClass mc) => mc.MyProperty)</summary>
public static IEqualityComparer<T> Create<T, TKey>(Func<T, TKey> compareKeySelector)
{
if (compareKeySelector == null) throw new ArgumentNullException("compareKeySelector");
return new EqualityComparer<T>(
(x, y) =>
{
if (object.ReferenceEquals(x, y)) return true;
if (x == null || y == null) return false;
return compareKeySelector(x).Equals(compareKeySelector(y));
},
obj =>
{
if (obj == null) return 0;
return compareKeySelector(obj).GetHashCode();
});
}
public static IEqualityComparer<T> Create<T>(Func<T, T, bool> equals, Func<T, int> getHashCode)
{
if (equals == null) throw new ArgumentNullException("equals");
if (getHashCode == null) throw new ArgumentNullException("getHashCode");
return new EqualityComparer<T>(equals, getHashCode);
}
private class EqualityComparer<T> : IEqualityComparer<T>
{
private readonly Func<T, T, bool> equals;
private readonly Func<T, int> getHashCode;
public EqualityComparer(Func<T, T, bool> equals, Func<T, int> getHashCode)
{
this.equals = equals;
this.getHashCode = getHashCode;
}
public bool Equals(T x, T y)
{
return equals(x, y);
}
public int GetHashCode(T obj)
{
return getHashCode(obj);
}
}
#endregion
#region Extensions for LINQ Standard Query Operators
// IComparer<T>
public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TKey, TKey, int> compare)
{
return source.OrderBy(keySelector, AnonymousComparer.Create(compare));
}
public static IOrderedEnumerable<TSource> OrderByDescending<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TKey, TKey, int> compare)
{
return source.OrderByDescending(keySelector, AnonymousComparer.Create(compare));
}
public static IOrderedEnumerable<TSource> ThenBy<TSource, TKey>(this IOrderedEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TKey, TKey, int> compare)
{
return source.ThenBy(keySelector, AnonymousComparer.Create(compare));
}
public static IOrderedEnumerable<TSource> ThenByDescending<TSource, TKey>(this IOrderedEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TKey, TKey, int> compare)
{
return source.ThenByDescending(keySelector, AnonymousComparer.Create(compare));
}
// IEqualityComparer<T>
public static bool Contains<TSource, TCompareKey>(this IEnumerable<TSource> source, TSource value, Func<TSource, TCompareKey> compareKeySelector)
{
return source.Contains(value, AnonymousComparer.Create(compareKeySelector));
}
public static IEnumerable<TSource> Distinct<TSource, TCompareKey>(this IEnumerable<TSource> source, Func<TSource, TCompareKey> compareKeySelector)
{
return source.Distinct(AnonymousComparer.Create(compareKeySelector));
}
public static IEnumerable<TSource> Except<TSource, TCompareKey>(this IEnumerable<TSource> first, IEnumerable<TSource> second, Func<TSource, TCompareKey> compareKeySelector)
{
return first.Except(second, AnonymousComparer.Create(compareKeySelector));
}
public static IEnumerable<TResult> GroupBy<TSource, TKey, TResult, TCompareKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TKey, IEnumerable<TSource>, TResult> resultSelector, Func<TKey, TCompareKey> compareKeySelector)
{
return source.GroupBy(keySelector, resultSelector, AnonymousComparer.Create(compareKeySelector));
}
public static IEnumerable<IGrouping<TKey, TElement>> GroupBy<TSource, TKey, TElement, TCompareKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, Func<TKey, TCompareKey> compareKeySelector)
{
return source.GroupBy(keySelector, elementSelector, AnonymousComparer.Create(compareKeySelector));
}
public static IEnumerable<TResult> GroupBy<TSource, TKey, TElement, TResult, TCompareKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, Func<TKey, IEnumerable<TElement>, TResult> resultSelector, Func<TKey, TCompareKey> compareKeySelector)
{
return source.GroupBy(keySelector, elementSelector, resultSelector, AnonymousComparer.Create(compareKeySelector));
}
public static IEnumerable<TResult> GroupJoin<TOuter, TInner, TKey, TResult, TCompareKey>(this IEnumerable<TOuter> outer, IEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector, Func<TInner, TKey> innerKeySelector, Func<TOuter, IEnumerable<TInner>, TResult> resultSelector, Func<TKey, TCompareKey> compareKeySelector)
{
return outer.GroupJoin(inner, outerKeySelector, innerKeySelector, resultSelector, AnonymousComparer.Create(compareKeySelector));
}
public static IEnumerable<TSource> Intersect<TSource, TCompareKey>(this IEnumerable<TSource> first, IEnumerable<TSource> second, Func<TSource, TCompareKey> compareKeySelector)
{
return first.Intersect(second, AnonymousComparer.Create(compareKeySelector));
}
public static IEnumerable<TResult> Join<TOuter, TInner, TKey, TResult, TCompareKey>(this IEnumerable<TOuter> outer, IEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector, Func<TInner, TKey> innerKeySelector, Func<TOuter, TInner, TResult> resultSelector, Func<TKey, TCompareKey> compareKeySelector)
{
return outer.Join(inner, outerKeySelector, innerKeySelector, resultSelector, AnonymousComparer.Create(compareKeySelector));
}
public static bool SequenceEqual<TSource, TCompareKey>(this IEnumerable<TSource> first, IEnumerable<TSource> second, Func<TSource, TCompareKey> compareKeySelector)
{
return first.SequenceEqual(second, AnonymousComparer.Create(compareKeySelector));
}
public static Dictionary<TKey, TElement> ToDictionary<TSource, TKey, TElement, TCompareKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, Func<TKey, TCompareKey> compareKeySelector)
{
return source.ToDictionary(keySelector, elementSelector, AnonymousComparer.Create(compareKeySelector));
}
public static ILookup<TKey, TElement> ToLookup<TSource, TKey, TElement, TCompareKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, Func<TKey, TCompareKey> compareKeySelector)
{
return source.ToLookup(keySelector, elementSelector, AnonymousComparer.Create(compareKeySelector));
}
public static IEnumerable<TSource> Union<TSource, TCompareKey>(this IEnumerable<TSource> first, IEnumerable<TSource> second, Func<TSource, TCompareKey> compareKeySelector)
{
return first.Union(second, AnonymousComparer.Create(compareKeySelector));
}
#endregion
}
}