Reverse.cs 4.0 KB

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