SkipLast.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT 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. /// <summary>
  13. /// Bypasses a specified number of elements at the end of an async-enumerable sequence.
  14. /// </summary>
  15. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  16. /// <param name="source">Source sequence.</param>
  17. /// <param name="count">Number of elements to bypass at the end of the source sequence.</param>
  18. /// <returns>An async-enumerable sequence containing the source sequence elements except for the bypassed ones at the end.</returns>
  19. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  20. /// <exception cref="ArgumentOutOfRangeException"><paramref name="count"/> is less than zero.</exception>
  21. /// <remarks>
  22. /// This operator accumulates a queue with a length enough to store the first <paramref name="count"/> elements. As more elements are
  23. /// received, elements are taken from the front of the queue and produced on the result sequence. This causes elements to be delayed.
  24. /// </remarks>
  25. public static IAsyncEnumerable<TSource> SkipLast<TSource>(this IAsyncEnumerable<TSource> source, int count)
  26. {
  27. if (source == null)
  28. throw Error.ArgumentNull(nameof(source));
  29. if (count <= 0)
  30. {
  31. // Return source if not actually skipping, but only if it's a type from here, to avoid
  32. // issues if collections are used as keys or otherwise must not be aliased.
  33. if (source is AsyncIteratorBase<TSource>)
  34. {
  35. return source;
  36. }
  37. count = 0;
  38. }
  39. #if HAS_ASYNC_ENUMERABLE_CANCELLATION
  40. return Core(source, count);
  41. static async IAsyncEnumerable<TSource> Core(IAsyncEnumerable<TSource> source, int count, [System.Runtime.CompilerServices.EnumeratorCancellation]CancellationToken cancellationToken = default)
  42. #else
  43. return Create(Core);
  44. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  45. #endif
  46. {
  47. var queue = new Queue<TSource>();
  48. await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
  49. while (await e.MoveNextAsync())
  50. {
  51. if (queue.Count == count)
  52. {
  53. do
  54. {
  55. yield return queue.Dequeue();
  56. queue.Enqueue(e.Current);
  57. }
  58. while (await e.MoveNextAsync());
  59. break;
  60. }
  61. else
  62. {
  63. queue.Enqueue(e.Current);
  64. }
  65. }
  66. }
  67. }
  68. }
  69. }