diff --git a/primitiveCollections/src/main/java/org/lucares/collections/IntList.java b/primitiveCollections/src/main/java/org/lucares/collections/IntList.java index feb113d..3cfb94b 100644 --- a/primitiveCollections/src/main/java/org/lucares/collections/IntList.java +++ b/primitiveCollections/src/main/java/org/lucares/collections/IntList.java @@ -26,7 +26,6 @@ public final class IntList implements Serializable, Cloneable { // TODO add removeRange // TODO add replace // TODO add lastIndexOf - // TODO add indexOf // TODO remove bounds checks that are handled by java, e.g. negative indices // TODO toString @@ -352,6 +351,39 @@ public final class IntList implements Serializable, Cloneable { return StreamSupport.intStream(spliterator, true); } + /** + * Returns the index of the first occurrence of {@code value}, or -1 if it does + * not exist. + * + * @param value + * the value + * @return the index, or -1 + */ + public int indexOf(final int value) { + return indexOf(value, 0); + } + + /** + * Returns the index of the first occurrence of {@code value} starting at the + * {@code offset}'s element, or -1 if it does not exist. + * + * @param value + * the value + * @param offset + * the offset + * @return the index, or -1 + * @throws ArrayIndexOutOfBoundsException + * 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; + } + } + return -1; + } + @Override public String toString() { diff --git a/primitiveCollections/src/test/java/org/lucares/collections/IntListTest.java b/primitiveCollections/src/test/java/org/lucares/collections/IntListTest.java index 5be1a76..d0294ad 100644 --- a/primitiveCollections/src/test/java/org/lucares/collections/IntListTest.java +++ b/primitiveCollections/src/test/java/org/lucares/collections/IntListTest.java @@ -583,4 +583,36 @@ public class IntListTest { Assert.assertNotEquals("should use parallelism during computation", processingOrder.toString(), list.toString()); } + + @Test + public void testIndexOf() { + final IntList list = new IntList(); + Assert.assertEquals(-1, list.indexOf(0)); + + list.add(1); + Assert.assertEquals(-1, list.indexOf(0)); + Assert.assertEquals(0, list.indexOf(1)); + + list.add(2); + Assert.assertEquals(-1, list.indexOf(0)); + Assert.assertEquals(0, list.indexOf(1)); + Assert.assertEquals(1, list.indexOf(2)); + } + + @Test + public void testIndexOfWithOffset() { + final IntList list = new IntList(); + list.addAll(0, 2, 0, 2); + Assert.assertEquals(1, list.indexOf(2, 0)); + Assert.assertEquals(1, list.indexOf(2, 1)); + Assert.assertEquals(3, list.indexOf(2, 2)); + Assert.assertEquals(3, list.indexOf(2, 3)); + Assert.assertEquals(-1, list.indexOf(2, 4)); + + // indexed returned by indexOf() are consistent with get() + Assert.assertEquals(list.get(list.indexOf(2, 0)), 2); + Assert.assertEquals(list.get(list.indexOf(2, 1)), 2); + Assert.assertEquals(list.get(list.indexOf(2, 2)), 2); + Assert.assertEquals(list.get(list.indexOf(2, 3)), 2); + } }