use 'sorted' flag in indexOf

This commit is contained in:
2017-12-03 09:04:17 +01:00
parent c7cacf1ad4
commit 477820050b
2 changed files with 35 additions and 6 deletions

View File

@@ -566,12 +566,20 @@ public final class IntList implements Serializable, Cloneable {
* if offset is negative, or larger than the size of the list
*/
public int indexOf(final int value, final int offset) {
for (int i = offset; i < index; i++) {
if (data[i] == value) {
return i;
int result = -1;
if (sorted) {
final int insertionPoint = Arrays.binarySearch(data, offset, size(), value);
result = insertionPoint < 0 ? -1 : insertionPoint;
} else {
for (int i = offset; i < index; i++) {
if (data[i] == value) {
result = i;
break;
}
}
}
return -1;
return result;
}
@Override