-
Notifications
You must be signed in to change notification settings - Fork 735
/
GroupBy.cs
177 lines (153 loc) · 6.55 KB
/
GroupBy.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
using System.Reactive.Disposables;
using System.Reactive.Subjects;
namespace System.Reactive.Linq.ObservableImpl
{
internal sealed class GroupBy<TSource, TKey, TElement> : Producer<IGroupedObservable<TKey, TElement>, GroupBy<TSource, TKey, TElement>._>
{
private readonly IObservable<TSource> _source;
private readonly Func<TSource, TKey> _keySelector;
private readonly Func<TSource, TElement> _elementSelector;
private readonly int? _capacity;
private readonly IEqualityComparer<TKey> _comparer;
public GroupBy(IObservable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, int? capacity, IEqualityComparer<TKey> comparer)
{
_source = source;
_keySelector = keySelector;
_elementSelector = elementSelector;
_capacity = capacity;
_comparer = comparer;
}
protected override _ CreateSink(IObserver<IGroupedObservable<TKey, TElement>> observer) => new _(this, observer);
protected override void Run(_ sink) => sink.Run(_source);
internal sealed class _ : Sink<TSource, IGroupedObservable<TKey, TElement>>
{
private readonly Func<TSource, TKey> _keySelector;
private readonly Func<TSource, TElement> _elementSelector;
private readonly Dictionary<TKey, Subject<TElement>> _map;
private RefCountDisposable _refCountDisposable;
private Subject<TElement> _null;
public _(GroupBy<TSource, TKey, TElement> parent, IObserver<IGroupedObservable<TKey, TElement>> observer)
: base(observer)
{
_keySelector = parent._keySelector;
_elementSelector = parent._elementSelector;
if (parent._capacity.HasValue)
{
_map = new Dictionary<TKey, Subject<TElement>>(parent._capacity.Value, parent._comparer);
}
else
{
_map = new Dictionary<TKey, Subject<TElement>>(parent._comparer);
}
}
public override void Run(IObservable<TSource> source)
{
var sourceSubscription = new SingleAssignmentDisposable();
_refCountDisposable = new RefCountDisposable(sourceSubscription);
sourceSubscription.Disposable = source.SubscribeSafe(this);
SetUpstream(_refCountDisposable);
}
public override void OnNext(TSource value)
{
TKey key;
try
{
key = _keySelector(value);
}
catch (Exception exception)
{
Error(exception);
return;
}
var fireNewMapEntry = false;
var writer = default(Subject<TElement>);
try
{
//
// Note: The box instruction in the IL will be erased by the JIT in case T is
// a value type. In fact, the whole if block will go away and we'll end
// up with nothing but the TryGetValue check below.
//
// // var fireNewMapEntry = false;
// C:\Projects\Rx\Rx\Experimental\Main\Source\Rx\System.Reactive.Linq\Reactive\Linq\Observable\GroupBy.cs @ 67:
// 000007fb`6d544b80 48c7452800000000 mov qword ptr [rbp+28h],0
//
// // var writer = default(ISubject<TElement>);
// C:\Projects\Rx\Rx\Experimental\Main\Source\Rx\System.Reactive.Linq\Reactive\Linq\Observable\GroupBy.cs @ 66:
// 000007fb`6d544b88 c6453400 mov byte ptr [rbp+34h],0
//
// // if (!_map.TryGetValue(key, out writer))
// C:\Projects\Rx\Rx\Experimental\Main\Source\Rx\System.Reactive.Linq\Reactive\Linq\Observable\GroupBy.cs @ 86:
// 000007fb`6d544b8c 488b4560 mov rax,qword ptr [rbp+60h]
// ...
//
if (key == null)
{
if (_null == null)
{
_null = new Subject<TElement>();
fireNewMapEntry = true;
}
writer = _null;
}
else
{
if (!_map.TryGetValue(key, out writer))
{
writer = new Subject<TElement>();
_map.Add(key, writer);
fireNewMapEntry = true;
}
}
}
catch (Exception exception)
{
Error(exception);
return;
}
if (fireNewMapEntry)
{
var group = new GroupedObservable<TKey, TElement>(key, writer, _refCountDisposable);
ForwardOnNext(group);
}
TElement element;
try
{
element = _elementSelector(value);
}
catch (Exception exception)
{
Error(exception);
return;
}
writer.OnNext(element);
}
public override void OnError(Exception error)
{
Error(error);
}
public override void OnCompleted()
{
_null?.OnCompleted();
foreach (var w in _map.Values)
{
w.OnCompleted();
}
ForwardOnCompleted();
}
private void Error(Exception exception)
{
_null?.OnError(exception);
foreach (var w in _map.Values)
{
w.OnError(exception);
}
ForwardOnError(exception);
}
}
}
}