Skip to content

Commit 725f67b

Browse files
committed
Fix bugs in deque
1 parent 83073aa commit 725f67b

File tree

2 files changed

+52
-9
lines changed

2 files changed

+52
-9
lines changed

Diff for: TerminalControl/Deque.cs

+35-4
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,21 @@ IEnumerator<T> IEnumerable<T>.GetEnumerator()
128128

129129
public void CopyTo(Array array, int index)
130130
{
131-
Array.Copy(backing, start, array, index, size);
131+
bool contiguous = end > start;
132+
if (contiguous)
133+
{
134+
Array.Copy(backing, start, array, index, size);
135+
}
136+
else
137+
{
138+
Array.Copy(backing, start, array, index, backing.Length - start);
139+
Array.Copy(backing, 0, array, index + backing.Length - start, end);
140+
}
132141
}
133142

134143
public void CopyTo(T[] array, int index)
135144
{
136-
Array.Copy(backing, start, array, index, size);
145+
CopyTo((Array) array, index);
137146
}
138147

139148
public T this[int index]
@@ -164,8 +173,24 @@ public bool Contains(T value)
164173

165174
public void Insert(int index, T value)
166175
{
167-
Array.Copy(backing, index, backing, index + 1, size - index);
168-
backing[index] = value;
176+
if (index < 0 || index >= size)
177+
throw new ArgumentOutOfRangeException(nameof(index), index, "Index has to be within the bounds of the collection");
178+
if (size == backing.Length)
179+
resize(size * GrowthMultiplier);
180+
181+
int actualIndex = (start + index) % backing.Length;
182+
bool contiguous = end > actualIndex;
183+
if (contiguous)
184+
{
185+
Array.Copy(backing, actualIndex, backing, actualIndex + 1, size - actualIndex);
186+
}
187+
else
188+
{
189+
Array.Copy(backing, 0, backing, 1, end);
190+
backing[0] = backing[backing.Length - 1];
191+
Array.Copy(backing, actualIndex, backing, actualIndex + 1, backing.Length - actualIndex - 1);
192+
}
193+
backing[actualIndex] = value;
169194
end = (end + 1) % backing.Length;
170195
size++;
171196
}
@@ -194,6 +219,12 @@ public void RemoveAt(int index)
194219
if (index < 0 || index >= size)
195220
throw new ArgumentOutOfRangeException(nameof(index), index, "Index has to be within the bounds of the collection");
196221

222+
if (index == 0)
223+
{
224+
PopFront();
225+
return;
226+
}
227+
197228
int actualIndex = (start + index) % backing.Length;
198229
int copyCount = Math.Min(size - index - 1, backing.Length - actualIndex - 1);
199230
Array.Copy(backing, actualIndex + 1, backing, actualIndex, copyCount);

Diff for: TerminalControlTests/DequeTests.cs

+17-5
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,41 @@ public void TestInsert()
1818
test.Add(4);
1919
test.Add(5);
2020
test.Add(6);
21+
test.Add(7);
22+
test.Add(8);
23+
test.Add(9);
24+
test.RemoveAt(0);
25+
test.RemoveAt(0);
2126
test.Insert(3, 7);
2227
test.Insert(3, 8);
2328

24-
CollectionAssert.AreEqual(test.ToArray(), new int[] { 0, 1, 2, 8, 7, 3, 4, 5, 6 });
25-
Assert.AreEqual(test.Count, 9);
29+
CollectionAssert.AreEqual(test.ToArray(), new int[] { 2, 3, 4, 8, 7, 5, 6, 7, 8, 9 });
30+
Assert.AreEqual(test.Count, 10);
2631
}
2732

2833
[TestMethod]
2934
public void TestRemoveAt()
3035
{
31-
Deque<int> test = new Deque<int>(10);
36+
Deque<int> test = new Deque<int>(6);
3237
test.Add(0);
3338
test.Add(1);
3439
test.Add(2);
3540
test.Add(3);
3641
test.Add(4);
3742
test.Add(5);
43+
44+
test.PopFront();
45+
test.PopFront();
46+
3847
test.Add(6);
48+
test.Add(7);
3949

4050
test.RemoveAt(2);
4151

42-
CollectionAssert.AreEqual(test.ToArray(), new int[] { 0, 1, 3, 4, 5, 6 });
43-
Assert.AreEqual(test.Count, 6);
52+
int[] arr = test.ToArray();
53+
54+
CollectionAssert.AreEqual(arr, new int[] { 2, 3, 5, 6, 7 });
55+
Assert.AreEqual(test.Count, 5);
4456
}
4557
}
4658
}

0 commit comments

Comments
 (0)