TakeLast.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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.Generic;
  5. using System.Diagnostics;
  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> TakeLast<TSource>(this IAsyncEnumerable<TSource> source, int count)
  13. {
  14. if (source == null)
  15. throw new ArgumentNullException(nameof(source));
  16. if (count <= 0)
  17. {
  18. return Empty<TSource>();
  19. }
  20. return new TakeLastAsyncIterator<TSource>(source, count);
  21. }
  22. private sealed class TakeLastAsyncIterator<TSource> : AsyncIterator<TSource>
  23. {
  24. private readonly int count;
  25. private readonly IAsyncEnumerable<TSource> source;
  26. private IAsyncEnumerator<TSource> enumerator;
  27. private bool isDone;
  28. private Queue<TSource> queue;
  29. public TakeLastAsyncIterator(IAsyncEnumerable<TSource> source, int count)
  30. {
  31. Debug.Assert(source != null);
  32. this.source = source;
  33. this.count = count;
  34. }
  35. public override AsyncIterator<TSource> Clone()
  36. {
  37. return new TakeLastAsyncIterator<TSource>(source, count);
  38. }
  39. public override async ValueTask DisposeAsync()
  40. {
  41. if (enumerator != null)
  42. {
  43. await enumerator.DisposeAsync().ConfigureAwait(false);
  44. enumerator = null;
  45. }
  46. queue = null; // release the memory
  47. await base.DisposeAsync().ConfigureAwait(false);
  48. }
  49. protected override async ValueTask<bool> MoveNextCore(CancellationToken cancellationToken)
  50. {
  51. switch (state)
  52. {
  53. case AsyncIteratorState.Allocated:
  54. enumerator = source.GetAsyncEnumerator(cancellationToken);
  55. queue = new Queue<TSource>();
  56. isDone = false;
  57. state = AsyncIteratorState.Iterating;
  58. goto case AsyncIteratorState.Iterating;
  59. case AsyncIteratorState.Iterating:
  60. while (true)
  61. {
  62. if (!isDone)
  63. {
  64. if (await enumerator.MoveNextAsync().ConfigureAwait(false))
  65. {
  66. if (count > 0)
  67. {
  68. var item = enumerator.Current;
  69. if (queue.Count >= count)
  70. {
  71. queue.Dequeue();
  72. }
  73. queue.Enqueue(item);
  74. }
  75. }
  76. else
  77. {
  78. isDone = true;
  79. // Dispose early here as we can
  80. await enumerator.DisposeAsync().ConfigureAwait(false);
  81. enumerator = null;
  82. }
  83. continue; // loop until queue is drained
  84. }
  85. if (queue.Count > 0)
  86. {
  87. current = queue.Dequeue();
  88. return true;
  89. }
  90. break; // while
  91. }
  92. break; // case
  93. }
  94. await DisposeAsync().ConfigureAwait(false);
  95. return false;
  96. }
  97. }
  98. }
  99. }