Count.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. 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. public Count(IObservable<TSource> source)
  13. {
  14. _source = source;
  15. }
  16. public Count(IObservable<TSource> source, Func<TSource, bool> predicate)
  17. {
  18. _source = source;
  19. _predicate = predicate;
  20. }
  21. protected override IDisposable Run(IObserver<int> observer, IDisposable cancel, Action<IDisposable> setSink)
  22. {
  23. if (_predicate == null)
  24. {
  25. var sink = new _(observer, cancel);
  26. setSink(sink);
  27. return _source.SubscribeSafe(sink);
  28. }
  29. else
  30. {
  31. var sink = new CountImpl(this, observer, cancel);
  32. setSink(sink);
  33. return _source.SubscribeSafe(sink);
  34. }
  35. }
  36. class _ : Sink<int>, IObserver<TSource>
  37. {
  38. private int _count;
  39. public _(IObserver<int> observer, IDisposable cancel)
  40. : base(observer, cancel)
  41. {
  42. _count = 0;
  43. }
  44. public void OnNext(TSource value)
  45. {
  46. try
  47. {
  48. checked
  49. {
  50. _count++;
  51. }
  52. }
  53. catch (Exception ex)
  54. {
  55. base._observer.OnError(ex);
  56. base.Dispose();
  57. }
  58. }
  59. public void OnError(Exception error)
  60. {
  61. base._observer.OnError(error);
  62. base.Dispose();
  63. }
  64. public void OnCompleted()
  65. {
  66. base._observer.OnNext(_count);
  67. base._observer.OnCompleted();
  68. base.Dispose();
  69. }
  70. }
  71. class CountImpl : Sink<int>, IObserver<TSource>
  72. {
  73. private readonly Count<TSource> _parent;
  74. private int _count;
  75. public CountImpl(Count<TSource> parent, IObserver<int> observer, IDisposable cancel)
  76. : base(observer, cancel)
  77. {
  78. _parent = parent;
  79. _count = 0;
  80. }
  81. public void OnNext(TSource value)
  82. {
  83. try
  84. {
  85. checked
  86. {
  87. if (_parent._predicate(value))
  88. _count++;
  89. }
  90. }
  91. catch (Exception ex)
  92. {
  93. base._observer.OnError(ex);
  94. base.Dispose();
  95. }
  96. }
  97. public void OnError(Exception error)
  98. {
  99. base._observer.OnError(error);
  100. base.Dispose();
  101. }
  102. public void OnCompleted()
  103. {
  104. base._observer.OnNext(_count);
  105. base._observer.OnCompleted();
  106. base.Dispose();
  107. }
  108. }
  109. }
  110. }
  111. #endif