GroupBy.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. using System.Linq;
  6. using System.Reactive.Disposables;
  7. using System.Reactive.Subjects;
  8. namespace System.Reactive.Linq.Observαble
  9. {
  10. class GroupBy<TSource, TKey, TElement> : Producer<IGroupedObservable<TKey, TElement>>
  11. {
  12. private readonly IObservable<TSource> _source;
  13. private readonly Func<TSource, TKey> _keySelector;
  14. private readonly Func<TSource, TElement> _elementSelector;
  15. private readonly IEqualityComparer<TKey> _comparer;
  16. public GroupBy(IObservable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey> comparer)
  17. {
  18. _source = source;
  19. _keySelector = keySelector;
  20. _elementSelector = elementSelector;
  21. _comparer = comparer;
  22. }
  23. private CompositeDisposable _groupDisposable;
  24. private RefCountDisposable _refCountDisposable;
  25. protected override IDisposable Run(IObserver<IGroupedObservable<TKey, TElement>> observer, IDisposable cancel, Action<IDisposable> setSink)
  26. {
  27. _groupDisposable = new CompositeDisposable();
  28. _refCountDisposable = new RefCountDisposable(_groupDisposable);
  29. var sink = new _(this, observer, cancel);
  30. setSink(sink);
  31. _groupDisposable.Add(_source.SubscribeSafe(sink));
  32. return _refCountDisposable;
  33. }
  34. class _ : Sink<IGroupedObservable<TKey, TElement>>, IObserver<TSource>
  35. {
  36. private readonly GroupBy<TSource, TKey, TElement> _parent;
  37. private readonly Dictionary<TKey, ISubject<TElement>> _map;
  38. public _(GroupBy<TSource, TKey, TElement> parent, IObserver<IGroupedObservable<TKey, TElement>> observer, IDisposable cancel)
  39. : base(observer, cancel)
  40. {
  41. _parent = parent;
  42. _map = new Dictionary<TKey, ISubject<TElement>>(_parent._comparer);
  43. }
  44. public void OnNext(TSource value)
  45. {
  46. var key = default(TKey);
  47. try
  48. {
  49. key = _parent._keySelector(value);
  50. }
  51. catch (Exception exception)
  52. {
  53. Error(exception);
  54. return;
  55. }
  56. var fireNewMapEntry = false;
  57. var writer = default(ISubject<TElement>);
  58. try
  59. {
  60. if (!_map.TryGetValue(key, out writer))
  61. {
  62. writer = new Subject<TElement>();
  63. _map.Add(key, writer);
  64. fireNewMapEntry = true;
  65. }
  66. }
  67. catch (Exception exception)
  68. {
  69. Error(exception);
  70. return;
  71. }
  72. if (fireNewMapEntry)
  73. {
  74. var group = new GroupedObservable<TKey, TElement>(key, writer, _parent._refCountDisposable);
  75. _observer.OnNext(group);
  76. }
  77. var element = default(TElement);
  78. try
  79. {
  80. element = _parent._elementSelector(value);
  81. }
  82. catch (Exception exception)
  83. {
  84. Error(exception);
  85. return;
  86. }
  87. writer.OnNext(element);
  88. }
  89. public void OnError(Exception error)
  90. {
  91. Error(error);
  92. }
  93. public void OnCompleted()
  94. {
  95. foreach (var w in _map.Values)
  96. w.OnCompleted();
  97. base._observer.OnCompleted();
  98. base.Dispose();
  99. }
  100. private void Error(Exception exception)
  101. {
  102. foreach (var w in _map.Values)
  103. w.OnError(exception);
  104. base._observer.OnError(exception);
  105. base.Dispose();
  106. }
  107. }
  108. }
  109. }
  110. #endif