Skip to content

Commit e205ea2

Browse files
committed
re-implemented Remove method
1 parent 9117b9b commit e205ea2

File tree

2 files changed

+62
-25
lines changed

2 files changed

+62
-25
lines changed

DataStructures/Lists/CircularBuffer.cs

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public T Pop()
119119
#region IEnumerable Implementation
120120
public IEnumerator<T> GetEnumerator()
121121
{
122-
for (int i = _start; i <= Count; i++)
122+
for (int i = _start; i < Count; i++)
123123
{
124124
yield return _circularBuffer[i];
125125
}
@@ -207,28 +207,21 @@ public void CopyTo(T[] array, int arrayIndex)
207207
/// </summary>
208208
public bool Remove(T item)
209209
{
210-
var result = false;
211-
for (int i = 0; i < _circularBuffer.Length; i++)
210+
if (!IsEmpty && Contains(item))
212211
{
213-
if (item.Equals(_circularBuffer[i]))
212+
var sourceArray = _circularBuffer.Except(new T[] { item }).ToArray();
213+
_circularBuffer = new T[Length + 1];
214+
Array.Copy(sourceArray, _circularBuffer, sourceArray.Length);
215+
216+
if (!item.Equals(default(T)))
214217
{
215-
_circularBuffer[i] = default(T);
216-
--_count;
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;
229-
}
218+
_end = sourceArray.Length - 1;
219+
_count = sourceArray.Length-1;
220+
}
221+
return true;
230222
}
231-
return result;
223+
224+
return false;
232225
}
233226
#endregion
234227
}

UnitTest/DataStructuresTests/CircularBufferTest.cs

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,19 +155,63 @@ public static void TestingICollectionImplementation()
155155
Assert.Equal(3, array[0]);
156156
Assert.Equal(34, array[1]);
157157
Assert.Equal(24, array[2]);
158-
159-
//Testing Remove
160-
Assert.True(circularBuffer.Remove(3));
161-
Assert.False(circularBuffer.Remove(14));
162158

163159
//Testing Count
164-
Assert.Equal(2, circularBuffer.Count);
160+
Assert.Equal(3, circularBuffer.Count);
165161
//Testing clear
166162
circularBuffer.Clear();
167163
Assert.Equal(0, circularBuffer.Pop());
168164
Assert.Equal(0, circularBuffer.Pop());
169165
Assert.Equal(0, circularBuffer.Pop());
170166
Assert.Empty(circularBuffer);
171167
}
168+
[Fact]
169+
public static void TestingRemoveMethod() {
170+
var circularBuffer = new CircularBuffer<byte>(5, false);
171+
circularBuffer.Add(3);
172+
circularBuffer.Add(34);
173+
circularBuffer.Add(24);
174+
circularBuffer.Add(31);
175+
circularBuffer.Add(14);
176+
177+
//Removing default(T) from the buffer. buffer should not be affected since default is not contained
178+
circularBuffer.Remove(default(byte));
179+
Assert.Equal(3, circularBuffer.Pop());
180+
Assert.Equal(34, circularBuffer.Pop());
181+
Assert.Equal(24, circularBuffer.Pop());
182+
Assert.Equal(31, circularBuffer.Pop());
183+
Assert.Equal(14, circularBuffer.Pop());
184+
185+
//Filling the buffer again with some duplicate entries
186+
circularBuffer.Add(3);
187+
circularBuffer.Add(3);
188+
circularBuffer.Add(3);
189+
circularBuffer.Add(31);
190+
circularBuffer.Add(14);
191+
192+
circularBuffer.Remove(3);
193+
Assert.Equal(5 - 3, circularBuffer.Count);
194+
195+
circularBuffer = new CircularBuffer<byte>(3, false);
196+
circularBuffer.Add(1);
197+
circularBuffer.Add(2);
198+
circularBuffer.Add(3);
199+
Assert.Equal(3, circularBuffer.Count);
200+
//Removing elements one by one from the end
201+
circularBuffer.Remove(3);
202+
circularBuffer.Remove(2);
203+
circularBuffer.Remove(1);
204+
Assert.Empty(circularBuffer);
205+
//Adding elements back
206+
circularBuffer.Add(1);
207+
circularBuffer.Add(2);
208+
circularBuffer.Add(3);
209+
Assert.Equal(3, circularBuffer.Count);
210+
211+
//buffer would yield these results if it was poped initially
212+
Assert.Equal(1, circularBuffer.Pop());
213+
Assert.Equal(2, circularBuffer.Pop());
214+
Assert.Equal(3, circularBuffer.Pop());
215+
}
172216
}
173217
}

0 commit comments

Comments
 (0)