Methods Summary |
---|
public void | add(int value)Adds an element to the end of the list. This will increase the
list's capacity if necessary.
throwIfImmutable();
growIfNeeded();
values[size++] = value;
if (sorted && (size > 1)) {
sorted = (value >= values[size - 2]);
}
|
public int | binarysearch(int value)Performs a binary search on a sorted list, returning the index of
the given value if it is present or
{@code (-(insertion point) - 1)} if the value is not present.
If the list is not sorted, then reverts to linear search and returns
{@code -size()} if the element is not found.
int sz = size;
if (!sorted) {
// Linear search.
for (int i = 0; i < sz; i++) {
if (values[i] == value) {
return i;
}
}
return -sz;
}
/*
* Binary search. This variant does only one value comparison
* per iteration but does one more iteration on average than
* the variant that includes a value equality check per
* iteration.
*/
int min = -1;
int max = sz;
while (max > (min + 1)) {
/*
* The guessIdx calculation is equivalent to ((min + max)
* / 2) but won't go wonky when min and max are close to
* Integer.MAX_VALUE.
*/
int guessIdx = min + ((max - min) >> 1);
int guess = values[guessIdx];
if (value <= guess) {
max = guessIdx;
} else {
min = guessIdx;
}
}
if ((max != sz)) {
return (value == values[max]) ? max : (-max - 1);
} else {
return -sz - 1;
}
|
public boolean | contains(int value)Returns whether or not the given value appears in the list.
This will do a binary search if the list is sorted or a linear
search if not.
return indexOf(value) >= 0;
|
public boolean | equals(java.lang.Object other){@inheritDoc}
if (other == this) {
return true;
}
if (! (other instanceof IntList)) {
return false;
}
IntList otherList = (IntList) other;
if (sorted != otherList.sorted) {
return false;
}
if (size != otherList.size) {
return false;
}
for (int i = 0; i < size; i++) {
if (values[i] != otherList.values[i]) {
return false;
}
}
return true;
|
public int | get(int n)Gets the indicated value.
if (n >= size) {
throw new IndexOutOfBoundsException("n >= size()");
}
try {
return values[n];
} catch (ArrayIndexOutOfBoundsException ex) {
// Translate exception.
throw new IndexOutOfBoundsException("n < 0");
}
|
private void | growIfNeeded()Increases size of array if needed
if (size == values.length) {
// Resize.
int[] newv = new int[size * 3 / 2 + 10];
System.arraycopy(values, 0, newv, 0, size);
values = newv;
}
|
public int | hashCode(){@inheritDoc}
int result = 0;
for (int i = 0; i < size; i++) {
result = (result * 31) + values[i];
}
return result;
|
public int | indexOf(int value)Returns the index of the given value, or -1 if the value does not
appear in the list. This will do a binary search if the list is
sorted or a linear search if not.
int ret = binarysearch(value);
return ret >= 0 ? ret : -1;
|
public void | insert(int n, int value)Inserts element into specified index, moving elements at and above
that index up one. May not be used to insert at an index beyond the
current size (that is, insertion as a last element is legal but
no further).
if (n > size) {
throw new IndexOutOfBoundsException("n > size()");
}
growIfNeeded();
System.arraycopy (values, n, values, n+1, size - n);
values[n] = value;
size++;
sorted = sorted
&& (n == 0 || value > values[n-1])
&& (n == (size - 1) || value < values[n+1]);
|
public static com.android.dexgen.util.IntList | makeImmutable(int value)Constructs a new immutable instance with the given element.
EMPTY.setImmutable();
IntList result = new IntList(1);
result.add(value);
result.setImmutable();
return result;
|
public static com.android.dexgen.util.IntList | makeImmutable(int value0, int value1)Constructs a new immutable instance with the given elements.
IntList result = new IntList(2);
result.add(value0);
result.add(value1);
result.setImmutable();
return result;
|
public com.android.dexgen.util.IntList | mutableCopy()Makes and returns a mutable copy of the list.
int sz = size;
IntList result = new IntList(sz);
for (int i = 0; i < sz; i++) {
result.add(values[i]);
}
return result;
|
public int | pop()Pops an element off the end of the list and decreasing the size by one.
throwIfImmutable();
int result;
result = get(size-1);
size--;
return result;
|
public void | pop(int n)Pops N elements off the end of the list and decreasing the size by N.
throwIfImmutable();
size -= n;
|
public void | removeIndex(int n)Removes an element at a given index, shifting elements at greater
indicies down one.
if (n >= size) {
throw new IndexOutOfBoundsException("n >= size()");
}
System.arraycopy (values, n + 1, values, n, size - n - 1);
size--;
// sort status is unchanged
|
public void | set(int n, int value)Sets the value at the given index.
throwIfImmutable();
if (n >= size) {
throw new IndexOutOfBoundsException("n >= size()");
}
try {
values[n] = value;
sorted = false;
} catch (ArrayIndexOutOfBoundsException ex) {
// Translate the exception.
if (n < 0) {
throw new IllegalArgumentException("n < 0");
}
}
|
public void | shrink(int newSize)Shrinks the size of the list.
if (newSize < 0) {
throw new IllegalArgumentException("newSize < 0");
}
if (newSize > size) {
throw new IllegalArgumentException("newSize > size");
}
throwIfImmutable();
size = newSize;
|
public int | size()Gets the number of elements in this list.
return size;
|
public void | sort()Sorts the elements in the list in-place.
throwIfImmutable();
if (!sorted) {
Arrays.sort(values, 0, size);
sorted = true;
}
|
public java.lang.String | toString(){@inheritDoc}
StringBuffer sb = new StringBuffer(size * 5 + 10);
sb.append('{");
for (int i = 0; i < size; i++) {
if (i != 0) {
sb.append(", ");
}
sb.append(values[i]);
}
sb.append('}");
return sb.toString();
|
public int | top()Returns the last element in the array without modifying the array
return get(size - 1);
|