Min.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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.Threading.Tasks;
  6. namespace System.Reactive.Linq
  7. {
  8. public partial class AsyncObservable
  9. {
  10. public static IAsyncObservable<TSource> Min<TSource>(IAsyncObservable<TSource> source)
  11. {
  12. if (source == null)
  13. throw new ArgumentNullException(nameof(source));
  14. return Create(source, static (source, observer) => source.SubscribeSafeAsync(AsyncObserver.Min(observer)));
  15. }
  16. public static IAsyncObservable<TSource> Min<TSource>(IAsyncObservable<TSource> source, IComparer<TSource> comparer)
  17. {
  18. if (source == null)
  19. throw new ArgumentNullException(nameof(source));
  20. if (comparer == null)
  21. throw new ArgumentNullException(nameof(comparer));
  22. return Create(
  23. source,
  24. comparer,
  25. default(TSource),
  26. (source, comparer, observer) => source.SubscribeSafeAsync(AsyncObserver.Min(observer, comparer)));
  27. }
  28. }
  29. public partial class AsyncObserver
  30. {
  31. public static IAsyncObserver<TSource> Min<TSource>(IAsyncObserver<TSource> observer)
  32. {
  33. if (observer == null)
  34. throw new ArgumentNullException(nameof(observer));
  35. return Min(observer, Comparer<TSource>.Default);
  36. }
  37. public static IAsyncObserver<TSource> Min<TSource>(IAsyncObserver<TSource> observer, IComparer<TSource> comparer)
  38. {
  39. if (observer == null)
  40. throw new ArgumentNullException(nameof(observer));
  41. if (comparer == null)
  42. throw new ArgumentNullException(nameof(comparer));
  43. var min = default(TSource);
  44. var found = false;
  45. return Create<TSource>(
  46. async x =>
  47. {
  48. if (found)
  49. {
  50. bool isGreater;
  51. try
  52. {
  53. isGreater = comparer.Compare(x, min) < 0;
  54. }
  55. catch (Exception ex)
  56. {
  57. await observer.OnErrorAsync(ex).ConfigureAwait(false);
  58. return;
  59. }
  60. if (isGreater)
  61. {
  62. min = x;
  63. }
  64. }
  65. else
  66. {
  67. min = x;
  68. found = true;
  69. }
  70. },
  71. observer.OnErrorAsync,
  72. async () =>
  73. {
  74. if (!found)
  75. {
  76. await observer.OnErrorAsync(new InvalidOperationException("The sequence is empty.")).ConfigureAwait(false);
  77. }
  78. else
  79. {
  80. await observer.OnNextAsync(min).ConfigureAwait(false);
  81. await observer.OnCompletedAsync().ConfigureAwait(false);
  82. }
  83. }
  84. );
  85. }
  86. public static IAsyncObserver<int> MinInt32(IAsyncObserver<int> observer)
  87. {
  88. if (observer == null)
  89. throw new ArgumentNullException(nameof(observer));
  90. var min = 0;
  91. var found = false;
  92. return Create<int>(
  93. x =>
  94. {
  95. if (found)
  96. {
  97. if (x < min)
  98. {
  99. min = x;
  100. }
  101. }
  102. else
  103. {
  104. min = x;
  105. found = true;
  106. }
  107. return default;
  108. },
  109. observer.OnErrorAsync,
  110. async () =>
  111. {
  112. if (!found)
  113. {
  114. await observer.OnErrorAsync(new InvalidOperationException("The sequence is empty.")).ConfigureAwait(false);
  115. }
  116. else
  117. {
  118. await observer.OnNextAsync(min).ConfigureAwait(false);
  119. await observer.OnCompletedAsync().ConfigureAwait(false);
  120. }
  121. }
  122. );
  123. }
  124. public static IAsyncObserver<long> MinInt64(IAsyncObserver<long> observer)
  125. {
  126. if (observer == null)
  127. throw new ArgumentNullException(nameof(observer));
  128. var min = 0L;
  129. var found = false;
  130. return Create<long>(
  131. x =>
  132. {
  133. if (found)
  134. {
  135. if (x < min)
  136. {
  137. min = x;
  138. }
  139. }
  140. else
  141. {
  142. min = x;
  143. found = true;
  144. }
  145. return default;
  146. },
  147. observer.OnErrorAsync,
  148. async () =>
  149. {
  150. if (!found)
  151. {
  152. await observer.OnErrorAsync(new InvalidOperationException("The sequence is empty.")).ConfigureAwait(false);
  153. }
  154. else
  155. {
  156. await observer.OnNextAsync(min).ConfigureAwait(false);
  157. await observer.OnCompletedAsync().ConfigureAwait(false);
  158. }
  159. }
  160. );
  161. }
  162. public static IAsyncObserver<float> MinSingle(IAsyncObserver<float> observer)
  163. {
  164. if (observer == null)
  165. throw new ArgumentNullException(nameof(observer));
  166. var min = 0.0f;
  167. var found = false;
  168. return Create<float>(
  169. x =>
  170. {
  171. if (found)
  172. {
  173. if (x < min || double.IsNaN(x))
  174. {
  175. min = x;
  176. }
  177. }
  178. else
  179. {
  180. min = x;
  181. found = true;
  182. }
  183. return default;
  184. },
  185. observer.OnErrorAsync,
  186. async () =>
  187. {
  188. if (!found)
  189. {
  190. await observer.OnErrorAsync(new InvalidOperationException("The sequence is empty.")).ConfigureAwait(false);
  191. }
  192. else
  193. {
  194. await observer.OnNextAsync(min).ConfigureAwait(false);
  195. await observer.OnCompletedAsync().ConfigureAwait(false);
  196. }
  197. }
  198. );
  199. }
  200. public static IAsyncObserver<double> MinDouble(IAsyncObserver<double> observer)
  201. {
  202. if (observer == null)
  203. throw new ArgumentNullException(nameof(observer));
  204. var min = 0.0;
  205. var found = false;
  206. return Create<double>(
  207. x =>
  208. {
  209. if (found)
  210. {
  211. if (x < min || double.IsNaN(x))
  212. {
  213. min = x;
  214. }
  215. }
  216. else
  217. {
  218. min = x;
  219. found = true;
  220. }
  221. return default;
  222. },
  223. observer.OnErrorAsync,
  224. async () =>
  225. {
  226. if (!found)
  227. {
  228. await observer.OnErrorAsync(new InvalidOperationException("The sequence is empty.")).ConfigureAwait(false);
  229. }
  230. else
  231. {
  232. await observer.OnNextAsync(min).ConfigureAwait(false);
  233. await observer.OnCompletedAsync().ConfigureAwait(false);
  234. }
  235. }
  236. );
  237. }
  238. public static IAsyncObserver<decimal> MinDecimal(IAsyncObserver<decimal> observer)
  239. {
  240. if (observer == null)
  241. throw new ArgumentNullException(nameof(observer));
  242. var min = 0m;
  243. var found = false;
  244. return Create<decimal>(
  245. x =>
  246. {
  247. if (found)
  248. {
  249. if (x < min)
  250. {
  251. min = x;
  252. }
  253. }
  254. else
  255. {
  256. min = x;
  257. found = true;
  258. }
  259. return default;
  260. },
  261. observer.OnErrorAsync,
  262. async () =>
  263. {
  264. if (!found)
  265. {
  266. await observer.OnErrorAsync(new InvalidOperationException("The sequence is empty.")).ConfigureAwait(false);
  267. }
  268. else
  269. {
  270. await observer.OnNextAsync(min).ConfigureAwait(false);
  271. await observer.OnCompletedAsync().ConfigureAwait(false);
  272. }
  273. }
  274. );
  275. }
  276. public static IAsyncObserver<int?> MinNullableInt32(IAsyncObserver<int?> observer)
  277. {
  278. if (observer == null)
  279. throw new ArgumentNullException(nameof(observer));
  280. var min = default(int?);
  281. return Create<int?>(
  282. x =>
  283. {
  284. if (min == null || x < min)
  285. {
  286. min = x;
  287. }
  288. return default;
  289. },
  290. observer.OnErrorAsync,
  291. async () =>
  292. {
  293. await observer.OnNextAsync(min).ConfigureAwait(false);
  294. await observer.OnCompletedAsync().ConfigureAwait(false);
  295. }
  296. );
  297. }
  298. public static IAsyncObserver<long?> MinNullableInt64(IAsyncObserver<long?> observer)
  299. {
  300. if (observer == null)
  301. throw new ArgumentNullException(nameof(observer));
  302. var min = default(long?);
  303. return Create<long?>(
  304. x =>
  305. {
  306. if (min == null || x < min)
  307. {
  308. min = x;
  309. }
  310. return default;
  311. },
  312. observer.OnErrorAsync,
  313. async () =>
  314. {
  315. await observer.OnNextAsync(min).ConfigureAwait(false);
  316. await observer.OnCompletedAsync().ConfigureAwait(false);
  317. }
  318. );
  319. }
  320. public static IAsyncObserver<float?> MinNullableSingle(IAsyncObserver<float?> observer)
  321. {
  322. if (observer == null)
  323. throw new ArgumentNullException(nameof(observer));
  324. var min = default(float?);
  325. return Create<float?>(
  326. x =>
  327. {
  328. if (x != null && (min == null || x < min || double.IsNaN(x.Value)))
  329. {
  330. min = x;
  331. }
  332. return default;
  333. },
  334. observer.OnErrorAsync,
  335. async () =>
  336. {
  337. await observer.OnNextAsync(min).ConfigureAwait(false);
  338. await observer.OnCompletedAsync().ConfigureAwait(false);
  339. }
  340. );
  341. }
  342. public static IAsyncObserver<double?> MinNullableDouble(IAsyncObserver<double?> observer)
  343. {
  344. if (observer == null)
  345. throw new ArgumentNullException(nameof(observer));
  346. var min = default(double?);
  347. return Create<double?>(
  348. x =>
  349. {
  350. if (x != null && (min == null || x < min || double.IsNaN(x.Value)))
  351. {
  352. min = x;
  353. }
  354. return default;
  355. },
  356. observer.OnErrorAsync,
  357. async () =>
  358. {
  359. await observer.OnNextAsync(min).ConfigureAwait(false);
  360. await observer.OnCompletedAsync().ConfigureAwait(false);
  361. }
  362. );
  363. }
  364. public static IAsyncObserver<decimal?> MinNullableDecimal(IAsyncObserver<decimal?> observer)
  365. {
  366. if (observer == null)
  367. throw new ArgumentNullException(nameof(observer));
  368. var min = default(decimal?);
  369. return Create<decimal?>(
  370. x =>
  371. {
  372. if (min == null || x < min)
  373. {
  374. min = x;
  375. }
  376. return default;
  377. },
  378. observer.OnErrorAsync,
  379. async () =>
  380. {
  381. await observer.OnNextAsync(min).ConfigureAwait(false);
  382. await observer.OnCompletedAsync().ConfigureAwait(false);
  383. }
  384. );
  385. }
  386. }
  387. }