| 1 |
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. |
| 2 |
|
| 3 |
#if !NO_PERF |
| 4 |
using System; |
| 5 |
|
| 6 |
namespace System.Reactive.Linq.ObservableImpl |
| 7 |
{ |
| 8 |
class Count<TSource> : Producer<int> |
| 9 |
{ |
| 10 |
private readonly IObservable<TSource> _source; |
| 11 |
private readonly Func<TSource, bool> _predicate; |
| 12 |
|
| 13 |
public Count(IObservable<TSource> source) |
| 14 |
{ |
| 15 |
_source = source; |
| 16 |
} |
| 17 |
|
| 18 |
public Count(IObservable<TSource> source, Func<TSource, bool> predicate) |
| 19 |
{ |
| 20 |
_source = source; |
| 21 |
_predicate = predicate; |
| 22 |
} |
| 23 |
|
| 24 |
protected override IDisposable Run(IObserver<int> observer, IDisposable cancel, Action<IDisposable> setSink) |
| 25 |
{ |
| 26 |
if (_predicate == null) |
| 27 |
{ |
| 28 |
var sink = new _(observer, cancel); |
| 29 |
setSink(sink); |
| 30 |
return _source.SubscribeSafe(sink); |
| 31 |
} |
| 32 |
else |
| 33 |
{ |
| 34 |
var sink = new CountImpl(this, observer, cancel); |
| 35 |
setSink(sink); |
| 36 |
return _source.SubscribeSafe(sink); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
class _ : Sink<int>, IObserver<TSource> |
| 41 |
{ |
| 42 |
private int _count; |
| 43 |
|
| 44 |
public _(IObserver<int> observer, IDisposable cancel) |
| 45 |
: base(observer, cancel) |
| 46 |
{ |
| 47 |
_count = 0; |
| 48 |
} |
| 49 |
|
| 50 |
public void OnNext(TSource value) |
| 51 |
{ |
| 52 |
try |
| 53 |
{ |
| 54 |
checked |
| 55 |
{ |
| 56 |
_count++; |
| 57 |
} |
| 58 |
} |
| 59 |
catch (Exception ex) |
| 60 |
{ |
| 61 |
base._observer.OnError(ex); |
| 62 |
base.Dispose(); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
public void OnError(Exception error) |
| 67 |
{ |
| 68 |
base._observer.OnError(error); |
| 69 |
base.Dispose(); |
| 70 |
} |
| 71 |
|
| 72 |
public void OnCompleted() |
| 73 |
{ |
| 74 |
base._observer.OnNext(_count); |
| 75 |
base._observer.OnCompleted(); |
| 76 |
base.Dispose(); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
class CountImpl : Sink<int>, IObserver<TSource> |
| 81 |
{ |
| 82 |
private readonly Count<TSource> _parent; |
| 83 |
private int _count; |
| 84 |
|
| 85 |
public CountImpl(Count<TSource> parent, IObserver<int> observer, IDisposable cancel) |
| 86 |
: base(observer, cancel) |
| 87 |
{ |
| 88 |
_parent = parent; |
| 89 |
_count = 0; |
| 90 |
} |
| 91 |
|
| 92 |
public void OnNext(TSource value) |
| 93 |
{ |
| 94 |
try |
| 95 |
{ |
| 96 |
checked |
| 97 |
{ |
| 98 |
if (_parent._predicate(value)) |
| 99 |
_count++; |
| 100 |
} |
| 101 |
} |
| 102 |
catch (Exception ex) |
| 103 |
{ |
| 104 |
base._observer.OnError(ex); |
| 105 |
base.Dispose(); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
public void OnError(Exception error) |
| 110 |
{ |
| 111 |
base._observer.OnError(error); |
| 112 |
base.Dispose(); |
| 113 |
} |
| 114 |
|
| 115 |
public void OnCompleted() |
| 116 |
{ |
| 117 |
base._observer.OnNext(_count); |
| 118 |
base._observer.OnCompleted(); |
| 119 |
base.Dispose(); |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
#endif |