Reverse.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. var e = source.GetEnumerator();
  19. var stack = default(Stack<TSource>);
  20. var cts = new CancellationTokenDisposable();
  21. var d = Disposable.Create(cts, e);
  22. return CreateEnumerator(
  23. async ct =>
  24. {
  25. if (stack == null)
  26. {
  27. stack = await CreateEnumerable(() => e)
  28. .Aggregate(new Stack<TSource>(), (s, x) =>
  29. {
  30. s.Push(x);
  31. return s;
  32. }, cts.Token)
  33. .ConfigureAwait(false);
  34. return stack.Count > 0;
  35. }
  36. stack.Pop();
  37. return stack.Count > 0;
  38. },
  39. () => stack.Peek(),
  40. d.Dispose,
  41. e
  42. );
  43. });
  44. }
  45. }
  46. }