Skip to content

Commit

Permalink
Standardize on Array copyOf
Browse files Browse the repository at this point in the history
  • Loading branch information
belugabehr committed Oct 27, 2021
1 parent b3b3162 commit 935d099
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 53 deletions.
13 changes: 3 additions & 10 deletions java/core/src/main/java/com/google/protobuf/BooleanArrayList.java
Expand Up @@ -197,10 +197,7 @@ public void addBoolean(boolean element) {
if (size == array.length) {
// Resize to 1.5x the size
int length = ((size * 3) / 2) + 1;
boolean[] newArray = new boolean[length];

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

array = newArray;
array = Arrays.copyOf(array, length);
}

array[size++] = element;
Expand Down

0 comments on commit 935d099

Please sign in to comment.