TakeLast.cs 4.1 KB

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