Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Standardize on Array copyOf" #9400

Merged
merged 1 commit into from Jan 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 10 additions & 3 deletions java/core/src/main/java/com/google/protobuf/BooleanArrayList.java
Expand Up @@ -197,7 +197,10 @@ public void addBoolean(boolean element) {
if (size == array.length) {
// Resize to 1.5x the size
int length = ((size * 3) / 2) + 1;
array = Arrays.copyOf(array, length);
boolean[] newArray = new boolean[length];

System.arraycopy(array, 0, newArray, 0, size);
array = newArray;
}

array[size++] = element;
Expand All @@ -216,10 +219,14 @@ private void addBoolean(int index, boolean element) {
} else {
// Resize to 1.5x the size
int length = ((size * 3) / 2) + 1;
array = Arrays.copyOf(array, length);
boolean[] newArray = new boolean[length];

// Copy the first part directly
System.arraycopy(array, 0, newArray, 0, index);

// Copy the rest shifted over by one to make room
System.arraycopy(array, index, array, index + 1, size - index);
System.arraycopy(array, index, newArray, index + 1, size - index);
array = newArray;
}

array[index] = element;
Expand Down
13 changes: 10 additions & 3 deletions java/core/src/main/java/com/google/protobuf/DoubleArrayList.java
Expand Up @@ -197,7 +197,10 @@ public void addDouble(double element) {
if (size == array.length) {
// Resize to 1.5x the size
int length = ((size * 3) / 2) + 1;
array = Arrays.copyOf(array, length);
double[] newArray = new double[length];

System.arraycopy(array, 0, newArray, 0, size);
array = newArray;
}

array[size++] = element;
Expand All @@ -216,10 +219,14 @@ private void addDouble(int index, double element) {
} else {
// Resize to 1.5x the size
int length = ((size * 3) / 2) + 1;
array = Arrays.copyOf(array, length);
double[] newArray = new double[length];

// Copy the first part directly
System.arraycopy(array, 0, newArray, 0, index);

// Copy the rest shifted over by one to make room
System.arraycopy(array, index, array, index + 1, size - index);
System.arraycopy(array, index, newArray, index + 1, size - index);
array = newArray;
}

array[index] = element;
Expand Down
13 changes: 10 additions & 3 deletions java/core/src/main/java/com/google/protobuf/FloatArrayList.java
Expand Up @@ -196,7 +196,10 @@ public void addFloat(float element) {
if (size == array.length) {
// Resize to 1.5x the size
int length = ((size * 3) / 2) + 1;
array = Arrays.copyOf(array, length);
float[] newArray = new float[length];

System.arraycopy(array, 0, newArray, 0, size);
array = newArray;
}

array[size++] = element;
Expand All @@ -215,10 +218,14 @@ private void addFloat(int index, float element) {
} else {
// Resize to 1.5x the size
int length = ((size * 3) / 2) + 1;
array = Arrays.copyOf(array, length);
float[] newArray = new float[length];

// Copy the first part directly
System.arraycopy(array, 0, newArray, 0, index);

// Copy the rest shifted over by one to make room
System.arraycopy(array, index, array, index + 1, size - index);
System.arraycopy(array, index, newArray, index + 1, size - index);
array = newArray;
}

array[index] = element;
Expand Down
13 changes: 10 additions & 3 deletions java/core/src/main/java/com/google/protobuf/IntArrayList.java
Expand Up @@ -196,7 +196,10 @@ public void addInt(int element) {
if (size == array.length) {
// Resize to 1.5x the size
int length = ((size * 3) / 2) + 1;
array = Arrays.copyOf(array, length);
int[] newArray = new int[length];

System.arraycopy(array, 0, newArray, 0, size);
array = newArray;
}

array[size++] = element;
Expand All @@ -215,10 +218,14 @@ private void addInt(int index, int element) {
} else {
// Resize to 1.5x the size
int length = ((size * 3) / 2) + 1;
array = Arrays.copyOf(array, length);
int[] newArray = new int[length];

// Copy the first part directly
System.arraycopy(array, 0, newArray, 0, index);

// Copy the rest shifted over by one to make room
System.arraycopy(array, index, array, index + 1, size - index);
System.arraycopy(array, index, newArray, index + 1, size - index);
array = newArray;
}

array[index] = element;
Expand Down
13 changes: 10 additions & 3 deletions java/core/src/main/java/com/google/protobuf/LongArrayList.java
Expand Up @@ -196,7 +196,10 @@ public void addLong(long element) {
if (size == array.length) {
// Resize to 1.5x the size
int length = ((size * 3) / 2) + 1;
array = Arrays.copyOf(array, length);
long[] newArray = new long[length];

System.arraycopy(array, 0, newArray, 0, size);
array = newArray;
}

array[size++] = element;
Expand All @@ -215,10 +218,14 @@ private void addLong(int index, long element) {
} else {
// Resize to 1.5x the size
int length = ((size * 3) / 2) + 1;
array = Arrays.copyOf(array, length);
long[] newArray = new long[length];

// Copy the first part directly
System.arraycopy(array, 0, newArray, 0, index);

// Copy the rest shifted over by one to make room
System.arraycopy(array, index, array, index + 1, size - index);
System.arraycopy(array, index, newArray, index + 1, size - index);
array = newArray;
}

array[index] = element;
Expand Down
Expand Up @@ -80,7 +80,9 @@ public boolean add(E element) {
if (size == array.length) {
// Resize to 1.5x the size
int length = ((size * 3) / 2) + 1;
array = Arrays.copyOf(array, length);
E[] newArray = Arrays.copyOf(array, length);

array = newArray;
}

array[size++] = element;
Expand Down