Take.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace System.Linq
  10. {
  11. public static partial class AsyncEnumerable
  12. {
  13. public static IAsyncEnumerable<TSource> Take<TSource>(this IAsyncEnumerable<TSource> source, int count)
  14. {
  15. if (source == null)
  16. throw new ArgumentNullException(nameof(source));
  17. if (count < 0)
  18. throw new ArgumentOutOfRangeException(nameof(count));
  19. return Create(() =>
  20. {
  21. var e = source.GetEnumerator();
  22. var n = count;
  23. var cts = new CancellationTokenDisposable();
  24. var d = Disposable.Create(cts, e);
  25. return Create(
  26. async ct =>
  27. {
  28. if (n == 0)
  29. return false;
  30. var result = await e.MoveNext(cts.Token)
  31. .ConfigureAwait(false);
  32. --n;
  33. if (n == 0)
  34. e.Dispose();
  35. return result;
  36. },
  37. () => e.Current,
  38. d.Dispose,
  39. e
  40. );
  41. });
  42. }
  43. public static IAsyncEnumerable<TSource> TakeLast<TSource>(this IAsyncEnumerable<TSource> source, int count)
  44. {
  45. if (source == null)
  46. throw new ArgumentNullException(nameof(source));
  47. if (count < 0)
  48. throw new ArgumentOutOfRangeException(nameof(count));
  49. return Create(() =>
  50. {
  51. var e = source.GetEnumerator();
  52. var cts = new CancellationTokenDisposable();
  53. var d = Disposable.Create(cts, e);
  54. var q = new Queue<TSource>(count);
  55. var done = false;
  56. var current = default(TSource);
  57. var f = default(Func<CancellationToken, Task<bool>>);
  58. f = async ct =>
  59. {
  60. if (!done)
  61. {
  62. if (await e.MoveNext(ct)
  63. .ConfigureAwait(false))
  64. {
  65. if (count > 0)
  66. {
  67. var item = e.Current;
  68. if (q.Count >= count)
  69. q.Dequeue();
  70. q.Enqueue(item);
  71. }
  72. }
  73. else
  74. {
  75. done = true;
  76. e.Dispose();
  77. }
  78. return await f(ct)
  79. .ConfigureAwait(false);
  80. }
  81. if (q.Count > 0)
  82. {
  83. current = q.Dequeue();
  84. return true;
  85. }
  86. return false;
  87. };
  88. return Create(
  89. f,
  90. () => current,
  91. d.Dispose,
  92. e
  93. );
  94. });
  95. }
  96. public static IAsyncEnumerable<TSource> TakeWhile<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate)
  97. {
  98. if (source == null)
  99. throw new ArgumentNullException(nameof(source));
  100. if (predicate == null)
  101. throw new ArgumentNullException(nameof(predicate));
  102. return Create(() =>
  103. {
  104. var e = source.GetEnumerator();
  105. var cts = new CancellationTokenDisposable();
  106. var d = Disposable.Create(cts, e);
  107. return Create(
  108. async ct =>
  109. {
  110. if (await e.MoveNext(cts.Token)
  111. .ConfigureAwait(false))
  112. {
  113. return predicate(e.Current);
  114. }
  115. return false;
  116. },
  117. () => e.Current,
  118. d.Dispose,
  119. e
  120. );
  121. });
  122. }
  123. public static IAsyncEnumerable<TSource> TakeWhile<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int, bool> predicate)
  124. {
  125. if (source == null)
  126. throw new ArgumentNullException(nameof(source));
  127. if (predicate == null)
  128. throw new ArgumentNullException(nameof(predicate));
  129. return Create(() =>
  130. {
  131. var e = source.GetEnumerator();
  132. var index = 0;
  133. var cts = new CancellationTokenDisposable();
  134. var d = Disposable.Create(cts, e);
  135. return Create(
  136. async ct =>
  137. {
  138. if (await e.MoveNext(cts.Token)
  139. .ConfigureAwait(false))
  140. {
  141. return predicate(e.Current, checked(index++));
  142. }
  143. return false;
  144. },
  145. () => e.Current,
  146. d.Dispose,
  147. e
  148. );
  149. });
  150. }
  151. }
  152. }