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;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.Diagnostics;
  8. using System.Linq;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. namespace System.Linq
  12. {
  13. public static partial class AsyncEnumerable
  14. {
  15. public static IAsyncEnumerable<TSource> Reverse<TSource>(this IAsyncEnumerable<TSource> source)
  16. {
  17. if (source == null)
  18. throw new ArgumentNullException(nameof(source));
  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. var listProv = source as IIListProvider<TSource>;
  57. if (listProv != null)
  58. {
  59. return listProv.GetCountAsync(onlyIfCheap: true, cancellationToken: cancellationToken);
  60. }
  61. if (!(source is ICollection<TSource>) && !(source is ICollection))
  62. {
  63. return Task.FromResult(-1);
  64. }
  65. }
  66. return source.Count(cancellationToken);
  67. }
  68. public override AsyncIterator<TSource> Clone()
  69. {
  70. return new ReverseAsyncIterator<TSource>(source);
  71. }
  72. public override void Dispose()
  73. {
  74. items = null; // Just in case this ends up being long-lived, allow the memory to be reclaimed.
  75. base.Dispose();
  76. }
  77. protected override async Task<bool> MoveNextCore(CancellationToken cancellationToken)
  78. {
  79. switch (state)
  80. {
  81. case AsyncIteratorState.Allocated:
  82. items = await source.ToArray(cancellationToken)
  83. .ConfigureAwait(false);
  84. index = items.Length - 1;
  85. state = AsyncIteratorState.Iterating;
  86. goto case AsyncIteratorState.Iterating;
  87. case AsyncIteratorState.Iterating:
  88. if (index != -1)
  89. {
  90. current = items[index];
  91. --index;
  92. return true;
  93. }
  94. break;
  95. }
  96. Dispose();
  97. return false;
  98. }
  99. }
  100. }
  101. }