-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
FullGroupJoin.cs
233 lines (224 loc) · 9.43 KB
/
FullGroupJoin.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
namespace SuperLinq;
// Inspiration & credit: http://stackoverflow.com/a/13503860/6682
public static partial class SuperEnumerable
{
/// <summary>
/// Performs a Full Group Join between the <paramref name="first"/> and <paramref name="second"/> sequences.
/// </summary>
/// <typeparam name="TFirst">
/// The type of the elements in the first input sequence
/// </typeparam>
/// <typeparam name="TSecond">
/// The type of the elements in the second input sequence
/// </typeparam>
/// <typeparam name="TKey">
/// The type of the key to use to join
/// </typeparam>
/// <param name="first">
/// First sequence
/// </param>
/// <param name="second">
/// Second sequence
/// </param>
/// <param name="firstKeySelector">
/// The mapping from first sequence to key
/// </param>
/// <param name="secondKeySelector">
/// The mapping from second sequence to key
/// </param>
/// <returns>
/// A sequence of elements joined from <paramref name="first"/> and <paramref name="second"/>.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="first"/>, <paramref name="second"/>, <paramref name="firstKeySelector"/>, or <paramref
/// name="secondKeySelector"/> is <see langword="null"/>.
/// </exception>
/// <remarks>
/// This operator uses deferred execution and streams the results. The results are yielded in the order of the
/// elements found in the first sequence followed by those found only in the second. In addition, the callback
/// responsible for projecting the results is supplied with sequences which preserve their source order.
/// </remarks>
public static IEnumerable<(TKey Key, IEnumerable<TFirst> First, IEnumerable<TSecond> Second)> FullGroupJoin<TFirst, TSecond, TKey>(
this IEnumerable<TFirst> first,
IEnumerable<TSecond> second,
Func<TFirst, TKey> firstKeySelector,
Func<TSecond, TKey> secondKeySelector)
{
return FullGroupJoin(first, second, firstKeySelector, secondKeySelector, ValueTuple.Create, EqualityComparer<TKey>.Default);
}
/// <summary>
/// Performs a Full Group Join between the <paramref name="first"/> and <paramref name="second"/> sequences.
/// </summary>
/// <typeparam name="TFirst">
/// The type of the elements in the first input sequence
/// </typeparam>
/// <typeparam name="TSecond">
/// The type of the elements in the second input sequence
/// </typeparam>
/// <typeparam name="TKey">
/// The type of the key to use to join
/// </typeparam>
/// <param name="first">
/// First sequence
/// </param>
/// <param name="second">
/// Second sequence
/// </param>
/// <param name="firstKeySelector">
/// The mapping from first sequence to key
/// </param>
/// <param name="secondKeySelector">
/// The mapping from second sequence to key
/// </param>
/// <param name="comparer">
/// The equality comparer to use to determine whether or not keys are equal. If <see langword="null"/>, the
/// default equality comparer for <typeparamref name="TKey"/> is used.
/// </param>
/// <returns>
/// A sequence of elements joined from <paramref name="first"/> and <paramref name="second"/>.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="first"/>, <paramref name="second"/>, <paramref name="firstKeySelector"/>, or <paramref
/// name="secondKeySelector"/> is <see langword="null"/>.
/// </exception>
/// <remarks>
/// This operator uses deferred execution and streams the results. The results are yielded in the order of the
/// elements found in the first sequence followed by those found only in the second. In addition, the callback
/// responsible for projecting the results is supplied with sequences which preserve their source order.
/// </remarks>
public static IEnumerable<(TKey Key, IEnumerable<TFirst> First, IEnumerable<TSecond> Second)> FullGroupJoin<TFirst, TSecond, TKey>(
this IEnumerable<TFirst> first,
IEnumerable<TSecond> second,
Func<TFirst, TKey> firstKeySelector,
Func<TSecond, TKey> secondKeySelector,
IEqualityComparer<TKey>? comparer)
{
return FullGroupJoin(first, second, firstKeySelector, secondKeySelector, ValueTuple.Create, comparer);
}
/// <summary>
/// Performs a full group-join between two sequences.
/// </summary>
/// <typeparam name="TFirst">
/// The type of the elements in the first input sequence
/// </typeparam>
/// <typeparam name="TSecond">
/// The type of the elements in the second input sequence
/// </typeparam>
/// <typeparam name="TKey">
/// The type of the key to use to join
/// </typeparam>
/// <typeparam name="TResult">
/// The type of the elements of the resulting sequence
/// </typeparam>
/// <param name="first">
/// First sequence
/// </param>
/// <param name="second">
/// Second sequence
/// </param>
/// <param name="firstKeySelector">
/// The mapping from first sequence to key
/// </param>
/// <param name="secondKeySelector">
/// The mapping from second sequence to key
/// </param>
/// <param name="resultSelector">
/// Function to apply to each pair of elements plus the key
/// </param>
/// <returns>
/// A sequence of elements joined from <paramref name="first"/> and <paramref name="second"/>.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="first"/>, <paramref name="second"/>, <paramref name="firstKeySelector"/>, <paramref
/// name="secondKeySelector"/>, or <paramref name="resultSelector"/> is <see langword="null"/>.
/// </exception>
/// <remarks>
/// This operator uses deferred execution and streams the results. The results are yielded in the order of the
/// elements found in the first sequence followed by those found only in the second. In addition, the callback
/// responsible for projecting the results is supplied with sequences which preserve their source order.
/// </remarks>
public static IEnumerable<TResult> FullGroupJoin<TFirst, TSecond, TKey, TResult>(
this IEnumerable<TFirst> first,
IEnumerable<TSecond> second,
Func<TFirst, TKey> firstKeySelector,
Func<TSecond, TKey> secondKeySelector,
Func<TKey, IEnumerable<TFirst>, IEnumerable<TSecond>, TResult> resultSelector)
{
return FullGroupJoin(first, second, firstKeySelector, secondKeySelector, resultSelector, EqualityComparer<TKey>.Default);
}
/// <summary>
/// Performs a full group-join between two sequences.
/// </summary>
/// <typeparam name="TFirst">
/// The type of the elements in the first input sequence
/// </typeparam>
/// <typeparam name="TSecond">
/// The type of the elements in the second input sequence
/// </typeparam>
/// <typeparam name="TKey">
/// The type of the key to use to join
/// </typeparam>
/// <typeparam name="TResult">
/// The type of the elements of the resulting sequence
/// </typeparam>
/// <param name="first">
/// First sequence
/// </param>
/// <param name="second">
/// Second sequence
/// </param>
/// <param name="firstKeySelector">
/// The mapping from first sequence to key
/// </param>
/// <param name="secondKeySelector">
/// The mapping from second sequence to key
/// </param>
/// <param name="resultSelector">
/// Function to apply to each pair of elements plus the key
/// </param>
/// <param name="comparer">
/// The equality comparer to use to determine whether or not keys are equal. If <see langword="null"/>, the
/// default equality comparer for <typeparamref name="TKey"/> is used.
/// </param>
/// <returns>
/// A sequence of elements joined from <paramref name="first"/> and <paramref name="second"/>.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="first"/>, <paramref name="second"/>, <paramref name="firstKeySelector"/>, <paramref
/// name="secondKeySelector"/>, or <paramref name="resultSelector"/> is <see langword="null"/>.
/// </exception>
/// <remarks>
/// This operator uses deferred execution and streams the results. The results are yielded in the order of the
/// elements found in the first sequence followed by those found only in the second. In addition, the callback
/// responsible for projecting the results is supplied with sequences which preserve their source order.
/// </remarks>
public static IEnumerable<TResult> FullGroupJoin<TFirst, TSecond, TKey, TResult>(
this IEnumerable<TFirst> first,
IEnumerable<TSecond> second,
Func<TFirst, TKey> firstKeySelector,
Func<TSecond, TKey> secondKeySelector,
Func<TKey, IEnumerable<TFirst>, IEnumerable<TSecond>, TResult> resultSelector,
IEqualityComparer<TKey>? comparer)
{
ArgumentNullException.ThrowIfNull(first);
ArgumentNullException.ThrowIfNull(second);
ArgumentNullException.ThrowIfNull(firstKeySelector);
ArgumentNullException.ThrowIfNull(secondKeySelector);
ArgumentNullException.ThrowIfNull(resultSelector);
return Core(comparer ?? EqualityComparer<TKey>.Default);
IEnumerable<TResult> Core(IEqualityComparer<TKey> comparer)
{
var alookup = Lookup<TKey, TFirst>.CreateForJoin(first, firstKeySelector, comparer);
var blookup = Lookup<TKey, TSecond>.CreateForJoin(second, secondKeySelector, comparer);
foreach (var a in alookup)
yield return resultSelector(a.Key, a, blookup[a.Key]);
foreach (var b in blookup)
{
if (alookup.Contains(b.Key))
continue;
// We can skip the lookup because we are iterating over keys not found in the first sequence
yield return resultSelector(b.Key, [], b);
}
}
}
}