Min.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using Cysharp.Threading.Tasks.Internal;
  5. namespace Cysharp.Threading.Tasks.Linq
  6. {
  7. public static partial class UniTaskAsyncEnumerable
  8. {
  9. public static UniTask<TSource> MinAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
  10. {
  11. Error.ThrowArgumentNullException(source, nameof(source));
  12. return Min.MinAsync(source, cancellationToken);
  13. }
  14. public static UniTask<TResult> MinAsync<TSource, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TResult> selector, CancellationToken cancellationToken = default)
  15. {
  16. Error.ThrowArgumentNullException(source, nameof(source));
  17. Error.ThrowArgumentNullException(source, nameof(selector));
  18. return Min.MinAsync(source, selector, cancellationToken);
  19. }
  20. public static UniTask<TResult> MinAwaitAsync<TSource, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TResult>> selector, CancellationToken cancellationToken = default)
  21. {
  22. Error.ThrowArgumentNullException(source, nameof(source));
  23. Error.ThrowArgumentNullException(source, nameof(selector));
  24. return Min.MinAwaitAsync(source, selector, cancellationToken);
  25. }
  26. public static UniTask<TResult> MinAwaitWithCancellationAsync<TSource, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TResult>> selector, CancellationToken cancellationToken = default)
  27. {
  28. Error.ThrowArgumentNullException(source, nameof(source));
  29. Error.ThrowArgumentNullException(source, nameof(selector));
  30. return Min.MinAwaitWithCancellationAsync(source, selector, cancellationToken);
  31. }
  32. }
  33. internal static partial class Min
  34. {
  35. public static async UniTask<TSource> MinAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  36. {
  37. TSource value = default;
  38. var comparer = Comparer<TSource>.Default;
  39. var e = source.GetAsyncEnumerator(cancellationToken);
  40. try
  41. {
  42. while (await e.MoveNextAsync())
  43. {
  44. value = e.Current;
  45. goto NEXT_LOOP;
  46. }
  47. return value;
  48. NEXT_LOOP:
  49. while (await e.MoveNextAsync())
  50. {
  51. var x = e.Current;
  52. if (comparer.Compare(value, x) > 0)
  53. {
  54. value = x;
  55. }
  56. }
  57. }
  58. finally
  59. {
  60. if (e != null)
  61. {
  62. await e.DisposeAsync();
  63. }
  64. }
  65. return value;
  66. }
  67. public static async UniTask<TResult> MinAsync<TSource, TResult>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TResult> selector, CancellationToken cancellationToken)
  68. {
  69. TResult value = default;
  70. var comparer = Comparer<TResult>.Default;
  71. var e = source.GetAsyncEnumerator(cancellationToken);
  72. try
  73. {
  74. while (await e.MoveNextAsync())
  75. {
  76. value = selector(e.Current);
  77. goto NEXT_LOOP;
  78. }
  79. return value;
  80. NEXT_LOOP:
  81. while (await e.MoveNextAsync())
  82. {
  83. var x = selector(e.Current);
  84. if (comparer.Compare(value, x) > 0)
  85. {
  86. value = x;
  87. }
  88. }
  89. }
  90. finally
  91. {
  92. if (e != null)
  93. {
  94. await e.DisposeAsync();
  95. }
  96. }
  97. return value;
  98. }
  99. public static async UniTask<TResult> MinAwaitAsync<TSource, TResult>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TResult>> selector, CancellationToken cancellationToken)
  100. {
  101. TResult value = default;
  102. var comparer = Comparer<TResult>.Default;
  103. var e = source.GetAsyncEnumerator(cancellationToken);
  104. try
  105. {
  106. while (await e.MoveNextAsync())
  107. {
  108. value = await selector(e.Current);
  109. goto NEXT_LOOP;
  110. }
  111. return value;
  112. NEXT_LOOP:
  113. while (await e.MoveNextAsync())
  114. {
  115. var x = await selector(e.Current);
  116. if (comparer.Compare(value, x) > 0)
  117. {
  118. value = x;
  119. }
  120. }
  121. }
  122. finally
  123. {
  124. if (e != null)
  125. {
  126. await e.DisposeAsync();
  127. }
  128. }
  129. return value;
  130. }
  131. public static async UniTask<TResult> MinAwaitWithCancellationAsync<TSource, TResult>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TResult>> selector, CancellationToken cancellationToken)
  132. {
  133. TResult value = default;
  134. var comparer = Comparer<TResult>.Default;
  135. var e = source.GetAsyncEnumerator(cancellationToken);
  136. try
  137. {
  138. while (await e.MoveNextAsync())
  139. {
  140. value = await selector(e.Current, cancellationToken);
  141. goto NEXT_LOOP;
  142. }
  143. return value;
  144. NEXT_LOOP:
  145. while (await e.MoveNextAsync())
  146. {
  147. var x = await selector(e.Current, cancellationToken);
  148. if (comparer.Compare(value, x) > 0)
  149. {
  150. value = x;
  151. }
  152. }
  153. }
  154. finally
  155. {
  156. if (e != null)
  157. {
  158. await e.DisposeAsync();
  159. }
  160. }
  161. return value;
  162. }
  163. }
  164. }