Min.cs 14 KB

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