SkipLast.cs 3.3 KB

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