| 1 |
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. |
| 2 |
using System; |
| 3 |
using System.Collections.Generic; |
| 4 |
using System.Linq; |
| 5 |
using System.Threading.Tasks; |
| 6 |
using System.Threading; |
| 7 |
|
| 8 |
namespace System.Linq |
| 9 |
{ |
| 10 |
public static partial class AsyncEnumerable |
| 11 |
{ |
| 12 |
public static IAsyncEnumerable<TSource> Concat<TSource>(this IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second) |
| 13 |
{ |
| 14 |
if (first == null) |
| 15 |
throw new ArgumentNullException("first"); |
| 16 |
if (second == null) |
| 17 |
throw new ArgumentNullException("second"); |
| 18 |
|
| 19 |
return Create(() => |
| 20 |
{ |
| 21 |
var switched = false; |
| 22 |
var e = first.GetEnumerator(); |
| 23 |
|
| 24 |
var cts = new CancellationTokenDisposable(); |
| 25 |
var a = new AssignableDisposable { Disposable = e }; |
| 26 |
var d = new CompositeDisposable(cts, a); |
| 27 |
|
| 28 |
var f = default(Action<TaskCompletionSource<bool>, CancellationToken>); |
| 29 |
f = (tcs, ct) => e.MoveNext(ct).ContinueWith(t => |
| 30 |
{ |
| 31 |
t.Handle(tcs, res => |
| 32 |
{ |
| 33 |
if (res) |
| 34 |
{ |
| 35 |
tcs.TrySetResult(true); |
| 36 |
} |
| 37 |
else |
| 38 |
{ |
| 39 |
if (switched) |
| 40 |
{ |
| 41 |
tcs.TrySetResult(false); |
| 42 |
} |
| 43 |
else |
| 44 |
{ |
| 45 |
switched = true; |
| 46 |
|
| 47 |
e = second.GetEnumerator(); |
| 48 |
a.Disposable = e; |
| 49 |
|
| 50 |
f(tcs, ct); |
| 51 |
} |
| 52 |
} |
| 53 |
}); |
| 54 |
}); |
| 55 |
|
| 56 |
return Create( |
| 57 |
(ct, tcs) => |
| 58 |
{ |
| 59 |
f(tcs, cts.Token); |
| 60 |
return tcs.Task.UsingEnumerator(a); |
| 61 |
}, |
| 62 |
() => e.Current, |
| 63 |
d.Dispose |
| 64 |
); |
| 65 |
}); |
| 66 |
} |
| 67 |
|
| 68 |
public static IAsyncEnumerable<TResult> Zip<TFirst, TSecond, TResult>(this IAsyncEnumerable<TFirst> first, IAsyncEnumerable<TSecond> second, Func<TFirst, TSecond, TResult> selector) |
| 69 |
{ |
| 70 |
if (first == null) |
| 71 |
throw new ArgumentNullException("first"); |
| 72 |
if (second == null) |
| 73 |
throw new ArgumentNullException("second"); |
| 74 |
if (selector == null) |
| 75 |
throw new ArgumentNullException("selector"); |
| 76 |
|
| 77 |
return Create(() => |
| 78 |
{ |
| 79 |
var e1 = first.GetEnumerator(); |
| 80 |
var e2 = second.GetEnumerator(); |
| 81 |
var current = default(TResult); |
| 82 |
|
| 83 |
var cts = new CancellationTokenDisposable(); |
| 84 |
var d = new CompositeDisposable(cts, e1, e2); |
| 85 |
|
| 86 |
return Create( |
| 87 |
(ct, tcs) => |
| 88 |
{ |
| 89 |
e1.MoveNext(cts.Token).Zip(e2.MoveNext(cts.Token), (f, s) => |
| 90 |
{ |
| 91 |
var result = f && s; |
| 92 |
if (result) |
| 93 |
current = selector(e1.Current, e2.Current); |
| 94 |
return result; |
| 95 |
}).ContinueWith(t => |
| 96 |
{ |
| 97 |
t.Handle(tcs, x => tcs.TrySetResult(x)); |
| 98 |
}); |
| 99 |
|
| 100 |
return tcs.Task.UsingEnumerator(e1).UsingEnumerator(e2); |
| 101 |
}, |
| 102 |
() => current, |
| 103 |
d.Dispose |
| 104 |
); |
| 105 |
}); |
| 106 |
} |
| 107 |
|
| 108 |
public static IAsyncEnumerable<TSource> Except<TSource>(this IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second, IEqualityComparer<TSource> comparer) |
| 109 |
{ |
| 110 |
if (first == null) |
| 111 |
throw new ArgumentNullException("first"); |
| 112 |
if (second == null) |
| 113 |
throw new ArgumentNullException("second"); |
| 114 |
if (comparer == null) |
| 115 |
throw new ArgumentNullException("comparer"); |
| 116 |
|
| 117 |
return Create(() => |
| 118 |
{ |
| 119 |
var e = first.GetEnumerator(); |
| 120 |
|
| 121 |
var cts = new CancellationTokenDisposable(); |
| 122 |
var d = new CompositeDisposable(cts, e); |
| 123 |
|
| 124 |
var mapTask = default(Task<Dictionary<TSource, TSource>>); |
| 125 |
var getMapTask = new Func<CancellationToken, Task<Dictionary<TSource, TSource>>>(ct => |
| 126 |
{ |
| 127 |
if (mapTask == null) |
| 128 |
mapTask = second.ToDictionary(x => x, comparer, ct); |
| 129 |
return mapTask; |
| 130 |
}); |
| 131 |
|
| 132 |
var f = default(Action<TaskCompletionSource<bool>, CancellationToken>); |
| 133 |
f = (tcs, ct) => |
| 134 |
{ |
| 135 |
e.MoveNext(ct).Zip(getMapTask(ct), (b, _) => b).ContinueWith(t => |
| 136 |
{ |
| 137 |
t.Handle(tcs, res => |
| 138 |
{ |
| 139 |
if (res) |
| 140 |
{ |
| 141 |
if (!mapTask.Result.ContainsKey(e.Current)) |
| 142 |
tcs.TrySetResult(true); |
| 143 |
else |
| 144 |
f(tcs, ct); |
| 145 |
} |
| 146 |
else |
| 147 |
tcs.TrySetResult(false); |
| 148 |
}); |
| 149 |
}); |
| 150 |
}; |
| 151 |
|
| 152 |
return Create( |
| 153 |
(ct, tcs) => |
| 154 |
{ |
| 155 |
f(tcs, cts.Token); |
| 156 |
return tcs.Task.UsingEnumerator(e); |
| 157 |
}, |
| 158 |
() => e.Current, |
| 159 |
d.Dispose |
| 160 |
); |
| 161 |
}); |
| 162 |
} |
| 163 |
|
| 164 |
public static IAsyncEnumerable<TSource> Except<TSource>(this IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second) |
| 165 |
{ |
| 166 |
if (first == null) |
| 167 |
throw new ArgumentNullException("first"); |
| 168 |
if (second == null) |
| 169 |
throw new ArgumentNullException("second"); |
| 170 |
|
| 171 |
return first.Except(second, EqualityComparer<TSource>.Default); |
| 172 |
} |
| 173 |
|
| 174 |
public static IAsyncEnumerable<TSource> Intersect<TSource>(this IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second, IEqualityComparer<TSource> comparer) |
| 175 |
{ |
| 176 |
if (first == null) |
| 177 |
throw new ArgumentNullException("first"); |
| 178 |
if (second == null) |
| 179 |
throw new ArgumentNullException("second"); |
| 180 |
if (comparer == null) |
| 181 |
throw new ArgumentNullException("comparer"); |
| 182 |
|
| 183 |
return Create(() => |
| 184 |
{ |
| 185 |
var e = first.GetEnumerator(); |
| 186 |
|
| 187 |
var cts = new CancellationTokenDisposable(); |
| 188 |
var d = new CompositeDisposable(cts, e); |
| 189 |
|
| 190 |
var mapTask = default(Task<Dictionary<TSource, TSource>>); |
| 191 |
var getMapTask = new Func<CancellationToken, Task<Dictionary<TSource, TSource>>>(ct => |
| 192 |
{ |
| 193 |
if (mapTask == null) |
| 194 |
mapTask = second.ToDictionary(x => x, comparer, ct); |
| 195 |
return mapTask; |
| 196 |
}); |
| 197 |
|
| 198 |
var f = default(Action<TaskCompletionSource<bool>, CancellationToken>); |
| 199 |
f = (tcs, ct) => |
| 200 |
{ |
| 201 |
e.MoveNext(ct).Zip(getMapTask(ct), (b, _) => b).ContinueWith(t => |
| 202 |
{ |
| 203 |
t.Handle(tcs, res => |
| 204 |
{ |
| 205 |
if (res) |
| 206 |
{ |
| 207 |
if (mapTask.Result.ContainsKey(e.Current)) |
| 208 |
tcs.TrySetResult(true); |
| 209 |
else |
| 210 |
f(tcs, ct); |
| 211 |
} |
| 212 |
else |
| 213 |
tcs.TrySetResult(false); |
| 214 |
}); |
| 215 |
}); |
| 216 |
}; |
| 217 |
|
| 218 |
return Create( |
| 219 |
(ct, tcs) => |
| 220 |
{ |
| 221 |
f(tcs, cts.Token); |
| 222 |
return tcs.Task.UsingEnumerator(e); |
| 223 |
}, |
| 224 |
() => e.Current, |
| 225 |
d.Dispose |
| 226 |
); |
| 227 |
}); |
| 228 |
} |
| 229 |
|
| 230 |
public static IAsyncEnumerable<TSource> Intersect<TSource>(this IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second) |
| 231 |
{ |
| 232 |
if (first == null) |
| 233 |
throw new ArgumentNullException("first"); |
| 234 |
if (second == null) |
| 235 |
throw new ArgumentNullException("second"); |
| 236 |
|
| 237 |
return first.Intersect(second, EqualityComparer<TSource>.Default); |
| 238 |
} |
| 239 |
|
| 240 |
public static IAsyncEnumerable<TSource> Union<TSource>(this IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second, IEqualityComparer<TSource> comparer) |
| 241 |
{ |
| 242 |
if (first == null) |
| 243 |
throw new ArgumentNullException("first"); |
| 244 |
if (second == null) |
| 245 |
throw new ArgumentNullException("second"); |
| 246 |
if (comparer == null) |
| 247 |
throw new ArgumentNullException("comparer"); |
| 248 |
|
| 249 |
return first.Concat(second).Distinct(comparer); |
| 250 |
} |
| 251 |
|
| 252 |
public static IAsyncEnumerable<TSource> Union<TSource>(this IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second) |
| 253 |
{ |
| 254 |
if (first == null) |
| 255 |
throw new ArgumentNullException("first"); |
| 256 |
if (second == null) |
| 257 |
throw new ArgumentNullException("second"); |
| 258 |
|
| 259 |
return first.Union(second, EqualityComparer<TSource>.Default); |
| 260 |
} |
| 261 |
|
| 262 |
public static Task<bool> SequenceEqual<TSource>(this IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second, IEqualityComparer<TSource> comparer, CancellationToken cancellationToken) |
| 263 |
{ |
| 264 |
if (first == null) |
| 265 |
throw new ArgumentNullException("first"); |
| 266 |
if (second == null) |
| 267 |
throw new ArgumentNullException("second"); |
| 268 |
if (comparer == null) |
| 269 |
throw new ArgumentNullException("comparer"); |
| 270 |
|
| 271 |
var tcs = new TaskCompletionSource<bool>(); |
| 272 |
|
| 273 |
var e1 = first.GetEnumerator(); |
| 274 |
var e2 = second.GetEnumerator(); |
| 275 |
|
| 276 |
var run = default(Action<CancellationToken>); |
| 277 |
run = ct => |
| 278 |
{ |
| 279 |
e1.MoveNext(ct).Zip(e2.MoveNext(ct), (f, s) => |
| 280 |
{ |
| 281 |
if (f ^ s) |
| 282 |
{ |
| 283 |
tcs.TrySetResult(false); |
| 284 |
return false; |
| 285 |
} |
| 286 |
|
| 287 |
if (f && s) |
| 288 |
{ |
| 289 |
var eq = default(bool); |
| 290 |
try |
| 291 |
{ |
| 292 |
eq = comparer.Equals(e1.Current, e2.Current); |
| 293 |
} |
| 294 |
catch (Exception ex) |
| 295 |
{ |
| 296 |
tcs.TrySetException(ex); |
| 297 |
return false; |
| 298 |
} |
| 299 |
|
| 300 |
if (!eq) |
| 301 |
{ |
| 302 |
tcs.TrySetResult(false); |
| 303 |
return false; |
| 304 |
} |
| 305 |
else |
| 306 |
return true; |
| 307 |
} |
| 308 |
else |
| 309 |
{ |
| 310 |
tcs.TrySetResult(true); |
| 311 |
return false; |
| 312 |
} |
| 313 |
}).ContinueWith(t => |
| 314 |
{ |
| 315 |
t.Handle(tcs, res => |
| 316 |
{ |
| 317 |
if (res) |
| 318 |
run(ct); |
| 319 |
}); |
| 320 |
}); |
| 321 |
}; |
| 322 |
|
| 323 |
run(cancellationToken); |
| 324 |
|
| 325 |
return tcs.Task.Finally(() => |
| 326 |
{ |
| 327 |
e1.Dispose(); |
| 328 |
e2.Dispose(); |
| 329 |
}); |
| 330 |
} |
| 331 |
|
| 332 |
public static Task<bool> SequenceEqual<TSource>(this IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second, CancellationToken cancellationToken) |
| 333 |
{ |
| 334 |
if (first == null) |
| 335 |
throw new ArgumentNullException("first"); |
| 336 |
if (second == null) |
| 337 |
throw new ArgumentNullException("second"); |
| 338 |
|
| 339 |
return first.SequenceEqual(second, EqualityComparer<TSource>.Default, cancellationToken); |
| 340 |
} |
| 341 |
|
| 342 |
public static IAsyncEnumerable<TResult> GroupJoin<TOuter, TInner, TKey, TResult>(this IAsyncEnumerable<TOuter> outer, IAsyncEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector, Func<TInner, TKey> innerKeySelector, Func<TOuter, IAsyncEnumerable<TInner>, TResult> resultSelector, IEqualityComparer<TKey> comparer) |
| 343 |
{ |
| 344 |
if (outer == null) |
| 345 |
throw new ArgumentNullException("outer"); |
| 346 |
if (inner == null) |
| 347 |
throw new ArgumentNullException("inner"); |
| 348 |
if (outerKeySelector == null) |
| 349 |
throw new ArgumentNullException("outerKeySelector"); |
| 350 |
if (innerKeySelector == null) |
| 351 |
throw new ArgumentNullException("innerKeySelector"); |
| 352 |
if (resultSelector == null) |
| 353 |
throw new ArgumentNullException("resultSelector"); |
| 354 |
if (comparer == null) |
| 355 |
throw new ArgumentNullException("comparer"); |
| 356 |
|
| 357 |
return Create(() => |
| 358 |
{ |
| 359 |
var innerMap = default(Task<ILookup<TKey, TInner>>); |
| 360 |
var getInnerMap = new Func<CancellationToken, Task<ILookup<TKey, TInner>>>(ct => |
| 361 |
{ |
| 362 |
if (innerMap == null) |
| 363 |
innerMap = inner.ToLookup(innerKeySelector, comparer, ct); |
| 364 |
|
| 365 |
return innerMap; |
| 366 |
}); |
| 367 |
|
| 368 |
var outerE = outer.GetEnumerator(); |
| 369 |
var current = default(TResult); |
| 370 |
|
| 371 |
var cts = new CancellationTokenDisposable(); |
| 372 |
var d = new CompositeDisposable(cts, outerE); |
| 373 |
|
| 374 |
var f = default(Action<TaskCompletionSource<bool>, CancellationToken>); |
| 375 |
f = (tcs, ct) => |
| 376 |
{ |
| 377 |
getInnerMap(ct).ContinueWith(ti => |
| 378 |
{ |
| 379 |
ti.Handle(tcs, map => |
| 380 |
{ |
| 381 |
outerE.MoveNext(ct).ContinueWith(to => |
| 382 |
{ |
| 383 |
to.Handle(tcs, res => |
| 384 |
{ |
| 385 |
if (res) |
| 386 |
{ |
| 387 |
var element = outerE.Current; |
| 388 |
var key = default(TKey); |
| 389 |
|
| 390 |
try |
| 391 |
{ |
| 392 |
key = outerKeySelector(element); |
| 393 |
} |
| 394 |
catch (Exception ex) |
| 395 |
{ |
| 396 |
tcs.TrySetException(ex); |
| 397 |
return; |
| 398 |
} |
| 399 |
|
| 400 |
var innerE = default(IAsyncEnumerable<TInner>); |
| 401 |
if (!map.Contains(key)) |
| 402 |
innerE = AsyncEnumerable.Empty<TInner>(); |
| 403 |
else |
| 404 |
innerE = map[key].ToAsyncEnumerable(); |
| 405 |
|
| 406 |
try |
| 407 |
{ |
| 408 |
current = resultSelector(element, innerE); |
| 409 |
} |
| 410 |
catch (Exception ex) |
| 411 |
{ |
| 412 |
tcs.TrySetException(ex); |
| 413 |
return; |
| 414 |
} |
| 415 |
|
| 416 |
tcs.TrySetResult(true); |
| 417 |
} |
| 418 |
else |
| 419 |
{ |
| 420 |
tcs.TrySetResult(false); |
| 421 |
} |
| 422 |
}); |
| 423 |
}); |
| 424 |
}); |
| 425 |
}); |
| 426 |
}; |
| 427 |
|
| 428 |
return Create( |
| 429 |
(ct, tcs) => |
| 430 |
{ |
| 431 |
f(tcs, cts.Token); |
| 432 |
return tcs.Task.UsingEnumerator(outerE); |
| 433 |
}, |
| 434 |
() => current, |
| 435 |
d.Dispose |
| 436 |
); |
| 437 |
}); |
| 438 |
} |
| 439 |
|
| 440 |
public static IAsyncEnumerable<TResult> GroupJoin<TOuter, TInner, TKey, TResult>(this IAsyncEnumerable<TOuter> outer, IAsyncEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector, Func<TInner, TKey> innerKeySelector, Func<TOuter, IAsyncEnumerable<TInner>, TResult> resultSelector) |
| 441 |
{ |
| 442 |
if (outer == null) |
| 443 |
throw new ArgumentNullException("outer"); |
| 444 |
if (inner == null) |
| 445 |
throw new ArgumentNullException("inner"); |
| 446 |
if (outerKeySelector == null) |
| 447 |
throw new ArgumentNullException("outerKeySelector"); |
| 448 |
if (innerKeySelector == null) |
| 449 |
throw new ArgumentNullException("innerKeySelector"); |
| 450 |
if (resultSelector == null) |
| 451 |
throw new ArgumentNullException("resultSelector"); |
| 452 |
|
| 453 |
return outer.GroupJoin(inner, outerKeySelector, innerKeySelector, resultSelector, EqualityComparer<TKey>.Default); |
| 454 |
} |
| 455 |
|
| 456 |
public static IAsyncEnumerable<TResult> Join<TOuter, TInner, TKey, TResult>(this IAsyncEnumerable<TOuter> outer, IAsyncEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector, Func<TInner, TKey> innerKeySelector, Func<TOuter, TInner, TResult> resultSelector, IEqualityComparer<TKey> comparer) |
| 457 |
{ |
| 458 |
if (outer == null) |
| 459 |
throw new ArgumentNullException("outer"); |
| 460 |
if (inner == null) |
| 461 |
throw new ArgumentNullException("inner"); |
| 462 |
if (outerKeySelector == null) |
| 463 |
throw new ArgumentNullException("outerKeySelector"); |
| 464 |
if (innerKeySelector == null) |
| 465 |
throw new ArgumentNullException("innerKeySelector"); |
| 466 |
if (resultSelector == null) |
| 467 |
throw new ArgumentNullException("resultSelector"); |
| 468 |
if (comparer == null) |
| 469 |
throw new ArgumentNullException("comparer"); |
| 470 |
|
| 471 |
return Create(() => |
| 472 |
{ |
| 473 |
var oe = outer.GetEnumerator(); |
| 474 |
var ie = inner.GetEnumerator(); |
| 475 |
|
| 476 |
var cts = new CancellationTokenDisposable(); |
| 477 |
var d = new CompositeDisposable(cts, oe, ie); |
| 478 |
|
| 479 |
var current = default(TResult); |
| 480 |
var useOuter = true; |
| 481 |
var outerMap = new Dictionary<TKey, List<TOuter>>(comparer); |
| 482 |
var innerMap = new Dictionary<TKey, List<TInner>>(comparer); |
| 483 |
var q = new Queue<TResult>(); |
| 484 |
|
| 485 |
var gate = new object(); |
| 486 |
|
| 487 |
var f = default(Action<TaskCompletionSource<bool>, CancellationToken>); |
| 488 |
f = (tcs, ct) => |
| 489 |
{ |
| 490 |
if (q.Count > 0) |
| 491 |
{ |
| 492 |
current = q.Dequeue(); |
| 493 |
tcs.TrySetResult(true); |
| 494 |
return; |
| 495 |
} |
| 496 |
|
| 497 |
var b = useOuter; |
| 498 |
if (ie == null && oe == null) |
| 499 |
{ |
| 500 |
tcs.TrySetResult(false); |
| 501 |
return; |
| 502 |
} |
| 503 |
else if (ie == null) |
| 504 |
b = true; |
| 505 |
else if (oe == null) |
| 506 |
b = false; |
| 507 |
useOuter = !useOuter; |
| 508 |
|
| 509 |
var enqueue = new Func<TOuter, TInner, bool>((o, i) => |
| 510 |
{ |
| 511 |
var result = default(TResult); |
| 512 |
try |
| 513 |
{ |
| 514 |
result = resultSelector(o, i); |
| 515 |
} |
| 516 |
catch (Exception exception) |
| 517 |
{ |
| 518 |
tcs.TrySetException(exception); |
| 519 |
return false; |
| 520 |
} |
| 521 |
|
| 522 |
q.Enqueue(result); |
| 523 |
return true; |
| 524 |
}); |
| 525 |
|
| 526 |
if (b) |
| 527 |
oe.MoveNext(ct).ContinueWith(t => |
| 528 |
{ |
| 529 |
t.Handle(tcs, res => |
| 530 |
{ |
| 531 |
if (res) |
| 532 |
{ |
| 533 |
var element = oe.Current; |
| 534 |
var key = default(TKey); |
| 535 |
|
| 536 |
try |
| 537 |
{ |
| 538 |
key = outerKeySelector(element); |
| 539 |
} |
| 540 |
catch (Exception exception) |
| 541 |
{ |
| 542 |
tcs.TrySetException(exception); |
| 543 |
return; |
| 544 |
} |
| 545 |
|
| 546 |
var outerList = default(List<TOuter>); |
| 547 |
if (!outerMap.TryGetValue(key, out outerList)) |
| 548 |
{ |
| 549 |
outerList = new List<TOuter>(); |
| 550 |
outerMap.Add(key, outerList); |
| 551 |
} |
| 552 |
|
| 553 |
outerList.Add(element); |
| 554 |
|
| 555 |
var innerList = default(List<TInner>); |
| 556 |
if (!innerMap.TryGetValue(key, out innerList)) |
| 557 |
{ |
| 558 |
innerList = new List<TInner>(); |
| 559 |
innerMap.Add(key, innerList); |
| 560 |
} |
| 561 |
|
| 562 |
foreach (var v in innerList) |
| 563 |
{ |
| 564 |
if (!enqueue(element, v)) |
| 565 |
return; |
| 566 |
} |
| 567 |
|
| 568 |
f(tcs, ct); |
| 569 |
} |
| 570 |
else |
| 571 |
{ |
| 572 |
oe.Dispose(); |
| 573 |
oe = null; |
| 574 |
f(tcs, ct); |
| 575 |
} |
| 576 |
}); |
| 577 |
}); |
| 578 |
else |
| 579 |
ie.MoveNext(ct).ContinueWith(t => |
| 580 |
{ |
| 581 |
t.Handle(tcs, res => |
| 582 |
{ |
| 583 |
if (res) |
| 584 |
{ |
| 585 |
var element = ie.Current; |
| 586 |
var key = default(TKey); |
| 587 |
|
| 588 |
try |
| 589 |
{ |
| 590 |
key = innerKeySelector(element); |
| 591 |
} |
| 592 |
catch (Exception exception) |
| 593 |
{ |
| 594 |
tcs.TrySetException(exception); |
| 595 |
return; |
| 596 |
} |
| 597 |
|
| 598 |
var innerList = default(List<TInner>); |
| 599 |
if (!innerMap.TryGetValue(key, out innerList)) |
| 600 |
{ |
| 601 |
innerList = new List<TInner>(); |
| 602 |
innerMap.Add(key, innerList); |
| 603 |
} |
| 604 |
|
| 605 |
innerList.Add(element); |
| 606 |
|
| 607 |
var outerList = default(List<TOuter>); |
| 608 |
if (!outerMap.TryGetValue(key, out outerList)) |
| 609 |
{ |
| 610 |
outerList = new List<TOuter>(); |
| 611 |
outerMap.Add(key, outerList); |
| 612 |
} |
| 613 |
|
| 614 |
foreach (var v in outerList) |
| 615 |
{ |
| 616 |
if (!enqueue(v, element)) |
| 617 |
return; |
| 618 |
} |
| 619 |
|
| 620 |
f(tcs, ct); |
| 621 |
} |
| 622 |
else |
| 623 |
{ |
| 624 |
ie.Dispose(); |
| 625 |
ie = null; |
| 626 |
f(tcs, ct); |
| 627 |
} |
| 628 |
}); |
| 629 |
}); |
| 630 |
}; |
| 631 |
|
| 632 |
return Create( |
| 633 |
(ct, tcs) => |
| 634 |
{ |
| 635 |
f(tcs, cts.Token); |
| 636 |
return tcs.Task.UsingEnumerator(oe).UsingEnumerator(ie); |
| 637 |
}, |
| 638 |
() => current, |
| 639 |
d.Dispose |
| 640 |
); |
| 641 |
}); |
| 642 |
} |
| 643 |
|
| 644 |
public static IAsyncEnumerable<TResult> Join<TOuter, TInner, TKey, TResult>(this IAsyncEnumerable<TOuter> outer, IAsyncEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector, Func<TInner, TKey> innerKeySelector, Func<TOuter, TInner, TResult> resultSelector) |
| 645 |
{ |
| 646 |
if (outer == null) |
| 647 |
throw new ArgumentNullException("outer"); |
| 648 |
if (inner == null) |
| 649 |
throw new ArgumentNullException("inner"); |
| 650 |
if (outerKeySelector == null) |
| 651 |
throw new ArgumentNullException("outerKeySelector"); |
| 652 |
if (innerKeySelector == null) |
| 653 |
throw new ArgumentNullException("innerKeySelector"); |
| 654 |
if (resultSelector == null) |
| 655 |
throw new ArgumentNullException("resultSelector"); |
| 656 |
|
| 657 |
return outer.Join(inner, outerKeySelector, innerKeySelector, resultSelector, EqualityComparer<TKey>.Default); |
| 658 |
} |
| 659 |
|
| 660 |
public static IAsyncEnumerable<TSource> Concat<TSource>(this IEnumerable<IAsyncEnumerable<TSource>> sources) |
| 661 |
{ |
| 662 |
if (sources == null) |
| 663 |
throw new ArgumentNullException("sources"); |
| 664 |
|
| 665 |
return sources.Concat_(); |
| 666 |
} |
| 667 |
|
| 668 |
public static IAsyncEnumerable<TSource> Concat<TSource>(params IAsyncEnumerable<TSource>[] sources) |
| 669 |
{ |
| 670 |
if (sources == null) |
| 671 |
throw new ArgumentNullException("sources"); |
| 672 |
|
| 673 |
return sources.Concat_(); |
| 674 |
} |
| 675 |
|
| 676 |
private static IAsyncEnumerable<TSource> Concat_<TSource>(this IEnumerable<IAsyncEnumerable<TSource>> sources) |
| 677 |
{ |
| 678 |
return Create(() => |
| 679 |
{ |
| 680 |
var se = sources.GetEnumerator(); |
| 681 |
var e = default(IAsyncEnumerator<TSource>); |
| 682 |
|
| 683 |
var cts = new CancellationTokenDisposable(); |
| 684 |
var a = new AssignableDisposable(); |
| 685 |
var d = new CompositeDisposable(cts, se, a); |
| 686 |
|
| 687 |
var f = default(Action<TaskCompletionSource<bool>, CancellationToken>); |
| 688 |
f = (tcs, ct) => |
| 689 |
{ |
| 690 |
if (e == null) |
| 691 |
{ |
| 692 |
var b = false; |
| 693 |
try |
| 694 |
{ |
| 695 |
b = se.MoveNext(); |
| 696 |
if (b) |
| 697 |
e = se.Current.GetEnumerator(); |
| 698 |
} |
| 699 |
catch (Exception ex) |
| 700 |
{ |
| 701 |
tcs.TrySetException(ex); |
| 702 |
return; |
| 703 |
} |
| 704 |
|
| 705 |
if (!b) |
| 706 |
{ |
| 707 |
tcs.TrySetResult(false); |
| 708 |
return; |
| 709 |
} |
| 710 |
|
| 711 |
a.Disposable = e; |
| 712 |
} |
| 713 |
|
| 714 |
e.MoveNext(ct).ContinueWith(t => |
| 715 |
{ |
| 716 |
t.Handle(tcs, res => |
| 717 |
{ |
| 718 |
if (res) |
| 719 |
{ |
| 720 |
tcs.TrySetResult(true); |
| 721 |
} |
| 722 |
else |
| 723 |
{ |
| 724 |
e.Dispose(); |
| 725 |
e = null; |
| 726 |
|
| 727 |
f(tcs, ct); |
| 728 |
} |
| 729 |
}); |
| 730 |
}); |
| 731 |
}; |
| 732 |
|
| 733 |
return Create( |
| 734 |
(ct, tcs) => |
| 735 |
{ |
| 736 |
f(tcs, cts.Token); |
| 737 |
return tcs.Task.UsingEnumerator(a); |
| 738 |
}, |
| 739 |
() => e.Current, |
| 740 |
d.Dispose |
| 741 |
); |
| 742 |
}); |
| 743 |
} |
| 744 |
|
| 745 |
public static IAsyncEnumerable<TOther> SelectMany<TSource, TOther>(this IAsyncEnumerable<TSource> source, IAsyncEnumerable<TOther> other) |
| 746 |
{ |
| 747 |
if (source == null) |
| 748 |
throw new ArgumentNullException("source"); |
| 749 |
if (other == null) |
| 750 |
throw new ArgumentNullException("other"); |
| 751 |
|
| 752 |
return source.SelectMany(_ => other); |
| 753 |
} |
| 754 |
} |
| 755 |
} |