Skip to content

Commit 9117b9b

Browse files
committed
moved all elements back one spot after Removing an element
1 parent 0719b39 commit 9117b9b

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

DataStructures/Lists/CircularBuffer.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ public class CircularBuffer<T> : IEnumerable<T>, ICollection<T> where T : ICompa
1010
private T[] _circularBuffer;
1111
private int _end;
1212
private int _start;
13-
private readonly T _default;
1413
private static readonly int _defaultBufferLength = 10;
1514

1615
/// <summary>
@@ -72,7 +71,6 @@ public CircularBuffer(int length, bool canOverride = true)
7271
throw new ArgumentOutOfRangeException("length can not be zero or negative");
7372
}
7473
_circularBuffer = new T[length + 1];
75-
_default = _circularBuffer[length];
7674
_end = 0;
7775
_start = 0;
7876
CanOverride = canOverride;
@@ -121,7 +119,7 @@ public T Pop()
121119
#region IEnumerable Implementation
122120
public IEnumerator<T> GetEnumerator()
123121
{
124-
for (int i = 0; i < Count; i++)
122+
for (int i = _start; i <= Count; i++)
125123
{
126124
yield return _circularBuffer[i];
127125
}
@@ -161,6 +159,8 @@ public bool IsReadOnly
161159
public void Clear()
162160
{
163161
_count = 0;
162+
_start = 0;
163+
_end = 0;
164164
_circularBuffer = new T[Length + 1];
165165
}
166166
/// <summary>
@@ -212,13 +212,23 @@ public bool Remove(T item)
212212
{
213213
if (item.Equals(_circularBuffer[i]))
214214
{
215-
_circularBuffer[i] = _default;
215+
_circularBuffer[i] = default(T);
216216
--_count;
217217
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;
218229
}
219230
}
220231
return result;
221-
222232
}
223233
#endregion
224234
}

0 commit comments

Comments
 (0)