Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cdafbb9

Browse files
Found a bug in the grow method of the ArrayQueue
1 parent e596644 commit cdafbb9

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

‎src/com/jwetherell/algorithms/data_structures/Queue.java‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,11 @@ private void grow(int size) {
117117
T[] temp = (T[]) new Object[growSize];
118118
// Since the array can wrap around, make sure you grab the first chunk
119119
int adjLast = lastIndex % array.length;
120-
if (adjLast < firstIndex) {
121-
System.arraycopy(array, 0, temp, array.length-adjLast, adjLast+1);
120+
if (adjLast > 0 && adjLast <= firstIndex) {
121+
System.arraycopy(array, 0, temp, array.length-adjLast, adjLast);
122122
}
123123
// Copy the remaining
124-
System.arraycopy(array, firstIndex, temp, 0, array.length-firstIndex);
124+
System.arraycopy(array, firstIndex, temp, 0, array.length - firstIndex);
125125
array = null;
126126
array = temp;
127127
lastIndex = (lastIndex - firstIndex);
@@ -134,9 +134,9 @@ private void shrink() {
134134
T[] temp = (T[]) new Object[shrinkSize];
135135
// Since the array can wrap around, make sure you grab the first chunk
136136
int adjLast = lastIndex % array.length;
137-
int endIndex = (lastIndex>array.length)?array.length:lastIndex;
137+
int endIndex = (lastIndex>array.length) ? array.length : lastIndex;
138138
if (adjLast <= firstIndex) {
139-
System.arraycopy(array, 0, temp, array.length-firstIndex, adjLast);
139+
System.arraycopy(array, 0, temp, array.length - firstIndex, adjLast);
140140
}
141141
// Copy the remaining
142142
System.arraycopy(array, firstIndex, temp, 0, endIndex-firstIndex);

‎test/com/jwetherell/algorithms/data_structures/test/QueueTests.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class QueueTests {
1616

1717
@Test
1818
public void testArrayQueue() {
19-
TestData data = Utils.generateTestData(100000);
19+
TestData data = Utils.generateTestData(2500);
2020

2121
String aName = "Queue [array]";
2222
Queue.ArrayQueue<Integer> aQueue = new Queue.ArrayQueue<Integer>();
@@ -30,7 +30,7 @@ public void testArrayQueue() {
3030

3131
@Test
3232
public void testLinkedQueue() {
33-
TestData data = Utils.generateTestData(100);
33+
TestData data = Utils.generateTestData(250);
3434

3535
String lName = "Queue [linked]";
3636
Queue.LinkedQueue<Integer> lQueue = new Queue.LinkedQueue<Integer>();

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /