rename field 'index' to 'size'

The field 'index' describes two things:
1. the index the next element will be added to
2. the size of the list

Most methods use the second meaning.
This commit is contained in:
2017-12-03 09:10:03 +01:00
parent 477820050b
commit 63c7740fd6

View File

@@ -40,7 +40,7 @@ public final class IntList implements Serializable, Cloneable {
*/
transient private int[] data;
private int index = 0;
private int size = 0;
/**
* Keeps track of whether or not the list is sorted. This allows us to use
@@ -83,7 +83,7 @@ public final class IntList implements Serializable, Cloneable {
public IntList(final IntList intList) {
data = new int[intList.getCapacity()];
System.arraycopy(intList.data, 0, data, 0, intList.size());
index = intList.size();
size = intList.size();
sorted = intList.sorted;
}
@@ -108,7 +108,7 @@ public final class IntList implements Serializable, Cloneable {
* @return {@code true} if this list contains no elements
*/
public boolean isEmpty() {
return index == 0;
return size == 0;
}
/**
@@ -117,7 +117,7 @@ public final class IntList implements Serializable, Cloneable {
* @return the number of elements in this list
*/
public int size() {
return index;
return size;
}
/**
@@ -151,12 +151,12 @@ public final class IntList implements Serializable, Cloneable {
public void add(final int value) {
ensureCapacity(1);
data[index] = value;
data[size] = value;
if (sorted) {
sorted = index == 0 ? sorted : data[index - 1] <= data[index];
sorted = size == 0 ? sorted : data[size - 1] <= data[size];
}
index++;
size++;
}
/**
@@ -176,7 +176,7 @@ public final class IntList implements Serializable, Cloneable {
if (pos < 0) {
throw new IndexOutOfBoundsException("pos must not be negative, but was: " + pos);
}
if (pos > index) {
if (pos > size) {
throw new IndexOutOfBoundsException("pos must be smaller than size(), but was: " + pos);
}
if (values == null) {
@@ -189,10 +189,10 @@ public final class IntList implements Serializable, Cloneable {
ensureCapacity(values.length);
// move everything after the insert position to make room for the new values
System.arraycopy(data, pos, data, pos + values.length, index - pos);
System.arraycopy(data, pos, data, pos + values.length, size - pos);
// insert the new values
System.arraycopy(values, 0, data, pos, values.length);
index += values.length;
size += values.length;
if (sorted) {
// compare with element before the insertion
@@ -228,7 +228,7 @@ public final class IntList implements Serializable, Cloneable {
throw new IndexOutOfBoundsException("pos must not be negative, but was: " + pos);
}
if (pos >= index) {
if (pos >= size) {
throw new IndexOutOfBoundsException("pos must not smaller than size(), but was: " + pos);
}
@@ -251,15 +251,15 @@ public final class IntList implements Serializable, Cloneable {
public void addAll(final int... values) {
ensureCapacity(values.length);
System.arraycopy(values, 0, data, index, values.length);
System.arraycopy(values, 0, data, size, values.length);
if (sorted) {
for (int i = 0; i < values.length && sorted; i++) {
sorted = index + i - 1 < 0 ? sorted : data[index + i - 1] <= data[index + i];
sorted = size + i - 1 < 0 ? sorted : data[size + i - 1] <= data[size + i];
}
}
index += values.length;
size += values.length;
}
/**
@@ -286,15 +286,15 @@ public final class IntList implements Serializable, Cloneable {
if (toIndex < fromIndex) {
throw new IndexOutOfBoundsException("toIndex must not be smaller than fromIndex, but was: " + toIndex);
}
if (toIndex > index) {
if (toIndex > size) {
throw new IndexOutOfBoundsException(
"toIndex must not be larger than the size of this list, but was: " + toIndex);
}
final int numRemoved = index - toIndex;
final int numRemoved = size - toIndex;
System.arraycopy(data, toIndex, data, fromIndex, numRemoved);
index = index - (toIndex - fromIndex);
size = size - (toIndex - fromIndex);
sorted = size() <= 1 ? true : sorted; // lists of size 1 or smaller are always sorted
}
@@ -313,7 +313,6 @@ public final class IntList implements Serializable, Cloneable {
*/
public void removeAll(final IntList remove) {
final int size = index;
int insertPosition = 0;
for (int i = 0; i < size; i++) {
final int current = data[i];
@@ -323,7 +322,7 @@ public final class IntList implements Serializable, Cloneable {
insertPosition++;
}
}
index = insertPosition;
size = insertPosition;
sorted = size() <= 1 ? true : sorted; // lists of size 1 or smaller are always sorted
}
@@ -343,7 +342,6 @@ public final class IntList implements Serializable, Cloneable {
*/
public void removeIf(final IntPredicate predicate) {
final int size = index;
int insertPosition = 0;
for (int i = 0; i < size; i++) {
final int current = data[i];
@@ -353,7 +351,7 @@ public final class IntList implements Serializable, Cloneable {
insertPosition++;
}
}
index = insertPosition;
size = insertPosition;
sorted = size() <= 1 ? true : sorted; // lists of size 1 or smaller are always sorted
}
@@ -371,7 +369,6 @@ public final class IntList implements Serializable, Cloneable {
* @see #trim()
*/
public void retainAll(final IntList retain) {
final int size = index;
int insertPosition = 0;
for (int i = 0; i < size; i++) {
final int current = data[i];
@@ -381,7 +378,7 @@ public final class IntList implements Serializable, Cloneable {
insertPosition++;
}
}
index = insertPosition;
size = insertPosition;
}
/**
@@ -394,7 +391,7 @@ public final class IntList implements Serializable, Cloneable {
*/
public void replaceAll(final UnaryIntOperator operator) {
for (int i = 0; i < index; i++) {
for (int i = 0; i < size; i++) {
final int newValue = operator.apply(data[i]);
set(i, newValue);
}
@@ -411,7 +408,7 @@ public final class IntList implements Serializable, Cloneable {
* {@code index < 0 || index >= size()}
*/
public int get(final int pos) {
if (pos < 0 || pos >= index) {
if (pos < 0 || pos >= size) {
throw new IndexOutOfBoundsException();
}
return data[pos];
@@ -437,7 +434,7 @@ public final class IntList implements Serializable, Cloneable {
throw new IndexOutOfBoundsException("length must not be negative, but was: " + length);
}
if (from + length > index) {
if (from + length > size) {
throw new IndexOutOfBoundsException("from: " + from + " length: " + length);
}
final int[] result = new int[length];
@@ -451,7 +448,7 @@ public final class IntList implements Serializable, Cloneable {
* @return an array containing all elements of this list
*/
public int[] toArray() {
return get(0, index);
return get(0, size);
}
/**
@@ -465,10 +462,10 @@ public final class IntList implements Serializable, Cloneable {
*/
public int[] toArray(final int[] input) {
if (input.length < index) {
if (input.length < size) {
return toArray();
}
System.arraycopy(data, 0, input, 0, index);
System.arraycopy(data, 0, input, 0, size);
return input;
}
@@ -476,7 +473,7 @@ public final class IntList implements Serializable, Cloneable {
* Sorts the list into ascending order.
*/
public void sort() {
Arrays.sort(data, 0, index);
Arrays.sort(data, 0, size);
sorted = true;
}
@@ -485,13 +482,13 @@ public final class IntList implements Serializable, Cloneable {
* parallelism.
*/
public void parallelSort() {
Arrays.parallelSort(data, 0, index);
Arrays.parallelSort(data, 0, size);
sorted = true;
}
private void ensureCapacity(final int newElements) {
final int requiredCapacity = index + newElements;
final int requiredCapacity = size + newElements;
if (requiredCapacity < 0 || requiredCapacity > MAX_ARRAY_SIZE) {
// overflow, or too big
throw new OutOfMemoryError();
@@ -512,11 +509,11 @@ public final class IntList implements Serializable, Cloneable {
* Call this method to reduce the memory consumption of this list.
*/
public void trim() {
if (index == 0) {
if (size == 0) {
data = EMPTY_ARRAY;
} else {
final int[] newData = new int[index];
System.arraycopy(data, 0, newData, 0, index);
final int[] newData = new int[size];
System.arraycopy(data, 0, newData, 0, size);
data = newData;
}
}
@@ -528,7 +525,7 @@ public final class IntList implements Serializable, Cloneable {
*/
public IntStream stream() {
return Arrays.stream(data, 0, index);
return Arrays.stream(data, 0, size);
}
/**
@@ -537,7 +534,7 @@ public final class IntList implements Serializable, Cloneable {
* @return a parallel {@link IntStream}
*/
public IntStream parallelStream() {
final OfInt spliterator = Arrays.spliterator(data, 0, index);
final OfInt spliterator = Arrays.spliterator(data, 0, size);
return StreamSupport.intStream(spliterator, true);
}
@@ -572,7 +569,7 @@ public final class IntList implements Serializable, Cloneable {
final int insertionPoint = Arrays.binarySearch(data, offset, size(), value);
result = insertionPoint < 0 ? -1 : insertionPoint;
} else {
for (int i = offset; i < index; i++) {
for (int i = offset; i < size; i++) {
if (data[i] == value) {
result = i;
break;
@@ -593,7 +590,7 @@ public final class IntList implements Serializable, Cloneable {
final StringBuilder result = new StringBuilder();
result.append('[');
for (int i = 0; i < index; i++) {
for (int i = 0; i < size; i++) {
if (i > 0) {
result.append(", ");
}
@@ -604,12 +601,12 @@ public final class IntList implements Serializable, Cloneable {
@Override
public int hashCode() {
if (index == 0) {
if (size == 0) {
return 0;
}
int result = 1;
for (int i = 0; i < index; i++) {
for (int i = 0; i < size; i++) {
result = 31 * result + data[i];
}
@@ -628,11 +625,11 @@ public final class IntList implements Serializable, Cloneable {
return false;
}
final IntList other = (IntList) obj;
if (index != other.index) {
if (size != other.size) {
return false;
}
for (int i = 0; i < index; i++) {
for (int i = 0; i < size; i++) {
if (data[i] != other.data[i]) {
return false;
}
@@ -654,9 +651,9 @@ public final class IntList implements Serializable, Cloneable {
// write out the array length. According to the implementation in ArrayList this
// is needed to be compatible with clone.
s.writeInt(index);
s.writeInt(size);
for (int i = 0; i < index; i++) {
for (int i = 0; i < size; i++) {
s.writeInt(data[i]);
}
}
@@ -672,10 +669,10 @@ public final class IntList implements Serializable, Cloneable {
// Read in capacity
s.readInt(); // ignored
if (index > 0) {
final int[] local_data = new int[index];
if (size > 0) {
final int[] local_data = new int[size];
for (int i = 0; i < index; i++) {
for (int i = 0; i < size; i++) {
local_data[i] = s.readInt();
}
data = local_data;
@@ -686,7 +683,7 @@ public final class IntList implements Serializable, Cloneable {
public IntList clone() {
try {
final IntList result = (IntList) super.clone();
result.data = index == 0 ? EMPTY_ARRAY : Arrays.copyOf(data, index);
result.data = size == 0 ? EMPTY_ARRAY : Arrays.copyOf(data, size);
return result;
} catch (final CloneNotSupportedException e) {
throw new IllegalStateException(e);