forked from ThatRendle/Simple.Data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBufferedEnumerable1.cs
More file actions
205 lines (184 loc) · 6.31 KB
/
BufferedEnumerable1.cs
File metadata and controls
205 lines (184 loc) · 6.31 KB
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
namespace Simple.Data
{
sealed class BufferedEnumerable<T> : IEnumerable<T>
{
private readonly ReaderWriterLockSlim _lock = new ReaderWriterLockSlim();
private readonly List<T> _buffer = new List<T>();
private bool _done;
internal void Iterate(Func<Maybe<T>> iterator)
{
Maybe<T> maybe;
while ((maybe = iterator()).HasValue)
{
Add(maybe.Value);
}
SetDone();
}
private void SetDone()
{
_lock.EnterWriteLock();
try
{
_done = true;
}
finally
{
_lock.ExitWriteLock();
}
}
private void Add(T item)
{
_lock.EnterWriteLock();
try
{
_buffer.Add(item);
}
finally
{
_lock.ExitWriteLock();
}
}
private T this[int index]
{
get
{
_lock.EnterReadLock();
try
{
return _buffer[index];
}
finally
{
_lock.ExitReadLock();
}
}
}
private int Count
{
get
{
_lock.EnterReadLock();
try
{
return _buffer.Count;
}
finally
{
_lock.ExitReadLock();
}
}
}
private bool Done
{
get
{
_lock.EnterReadLock();
try
{
return _done;
}
finally
{
_lock.ExitReadLock();
}
}
}
class BufferedEnumerator : IEnumerator<T>
{
private readonly BufferedEnumerable<T> _bufferedEnumerable;
public BufferedEnumerator(BufferedEnumerable<T> bufferedEnumerable)
{
_bufferedEnumerable = bufferedEnumerable;
}
private int _current = -1;
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
/// <filterpriority>2</filterpriority>
public void Dispose()
{
}
/// <summary>
/// Advances the enumerator to the next element of the collection.
/// </summary>
/// <returns>
/// true if the enumerator was successfully advanced to the next element; false if the enumerator has passed the end of the collection.
/// </returns>
/// <exception cref="T:System.InvalidOperationException">The collection was modified after the enumerator was created. </exception><filterpriority>2</filterpriority>
public bool MoveNext()
{
++_current;
if (_bufferedEnumerable._done)
{
return _current < _bufferedEnumerable.Count;
}
WaitForBuffer();
return _current < _bufferedEnumerable.Count;
}
private void WaitForBuffer()
{
// Block until next item delivered or iterator is done.
SpinWait.SpinUntil(() => _bufferedEnumerable.Done || _bufferedEnumerable.Count > _current);
}
/// <summary>
/// Sets the enumerator to its initial position, which is before the first element in the collection.
/// </summary>
/// <exception cref="T:System.InvalidOperationException">The collection was modified after the enumerator was created. </exception><filterpriority>2</filterpriority>
public void Reset()
{
_current = -1;
}
/// <summary>
/// Gets the element in the collection at the current position of the enumerator.
/// </summary>
/// <returns>
/// The element in the collection at the current position of the enumerator.
/// </returns>
public T Current
{
get
{
if (_current >= _bufferedEnumerable.Count) throw new InvalidOperationException();
return _bufferedEnumerable[_current];
}
}
/// <summary>
/// Gets the current element in the collection.
/// </summary>
/// <returns>
/// The current element in the collection.
/// </returns>
/// <exception cref="T:System.InvalidOperationException">The enumerator is positioned before the first element of the collection or after the last element.</exception><filterpriority>2</filterpriority>
object IEnumerator.Current
{
get { return Current; }
}
}
/// <summary>
/// Returns an enumerator that iterates through the collection.
/// </summary>
/// <returns>
/// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
/// </returns>
/// <filterpriority>1</filterpriority>
public IEnumerator<T> GetEnumerator()
{
return new BufferedEnumerator(this);
}
/// <summary>
/// Returns an enumerator that iterates through a collection.
/// </summary>
/// <returns>
/// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
/// </returns>
/// <filterpriority>2</filterpriority>
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}