Min.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  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. #if !NO_PERF
  5. using System;
  6. using System.Collections.Generic;
  7. namespace System.Reactive.Linq.ObservableImpl
  8. {
  9. class Min<TSource> : Producer<TSource>
  10. {
  11. private readonly IObservable<TSource> _source;
  12. private readonly IComparer<TSource> _comparer;
  13. public Min(IObservable<TSource> source, IComparer<TSource> comparer)
  14. {
  15. _source = source;
  16. _comparer = comparer;
  17. }
  18. protected override IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink)
  19. {
  20. // LINQ to Objects makes this distinction in order to make [Min|Max] of an empty collection of reference type objects equal to null.
  21. if (default(TSource) == null)
  22. {
  23. var sink = new _(this, observer, cancel);
  24. setSink(sink);
  25. return _source.SubscribeSafe(sink);
  26. }
  27. else
  28. {
  29. var sink = new Delta(this, observer, cancel);
  30. setSink(sink);
  31. return _source.SubscribeSafe(sink);
  32. }
  33. }
  34. class Delta : Sink<TSource>, IObserver<TSource>
  35. {
  36. private readonly Min<TSource> _parent;
  37. private bool _hasValue;
  38. private TSource _lastValue;
  39. public Delta(Min<TSource> parent, IObserver<TSource> observer, IDisposable cancel)
  40. : base(observer, cancel)
  41. {
  42. _parent = parent;
  43. _hasValue = false;
  44. _lastValue = default(TSource);
  45. }
  46. public void OnNext(TSource value)
  47. {
  48. if (_hasValue)
  49. {
  50. var comparison = 0;
  51. try
  52. {
  53. comparison = _parent._comparer.Compare(value, _lastValue);
  54. }
  55. catch (Exception ex)
  56. {
  57. base._observer.OnError(ex);
  58. base.Dispose();
  59. return;
  60. }
  61. if (comparison < 0)
  62. {
  63. _lastValue = value;
  64. }
  65. }
  66. else
  67. {
  68. _hasValue = true;
  69. _lastValue = value;
  70. }
  71. }
  72. public void OnError(Exception error)
  73. {
  74. base._observer.OnError(error);
  75. base.Dispose();
  76. }
  77. public void OnCompleted()
  78. {
  79. if (!_hasValue)
  80. {
  81. base._observer.OnError(new InvalidOperationException(Strings_Linq.NO_ELEMENTS));
  82. }
  83. else
  84. {
  85. base._observer.OnNext(_lastValue);
  86. base._observer.OnCompleted();
  87. }
  88. base.Dispose();
  89. }
  90. }
  91. class _ : Sink<TSource>, IObserver<TSource>
  92. {
  93. private readonly Min<TSource> _parent;
  94. private TSource _lastValue;
  95. public _(Min<TSource> parent, IObserver<TSource> observer, IDisposable cancel)
  96. : base(observer, cancel)
  97. {
  98. _parent = parent;
  99. _lastValue = default(TSource);
  100. }
  101. public void OnNext(TSource value)
  102. {
  103. if (value != null)
  104. {
  105. if (_lastValue == null)
  106. {
  107. _lastValue = value;
  108. }
  109. else
  110. {
  111. var comparison = 0;
  112. try
  113. {
  114. comparison = _parent._comparer.Compare(value, _lastValue);
  115. }
  116. catch (Exception ex)
  117. {
  118. base._observer.OnError(ex);
  119. base.Dispose();
  120. return;
  121. }
  122. if (comparison < 0)
  123. {
  124. _lastValue = value;
  125. }
  126. }
  127. }
  128. }
  129. public void OnError(Exception error)
  130. {
  131. base._observer.OnError(error);
  132. base.Dispose();
  133. }
  134. public void OnCompleted()
  135. {
  136. base._observer.OnNext(_lastValue);
  137. base._observer.OnCompleted();
  138. base.Dispose();
  139. }
  140. }
  141. }
  142. class MinDouble : Producer<double>
  143. {
  144. private readonly IObservable<double> _source;
  145. public MinDouble(IObservable<double> source)
  146. {
  147. _source = source;
  148. }
  149. protected override IDisposable Run(IObserver<double> observer, IDisposable cancel, Action<IDisposable> setSink)
  150. {
  151. var sink = new _(observer, cancel);
  152. setSink(sink);
  153. return _source.SubscribeSafe(sink);
  154. }
  155. class _ : Sink<double>, IObserver<double>
  156. {
  157. private bool _hasValue;
  158. private double _lastValue;
  159. public _(IObserver<double> observer, IDisposable cancel)
  160. : base(observer, cancel)
  161. {
  162. _hasValue = false;
  163. _lastValue = default(double);
  164. }
  165. public void OnNext(double value)
  166. {
  167. if (_hasValue)
  168. {
  169. if (value < _lastValue || double.IsNaN(value))
  170. {
  171. _lastValue = value;
  172. }
  173. }
  174. else
  175. {
  176. _lastValue = value;
  177. _hasValue = true;
  178. }
  179. }
  180. public void OnError(Exception error)
  181. {
  182. base._observer.OnError(error);
  183. base.Dispose();
  184. }
  185. public void OnCompleted()
  186. {
  187. if (!_hasValue)
  188. {
  189. base._observer.OnError(new InvalidOperationException(Strings_Linq.NO_ELEMENTS));
  190. }
  191. else
  192. {
  193. base._observer.OnNext(_lastValue);
  194. base._observer.OnCompleted();
  195. }
  196. base.Dispose();
  197. }
  198. }
  199. }
  200. class MinSingle : Producer<float>
  201. {
  202. private readonly IObservable<float> _source;
  203. public MinSingle(IObservable<float> source)
  204. {
  205. _source = source;
  206. }
  207. protected override IDisposable Run(IObserver<float> observer, IDisposable cancel, Action<IDisposable> setSink)
  208. {
  209. var sink = new _(observer, cancel);
  210. setSink(sink);
  211. return _source.SubscribeSafe(sink);
  212. }
  213. class _ : Sink<float>, IObserver<float>
  214. {
  215. private bool _hasValue;
  216. private float _lastValue;
  217. public _(IObserver<float> observer, IDisposable cancel)
  218. : base(observer, cancel)
  219. {
  220. _hasValue = false;
  221. _lastValue = default(float);
  222. }
  223. public void OnNext(float value)
  224. {
  225. if (_hasValue)
  226. {
  227. if (value < _lastValue || float.IsNaN(value))
  228. {
  229. _lastValue = value;
  230. }
  231. }
  232. else
  233. {
  234. _lastValue = value;
  235. _hasValue = true;
  236. }
  237. }
  238. public void OnError(Exception error)
  239. {
  240. base._observer.OnError(error);
  241. base.Dispose();
  242. }
  243. public void OnCompleted()
  244. {
  245. if (!_hasValue)
  246. {
  247. base._observer.OnError(new InvalidOperationException(Strings_Linq.NO_ELEMENTS));
  248. }
  249. else
  250. {
  251. base._observer.OnNext(_lastValue);
  252. base._observer.OnCompleted();
  253. }
  254. base.Dispose();
  255. }
  256. }
  257. }
  258. class MinDecimal : Producer<decimal>
  259. {
  260. private readonly IObservable<decimal> _source;
  261. public MinDecimal(IObservable<decimal> source)
  262. {
  263. _source = source;
  264. }
  265. protected override IDisposable Run(IObserver<decimal> observer, IDisposable cancel, Action<IDisposable> setSink)
  266. {
  267. var sink = new _(observer, cancel);
  268. setSink(sink);
  269. return _source.SubscribeSafe(sink);
  270. }
  271. class _ : Sink<decimal>, IObserver<decimal>
  272. {
  273. private bool _hasValue;
  274. private decimal _lastValue;
  275. public _(IObserver<decimal> observer, IDisposable cancel)
  276. : base(observer, cancel)
  277. {
  278. _hasValue = false;
  279. _lastValue = default(decimal);
  280. }
  281. public void OnNext(decimal value)
  282. {
  283. if (_hasValue)
  284. {
  285. if (value < _lastValue)
  286. {
  287. _lastValue = value;
  288. }
  289. }
  290. else
  291. {
  292. _lastValue = value;
  293. _hasValue = true;
  294. }
  295. }
  296. public void OnError(Exception error)
  297. {
  298. base._observer.OnError(error);
  299. base.Dispose();
  300. }
  301. public void OnCompleted()
  302. {
  303. if (!_hasValue)
  304. {
  305. base._observer.OnError(new InvalidOperationException(Strings_Linq.NO_ELEMENTS));
  306. }
  307. else
  308. {
  309. base._observer.OnNext(_lastValue);
  310. base._observer.OnCompleted();
  311. }
  312. base.Dispose();
  313. }
  314. }
  315. }
  316. class MinInt32 : Producer<int>
  317. {
  318. private readonly IObservable<int> _source;
  319. public MinInt32(IObservable<int> source)
  320. {
  321. _source = source;
  322. }
  323. protected override IDisposable Run(IObserver<int> observer, IDisposable cancel, Action<IDisposable> setSink)
  324. {
  325. var sink = new _(observer, cancel);
  326. setSink(sink);
  327. return _source.SubscribeSafe(sink);
  328. }
  329. class _ : Sink<int>, IObserver<int>
  330. {
  331. private bool _hasValue;
  332. private int _lastValue;
  333. public _(IObserver<int> observer, IDisposable cancel)
  334. : base(observer, cancel)
  335. {
  336. _hasValue = false;
  337. _lastValue = default(int);
  338. }
  339. public void OnNext(int value)
  340. {
  341. if (_hasValue)
  342. {
  343. if (value < _lastValue)
  344. {
  345. _lastValue = value;
  346. }
  347. }
  348. else
  349. {
  350. _lastValue = value;
  351. _hasValue = true;
  352. }
  353. }
  354. public void OnError(Exception error)
  355. {
  356. base._observer.OnError(error);
  357. base.Dispose();
  358. }
  359. public void OnCompleted()
  360. {
  361. if (!_hasValue)
  362. {
  363. base._observer.OnError(new InvalidOperationException(Strings_Linq.NO_ELEMENTS));
  364. }
  365. else
  366. {
  367. base._observer.OnNext(_lastValue);
  368. base._observer.OnCompleted();
  369. }
  370. base.Dispose();
  371. }
  372. }
  373. }
  374. class MinInt64 : Producer<long>
  375. {
  376. private readonly IObservable<long> _source;
  377. public MinInt64(IObservable<long> source)
  378. {
  379. _source = source;
  380. }
  381. protected override IDisposable Run(IObserver<long> observer, IDisposable cancel, Action<IDisposable> setSink)
  382. {
  383. var sink = new _(observer, cancel);
  384. setSink(sink);
  385. return _source.SubscribeSafe(sink);
  386. }
  387. class _ : Sink<long>, IObserver<long>
  388. {
  389. private bool _hasValue;
  390. private long _lastValue;
  391. public _(IObserver<long> observer, IDisposable cancel)
  392. : base(observer, cancel)
  393. {
  394. _hasValue = false;
  395. _lastValue = default(long);
  396. }
  397. public void OnNext(long value)
  398. {
  399. if (_hasValue)
  400. {
  401. if (value < _lastValue)
  402. {
  403. _lastValue = value;
  404. }
  405. }
  406. else
  407. {
  408. _lastValue = value;
  409. _hasValue = true;
  410. }
  411. }
  412. public void OnError(Exception error)
  413. {
  414. base._observer.OnError(error);
  415. base.Dispose();
  416. }
  417. public void OnCompleted()
  418. {
  419. if (!_hasValue)
  420. {
  421. base._observer.OnError(new InvalidOperationException(Strings_Linq.NO_ELEMENTS));
  422. }
  423. else
  424. {
  425. base._observer.OnNext(_lastValue);
  426. base._observer.OnCompleted();
  427. }
  428. base.Dispose();
  429. }
  430. }
  431. }
  432. class MinDoubleNullable : Producer<double?>
  433. {
  434. private readonly IObservable<double?> _source;
  435. public MinDoubleNullable(IObservable<double?> source)
  436. {
  437. _source = source;
  438. }
  439. protected override IDisposable Run(IObserver<double?> observer, IDisposable cancel, Action<IDisposable> setSink)
  440. {
  441. var sink = new _(observer, cancel);
  442. setSink(sink);
  443. return _source.SubscribeSafe(sink);
  444. }
  445. class _ : Sink<double?>, IObserver<double?>
  446. {
  447. private double? _lastValue;
  448. public _(IObserver<double?> observer, IDisposable cancel)
  449. : base(observer, cancel)
  450. {
  451. _lastValue = default(double?);
  452. }
  453. public void OnNext(double? value)
  454. {
  455. if (!value.HasValue)
  456. return;
  457. if (_lastValue.HasValue)
  458. {
  459. if (value < _lastValue || double.IsNaN((double)value))
  460. {
  461. _lastValue = value;
  462. }
  463. }
  464. else
  465. {
  466. _lastValue = value;
  467. }
  468. }
  469. public void OnError(Exception error)
  470. {
  471. base._observer.OnError(error);
  472. base.Dispose();
  473. }
  474. public void OnCompleted()
  475. {
  476. base._observer.OnNext(_lastValue);
  477. base._observer.OnCompleted();
  478. base.Dispose();
  479. }
  480. }
  481. }
  482. class MinSingleNullable : Producer<float?>
  483. {
  484. private readonly IObservable<float?> _source;
  485. public MinSingleNullable(IObservable<float?> source)
  486. {
  487. _source = source;
  488. }
  489. protected override IDisposable Run(IObserver<float?> observer, IDisposable cancel, Action<IDisposable> setSink)
  490. {
  491. var sink = new _(observer, cancel);
  492. setSink(sink);
  493. return _source.SubscribeSafe(sink);
  494. }
  495. class _ : Sink<float?>, IObserver<float?>
  496. {
  497. private float? _lastValue;
  498. public _(IObserver<float?> observer, IDisposable cancel)
  499. : base(observer, cancel)
  500. {
  501. _lastValue = default(float?);
  502. }
  503. public void OnNext(float? value)
  504. {
  505. if (!value.HasValue)
  506. return;
  507. if (_lastValue.HasValue)
  508. {
  509. if (value < _lastValue || float.IsNaN((float)value))
  510. {
  511. _lastValue = value;
  512. }
  513. }
  514. else
  515. {
  516. _lastValue = value;
  517. }
  518. }
  519. public void OnError(Exception error)
  520. {
  521. base._observer.OnError(error);
  522. base.Dispose();
  523. }
  524. public void OnCompleted()
  525. {
  526. base._observer.OnNext(_lastValue);
  527. base._observer.OnCompleted();
  528. base.Dispose();
  529. }
  530. }
  531. }
  532. class MinDecimalNullable : Producer<decimal?>
  533. {
  534. private readonly IObservable<decimal?> _source;
  535. public MinDecimalNullable(IObservable<decimal?> source)
  536. {
  537. _source = source;
  538. }
  539. protected override IDisposable Run(IObserver<decimal?> observer, IDisposable cancel, Action<IDisposable> setSink)
  540. {
  541. var sink = new _(observer, cancel);
  542. setSink(sink);
  543. return _source.SubscribeSafe(sink);
  544. }
  545. class _ : Sink<decimal?>, IObserver<decimal?>
  546. {
  547. private decimal? _lastValue;
  548. public _(IObserver<decimal?> observer, IDisposable cancel)
  549. : base(observer, cancel)
  550. {
  551. _lastValue = default(decimal?);
  552. }
  553. public void OnNext(decimal? value)
  554. {
  555. if (!value.HasValue)
  556. return;
  557. if (_lastValue.HasValue)
  558. {
  559. if (value < _lastValue)
  560. {
  561. _lastValue = value;
  562. }
  563. }
  564. else
  565. {
  566. _lastValue = value;
  567. }
  568. }
  569. public void OnError(Exception error)
  570. {
  571. base._observer.OnError(error);
  572. base.Dispose();
  573. }
  574. public void OnCompleted()
  575. {
  576. base._observer.OnNext(_lastValue);
  577. base._observer.OnCompleted();
  578. base.Dispose();
  579. }
  580. }
  581. }
  582. class MinInt32Nullable : Producer<int?>
  583. {
  584. private readonly IObservable<int?> _source;
  585. public MinInt32Nullable(IObservable<int?> source)
  586. {
  587. _source = source;
  588. }
  589. protected override IDisposable Run(IObserver<int?> observer, IDisposable cancel, Action<IDisposable> setSink)
  590. {
  591. var sink = new _(observer, cancel);
  592. setSink(sink);
  593. return _source.SubscribeSafe(sink);
  594. }
  595. class _ : Sink<int?>, IObserver<int?>
  596. {
  597. private int? _lastValue;
  598. public _(IObserver<int?> observer, IDisposable cancel)
  599. : base(observer, cancel)
  600. {
  601. _lastValue = default(int?);
  602. }
  603. public void OnNext(int? value)
  604. {
  605. if (!value.HasValue)
  606. return;
  607. if (_lastValue.HasValue)
  608. {
  609. if (value < _lastValue)
  610. {
  611. _lastValue = value;
  612. }
  613. }
  614. else
  615. {
  616. _lastValue = value;
  617. }
  618. }
  619. public void OnError(Exception error)
  620. {
  621. base._observer.OnError(error);
  622. base.Dispose();
  623. }
  624. public void OnCompleted()
  625. {
  626. base._observer.OnNext(_lastValue);
  627. base._observer.OnCompleted();
  628. base.Dispose();
  629. }
  630. }
  631. }
  632. class MinInt64Nullable : Producer<long?>
  633. {
  634. private readonly IObservable<long?> _source;
  635. public MinInt64Nullable(IObservable<long?> source)
  636. {
  637. _source = source;
  638. }
  639. protected override IDisposable Run(IObserver<long?> observer, IDisposable cancel, Action<IDisposable> setSink)
  640. {
  641. var sink = new _(observer, cancel);
  642. setSink(sink);
  643. return _source.SubscribeSafe(sink);
  644. }
  645. class _ : Sink<long?>, IObserver<long?>
  646. {
  647. private long? _lastValue;
  648. public _(IObserver<long?> observer, IDisposable cancel)
  649. : base(observer, cancel)
  650. {
  651. _lastValue = default(long?);
  652. }
  653. public void OnNext(long? value)
  654. {
  655. if (!value.HasValue)
  656. return;
  657. if (_lastValue.HasValue)
  658. {
  659. if (value < _lastValue)
  660. {
  661. _lastValue = value;
  662. }
  663. }
  664. else
  665. {
  666. _lastValue = value;
  667. }
  668. }
  669. public void OnError(Exception error)
  670. {
  671. base._observer.OnError(error);
  672. base.Dispose();
  673. }
  674. public void OnCompleted()
  675. {
  676. base._observer.OnNext(_lastValue);
  677. base._observer.OnCompleted();
  678. base.Dispose();
  679. }
  680. }
  681. }
  682. }
  683. #endif