GroupBy.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. using System.Collections.Generic;
  5. using System.Reactive.Disposables;
  6. using System.Reactive.Subjects;
  7. namespace System.Reactive.Linq.ObservableImpl
  8. {
  9. internal sealed class GroupBy<TSource, TKey, TElement> : Producer<IGroupedObservable<TKey, TElement>>
  10. {
  11. private readonly IObservable<TSource> _source;
  12. private readonly Func<TSource, TKey> _keySelector;
  13. private readonly Func<TSource, TElement> _elementSelector;
  14. private readonly int? _capacity;
  15. private readonly IEqualityComparer<TKey> _comparer;
  16. public GroupBy(IObservable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, int? capacity, IEqualityComparer<TKey> comparer)
  17. {
  18. _source = source;
  19. _keySelector = keySelector;
  20. _elementSelector = elementSelector;
  21. _capacity = capacity;
  22. _comparer = comparer;
  23. }
  24. private CompositeDisposable _groupDisposable;
  25. private RefCountDisposable _refCountDisposable;
  26. protected override IDisposable Run(IObserver<IGroupedObservable<TKey, TElement>> observer, IDisposable cancel, Action<IDisposable> setSink)
  27. {
  28. _groupDisposable = new CompositeDisposable();
  29. _refCountDisposable = new RefCountDisposable(_groupDisposable);
  30. var sink = new _(this, observer, cancel);
  31. setSink(sink);
  32. _groupDisposable.Add(_source.SubscribeSafe(sink));
  33. return _refCountDisposable;
  34. }
  35. class _ : Sink<IGroupedObservable<TKey, TElement>>, IObserver<TSource>
  36. {
  37. private readonly GroupBy<TSource, TKey, TElement> _parent;
  38. private readonly Dictionary<TKey, ISubject<TElement>> _map;
  39. private ISubject<TElement> _null;
  40. public _(GroupBy<TSource, TKey, TElement> parent, IObserver<IGroupedObservable<TKey, TElement>> observer, IDisposable cancel)
  41. : base(observer, cancel)
  42. {
  43. _parent = parent;
  44. if (_parent._capacity.HasValue)
  45. {
  46. _map = new Dictionary<TKey, ISubject<TElement>>(_parent._capacity.Value, _parent._comparer);
  47. }
  48. else
  49. {
  50. _map = new Dictionary<TKey, ISubject<TElement>>(_parent._comparer);
  51. }
  52. }
  53. public void OnNext(TSource value)
  54. {
  55. var key = default(TKey);
  56. try
  57. {
  58. key = _parent._keySelector(value);
  59. }
  60. catch (Exception exception)
  61. {
  62. Error(exception);
  63. return;
  64. }
  65. var fireNewMapEntry = false;
  66. var writer = default(ISubject<TElement>);
  67. try
  68. {
  69. //
  70. // Note: The box instruction in the IL will be erased by the JIT in case T is
  71. // a value type. In fact, the whole if block will go away and we'll end
  72. // up with nothing but the TryGetValue check below.
  73. //
  74. // // var fireNewMapEntry = false;
  75. // C:\Projects\Rx\Rx\Experimental\Main\Source\Rx\System.Reactive.Linq\Reactive\Linq\Observable\GroupBy.cs @ 67:
  76. // 000007fb`6d544b80 48c7452800000000 mov qword ptr [rbp+28h],0
  77. //
  78. // // var writer = default(ISubject<TElement>);
  79. // C:\Projects\Rx\Rx\Experimental\Main\Source\Rx\System.Reactive.Linq\Reactive\Linq\Observable\GroupBy.cs @ 66:
  80. // 000007fb`6d544b88 c6453400 mov byte ptr [rbp+34h],0
  81. //
  82. // // if (!_map.TryGetValue(key, out writer))
  83. // C:\Projects\Rx\Rx\Experimental\Main\Source\Rx\System.Reactive.Linq\Reactive\Linq\Observable\GroupBy.cs @ 86:
  84. // 000007fb`6d544b8c 488b4560 mov rax,qword ptr [rbp+60h]
  85. // ...
  86. //
  87. if (key == null)
  88. {
  89. if (_null == null)
  90. {
  91. _null = new Subject<TElement>();
  92. fireNewMapEntry = true;
  93. }
  94. writer = _null;
  95. }
  96. else
  97. {
  98. if (!_map.TryGetValue(key, out writer))
  99. {
  100. writer = new Subject<TElement>();
  101. _map.Add(key, writer);
  102. fireNewMapEntry = true;
  103. }
  104. }
  105. }
  106. catch (Exception exception)
  107. {
  108. Error(exception);
  109. return;
  110. }
  111. if (fireNewMapEntry)
  112. {
  113. var group = new GroupedObservable<TKey, TElement>(key, writer, _parent._refCountDisposable);
  114. _observer.OnNext(group);
  115. }
  116. var element = default(TElement);
  117. try
  118. {
  119. element = _parent._elementSelector(value);
  120. }
  121. catch (Exception exception)
  122. {
  123. Error(exception);
  124. return;
  125. }
  126. writer.OnNext(element);
  127. }
  128. public void OnError(Exception error)
  129. {
  130. Error(error);
  131. }
  132. public void OnCompleted()
  133. {
  134. if (_null != null)
  135. _null.OnCompleted();
  136. foreach (var w in _map.Values)
  137. w.OnCompleted();
  138. base._observer.OnCompleted();
  139. base.Dispose();
  140. }
  141. private void Error(Exception exception)
  142. {
  143. if (_null != null)
  144. _null.OnError(exception);
  145. foreach (var w in _map.Values)
  146. w.OnError(exception);
  147. base._observer.OnError(exception);
  148. base.Dispose();
  149. }
  150. }
  151. }
  152. }