Commit b90333af by Dustin

Clean up UnboundedIntArray

parent 3daa74dc
package com.google.android.exoplayer2.extractor.avi; package com.google.android.exoplayer2.extractor.avi;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import java.util.Arrays; import java.util.Arrays;
public class UnboundedIntArray { public class UnboundedIntArray {
@NonNull @NonNull
@VisibleForTesting
int[] array; int[] array;
//unint //unint
int size =0; private int size =0;
public UnboundedIntArray() { public UnboundedIntArray() {
this(8); this(8);
...@@ -32,14 +34,21 @@ public class UnboundedIntArray { ...@@ -32,14 +34,21 @@ public class UnboundedIntArray {
} }
public void pack() { public void pack() {
if (size != array.length) {
array = Arrays.copyOf(array, size); array = Arrays.copyOf(array, size);
} }
}
protected void grow() { protected void grow() {
int increase = Math.max(array.length /4, 1); int increase = Math.max(array.length /4, 1);
array = Arrays.copyOf(array, increase + array.length + size); array = Arrays.copyOf(array, increase + array.length + size);
} }
public int[] getArray() {
pack();
return array;
}
/** /**
* Only works if values are in sequential order * Only works if values are in sequential order
* @param v * @param v
......
...@@ -9,7 +9,7 @@ public class UnboundedIntArrayTest { ...@@ -9,7 +9,7 @@ public class UnboundedIntArrayTest {
final UnboundedIntArray unboundedIntArray = new UnboundedIntArray(); final UnboundedIntArray unboundedIntArray = new UnboundedIntArray();
unboundedIntArray.add(4); unboundedIntArray.add(4);
Assert.assertEquals(1, unboundedIntArray.getSize()); Assert.assertEquals(1, unboundedIntArray.getSize());
Assert.assertEquals(unboundedIntArray.array[0], 4); Assert.assertEquals(unboundedIntArray.getArray()[0], 4);
} }
@Test @Test
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment