-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
CircularBuffer.cs
241 lines (220 loc) · 6.82 KB
/
CircularBuffer.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
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace DataStructures.Lists
{
public class CircularBuffer<T> : IEnumerable<T>, ICollection<T> where T : IComparable<T>
{
private T[] _circularBuffer;
private int _end;
private int _start;
private static readonly int _defaultBufferLength = 10;
/// <summary>
/// Returns the length of the buffer
/// </summary>
public int Length
{
get
{
return _circularBuffer.Length - 1;
}
}
/// <summary>
/// Checks if no element is inserted into the buffer
/// </summary>
public bool IsEmpty
{
get
{
return _count == 0;
}
}
/// <summary>
/// Checks if the buffer is filled up
/// </summary>
public bool IsFilledUp
{
get
{
return ((_end + 1) % _circularBuffer.Length == _start) && !_circularBuffer[_start].Equals(_circularBuffer[_end]);
}
}
/// <summary>
/// Controls whether data should be overridden when it is continously inserted without reading
/// </summary>
public bool CanOverride
{
get;
}
/// <summary>
/// Initializes a circular buffer with initial length of 10
/// </summary>
public CircularBuffer(bool canOverride = true) : this(_defaultBufferLength, canOverride)
{
}
/// <summary>
/// Initializes a circular buffer with given length
/// </summary>
/// <param name="length">The length of the buffer</param>
public CircularBuffer(int length, bool canOverride = true)
{
if (length < 1)
{
throw new ArgumentOutOfRangeException("length can not be zero or negative");
}
_circularBuffer = new T[length + 1];
_end = 0;
_start = 0;
CanOverride = canOverride;
}
/// <summary>
/// Writes value to the back of the buffer
/// </summary>
/// <param name="value">value to be added to the buffer</param>
public void Add(T value)
{
if (CanOverride==false && IsFilledUp==true)
{
throw new CircularBufferFullException($"Circular Buffer is filled up. {value} can not be inserted");
}
innerInsert(value);
}
// Inserts data into the buffer without checking if it is full
private void innerInsert(T value)
{
_circularBuffer[_end] = value;
_end = (_end + 1) % _circularBuffer.Length;
if (_end == _start)
{
_start = (_start + 1) % _circularBuffer.Length;
}
// Count should not be greater than the length of the buffer when overriding
_count = _count < Length ? ++_count : _count;
}
/// <summary>
/// Reads and removes the value in front of the buffer, and places the next value in front.
/// </summary>
public T Pop()
{
var result = _circularBuffer[_start];
_circularBuffer[_start] = _circularBuffer[_end];
_start = (_start + 1) % _circularBuffer.Length;
//Count should not go below Zero when poping an empty buffer.
_count = _count > 0 ? --_count : _count;
return result;
}
#region IEnumerable Implementation
public IEnumerator<T> GetEnumerator()
{
for (int i = _start; i < Count; i++)
{
yield return _circularBuffer[i];
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion
#region ICollection Implementation
private int _count;
/// <summary>
/// Returns the number of elements.
/// </summary>
public int Count
{
get
{
return _count;
}
}
/// <summary>
/// Checks whether this collection is readonly
/// </summary>
public bool IsReadOnly
{
get
{
return false;
}
}
/// <summary>
/// Clears this instance
/// </summary>
public void Clear()
{
_count = 0;
_start = 0;
_end = 0;
_circularBuffer = new T[Length + 1];
}
/// <summary>
/// Checks whether the buffer contains an item
/// </summary>
public bool Contains(T item)
{
return _circularBuffer.Contains(item);
}
/// <summary>
/// Copies this buffer to an array
/// </summary>
public void CopyTo(T[] array, int arrayIndex)
{
if (array == null)
{
throw new ArgumentNullException("array can not be null");
}
if (array.Length == 0 || arrayIndex >= array.Length || arrayIndex < 0)
{
throw new IndexOutOfRangeException();
}
// Get enumerator
var enumarator = GetEnumerator();
// Copy elements if there is any in the buffer and if the index is within the valid range
while (arrayIndex < array.Length)
{
if (enumarator.MoveNext())
{
array[arrayIndex] = enumarator.Current;
arrayIndex++;
}
else
{
break;
}
}
}
/// <summary>
/// Removes an item from the buffer
/// </summary>
public bool Remove(T item)
{
if (!IsEmpty && Contains(item))
{
var sourceArray = _circularBuffer.Except(new T[] { item }).ToArray();
_circularBuffer = new T[Length + 1];
Array.Copy(sourceArray, _circularBuffer, sourceArray.Length);
if (!Equals(item,default(T)))
{
_end = sourceArray.Length - 1;
_count = sourceArray.Length-1;
}
else
{
_end = sourceArray.Length;
_count = sourceArray.Length;
}
return true;
}
return false;
}
#endregion
}
public class CircularBufferFullException : Exception
{
public CircularBufferFullException(string message) : base(message)
{
}
}
}