TakeLast.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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.Tasks;
  7. namespace System.Linq
  8. {
  9. public static partial class AsyncEnumerable
  10. {
  11. public static IAsyncEnumerable<TSource> TakeLast<TSource>(this IAsyncEnumerable<TSource> source, int count)
  12. {
  13. if (source == null)
  14. throw Error.ArgumentNull(nameof(source));
  15. if (count <= 0)
  16. {
  17. return Empty<TSource>();
  18. }
  19. #if CSHARP8 && USE_ASYNC_ITERATOR
  20. return Create(Core);
  21. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  22. {
  23. Queue<TSource> queue;
  24. await using (var e = source.GetAsyncEnumerator(cancellationToken).ConfigureAwait(false))
  25. {
  26. if (!await e.MoveNextAsync())
  27. {
  28. yield break;
  29. }
  30. queue = new Queue<TSource>();
  31. queue.Enqueue(e.Current);
  32. while (await e.MoveNextAsync())
  33. {
  34. if (queue.Count < count)
  35. {
  36. queue.Enqueue(e.Current);
  37. }
  38. else
  39. {
  40. do
  41. {
  42. queue.Dequeue();
  43. queue.Enqueue(e.Current);
  44. }
  45. while (await e.MoveNextAsync());
  46. break;
  47. }
  48. }
  49. }
  50. Debug.Assert(queue.Count <= count);
  51. do
  52. {
  53. yield return queue.Dequeue();
  54. }
  55. while (queue.Count > 0);
  56. }
  57. #else
  58. return new TakeLastAsyncIterator<TSource>(source, count);
  59. #endif
  60. }
  61. #if !(CSHARP8 && USE_ASYNC_ITERATOR)
  62. private sealed class TakeLastAsyncIterator<TSource> : AsyncIterator<TSource>
  63. {
  64. private readonly int _count;
  65. private readonly IAsyncEnumerable<TSource> _source;
  66. private IAsyncEnumerator<TSource> _enumerator;
  67. private bool _isDone;
  68. private Queue<TSource> _queue;
  69. public TakeLastAsyncIterator(IAsyncEnumerable<TSource> source, int count)
  70. {
  71. Debug.Assert(source != null);
  72. _source = source;
  73. _count = count;
  74. }
  75. public override AsyncIteratorBase<TSource> Clone()
  76. {
  77. return new TakeLastAsyncIterator<TSource>(_source, _count);
  78. }
  79. public override async ValueTask DisposeAsync()
  80. {
  81. if (_enumerator != null)
  82. {
  83. await _enumerator.DisposeAsync().ConfigureAwait(false);
  84. _enumerator = null;
  85. }
  86. _queue = null; // release the memory
  87. await base.DisposeAsync().ConfigureAwait(false);
  88. }
  89. protected override async ValueTask<bool> MoveNextCore()
  90. {
  91. switch (_state)
  92. {
  93. case AsyncIteratorState.Allocated:
  94. _enumerator = _source.GetAsyncEnumerator(_cancellationToken);
  95. _queue = new Queue<TSource>();
  96. _isDone = false;
  97. _state = AsyncIteratorState.Iterating;
  98. goto case AsyncIteratorState.Iterating;
  99. case AsyncIteratorState.Iterating:
  100. while (true)
  101. {
  102. if (!_isDone)
  103. {
  104. if (await _enumerator.MoveNextAsync().ConfigureAwait(false))
  105. {
  106. if (_count > 0)
  107. {
  108. var item = _enumerator.Current;
  109. if (_queue.Count >= _count)
  110. {
  111. _queue.Dequeue();
  112. }
  113. _queue.Enqueue(item);
  114. }
  115. }
  116. else
  117. {
  118. _isDone = true;
  119. // Dispose early here as we can
  120. await _enumerator.DisposeAsync().ConfigureAwait(false);
  121. _enumerator = null;
  122. }
  123. continue; // loop until queue is drained
  124. }
  125. if (_queue.Count > 0)
  126. {
  127. _current = _queue.Dequeue();
  128. return true;
  129. }
  130. break; // while
  131. }
  132. break; // case
  133. }
  134. await DisposeAsync().ConfigureAwait(false);
  135. return false;
  136. }
  137. }
  138. #endif
  139. }
  140. }