SkipLast.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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> SkipLast<TSource>(this IAsyncEnumerable<TSource> source, int count)
  13. {
  14. if (source == null)
  15. throw Error.ArgumentNull(nameof(source));
  16. if (count <= 0)
  17. {
  18. // Return source if not actually skipping, but only if it's a type from here, to avoid
  19. // issues if collections are used as keys or otherwise must not be aliased.
  20. if (source is AsyncIteratorBase<TSource>)
  21. {
  22. return source;
  23. }
  24. count = 0;
  25. }
  26. #if USE_ASYNC_ITERATOR
  27. return Create(Core);
  28. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  29. {
  30. var queue = new Queue<TSource>();
  31. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  32. {
  33. while (await e.MoveNextAsync())
  34. {
  35. if (queue.Count == count)
  36. {
  37. do
  38. {
  39. yield return queue.Dequeue();
  40. queue.Enqueue(e.Current);
  41. }
  42. while (await e.MoveNextAsync());
  43. break;
  44. }
  45. else
  46. {
  47. queue.Enqueue(e.Current);
  48. }
  49. }
  50. }
  51. }
  52. #else
  53. return new SkipLastAsyncIterator<TSource>(source, count);
  54. #endif
  55. }
  56. #if !USE_ASYNC_ITERATOR
  57. private sealed class SkipLastAsyncIterator<TSource> : AsyncIterator<TSource>
  58. {
  59. private readonly int _count;
  60. private readonly IAsyncEnumerable<TSource> _source;
  61. private IAsyncEnumerator<TSource> _enumerator;
  62. private Queue<TSource> _queue;
  63. public SkipLastAsyncIterator(IAsyncEnumerable<TSource> source, int count)
  64. {
  65. Debug.Assert(source != null);
  66. _source = source;
  67. _count = count;
  68. }
  69. public override AsyncIteratorBase<TSource> Clone()
  70. {
  71. return new SkipLastAsyncIterator<TSource>(_source, _count);
  72. }
  73. public override async ValueTask DisposeAsync()
  74. {
  75. if (_enumerator != null)
  76. {
  77. await _enumerator.DisposeAsync().ConfigureAwait(false);
  78. _enumerator = null;
  79. }
  80. _queue = null; // release the memory
  81. await base.DisposeAsync().ConfigureAwait(false);
  82. }
  83. protected override async ValueTask<bool> MoveNextCore()
  84. {
  85. switch (_state)
  86. {
  87. case AsyncIteratorState.Allocated:
  88. _enumerator = _source.GetAsyncEnumerator(_cancellationToken);
  89. _queue = new Queue<TSource>();
  90. _state = AsyncIteratorState.Iterating;
  91. goto case AsyncIteratorState.Iterating;
  92. case AsyncIteratorState.Iterating:
  93. while (await _enumerator.MoveNextAsync().ConfigureAwait(false))
  94. {
  95. var item = _enumerator.Current;
  96. _queue.Enqueue(item);
  97. if (_queue.Count > _count)
  98. {
  99. _current = _queue.Dequeue();
  100. return true;
  101. }
  102. }
  103. break;
  104. }
  105. await DisposeAsync().ConfigureAwait(false);
  106. return false;
  107. }
  108. }
  109. #endif
  110. }
  111. }