TakeLast.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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> TakeLast<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 Empty<TSource>();
  19. }
  20. return Create(Core);
  21. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  22. {
  23. Queue<TSource> queue;
  24. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  25. {
  26. if (!await e.MoveNextAsync())
  27. {
  28. yield break;
  29. }
  30. queue = new Queue<TSource>();
  31. queue.Enqueue(e.Current);
  32. while (await e.MoveNextAsync())
  33. {
  34. if (queue.Count < count)
  35. {
  36. queue.Enqueue(e.Current);
  37. }
  38. else
  39. {
  40. do
  41. {
  42. queue.Dequeue();
  43. queue.Enqueue(e.Current);
  44. }
  45. while (await e.MoveNextAsync());
  46. break;
  47. }
  48. }
  49. }
  50. Debug.Assert(queue.Count <= count);
  51. do
  52. {
  53. yield return queue.Dequeue();
  54. }
  55. while (queue.Count > 0);
  56. }
  57. }
  58. }
  59. }