Reverse.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 Error.ArgumentNull(nameof(source));
  17. return new ReverseAsyncIterator<TSource>(source);
  18. }
  19. private sealed class ReverseAsyncIterator<TSource> : AsyncIterator<TSource>, IAsyncIListProvider<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. _source = source;
  28. }
  29. public async ValueTask<TSource[]> ToArrayAsync(CancellationToken cancellationToken)
  30. {
  31. var array = await _source.ToArrayAsync(cancellationToken).ConfigureAwait(false);
  32. // Array.Reverse() involves boxing for non-primitive value types, but
  33. // checking that has its own cost, so just use this approach for all types.
  34. for (int i = 0, j = array.Length - 1; i < j; ++i, --j)
  35. {
  36. var temp = array[i];
  37. array[i] = array[j];
  38. array[j] = temp;
  39. }
  40. return array;
  41. }
  42. public async ValueTask<List<TSource>> ToListAsync(CancellationToken cancellationToken)
  43. {
  44. var list = await _source.ToListAsync(cancellationToken).ConfigureAwait(false);
  45. list.Reverse();
  46. return list;
  47. }
  48. public ValueTask<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken)
  49. {
  50. if (onlyIfCheap)
  51. {
  52. if (_source is IAsyncIListProvider<TSource> listProv)
  53. {
  54. return listProv.GetCountAsync(true, cancellationToken);
  55. }
  56. if (!(_source is ICollection<TSource>) && !(_source is ICollection))
  57. {
  58. return new ValueTask<int>(-1);
  59. }
  60. }
  61. return new ValueTask<int>(_source.CountAsync(cancellationToken));
  62. }
  63. public override AsyncIteratorBase<TSource> Clone()
  64. {
  65. return new ReverseAsyncIterator<TSource>(_source);
  66. }
  67. public override async ValueTask DisposeAsync()
  68. {
  69. _items = null; // Just in case this ends up being long-lived, allow the memory to be reclaimed.
  70. await base.DisposeAsync().ConfigureAwait(false);
  71. }
  72. protected override async ValueTask<bool> MoveNextCore()
  73. {
  74. switch (_state)
  75. {
  76. case AsyncIteratorState.Allocated:
  77. _items = await _source.ToArrayAsync(_cancellationToken).ConfigureAwait(false);
  78. _index = _items.Length - 1;
  79. _state = AsyncIteratorState.Iterating;
  80. goto case AsyncIteratorState.Iterating;
  81. case AsyncIteratorState.Iterating:
  82. if (_index != -1)
  83. {
  84. _current = _items[_index];
  85. --_index;
  86. return true;
  87. }
  88. break;
  89. }
  90. await DisposeAsync().ConfigureAwait(false);
  91. return false;
  92. }
  93. }
  94. }
  95. }