Reverse.cs 3.9 KB

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