Reverse.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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.Collections;
  5. using System.Collections.Generic;
  6. using System.Diagnostics;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace System.Linq
  10. {
  11. public static partial class AsyncEnumerable
  12. {
  13. public static IAsyncEnumerable<TSource> Reverse<TSource>(this IAsyncEnumerable<TSource> source)
  14. {
  15. if (source == null)
  16. throw new ArgumentNullException(nameof(source));
  17. return new ReverseAsyncIterator<TSource>(source);
  18. }
  19. private sealed class ReverseAsyncIterator<TSource> : AsyncIterator<TSource>, IIListProvider<TSource>
  20. {
  21. private readonly IAsyncEnumerable<TSource> source;
  22. private int index;
  23. private TSource[] items;
  24. public ReverseAsyncIterator(IAsyncEnumerable<TSource> source)
  25. {
  26. Debug.Assert(source != null);
  27. this.source = source;
  28. }
  29. public async Task<TSource[]> ToArrayAsync(CancellationToken cancellationToken)
  30. {
  31. var array = await source.ToArray(cancellationToken)
  32. .ConfigureAwait(false);
  33. // Array.Reverse() involves boxing for non-primitive value types, but
  34. // checking that has its own cost, so just use this approach for all types.
  35. for (int i = 0, j = array.Length - 1; i < j; ++i, --j)
  36. {
  37. var temp = array[i];
  38. array[i] = array[j];
  39. array[j] = temp;
  40. }
  41. return array;
  42. }
  43. public async Task<List<TSource>> ToListAsync(CancellationToken cancellationToken)
  44. {
  45. var list = await source.ToList(cancellationToken)
  46. .ConfigureAwait(false);
  47. list.Reverse();
  48. return list;
  49. }
  50. public Task<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken)
  51. {
  52. if (onlyIfCheap)
  53. {
  54. if (source is IIListProvider<TSource> listProv)
  55. {
  56. return listProv.GetCountAsync(true, cancellationToken);
  57. }
  58. if (!(source is ICollection<TSource>) && !(source is ICollection))
  59. {
  60. return Task.FromResult(-1);
  61. }
  62. }
  63. return source.Count(cancellationToken);
  64. }
  65. public override AsyncIterator<TSource> Clone()
  66. {
  67. return new ReverseAsyncIterator<TSource>(source);
  68. }
  69. public override void Dispose()
  70. {
  71. items = null; // Just in case this ends up being long-lived, allow the memory to be reclaimed.
  72. base.Dispose();
  73. }
  74. protected override async Task<bool> MoveNextCore(CancellationToken cancellationToken)
  75. {
  76. switch (state)
  77. {
  78. case AsyncIteratorState.Allocated:
  79. items = await source.ToArray(cancellationToken)
  80. .ConfigureAwait(false);
  81. index = items.Length - 1;
  82. state = AsyncIteratorState.Iterating;
  83. goto case AsyncIteratorState.Iterating;
  84. case AsyncIteratorState.Iterating:
  85. if (index != -1)
  86. {
  87. current = items[index];
  88. --index;
  89. return true;
  90. }
  91. break;
  92. }
  93. Dispose();
  94. return false;
  95. }
  96. }
  97. }
  98. }