-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCircularBuffer.cs
More file actions
337 lines (222 loc) · 9.94 KB
/
CircularBuffer.cs
File metadata and controls
337 lines (222 loc) · 9.94 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
using Gsemac.Collections.Properties;
using System;
namespace Gsemac.Collections {
/// <summary>
/// Provides a queue-like interface over a buffer.
/// </summary>
public class CircularBuffer<T> {
// Public members
/// <summary>
/// Returns the number of unread items available in the buffer.
/// </summary>
public int Length {
get {
// The length of the queue is the amount of data between the read head and the write head.
// (i.e. the amount of data left available for reading.)
if (buffer is null)
return 0;
if (wpos == rpos)
return 0;
else if (wpos > rpos)
return wpos - rpos;
else
return buffer.Length - rpos + wpos;
}
}
/// <summary>
/// Returns the size of the underlying buffer.
/// </summary>
public int Capacity {
get => buffer is null ? 0 : buffer.Length;
set => SetCapacity(value);
}
public T this[int index] {
get => GetItemAtIndex(index);
set => SetItemAtIndex(index, value);
}
/// <summary>
/// Initializes a new instance of the <see cref="CircularBuffer{T}"/> class.
/// </summary>
public CircularBuffer() {
}
/// <summary>
/// Initializes a new instance of the <see cref="CircularBuffer{T}"/> class with the specified initial capacity.
/// </summary>
/// <param name="initialCapacity">Starting capacity of the underlying buffer.</param>
public CircularBuffer(int initialCapacity) {
Capacity = initialCapacity;
}
/// <summary>
/// Initializes a new instance of the <see cref="CircularBuffer{T}"/> class that wraps the specified buffer.
/// </summary>
/// <param name="buffer">The underlying buffer for the <see cref="CircularBuffer{T}"/> instance.</param>
public CircularBuffer(T[] buffer) :
this(buffer.Length) {
this.fixedCapacity = true;
this.buffer = buffer;
}
/// <summary>
/// Reads a single value from the queue.
/// </summary>
/// <returns>A single value from the queue.</returns>
public T Read() {
if (Length <= 0)
throw new InvalidOperationException(Properties.ExceptionMessages.CircularBufferIsEmpty);
T[] buffer = new T[1];
Read(buffer, 0, 1);
return buffer[0];
}
/// <summary>
/// Reads an array of values from the queue, returning the number of values read.
/// </summary>
/// <param name="buffer">Buffer to read into.</param>
/// <param name="offset">Offset at which to begin writing.</param>
/// <param name="count">Maximum number of values to read.</param>
/// <returns></returns>
public int Read(T[] buffer, int offset, int count) {
int bytesToRead = count;
int bytesRead = 0;
if (rpos < wpos) {
// Read head is behind the write head, so reading the bytes is trivial.
// All we need to do is read up the requested byte count or the write head-- Whichever comes first.
bytesRead = Math.Min(bytesToRead, wpos - rpos);
Array.Copy(this.buffer, rpos, buffer, offset, Math.Min(bytesToRead, bytesRead));
rpos += Math.Min(bytesToRead, bytesRead);
}
else if (rpos > wpos) {
// Read head is in fron of the write head.
int capacityLeft = this.buffer.Length - rpos;
bytesRead = Math.Min(capacityLeft, count);
Array.Copy(this.buffer, rpos, buffer, offset, bytesRead);
rpos = (rpos + bytesRead) % this.buffer.Length;
bytesToRead -= bytesRead;
if (bytesToRead > 0) {
bytesToRead = Math.Min(bytesToRead, wpos - rpos);
Array.Copy(this.buffer, rpos, buffer, offset + bytesRead, bytesToRead);
bytesRead += bytesToRead;
rpos += bytesToRead;
}
}
return bytesRead;
}
/// <summary>
/// Writes a single value to the buffer.
/// </summary>
/// <param name="value">The value to add.</param>
public void Write(T value) {
Write(new T[] { value }, 0, 1);
}
/// <summary>
/// Adds an array of values to the queue.
/// </summary>
/// <param name="buffer">Values to add.</param>
/// <param name="offset">Starting offset in buffer.</param>
/// <param name="count">Number of values to write from the buffer.</param>
public void Write(T[] buffer, int offset, int count) {
int bytesToWrite = count;
// Make sure the capacity is large enough to store the new bytes.
EnsureCapacity(bytesToWrite);
if (wpos < rpos) {
// New capacity will have been provided between the write and read heads, so we can write guaranteed that there is enough space.
Array.Copy(buffer, offset, this.buffer, wpos, count);
wpos += count;
}
else {
int writeCapacityLeft = this.buffer.Length - wpos;
// Copy as much as we can to the end of the buffer.
int bytesWritten = Math.Min(bytesToWrite, writeCapacityLeft);
Array.Copy(buffer, offset, this.buffer, wpos, bytesWritten);
bytesToWrite -= bytesWritten;
wpos = (wpos + bytesWritten) % this.buffer.Length;
if (bytesToWrite > 0) {
// Go back to the front of the buffer to continue writing.
Array.Copy(buffer, offset + bytesWritten, this.buffer, wpos, bytesToWrite);
wpos = bytesToWrite;
}
}
}
/// <summary>
/// Returns the next item in the buffer without removing it.
/// </summary>
/// <returns>The item byte in the buffer.</returns>
public T Peek() {
return buffer[rpos];
}
/// <summary>
/// Clears the underlying buffer.
/// </summary>
public void Clear() {
Array.Clear(buffer, 0, buffer.Length);
wpos = 0;
rpos = 0;
}
// Private members
private const int growthFactor = 2;
private const int minCapacity = 4;
private readonly bool fixedCapacity = false;
private T[] buffer;
private int wpos = 0; // write position
private int rpos = 0; // read position
int GetNumberOfBytesAvailableForWriting() {
// Calculate the amount of space left in the buffer for writing new data.
if (buffer is null)
return 0;
if (wpos > rpos)
return rpos + buffer.Length - wpos;
else if (wpos < rpos)
return rpos - wpos;
else
return buffer.Length;
}
void EnsureCapacity(int spaceRequired) {
// Grow the buffer by increasing it by the growth factor, ensuring a minimum of our required capacity + 1.
// The extra 1 is added so that there's always at least one empty byte in the buffer, so the write and read heads are never equal.
// If they were, we wouldn't be able to tell that there was new data in the buffer.
int spaceLeft = GetNumberOfBytesAvailableForWriting();
if (spaceLeft < spaceRequired + 1) {
if (!fixedCapacity)
Capacity = Math.Max(Capacity + spaceRequired + 1 - spaceLeft, Math.Max(Capacity * growthFactor, minCapacity));
else
throw new InvalidOperationException(Properties.ExceptionMessages.BufferHasNoSpaceLeft);
}
}
void SetCapacity(int capacity) {
if (fixedCapacity)
throw new InvalidOperationException(Properties.ExceptionMessages.BufferIsNotExpandable);
T[] newBuffer = new T[capacity];
if (rpos < wpos) {
// If the read head is behind the write head, just copy in the data that we still have to read (between the heads).
// This places all of the new capacity after the write head.
Array.Copy(buffer, rpos, newBuffer, 0, wpos - rpos);
wpos -= rpos;
rpos = 0;
}
else if (rpos > wpos) {
// If the write head is behind the read head, we need to put the new capacity between them.
Array.Copy(buffer, 0, newBuffer, 0, wpos);
int unreadSegmentLength = buffer.Length - rpos;
Array.Copy(buffer, rpos, newBuffer, newBuffer.Length - unreadSegmentLength, unreadSegmentLength);
rpos = newBuffer.Length - unreadSegmentLength;
}
else {
// If they're equal, there's no data to read in the buffer, so we can just reset both heads.
wpos = 0;
rpos = 0;
}
buffer = newBuffer;
}
int GetActualIndex(int index) {
return (rpos + index) % Capacity;
}
T GetItemAtIndex(int index) {
if (index < 0 || index >= Length)
throw new IndexOutOfRangeException(ExceptionMessages.BufferIndexOutOfBounds);
return buffer[GetActualIndex(index)];
}
void SetItemAtIndex(int index, T value) {
if (index < 0 || index >= Length)
throw new IndexOutOfRangeException(ExceptionMessages.BufferIndexOutOfBounds);
buffer[GetActualIndex(index)] = value;
}
}
}