Reverse.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. var listProv = source as IIListProvider<TSource>;
  55. if (listProv != null)
  56. {
  57. return listProv.GetCountAsync(true, cancellationToken);
  58. }
  59. if (!(source is ICollection<TSource>) && !(source is ICollection))
  60. {
  61. return Task.FromResult(-1);
  62. }
  63. }
  64. return source.Count(cancellationToken);
  65. }
  66. public override AsyncIterator<TSource> Clone()
  67. {
  68. return new ReverseAsyncIterator<TSource>(source);
  69. }
  70. public override void Dispose()
  71. {
  72. items = null; // Just in case this ends up being long-lived, allow the memory to be reclaimed.
  73. base.Dispose();
  74. }
  75. protected override async Task<bool> MoveNextCore(CancellationToken cancellationToken)
  76. {
  77. switch (state)
  78. {
  79. case AsyncIteratorState.Allocated:
  80. items = await source.ToArray(cancellationToken)
  81. .ConfigureAwait(false);
  82. index = items.Length - 1;
  83. state = AsyncIteratorState.Iterating;
  84. goto case AsyncIteratorState.Iterating;
  85. case AsyncIteratorState.Iterating:
  86. if (index != -1)
  87. {
  88. current = items[index];
  89. --index;
  90. return true;
  91. }
  92. break;
  93. }
  94. Dispose();
  95. return false;
  96. }
  97. }
  98. }
  99. }