Min.cs 21 KB

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