Reverse.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. namespace System.Linq
  9. {
  10. public static partial class AsyncEnumerable
  11. {
  12. public static IAsyncEnumerable<TSource> Reverse<TSource>(this IAsyncEnumerable<TSource> source)
  13. {
  14. if (source == null)
  15. throw new ArgumentNullException(nameof(source));
  16. return CreateEnumerable(
  17. () =>
  18. {
  19. var e = source.GetEnumerator();
  20. var stack = default(Stack<TSource>);
  21. var cts = new CancellationTokenDisposable();
  22. var d = Disposable.Create(cts, e);
  23. return CreateEnumerator(
  24. async ct =>
  25. {
  26. if (stack == null)
  27. {
  28. stack = await CreateEnumerable(
  29. () => e)
  30. .Aggregate(new Stack<TSource>(), (s, x) =>
  31. {
  32. s.Push(x);
  33. return s;
  34. }, cts.Token)
  35. .ConfigureAwait(false);
  36. return stack.Count > 0;
  37. }
  38. stack.Pop();
  39. return stack.Count > 0;
  40. },
  41. () => stack.Peek(),
  42. d.Dispose,
  43. e
  44. );
  45. });
  46. }
  47. }
  48. }