@@ -10,7 +10,6 @@ public class CircularBuffer<T> : IEnumerable<T>, ICollection<T> where T : ICompa
10
10
private T [ ] _circularBuffer ;
11
11
private int _end ;
12
12
private int _start ;
13
- private readonly T _default ;
14
13
private static readonly int _defaultBufferLength = 10 ;
15
14
16
15
/// <summary>
@@ -72,7 +71,6 @@ public CircularBuffer(int length, bool canOverride = true)
72
71
throw new ArgumentOutOfRangeException ( "length can not be zero or negative" ) ;
73
72
}
74
73
_circularBuffer = new T [ length + 1 ] ;
75
- _default = _circularBuffer [ length ] ;
76
74
_end = 0 ;
77
75
_start = 0 ;
78
76
CanOverride = canOverride ;
@@ -121,7 +119,7 @@ public T Pop()
121
119
#region IEnumerable Implementation
122
120
public IEnumerator < T > GetEnumerator ( )
123
121
{
124
- for ( int i = 0 ; i < Count ; i ++ )
122
+ for ( int i = _start ; i <= Count ; i ++ )
125
123
{
126
124
yield return _circularBuffer [ i ] ;
127
125
}
@@ -161,6 +159,8 @@ public bool IsReadOnly
161
159
public void Clear ( )
162
160
{
163
161
_count = 0 ;
162
+ _start = 0 ;
163
+ _end = 0 ;
164
164
_circularBuffer = new T [ Length + 1 ] ;
165
165
}
166
166
/// <summary>
@@ -212,13 +212,23 @@ public bool Remove(T item)
212
212
{
213
213
if ( item . Equals ( _circularBuffer [ i ] ) )
214
214
{
215
- _circularBuffer [ i ] = _default ;
215
+ _circularBuffer [ i ] = default ( T ) ;
216
216
-- _count ;
217
217
result = true ;
218
+ int j = i ;
219
+ // Moving elements one step forward
220
+ while ( j < Length )
221
+ {
222
+ var temp = _circularBuffer [ j ] ;
223
+ _circularBuffer [ j ] = _circularBuffer [ j + 1 ] ;
224
+ _circularBuffer [ j + 1 ] = temp ;
225
+ j ++ ;
226
+ }
227
+ _end = i + _count ;
228
+ break ;
218
229
}
219
230
}
220
231
return result ;
221
-
222
232
}
223
233
#endregion
224
234
}
0 commit comments