SkipLast.cs 4.3 KB

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