Reverse.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 Error.ArgumentNull(nameof(source));
  18. }
  19. return new ReverseAsyncIterator<TSource>(source);
  20. }
  21. private sealed class ReverseAsyncIterator<TSource> : AsyncIterator<TSource>, IAsyncIListProvider<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. _source = source;
  30. }
  31. public async Task<TSource[]> ToArrayAsync(CancellationToken cancellationToken)
  32. {
  33. var array = await _source.ToArray(cancellationToken).ConfigureAwait(false);
  34. // Array.Reverse() involves boxing for non-primitive value types, but
  35. // checking that has its own cost, so just use this approach for all types.
  36. for (int i = 0, j = array.Length - 1; i < j; ++i, --j)
  37. {
  38. var temp = array[i];
  39. array[i] = array[j];
  40. array[j] = temp;
  41. }
  42. return array;
  43. }
  44. public async Task<List<TSource>> ToListAsync(CancellationToken cancellationToken)
  45. {
  46. var list = await _source.ToList(cancellationToken).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 IAsyncIListProvider<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 async ValueTask DisposeAsync()
  70. {
  71. _items = null; // Just in case this ends up being long-lived, allow the memory to be reclaimed.
  72. await base.DisposeAsync().ConfigureAwait(false);
  73. }
  74. protected override async ValueTask<bool> MoveNextCore(CancellationToken cancellationToken)
  75. {
  76. switch (state)
  77. {
  78. case AsyncIteratorState.Allocated:
  79. _items = await _source.ToArray().ConfigureAwait(false);
  80. _index = _items.Length - 1;
  81. state = AsyncIteratorState.Iterating;
  82. goto case AsyncIteratorState.Iterating;
  83. case AsyncIteratorState.Iterating:
  84. if (_index != -1)
  85. {
  86. current = _items[_index];
  87. --_index;
  88. return true;
  89. }
  90. break;
  91. }
  92. await DisposeAsync().ConfigureAwait(false);
  93. return false;
  94. }
  95. }
  96. }
  97. }