okaywit / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

IndexOf Methods for Primitives with Range #1504

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Hello,

I was browsing through the guava codebase and noticed that several of the 
primitives had a private overloaded indexOf of method specifying start and end 
indexes for range constrained look ups within the array. Also noted here seems 
that most of these have TODO's to consider making these public.

From a previous issue discussion thread (Issue:480) it seems to be noted that 
this change would essentially require the addition of 1) Javadoc Comments, 2) 
Precondition Checks and 3) Unit tests.

The question posed here is has this issue been picked up yet since the ticket 
(480) seems to be from a year ago and the follow up here is whether it would be 
a good attempt for me to pick this up as a first contribution.

Classes this would affect are:

Booleans, Bytes, Chars, Doubles, Floats, Ints, Longs and Shorts along with 
their corresponding unit tests.

Here a sample of what the proposed change might look like:

  /**
   * Returns the index of the first appearance of the value {@code target} in
   * {@code array}, in the index range of [{@code start}, {@code end}).
   *
   * @param array an array of {@code int} values, possibly empty
   * @param target a primitive {@code int} value
   * @return the least index {@code i} within the range 
   *     [start, end) for which {@code array[i] == target}, or
   *     {@code -1} if no such index exists.
   */
  public static int indexOf(
      int[] array, int target, int start, int end) {
    checkNotNull(array, "array");
    checkPositionIndexes(start, end, array.length);

    for (int i = start; i < end; i++) {
      if (array[i] == target) {
        return i;
      }
    }
    return -1;
  }

Unit Test:

  public void testIndexOf_customIndexes() {
    assertEquals(-1, Ints.indexOf(EMPTY, (int) 1, (int) 0, (int) 0));
    assertEquals(-1, Ints.indexOf(ARRAY1, (int) 2, (int) 0, (int) 1));
    assertEquals(-1, Ints.indexOf(ARRAY234, (int) 1, (int) 0, (int) 3));
    assertEquals(-1, Ints.indexOf(ARRAY234, (int) 2, (int) 1, (int) 3));
    assertEquals(-1, Ints.indexOf(ARRAY234, (int) 3, (int) 2, (int) 3));  
    assertEquals(0, Ints.indexOf(
        new int[] {(int) -1}, (int) -1, (int) 0, (int) 1));
    assertEquals(0, Ints.indexOf(ARRAY234, (int) 2, (int) 0, (int) 1));
    assertEquals(1, Ints.indexOf(ARRAY234, (int) 3, (int) 1, (int) 2));
    assertEquals(2, Ints.indexOf(ARRAY234, (int) 4, (int) 2, (int) 3));
    assertEquals(1, Ints.indexOf(
        new int[] { (int) 2, (int) 3, (int) 2, (int) 3 },
        (int) 3, (int) 0, (int) 4));
  }

Thanks

Original issue reported on code.google.com by abhishek...@gmail.com on 12 Aug 2013 at 6:08

GoogleCodeExporter commented 9 years ago

Original comment by lowas...@google.com on 25 Sep 2013 at 9:56

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<issue id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:12