123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792 |
- // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
- #if !NO_PERF
- using System;
- using System.Collections.Generic;
- namespace System.Reactive.Linq.Observαble
- {
- class Min<TSource> : Producer<TSource>
- {
- private readonly IObservable<TSource> _source;
- private readonly IComparer<TSource> _comparer;
- public Min(IObservable<TSource> source, IComparer<TSource> comparer)
- {
- _source = source;
- _comparer = comparer;
- }
- protected override IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink)
- {
- // LINQ to Objects makes this distinction in order to make [Min|Max] of an empty collection of reference type objects equal to null.
- if (default(TSource) == null)
- {
- var sink = new _(this, observer, cancel);
- setSink(sink);
- return _source.SubscribeSafe(sink);
- }
- else
- {
- var sink = new δ(this, observer, cancel);
- setSink(sink);
- return _source.SubscribeSafe(sink);
- }
- }
- class δ : Sink<TSource>, IObserver<TSource>
- {
- private readonly Min<TSource> _parent;
- private bool _hasValue;
- private TSource _lastValue;
- public δ(Min<TSource> parent, IObserver<TSource> observer, IDisposable cancel)
- : base(observer, cancel)
- {
- _parent = parent;
- _hasValue = false;
- _lastValue = default(TSource);
- }
- public void OnNext(TSource value)
- {
- if (_hasValue)
- {
- var comparison = 0;
- try
- {
- comparison = _parent._comparer.Compare(value, _lastValue);
- }
- catch (Exception ex)
- {
- base._observer.OnError(ex);
- base.Dispose();
- return;
- }
- if (comparison < 0)
- {
- _lastValue = value;
- }
- }
- else
- {
- _hasValue = true;
- _lastValue = value;
- }
- }
- public void OnError(Exception error)
- {
- base._observer.OnError(error);
- base.Dispose();
- }
- public void OnCompleted()
- {
- if (!_hasValue)
- {
- base._observer.OnError(new InvalidOperationException(Strings_Linq.NO_ELEMENTS));
- }
- else
- {
- base._observer.OnNext(_lastValue);
- base._observer.OnCompleted();
- }
- base.Dispose();
- }
- }
- class _ : Sink<TSource>, IObserver<TSource>
- {
- private readonly Min<TSource> _parent;
- private TSource _lastValue;
- public _(Min<TSource> parent, IObserver<TSource> observer, IDisposable cancel)
- : base(observer, cancel)
- {
- _parent = parent;
- _lastValue = default(TSource);
- }
- public void OnNext(TSource value)
- {
- if (value != null)
- {
- if (_lastValue == null)
- {
- _lastValue = value;
- }
- else
- {
- var comparison = 0;
- try
- {
- comparison = _parent._comparer.Compare(value, _lastValue);
- }
- catch (Exception ex)
- {
- base._observer.OnError(ex);
- base.Dispose();
- return;
- }
- if (comparison < 0)
- {
- _lastValue = value;
- }
- }
- }
- }
- public void OnError(Exception error)
- {
- base._observer.OnError(error);
- base.Dispose();
- }
- public void OnCompleted()
- {
- base._observer.OnNext(_lastValue);
- base._observer.OnCompleted();
- base.Dispose();
- }
- }
- }
- class MinDouble : Producer<double>
- {
- private readonly IObservable<double> _source;
- public MinDouble(IObservable<double> source)
- {
- _source = source;
- }
- protected override IDisposable Run(IObserver<double> observer, IDisposable cancel, Action<IDisposable> setSink)
- {
- var sink = new _(observer, cancel);
- setSink(sink);
- return _source.SubscribeSafe(sink);
- }
- class _ : Sink<double>, IObserver<double>
- {
- private bool _hasValue;
- private double _lastValue;
- public _(IObserver<double> observer, IDisposable cancel)
- : base(observer, cancel)
- {
- _hasValue = false;
- _lastValue = default(double);
- }
- public void OnNext(double value)
- {
- if (_hasValue)
- {
- if (value < _lastValue || double.IsNaN(value))
- {
- _lastValue = value;
- }
- }
- else
- {
- _lastValue = value;
- _hasValue = true;
- }
- }
- public void OnError(Exception error)
- {
- base._observer.OnError(error);
- base.Dispose();
- }
- public void OnCompleted()
- {
- if (!_hasValue)
- {
- base._observer.OnError(new InvalidOperationException(Strings_Linq.NO_ELEMENTS));
- }
- else
- {
- base._observer.OnNext(_lastValue);
- base._observer.OnCompleted();
- }
- base.Dispose();
- }
- }
- }
- class MinSingle : Producer<float>
- {
- private readonly IObservable<float> _source;
- public MinSingle(IObservable<float> source)
- {
- _source = source;
- }
- protected override IDisposable Run(IObserver<float> observer, IDisposable cancel, Action<IDisposable> setSink)
- {
- var sink = new _(observer, cancel);
- setSink(sink);
- return _source.SubscribeSafe(sink);
- }
- class _ : Sink<float>, IObserver<float>
- {
- private bool _hasValue;
- private float _lastValue;
- public _(IObserver<float> observer, IDisposable cancel)
- : base(observer, cancel)
- {
- _hasValue = false;
- _lastValue = default(float);
- }
- public void OnNext(float value)
- {
- if (_hasValue)
- {
- if (value < _lastValue || float.IsNaN(value))
- {
- _lastValue = value;
- }
- }
- else
- {
- _lastValue = value;
- _hasValue = true;
- }
- }
- public void OnError(Exception error)
- {
- base._observer.OnError(error);
- base.Dispose();
- }
- public void OnCompleted()
- {
- if (!_hasValue)
- {
- base._observer.OnError(new InvalidOperationException(Strings_Linq.NO_ELEMENTS));
- }
- else
- {
- base._observer.OnNext(_lastValue);
- base._observer.OnCompleted();
- }
- base.Dispose();
- }
- }
- }
- class MinDecimal : Producer<decimal>
- {
- private readonly IObservable<decimal> _source;
- public MinDecimal(IObservable<decimal> source)
- {
- _source = source;
- }
- protected override IDisposable Run(IObserver<decimal> observer, IDisposable cancel, Action<IDisposable> setSink)
- {
- var sink = new _(observer, cancel);
- setSink(sink);
- return _source.SubscribeSafe(sink);
- }
- class _ : Sink<decimal>, IObserver<decimal>
- {
- private bool _hasValue;
- private decimal _lastValue;
- public _(IObserver<decimal> observer, IDisposable cancel)
- : base(observer, cancel)
- {
- _hasValue = false;
- _lastValue = default(decimal);
- }
- public void OnNext(decimal value)
- {
- if (_hasValue)
- {
- if (value < _lastValue)
- {
- _lastValue = value;
- }
- }
- else
- {
- _lastValue = value;
- _hasValue = true;
- }
- }
- public void OnError(Exception error)
- {
- base._observer.OnError(error);
- base.Dispose();
- }
- public void OnCompleted()
- {
- if (!_hasValue)
- {
- base._observer.OnError(new InvalidOperationException(Strings_Linq.NO_ELEMENTS));
- }
- else
- {
- base._observer.OnNext(_lastValue);
- base._observer.OnCompleted();
- }
- base.Dispose();
- }
- }
- }
- class MinInt32 : Producer<int>
- {
- private readonly IObservable<int> _source;
- public MinInt32(IObservable<int> source)
- {
- _source = source;
- }
- protected override IDisposable Run(IObserver<int> observer, IDisposable cancel, Action<IDisposable> setSink)
- {
- var sink = new _(observer, cancel);
- setSink(sink);
- return _source.SubscribeSafe(sink);
- }
- class _ : Sink<int>, IObserver<int>
- {
- private bool _hasValue;
- private int _lastValue;
- public _(IObserver<int> observer, IDisposable cancel)
- : base(observer, cancel)
- {
- _hasValue = false;
- _lastValue = default(int);
- }
- public void OnNext(int value)
- {
- if (_hasValue)
- {
- if (value < _lastValue)
- {
- _lastValue = value;
- }
- }
- else
- {
- _lastValue = value;
- _hasValue = true;
- }
- }
- public void OnError(Exception error)
- {
- base._observer.OnError(error);
- base.Dispose();
- }
- public void OnCompleted()
- {
- if (!_hasValue)
- {
- base._observer.OnError(new InvalidOperationException(Strings_Linq.NO_ELEMENTS));
- }
- else
- {
- base._observer.OnNext(_lastValue);
- base._observer.OnCompleted();
- }
- base.Dispose();
- }
- }
- }
- class MinInt64 : Producer<long>
- {
- private readonly IObservable<long> _source;
- public MinInt64(IObservable<long> source)
- {
- _source = source;
- }
- protected override IDisposable Run(IObserver<long> observer, IDisposable cancel, Action<IDisposable> setSink)
- {
- var sink = new _(observer, cancel);
- setSink(sink);
- return _source.SubscribeSafe(sink);
- }
- class _ : Sink<long>, IObserver<long>
- {
- private bool _hasValue;
- private long _lastValue;
- public _(IObserver<long> observer, IDisposable cancel)
- : base(observer, cancel)
- {
- _hasValue = false;
- _lastValue = default(long);
- }
- public void OnNext(long value)
- {
- if (_hasValue)
- {
- if (value < _lastValue)
- {
- _lastValue = value;
- }
- }
- else
- {
- _lastValue = value;
- _hasValue = true;
- }
- }
- public void OnError(Exception error)
- {
- base._observer.OnError(error);
- base.Dispose();
- }
- public void OnCompleted()
- {
- if (!_hasValue)
- {
- base._observer.OnError(new InvalidOperationException(Strings_Linq.NO_ELEMENTS));
- }
- else
- {
- base._observer.OnNext(_lastValue);
- base._observer.OnCompleted();
- }
- base.Dispose();
- }
- }
- }
- class MinDoubleNullable : Producer<double?>
- {
- private readonly IObservable<double?> _source;
- public MinDoubleNullable(IObservable<double?> source)
- {
- _source = source;
- }
- protected override IDisposable Run(IObserver<double?> observer, IDisposable cancel, Action<IDisposable> setSink)
- {
- var sink = new _(observer, cancel);
- setSink(sink);
- return _source.SubscribeSafe(sink);
- }
- class _ : Sink<double?>, IObserver<double?>
- {
- private double? _lastValue;
- public _(IObserver<double?> observer, IDisposable cancel)
- : base(observer, cancel)
- {
- _lastValue = default(double?);
- }
- public void OnNext(double? value)
- {
- if (!value.HasValue)
- return;
- if (_lastValue.HasValue)
- {
- if (value < _lastValue || double.IsNaN((double)value))
- {
- _lastValue = value;
- }
- }
- else
- {
- _lastValue = value;
- }
- }
- public void OnError(Exception error)
- {
- base._observer.OnError(error);
- base.Dispose();
- }
- public void OnCompleted()
- {
- base._observer.OnNext(_lastValue);
- base._observer.OnCompleted();
- base.Dispose();
- }
- }
- }
- class MinSingleNullable : Producer<float?>
- {
- private readonly IObservable<float?> _source;
- public MinSingleNullable(IObservable<float?> source)
- {
- _source = source;
- }
- protected override IDisposable Run(IObserver<float?> observer, IDisposable cancel, Action<IDisposable> setSink)
- {
- var sink = new _(observer, cancel);
- setSink(sink);
- return _source.SubscribeSafe(sink);
- }
- class _ : Sink<float?>, IObserver<float?>
- {
- private float? _lastValue;
- public _(IObserver<float?> observer, IDisposable cancel)
- : base(observer, cancel)
- {
- _lastValue = default(float?);
- }
- public void OnNext(float? value)
- {
- if (!value.HasValue)
- return;
- if (_lastValue.HasValue)
- {
- if (value < _lastValue || float.IsNaN((float)value))
- {
- _lastValue = value;
- }
- }
- else
- {
- _lastValue = value;
- }
- }
- public void OnError(Exception error)
- {
- base._observer.OnError(error);
- base.Dispose();
- }
- public void OnCompleted()
- {
- base._observer.OnNext(_lastValue);
- base._observer.OnCompleted();
- base.Dispose();
- }
- }
- }
- class MinDecimalNullable : Producer<decimal?>
- {
- private readonly IObservable<decimal?> _source;
- public MinDecimalNullable(IObservable<decimal?> source)
- {
- _source = source;
- }
- protected override IDisposable Run(IObserver<decimal?> observer, IDisposable cancel, Action<IDisposable> setSink)
- {
- var sink = new _(observer, cancel);
- setSink(sink);
- return _source.SubscribeSafe(sink);
- }
- class _ : Sink<decimal?>, IObserver<decimal?>
- {
- private decimal? _lastValue;
- public _(IObserver<decimal?> observer, IDisposable cancel)
- : base(observer, cancel)
- {
- _lastValue = default(decimal?);
- }
- public void OnNext(decimal? value)
- {
- if (!value.HasValue)
- return;
- if (_lastValue.HasValue)
- {
- if (value < _lastValue)
- {
- _lastValue = value;
- }
- }
- else
- {
- _lastValue = value;
- }
- }
- public void OnError(Exception error)
- {
- base._observer.OnError(error);
- base.Dispose();
- }
- public void OnCompleted()
- {
- base._observer.OnNext(_lastValue);
- base._observer.OnCompleted();
- base.Dispose();
- }
- }
- }
- class MinInt32Nullable : Producer<int?>
- {
- private readonly IObservable<int?> _source;
- public MinInt32Nullable(IObservable<int?> source)
- {
- _source = source;
- }
- protected override IDisposable Run(IObserver<int?> observer, IDisposable cancel, Action<IDisposable> setSink)
- {
- var sink = new _(observer, cancel);
- setSink(sink);
- return _source.SubscribeSafe(sink);
- }
- class _ : Sink<int?>, IObserver<int?>
- {
- private int? _lastValue;
- public _(IObserver<int?> observer, IDisposable cancel)
- : base(observer, cancel)
- {
- _lastValue = default(int?);
- }
- public void OnNext(int? value)
- {
- if (!value.HasValue)
- return;
- if (_lastValue.HasValue)
- {
- if (value < _lastValue)
- {
- _lastValue = value;
- }
- }
- else
- {
- _lastValue = value;
- }
- }
- public void OnError(Exception error)
- {
- base._observer.OnError(error);
- base.Dispose();
- }
- public void OnCompleted()
- {
- base._observer.OnNext(_lastValue);
- base._observer.OnCompleted();
- base.Dispose();
- }
- }
- }
- class MinInt64Nullable : Producer<long?>
- {
- private readonly IObservable<long?> _source;
- public MinInt64Nullable(IObservable<long?> source)
- {
- _source = source;
- }
- protected override IDisposable Run(IObserver<long?> observer, IDisposable cancel, Action<IDisposable> setSink)
- {
- var sink = new _(observer, cancel);
- setSink(sink);
- return _source.SubscribeSafe(sink);
- }
- class _ : Sink<long?>, IObserver<long?>
- {
- private long? _lastValue;
- public _(IObserver<long?> observer, IDisposable cancel)
- : base(observer, cancel)
- {
- _lastValue = default(long?);
- }
- public void OnNext(long? value)
- {
- if (!value.HasValue)
- return;
- if (_lastValue.HasValue)
- {
- if (value < _lastValue)
- {
- _lastValue = value;
- }
- }
- else
- {
- _lastValue = value;
- }
- }
- public void OnError(Exception error)
- {
- base._observer.OnError(error);
- base.Dispose();
- }
- public void OnCompleted()
- {
- base._observer.OnNext(_lastValue);
- base._observer.OnCompleted();
- base.Dispose();
- }
- }
- }
- }
- #endif
|