-
Notifications
You must be signed in to change notification settings - Fork 735
/
Publish.cs
205 lines (180 loc) · 7.26 KB
/
Publish.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// 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;
using System.Collections.Generic;
namespace System.Linq
{
public static partial class EnumerableEx
{
/// <summary>
/// Creates a buffer with a view over the source sequence, causing each enumerator to obtain access to the remainder of
/// the sequence from the current index in the buffer.
/// </summary>
/// <typeparam name="TSource">Source sequence element type.</typeparam>
/// <param name="source">Source sequence.</param>
/// <returns>
/// Buffer enabling each enumerator to retrieve elements from the shared source sequence, starting from the index
/// at the point of obtaining the enumerator.
/// </returns>
/// <example>
/// var rng = Enumerable.Range(0, 10).Publish();
/// var e1 = rng.GetEnumerator(); // e1 has a view on the source starting from element 0
/// Assert.IsTrue(e1.MoveNext());
/// Assert.AreEqual(0, e1.Current);
/// Assert.IsTrue(e1.MoveNext());
/// Assert.AreEqual(1, e1.Current);
/// var e2 = rng.GetEnumerator();
/// Assert.IsTrue(e2.MoveNext()); // e2 has a view on the source starting from element 2
/// Assert.AreEqual(2, e2.Current);
/// Assert.IsTrue(e1.MoveNext()); // e1 continues to enumerate over its view
/// Assert.AreEqual(2, e1.Current);
/// </example>
public static IBuffer<TSource> Publish<TSource>(this IEnumerable<TSource> source)
{
if (source == null)
throw new ArgumentNullException(nameof(source));
return new PublishedBuffer<TSource>(source.GetEnumerator());
}
/// <summary>
/// Publishes the source sequence within a selector function where each enumerator can obtain a view over a tail of the
/// source sequence.
/// </summary>
/// <typeparam name="TSource">Source sequence element type.</typeparam>
/// <typeparam name="TResult">Result sequence element type.</typeparam>
/// <param name="source">Source sequence.</param>
/// <param name="selector">Selector function with published access to the source sequence for each enumerator.</param>
/// <returns>Sequence resulting from applying the selector function to the published view over the source sequence.</returns>
public static IEnumerable<TResult> Publish<TSource, TResult>(this IEnumerable<TSource> source, Func<IEnumerable<TSource>, IEnumerable<TResult>> selector)
{
if (source == null)
throw new ArgumentNullException(nameof(source));
if (selector == null)
throw new ArgumentNullException(nameof(selector));
return Create(() => selector(source.Publish()).GetEnumerator());
}
private sealed class PublishedBuffer<T> : IBuffer<T>
{
private readonly object _gate = new object();
private RefCountList<T> _buffer;
private bool _disposed;
private Exception? _error;
private IEnumerator<T> _source;
private bool _stopped;
public PublishedBuffer(IEnumerator<T> source)
{
_buffer = new RefCountList<T>(0);
_source = source;
}
public IEnumerator<T> GetEnumerator()
{
if (_disposed)
{
throw new ObjectDisposedException("");
}
var i = default(int);
lock (_gate)
{
i = _buffer.Count;
_buffer.ReaderCount++;
}
return GetEnumeratorCore(i);
}
IEnumerator IEnumerable.GetEnumerator()
{
if (_disposed)
{
throw new ObjectDisposedException("");
}
return GetEnumerator();
}
public void Dispose()
{
lock (_gate)
{
if (!_disposed)
{
_source.Dispose();
_source = null;
_buffer.Clear();
_buffer = null;
}
_disposed = true;
}
}
private IEnumerator<T> GetEnumeratorCore(int i)
{
try
{
while (true)
{
if (_disposed)
{
throw new ObjectDisposedException("");
}
var hasValue = default(bool);
var current = default(T);
lock (_gate)
{
if (i >= _buffer.Count)
{
if (!_stopped)
{
try
{
hasValue = _source.MoveNext();
if (hasValue)
{
current = _source.Current;
}
}
catch (Exception ex)
{
_stopped = true;
_error = ex;
_source.Dispose();
}
}
if (_stopped)
{
if (_error != null)
{
throw _error;
}
else
{
break;
}
}
if (hasValue)
{
_buffer.Add(current);
}
}
else
{
hasValue = true;
}
}
if (hasValue)
{
yield return _buffer[i];
}
else
{
break;
}
i++;
}
}
finally
{
if (_buffer != null)
{
_buffer.Done(i + 1);
}
}
}
}
}
}