-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Join.LoopJoin.cs
334 lines (322 loc) · 12.2 KB
/
Join.LoopJoin.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
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
namespace SuperLinq;
public static partial class SuperEnumerable
{
/// <summary>
/// Performs an inner join on two heterogeneous sequences.
/// </summary>
/// <typeparam name="TLeft">
/// The type of elements in the first sequence.
/// </typeparam>
/// <typeparam name="TRight">
/// The type of elements in the second sequence.
/// </typeparam>
/// <typeparam name="TKey">
/// The type of the key returned by the key selector functions.
/// </typeparam>
/// <param name="left">
/// The first sequence.
/// </param>
/// <param name="right">
/// The second sequence.
/// </param>
/// <param name="leftKeySelector">
/// A function to extract the join key from each element of the first sequence.
/// </param>
/// <param name="rightKeySelector">
/// A function to extract the join key from each element of the second sequence.
/// </param>
/// <param name="comparer">
/// An <see cref="IEqualityComparer{T}"/> to hash and compare keys.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="left"/>, <paramref name="right"/>, <paramref name="leftKeySelector"/>, or <paramref
/// name="rightKeySelector"/> is <see langword="null" />.
/// </exception>
/// <returns>
/// A sequence containing values from an inner join of the two input sequences.
/// </returns>
/// <remarks>
/// <para>
/// The result of this method is an `inner`-join. Values are only returned if matching elements are found in
/// both <paramref name="left"/> and <paramref name="right"/> sequences.
/// </para>
/// <para>
/// This method is implemented using a `loop`-join. The sequence <paramref name="right"/> is cached, and
/// for-each element of <paramref name="left"/>, the cached sequence is enumerated for matching elements.
/// </para>
/// <para>
/// This method uses deferred execution and streams its results.
/// </para>
/// </remarks>
public static IEnumerable<(TLeft Left, TRight Right)> InnerLoopJoin<TLeft, TRight, TKey>(
this IEnumerable<TLeft> left,
IEnumerable<TRight> right,
Func<TLeft, TKey> leftKeySelector,
Func<TRight, TKey> rightKeySelector,
IEqualityComparer<TKey>? comparer = null)
{
ArgumentNullException.ThrowIfNull(left);
ArgumentNullException.ThrowIfNull(right);
ArgumentNullException.ThrowIfNull(leftKeySelector);
ArgumentNullException.ThrowIfNull(rightKeySelector);
return LoopJoin(
left, right,
leftKeySelector, rightKeySelector,
leftResultSelector: default,
ValueTuple.Create,
comparer);
}
/// <summary>
/// Performs an inner join on two heterogeneous sequences.
/// </summary>
/// <typeparam name="TLeft">
/// The type of elements in the first sequence.
/// </typeparam>
/// <typeparam name="TRight">
/// The type of elements in the second sequence.
/// </typeparam>
/// <typeparam name="TKey">
/// The type of the key returned by the key selector functions.
/// </typeparam>
/// <typeparam name="TResult">
/// The type of the result elements.
/// </typeparam>
/// <param name="left">
/// The first sequence.
/// </param>
/// <param name="right">
/// The second sequence.
/// </param>
/// <param name="leftKeySelector">
/// A function to extract the join key from each element of the first sequence.
/// </param>
/// <param name="rightKeySelector">
/// A function to extract the join key from each element of the second sequence.
/// </param>
/// <param name="bothResultSelector">
/// A function to create a result element from two matching elements.
/// </param>
/// <param name="comparer">
/// An <see cref="IEqualityComparer{T}"/> to hash and compare keys.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="left"/>, <paramref name="right"/>, <paramref name="leftKeySelector"/>, <paramref
/// name="rightKeySelector"/>, or <paramref name="bothResultSelector"/> is <see langword="null" />.
/// </exception>
/// <returns>
/// A sequence containing results projected from an inner join of the two input sequences.
/// </returns>
/// <remarks>
/// <para>
/// The result of this method is an `inner`-join. Values are projected using <paramref
/// name="bothResultSelector"/> only if matching elements are found in both <paramref name="left"/> and
/// <paramref name="right"/> sequences.
/// </para>
/// <para>
/// This method is implemented using a `loop`-join. The sequence <paramref name="right"/> is cached, and
/// for-each element of <paramref name="left"/>, the cached sequence is enumerated for matching elements.
/// </para>
/// <para>
/// This method uses deferred execution and streams its results.
/// </para>
/// </remarks>
public static IEnumerable<TResult> InnerLoopJoin<TLeft, TRight, TKey, TResult>(
this IEnumerable<TLeft> left,
IEnumerable<TRight> right,
Func<TLeft, TKey> leftKeySelector,
Func<TRight, TKey> rightKeySelector,
Func<TLeft, TRight, TResult> bothResultSelector,
IEqualityComparer<TKey>? comparer = null)
{
ArgumentNullException.ThrowIfNull(left);
ArgumentNullException.ThrowIfNull(right);
ArgumentNullException.ThrowIfNull(leftKeySelector);
ArgumentNullException.ThrowIfNull(rightKeySelector);
ArgumentNullException.ThrowIfNull(bothResultSelector);
return LoopJoin(
left, right,
leftKeySelector, rightKeySelector,
leftResultSelector: default,
bothResultSelector,
comparer);
}
/// <summary>
/// Performs a left outer join on two heterogeneous sequences.
/// </summary>
/// <typeparam name="TLeft">
/// The type of elements in the first sequence.
/// </typeparam>
/// <typeparam name="TRight">
/// The type of elements in the second sequence.
/// </typeparam>
/// <typeparam name="TKey">
/// The type of the key returned by the key selector functions.
/// </typeparam>
/// <param name="left">
/// The first sequence.
/// </param>
/// <param name="right">
/// The second sequence.
/// </param>
/// <param name="leftKeySelector">
/// A function to extract the join key from each element of the first sequence.
/// </param>
/// <param name="rightKeySelector">
/// A function to extract the join key from each element of the second sequence.
/// </param>
/// <param name="comparer">
/// An <see cref="IEqualityComparer{T}"/> to hash and compare keys.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="left"/>, <paramref name="right"/>, <paramref name="leftKeySelector"/>, or <paramref
/// name="rightKeySelector"/> is <see langword="null" />.
/// </exception>
/// <returns>
/// A sequence containing values from a left outer join of the two input sequences.
/// </returns>
/// <remarks>
/// <para>
/// The result of this method is a `left outer`-join. All elements from <paramref name="left"/> are returned,
/// along with any matching elements from <paramref name="right"/>, if any are present. If no values in
/// <paramref name="right"/> match, <see langword="default" /> is returned for the right element.
/// </para>
/// <para>
/// This method is implemented using a `loop`-join. The sequence <paramref name="right"/> is cached, and
/// for-each element of <paramref name="left"/>, the cached sequence is enumerated for matching elements.
/// </para>
/// <para>
/// This method uses deferred execution and streams its results.
/// </para>
/// </remarks>
public static IEnumerable<(TLeft Left, TRight? Right)> LeftOuterLoopJoin<TLeft, TRight, TKey>(
this IEnumerable<TLeft> left,
IEnumerable<TRight> right,
Func<TLeft, TKey> leftKeySelector,
Func<TRight, TKey> rightKeySelector,
IEqualityComparer<TKey>? comparer = null)
{
ArgumentNullException.ThrowIfNull(left);
ArgumentNullException.ThrowIfNull(right);
ArgumentNullException.ThrowIfNull(leftKeySelector);
ArgumentNullException.ThrowIfNull(rightKeySelector);
return LoopJoin<TLeft, TRight, TKey, (TLeft, TRight?)>(
left, right,
leftKeySelector, rightKeySelector,
static left => (left, default),
ValueTuple.Create,
comparer);
}
/// <summary>
/// Performs a left outer join on two heterogeneous sequences.
/// </summary>
/// <typeparam name="TLeft">
/// The type of elements in the first sequence.
/// </typeparam>
/// <typeparam name="TRight">
/// The type of elements in the second sequence.
/// </typeparam>
/// <typeparam name="TKey">
/// The type of the key returned by the key selector functions.
/// </typeparam>
/// <typeparam name="TResult">
/// The type of the result elements.
/// </typeparam>
/// <param name="left">
/// The first sequence.
/// </param>
/// <param name="right">
/// The second sequence.
/// </param>
/// <param name="leftKeySelector">
/// A function to extract the join key from each element of the first sequence.
/// </param>
/// <param name="rightKeySelector">
/// A function to extract the join key from each element of the second sequence.
/// </param>
/// <param name="leftResultSelector">
/// A function to create a result element from a <paramref name="left"/> value that does not have a matching
/// value in <paramref name="right"/>.
/// </param>
/// <param name="bothResultSelector">
/// A function to create a result element from two matching elements.
/// </param>
/// <param name="comparer">
/// An <see cref="IEqualityComparer{T}"/> to hash and compare keys.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="left"/>, <paramref name="right"/>, <paramref name="leftKeySelector"/>, <paramref
/// name="rightKeySelector"/>, <paramref name="leftResultSelector"/>, or <paramref name="bothResultSelector"/>
/// is <see langword="null" />.
/// </exception>
/// <returns>
/// A sequence containing values projected from a left outer join of the two input sequences.
/// </returns>
/// <remarks>
/// <para>
/// The result of this method is a `left outer`-join. Values are projected using <paramref
/// name="bothResultSelector"/> when matching elements are found in both <paramref name="left"/> and <paramref
/// name="right"/> sequences. Values are projected using <paramref name="leftKeySelector"/> when elements in
/// <paramref name="left"/> do not have a matching element in <paramref name="right"/>.
/// </para>
/// <para>
/// This method is implemented using a `loop`-join. The sequence <paramref name="right"/> is cached, and
/// for-each element of <paramref name="left"/>, the cached sequence is enumerated for matching elements.
/// </para>
/// <para>
/// This method uses deferred execution and streams its results.
/// </para>
/// </remarks>
public static IEnumerable<TResult> LeftOuterLoopJoin<TLeft, TRight, TKey, TResult>(
this IEnumerable<TLeft> left,
IEnumerable<TRight> right,
Func<TLeft, TKey> leftKeySelector,
Func<TRight, TKey> rightKeySelector,
Func<TLeft, TResult> leftResultSelector,
Func<TLeft, TRight, TResult> bothResultSelector,
IEqualityComparer<TKey>? comparer = null)
{
ArgumentNullException.ThrowIfNull(left);
ArgumentNullException.ThrowIfNull(right);
ArgumentNullException.ThrowIfNull(leftKeySelector);
ArgumentNullException.ThrowIfNull(rightKeySelector);
ArgumentNullException.ThrowIfNull(leftResultSelector);
ArgumentNullException.ThrowIfNull(bothResultSelector);
return LoopJoin(
left, right,
leftKeySelector, rightKeySelector,
leftResultSelector,
bothResultSelector,
comparer);
}
private static IEnumerable<TResult> LoopJoin<TLeft, TRight, TKey, TResult>(
this IEnumerable<TLeft> left,
IEnumerable<TRight> right,
Func<TLeft, TKey> leftKeySelector,
Func<TRight, TKey> rightKeySelector,
Func<TLeft, TResult>? leftResultSelector,
Func<TLeft, TRight, TResult> bothResultSelector,
IEqualityComparer<TKey>? comparer)
{
comparer ??= EqualityComparer<TKey>.Default;
var rList = right as IList<TRight> ?? right.ToList();
foreach (var l in left)
{
var lKey = leftKeySelector(l);
var flag = false;
foreach (var r in rList)
{
var rKey = rightKeySelector(r);
if (comparer.Equals(lKey, rKey))
{
flag = true;
yield return bothResultSelector(l, r);
}
}
if (leftResultSelector is not null
&& !flag)
{
yield return leftResultSelector(l);
}
}
}
}