indexOf on sorted lists should return the first match
This commit is contained in:
@@ -616,7 +616,10 @@ public final class IntList implements Serializable, Cloneable {
|
|||||||
|
|
||||||
int result = -1;
|
int result = -1;
|
||||||
if (sorted) {
|
if (sorted) {
|
||||||
final int insertionPoint = Arrays.binarySearch(data, offset, size(), value);
|
int insertionPoint = Arrays.binarySearch(data, offset, size(), value);
|
||||||
|
while (insertionPoint > 0 && insertionPoint > offset && data[insertionPoint - 1] == value) {
|
||||||
|
insertionPoint--;
|
||||||
|
}
|
||||||
result = insertionPoint < 0 ? -1 : insertionPoint;
|
result = insertionPoint < 0 ? -1 : insertionPoint;
|
||||||
} else {
|
} else {
|
||||||
for (int i = offset; i < size; i++) {
|
for (int i = offset; i < size; i++) {
|
||||||
@@ -867,5 +870,4 @@ public final class IntList implements Serializable, Cloneable {
|
|||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -680,6 +680,13 @@ public class IntListTest {
|
|||||||
Assert.assertEquals(1, list.indexOf(2));
|
Assert.assertEquals(1, list.indexOf(2));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testIndexOfOnSortedListReturnsFirstMatch() {
|
||||||
|
final IntList list = IntList.of(0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4);
|
||||||
|
Assert.assertEquals(2, list.indexOf(2));
|
||||||
|
Assert.assertEquals(4, list.indexOf(2, 4));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testIndexOfOnUnsortedList() {
|
public void testIndexOfOnUnsortedList() {
|
||||||
final IntList list = IntList.of(2, 0, 1);
|
final IntList list = IntList.of(2, 0, 1);
|
||||||
|
|||||||
Reference in New Issue
Block a user