TakeLast.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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.takelast?view=net-9.0-pp
  14. /// <summary>
  15. /// Returns a specified number of contiguous elements from 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 take from the end of the source sequence.</param>
  20. /// <returns>An async-enumerable sequence containing the specified number of elements from the end of the source sequence.</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 buffer with a length enough to store elements <paramref name="count"/> elements. Upon completion of
  25. /// the source sequence, this buffer is drained on the result sequence. This causes the elements to be delayed.
  26. /// </remarks>
  27. public static IAsyncEnumerable<TSource> TakeLast<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 Empty<TSource>();
  34. }
  35. return Core(source, count);
  36. static async IAsyncEnumerable<TSource> Core(IAsyncEnumerable<TSource> source, int count, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default)
  37. {
  38. Queue<TSource> queue;
  39. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  40. {
  41. if (!await e.MoveNextAsync())
  42. {
  43. yield break;
  44. }
  45. queue = new Queue<TSource>();
  46. queue.Enqueue(e.Current);
  47. while (await e.MoveNextAsync())
  48. {
  49. if (queue.Count < count)
  50. {
  51. queue.Enqueue(e.Current);
  52. }
  53. else
  54. {
  55. do
  56. {
  57. queue.Dequeue();
  58. queue.Enqueue(e.Current);
  59. }
  60. while (await e.MoveNextAsync());
  61. break;
  62. }
  63. }
  64. }
  65. Debug.Assert(queue.Count <= count);
  66. do
  67. {
  68. yield return queue.Dequeue();
  69. }
  70. while (queue.Count > 0);
  71. }
  72. }
  73. #endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  74. }
  75. }