Skip to content

Commit 751834d

Browse files
committed
Fixed bug where calling "pop" from growable lists can sometimes yield incorrect value due to resizing
1 parent 47e2d1a commit 751834d

12 files changed

Lines changed: 54 additions & 6 deletions

File tree

lib/src/main/java/azuretriple/primitivecontainer/ByteList.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ default void insert(final int location,final byte[] v,final int start,final int
6161
/** Removes the value at the specified location from the list. */
6262
void remove(final int location);
6363
/** Removes and returns the last value from the list. */
64-
default byte pop() {delete(); return data()[((List)this).size];}
64+
default byte pop() {final byte out = data()[((List)this).size-1]; delete(); return out;}
6565
/** Removes and returns the value at the specified location from the list. */
6666
default byte extract(final int location) {final byte o = data()[location]; remove(location); return o;}
6767

lib/src/main/java/azuretriple/primitivecontainer/DoubleList.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ default void insert(final int location,final double[] v,final int start,final in
6161
/** Removes the value at the specified location from the list. */
6262
void remove(final int location);
6363
/** Removes and returns the last value from the list. */
64-
default double pop() {delete(); return data()[((List)this).size];}
64+
default double pop() {final double out = data()[((List)this).size-1]; delete(); return out;}
6565
/** Removes and returns the value at the specified location from the list. */
6666
default double extract(final int location) {final double o = data()[location]; remove(location); return o;}
6767

lib/src/main/java/azuretriple/primitivecontainer/FloatList.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ default void insert(final int location,final float[] v,final int start,final int
6161
/** Removes the value at the specified location from the list. */
6262
void remove(final int location);
6363
/** Removes and returns the last value from the list. */
64-
default float pop() {delete(); return data()[((List)this).size];}
64+
default float pop() {final float out = data()[((List)this).size-1]; delete(); return out;}
6565
/** Removes and returns the value at the specified location from the list. */
6666
default float extract(final int location) {final float o = data()[location]; remove(location); return o;}
6767

lib/src/main/java/azuretriple/primitivecontainer/IntList.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ default void insert(final int location,final int[] v,final int start,final int l
6161
/** Removes the value at the specified location from the list. */
6262
void remove(final int location);
6363
/** Removes and returns the last value from the list. */
64-
default int pop() {delete(); return data()[((List)this).size];}
64+
default int pop() {final int out = data()[((List)this).size-1]; delete(); return out;}
6565
/** Removes and returns the value at the specified location from the list. */
6666
default int extract(final int location) {final int o = data()[location]; remove(location); return o;}
6767

lib/src/main/java/azuretriple/primitivecontainer/LongList.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ default void insert(final int location,final long[] v,final int start,final int
6161
/** Removes the value at the specified location from the list. */
6262
void remove(final int location);
6363
/** Removes and returns the last value from the list. */
64-
default long pop() {delete(); return data()[((List)this).size];}
64+
default long pop() {final long out = data()[((List)this).size-1]; delete(); return out;}
6565
/** Removes and returns the value at the specified location from the list. */
6666
default long extract(final int location) {final long o = data()[location]; remove(location); return o;}
6767

lib/src/main/java/azuretriple/primitivecontainer/ShortList.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ default void insert(final int location,final short[] v,final int start,final int
6161
/** Removes the value at the specified location from the list. */
6262
void remove(final int location);
6363
/** Removes and returns the last value from the list. */
64-
default short pop() {delete(); return data()[((List)this).size];}
64+
default short pop() {final short out = data()[((List)this).size-1]; delete(); return out;}
6565
/** Removes and returns the value at the specified location from the list. */
6666
default short extract(final int location) {final short o = data()[location]; remove(location); return o;}
6767

lib/src/test/java/azuretriple/primitivecontainer/GrowableByteListTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,12 @@ void testClone()
6666
final GrowableByteList l = new GrowableByteList(new byte[] {0,1,2,3},2);
6767
assertEquals(l,l.clone());
6868
}
69+
70+
@Test
71+
void testPop()
72+
{
73+
final GrowableByteList l = new GrowableByteList(16);
74+
l.add((byte)1);
75+
assertEquals((byte)1,l.pop());
76+
}
6977
}

lib/src/test/java/azuretriple/primitivecontainer/GrowableDoubleListTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,12 @@ void testClone()
6666
final GrowableDoubleList l = new GrowableDoubleList(new double[] {0,1,2,3},2);
6767
assertEquals(l,l.clone());
6868
}
69+
70+
@Test
71+
void testPop()
72+
{
73+
final GrowableDoubleList l = new GrowableDoubleList(16);
74+
l.add(1.D);
75+
assertEquals(1.D,l.pop());
76+
}
6977
}

lib/src/test/java/azuretriple/primitivecontainer/GrowableFloatListTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,12 @@ void testClone()
6666
final GrowableFloatList l = new GrowableFloatList(new float[] {0,1,2,3},2);
6767
assertEquals(l,l.clone());
6868
}
69+
70+
@Test
71+
void testPop()
72+
{
73+
final GrowableFloatList l = new GrowableFloatList(16);
74+
l.add(1.F);
75+
assertEquals(1.F,l.pop());
76+
}
6977
}

lib/src/test/java/azuretriple/primitivecontainer/GrowableIntListTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,12 @@ void testClone()
6666
final GrowableIntList l = new GrowableIntList(new int[] {0,1,2,3},2);
6767
assertEquals(l,l.clone());
6868
}
69+
70+
@Test
71+
void testPop()
72+
{
73+
final GrowableIntList l = new GrowableIntList(16);
74+
l.add(1);
75+
assertEquals(1,l.pop());
76+
}
6977
}

0 commit comments

Comments
 (0)