Index: . =================================================================== --- . (revision 540127) +++ . (working copy) @@ -444,7 +444,7 @@ } /** - * Fills the specified array with the specified element. + * Fills the array with the given value. * * @param array * the byte array to fill @@ -458,7 +458,7 @@ } /** - * Fills the specified range in the array with the specified element. + * Fills the section of the array between the given indices with the given value. * * @param array * the byte array to fill @@ -463,9 +463,9 @@ * @param array * the byte array to fill * @param start - * the first index to fill + * the start index * @param end - * the last + 1 index to fill + * the end index + 1 * @param value * the byte element * @@ -490,7 +490,7 @@ } /** - * Fills the specified array with the specified element. + * Fills the array with the given value. * * @param array * the short array to fill @@ -504,7 +504,7 @@ } /** - * Fills the specified range in the array with the specified element. + * Fills the section of the array between the given indices with the given value. * * @param array * the short array to fill @@ -509,9 +509,9 @@ * @param array * the short array to fill * @param start - * the first index to fill + * the start index * @param end - * the last + 1 index to fill + * the end index + 1 * @param value * the short element * @@ -536,7 +536,7 @@ } /** - * Fills the specified array with the specified element. + * Fills the array with the given value. * * @param array * the char array to fill @@ -550,7 +550,7 @@ } /** - * Fills the specified range in the array with the specified element. + * Fills the section of the array between the given indices with the given value. * * @param array * the char array to fill @@ -555,9 +555,9 @@ * @param array * the char array to fill * @param start - * the first index to fill + * the start index * @param end - * the last + 1 index to fill + * the end index + 1 * @param value * the char element * @@ -582,7 +582,7 @@ } /** - * Fills the specified array with the specified element. + * Fills the array with the given value. * * @param array * the int array to fill @@ -596,7 +596,7 @@ } /** - * Fills the specified range in the array with the specified element. + * Fills the section of the array between the given indices with the given value. * * @param array * the int array to fill @@ -601,9 +601,9 @@ * @param array * the int array to fill * @param start - * the first index to fill + * the start index * @param end - * the last + 1 index to fill + * the end index + 1 * @param value * the int element * @@ -628,7 +628,7 @@ } /** - * Fills the specified array with the specified element. + * Fills the array with the given value. * * @param array * the long array to fill @@ -642,7 +642,7 @@ } /** - * Fills the specified range in the array with the specified element. + * Fills the section of the array between the given indices with the given value. * * @param array * the long array to fill @@ -647,9 +647,9 @@ * @param array * the long array to fill * @param start - * the first index to fill + * the start index * @param end - * the last + 1 index to fill + * the end index + 1 * @param value * the long element * @@ -674,7 +674,7 @@ } /** - * Fills the specified array with the specified element. + * Fills the array with the given value. * * @param array * the float array to fill @@ -688,7 +688,7 @@ } /** - * Fills the specified range in the array with the specified element. + * Fills the section of the array between the given indices with the given value. * * @param array * the float array to fill @@ -693,9 +693,9 @@ * @param array * the float array to fill * @param start - * the first index to fill + * the start index * @param end - * the last + 1 index to fill + * the end index + 1 * @param value * the float element * @@ -720,7 +720,7 @@ } /** - * Fills the specified array with the specified element. + * Fills the array with the given value. * * @param array * the float array to fill @@ -734,7 +734,7 @@ } /** - * Fills the specified range in the array with the specified element. + * Fills the section of the array between the given indices with the given value. * * @param array * the double array to fill @@ -739,9 +739,9 @@ * @param array * the double array to fill * @param start - * the first index to fill + * the start index * @param end - * the last + 1 index to fill + * the end index + 1 * @param value * the double element * @@ -766,7 +766,7 @@ } /** - * Fills the specified array with the specified element. + * Fills the array with the given value. * * @param array * the boolean array to fill @@ -780,7 +780,7 @@ } /** - * Fills the specified range in the array with the specified element. + * Fills the section of the array between the given indices with the given value. * * @param array * the boolean array to fill @@ -785,9 +785,9 @@ * @param array * the boolean array to fill * @param start - * the first index to fill + * the start index * @param end - * the last + 1 index to fill + * the end index + 1 * @param value * the boolean element * @@ -812,7 +812,7 @@ } /** - * Fills the specified array with the specified element. + * Fills the array with the given value. * * @param array * the Object array to fill @@ -826,7 +826,7 @@ } /** - * Fills the specified range in the array with the specified element. + * Fills the section of the array between the given indices with the given value. * * @param array * the Object array to fill @@ -831,9 +831,9 @@ * @param array * the Object array to fill * @param start - * the first index to fill + * the start index * @param end - * the last + 1 index to fill + * the end index + 1 * @param value * the Object element * @@ -858,18 +858,20 @@ } /** - * Answers a hash code based on the contents of the given array. For any two - * boolean arrays a and b, if Arrays.equals(a, b) returns true, it means - * that the return value of Arrays.hashCode(a) equals Arrays.hashCode(b). + * Returns the hash code for the given array. * + * If Arrays.equals(...) returns true for two arrays then their hash codes + * will also be equal. + * * The value returned by this method is the same value as the * {@link List#hashCode()}} method which is invoked on a {@link List}} * containing a sequence of {@link Boolean}} instances representing the - * elements of array in the same order. If the array is null, the return - * value is 0. + * elements of array in the same order. + * + * If the array is null the return value will be 0. * * @param array - * the array whose hash code to compute + * the array to return the hash code for * @return the hash code for array */ public static int hashCode(boolean[] array) { @@ -885,10 +887,10 @@ } /** - * Answers a hash code based on the contents of the given array. For any two - * not-null int arrays a and b, if Arrays.equals(a, b) returns true, it - * means that the return value of Arrays.hashCode(a) equals - * Arrays.hashCode(b). + * Returns the hash code for the given array. + * + * If Arrays.equals(...) returns true for two arrays then their hash codes + * will also be equal. * * The value returned by this method is the same value as the * {@link List#hashCode()}} method which is invoked on a {@link List}} @@ -893,11 +895,12 @@ * The value returned by this method is the same value as the * {@link List#hashCode()}} method which is invoked on a {@link List}} * containing a sequence of {@link Integer}} instances representing the - * elements of array in the same order. If the array is null, the return - * value is 0. + * elements of array in the same order. + * + * If the array is null the return value will be 0. * * @param array - * the array whose hash code to compute + * the array to return the hash code for * @return the hash code for array */ public static int hashCode(int[] array) { @@ -913,9 +916,10 @@ } /** - * Answers a hash code based on the contents of the given array. For any two - * short arrays a and b, if Arrays.equals(a, b) returns true, it means that - * the return value of Arrays.hashCode(a) equals Arrays.hashCode(b). + * Returns the hash code for the given array. + * + * If Arrays.equals(...) returns true for two arrays then their hash codes + * will also be equal. * * The value returned by this method is the same value as the * {@link List#hashCode()}} method which is invoked on a {@link List}} @@ -920,11 +924,12 @@ * The value returned by this method is the same value as the * {@link List#hashCode()}} method which is invoked on a {@link List}} * containing a sequence of {@link Short}} instances representing the - * elements of array in the same order. If the array is null, the return - * value is 0. + * elements of array in the same order. + * + * If the array is null the return value will be 0. * * @param array - * the array whose hash code to compute + * the array to return the hash code for * @return the hash code for array */ public static int hashCode(short[] array) { @@ -940,18 +945,20 @@ } /** - * Answers a hash code based on the contents of the given array. For any two - * char arrays a and b, if Arrays.equals(a, b) returns true, it means that - * the return value of Arrays.hashCode(a) equals Arrays.hashCode(b). + * Returns the hash code for the given array. * + * If Arrays.equals(...) returns true for two arrays then their hash codes + * will also be equal. + * * The value returned by this method is the same value as the * {@link List#hashCode()}} method which is invoked on a {@link List}} * containing a sequence of {@link Character}} instances representing the - * elements of array in the same order. If the array is null, the return - * value is 0. + * elements of array in the same order. + * + * If the array is null the return value will be 0. * * @param array - * the array whose hash code to compute + * the array to return the hash code for * @return the hash code for array */ public static int hashCode(char[] array) { @@ -967,18 +974,20 @@ } /** - * Answers a hash code based on the contents of the given array. For any two - * byte arrays a and b, if Arrays.equals(a, b) returns true, it means that - * the return value of Arrays.hashCode(a) equals Arrays.hashCode(b). + * Returns the hash code for the given array. * + * If Arrays.equals(...) returns true for two arrays then their hash codes + * will also be equal. + * * The value returned by this method is the same value as the * {@link List#hashCode()}} method which is invoked on a {@link List}} * containing a sequence of {@link Byte}} instances representing the - * elements of array in the same order. If the array is null, the return - * value is 0. + * elements of array in the same order. + * + * If the array is null the return value will be 0. * * @param array - * the array whose hash code to compute + * the array to return the hash code for * @return the hash code for array */ public static int hashCode(byte[] array) { @@ -994,18 +1003,20 @@ } /** - * Answers a hash code based on the contents of the given array. For any two - * long arrays a and b, if Arrays.equals(a, b) returns true, it means that - * the return value of Arrays.hashCode(a) equals Arrays.hashCode(b). + * Returns the hash code for the given array. * + * If Arrays.equals(...) returns true for two arrays then their hash codes + * will also be equal. + * * The value returned by this method is the same value as the * {@link List#hashCode()}} method which is invoked on a {@link List}} * containing a sequence of {@link Long}} instances representing the - * elements of array in the same order. If the array is null, the return - * value is 0. + * elements of array in the same order. + * + * If the array is null the return value will be 0. * * @param array - * the array whose hash code to compute + * the array to return the hash code for * @return the hash code for array */ public static int hashCode(long[] array) { @@ -1025,18 +1036,20 @@ } /** - * Answers a hash code based on the contents of the given array. For any two - * float arrays a and b, if Arrays.equals(a, b) returns true, it means that - * the return value of Arrays.hashCode(a) equals Arrays.hashCode(b). + * Returns the hash code for the given array. * + * If Arrays.equals(...) returns true for two arrays then their hash codes + * will also be equal. + * * The value returned by this method is the same value as the * {@link List#hashCode()}} method which is invoked on a {@link List}} * containing a sequence of {@link Float}} instances representing the - * elements of array in the same order. If the array is null, the return - * value is 0. + * elements of array in the same order. + * + * If the array is null the return value will be 0. * * @param array - * the array whose hash code to compute + * the array to return the hash code for * @return the hash code for array */ public static int hashCode(float[] array) { @@ -1055,9 +1068,10 @@ } /** - * Answers a hash code based on the contents of the given array. For any two - * double arrays a and b, if Arrays.equals(a, b) returns true, it means that - * the return value of Arrays.hashCode(a) equals Arrays.hashCode(b). + * Returns the hash code for the given array. + * + * If Arrays.equals(...) returns true for two arrays then their hash codes + * will also be equal. * * The value returned by this method is the same value as the * {@link List#hashCode()}} method which is invoked on a {@link List}} @@ -1062,11 +1076,12 @@ * The value returned by this method is the same value as the * {@link List#hashCode()}} method which is invoked on a {@link List}} * containing a sequence of {@link Double}} instances representing the - * elements of array in the same order. If the array is null, the return - * value is 0. + * elements of array in the same order. + * + * If the array is null, the return value will be 0. * * @param array - * the array whose hash code to compute + * the array to return the hash code for * @return the hash code for array */ public static int hashCode(double[] array) { @@ -1087,21 +1102,20 @@ } /** - * Answers a hash code based on the contents of the given array. If the - * array contains other arrays as its elements, the hash code is based on - * their identities not their contents. So it is acceptable to invoke this - * method on an array that contains itself as an element, either directly or - * indirectly. + * Returns the hash code for the given array. If this array contains other + * arrays, their contents will not be recursively searched so this method + * should be used if the array is likely to contain a reference to itself. * - * For any two arrays a and b, if Arrays.equals(a, b) returns true, it means - * that the return value of Arrays.hashCode(a) equals Arrays.hashCode(b). + * If Arrays.equals(...) returns true for two arrays then their hash codes + * will also be equal. * * The value returned by this method is the same value as the method - * Arrays.asList(array).hashCode(). If the array is null, the return value - * is 0. + * Arrays.asList(array).hashCode(). + * + * If the array is null, the return value will be 0. * * @param array - * the array whose hash code to compute + * the array to return the hash code for * @return the hash code for array */ public static int hashCode(Object[] array) { @@ -1123,10 +1137,11 @@ } /** - * Answers a hash code based on the deep contents of the given array. This - * means that if this array contains other arrays, their contents will also - * be included in the hash and so on recursively. This method should not be - * used if the array is likely to contain a reference to itself. + * Returns the 'deep' hash code for the given array. This means that if this + * array contains other arrays their contents will also be included in the + * hash and so on recursively. This method should not be used if the array + * or any arrays contained within it are likely to contain a reference to + * itself. * * If Arrays.deepEquals(...) returns true for two arrays then their deep * hash codes will also be equal. @@ -1131,12 +1146,10 @@ * If Arrays.deepEquals(...) returns true for two arrays then their deep * hash codes will also be equal. * - * The value returned by this method is the same value as the method - * Arrays.asList(array).hashCode(). If the array is null, the return value - * is 0. + * If the array is null the return value will be 0. * * @param array - * the array whose hash code to compute + * the array to return the hash code for * @return the hash code for array */ public static int deepHashCode(Object[] array) { @@ -1428,32 +1441,16 @@ } /** - * Returns true if the two given arrays are deeply equal to one another. - * Unlike the method equals(Object[] array1, Object[] array2), this method - * is appropriate for use for nested arrays of arbitrary depth. - * - * Two array references are considered deeply equal if they are both null, - * or if they refer to arrays that have the same length and the elements at - * each index in the two arrays are equal. - * - * Two null elements element1 and element2 are possibly deeply equal if any - * of the following conditions satisfied: + * Returns the 'deep' equals for the two given arrays. This means that if + * either of the arrays contains other arrays then their contents are also + * checked for (deep) equality. If two corresponding elements are arrays of + * a primitive type then the appropriate Arrays.equals method + * is used to determine their equality. Otherwise for two + * Object arrays deepEquals is called + * recursively. * - * element1 and element2 are both arrays of object reference types, and - * Arrays.deepEquals(element1, element2) would return true. - * - * element1 and element2 are arrays of the same primitive type, and the - * appropriate overloading of Arrays.equals(element1, element2) would return - * true. - * - * element1 == element2 - * - * element1.equals(element2) would return true. - * - * Note that this definition permits null elements at any depth. - * - * If either of the given arrays contain themselves as elements, the - * behavior of this method is incertitude. + * This method should not be used if either of the arrays, or any arrays + * contained within it are likely to contain a reference to itself. * * @param array1 * the first Object array @@ -1613,10 +1610,11 @@ } /** - * Sorts the specified array in ascending order. + * Performs a sort on the given array. Elements will be re-ordered into + * ascending order. * * @param array - * the byte array to be sorted + * the byte array to sort */ public static void sort(byte[] array) { sort(0, array.length, array); @@ -1623,14 +1621,15 @@ } /** - * Sorts the specified range in the array in ascending order. + * Performs a sort on the section of the array between the given indices. + * Elements will be re-ordered into ascending order. * * @param array - * the byte array to be sorted + * the byte array to sort * @param start - * the start index to sort + * the start index * @param end - * the last + 1 index to sort + * the end index + 1 * * @exception IllegalArgumentException * when start > end @@ -1648,8 +1647,8 @@ private static void checkBounds(int arrLength, int start, int end) { if (start > end) { - throw new IllegalArgumentException("fromIndex(" + start //$NON-NLS-1$ - + ") > toIndex(" + end + ")"); //$NON-NLS-1$ //$NON-NLS-2$ + throw new IllegalArgumentException("start(" + start //$NON-NLS-1$ + + ") > end(" + end + ")"); //$NON-NLS-1$ //$NON-NLS-2$ } if (start < 0 || end > arrLength) { throw new ArrayIndexOutOfBoundsException(); @@ -1735,10 +1734,11 @@ } /** - * Sorts the specified array in ascending order. + * Performs a sort on the given array. Elements will be re-ordered into + * ascending order. * * @param array - * the char array to be sorted + * the char array to sort */ public static void sort(char[] array) { sort(0, array.length, array); @@ -1745,14 +1745,15 @@ } /** - * Sorts the specified range in the array in ascending order. + * Performs a sort on the section of the array between the given indices. + * Elements will be re-ordered into ascending order. * * @param array - * the char array to be sorted + * the char array to sort * @param start - * the start index to sort + * the start index * @param end - * the last + 1 index to sort + * the end index + 1 * * @exception IllegalArgumentException * when start > end @@ -1847,10 +1848,11 @@ } /** - * Sorts the specified array in ascending order. + * Performs a sort on the given array. Elements will be re-ordered into + * ascending order. * * @param array - * the double array to be sorted + * the double array to sort * * @see #sort(double[], int, int) */ @@ -1859,15 +1861,15 @@ } /** - * Sorts the specified range in the array in ascending order. The values are - * sorted according to the order imposed by Double.compareTo(). + * Performs a sort on the section of the array between the given indices. + * Elements will be re-ordered into ascending order. * * @param array - * the double array to be sorted + * the double array to sort * @param start - * the start index to sort + * the start index * @param end - * the last + 1 index to sort + * the end index + 1 * * @exception IllegalArgumentException * when start > end @@ -1964,10 +1966,11 @@ } /** - * Sorts the specified array in ascending order. + * Performs a sort on the given array. Elements will be re-ordered into + * ascending order. * * @param array - * the float array to be sorted + * the float array to sort * * @see #sort(float[], int, int) */ @@ -1976,15 +1979,15 @@ } /** - * Sorts the specified range in the array in ascending order. The values are - * sorted according to the order imposed by Float.compareTo(). + * Performs a sort on the section of the array between the given indices. + * Elements will be re-ordered into ascending order. * * @param array - * the float array to be sorted + * the float array to sort * @param start - * the start index to sort + * the start index * @param end - * the last + 1 index to sort + * the end index + 1 * * @exception IllegalArgumentException * when start > end @@ -2081,10 +2084,11 @@ } /** - * Sorts the specified array in ascending order. + * Performs a sort on the given array. Elements will be re-ordered into + * ascending order. * * @param array - * the int array to be sorted + * the int array to sort */ public static void sort(int[] array) { sort(0, array.length, array); @@ -2091,14 +2095,15 @@ } /** - * Sorts the specified range in the array in ascending order. + * Performs a sort on the section of the array between the given indices. + * Elements will be re-ordered into ascending order. * * @param array - * the int array to be sorted + * the int array to sort * @param start - * the start index to sort + * the start index * @param end - * the last + 1 index to sort + * the end index + 1 * * @exception IllegalArgumentException * when start > end @@ -2193,10 +2198,11 @@ } /** - * Sorts the specified array in ascending order. + * Performs a sort on given array. Elements will be re-ordered into + * ascending order. * * @param array - * the long array to be sorted + * the long array to sort */ public static void sort(long[] array) { sort(0, array.length, array); @@ -2203,14 +2209,15 @@ } /** - * Sorts the specified range in the array in ascending order. + * Performs a sort on the section of the array between the given indices. + * Elements will be re-ordered into ascending order. * * @param array - * the long array to be sorted + * the long array to sort * @param start - * the start index to sort + * the start index * @param end - * the last + 1 index to sort + * the end index + 1 * * @exception IllegalArgumentException * when start > end @@ -2305,10 +2312,11 @@ } /** - * Sorts the specified array in ascending order. + * Performs a sort on the given array. Elements will be re-ordered into + * ascending order. * * @param array - * the Object array to be sorted + * the Object array to sort * * @exception ClassCastException * when an element in the array does not implement Comparable @@ -2319,17 +2327,18 @@ } /** - * Sorts the specified range in the array in ascending order. + * Performs a sort on the section of the array between the given indices. + * Elements will be re-ordered into ascending order. * * @param array - * the Object array to be sorted + * the Object array to sort * @param start - * the start index to sort + * the start index * @param end - * the last + 1 index to sort + * the end index + 1 * * @exception ClassCastException - * when an element in the array does not implement Comparable + * when an element in the array does not implement Comparable * or elements cannot be compared to each other * @exception IllegalArgumentException * when start > end @@ -2345,8 +2354,8 @@ sort(start, end, array); } - private static void sort(int fromIndex, int toIndex, Object[] array) { - int length = toIndex - fromIndex; + private static void sort(int start, int end, Object[] array) { + int length = end - start; if (length <= 0) { return; } @@ -2351,11 +2360,11 @@ return; } if (array instanceof String[]) { - stableStringSort((String[]) array, fromIndex, toIndex); + stableStringSort((String[]) array, start, end); } else { - Object[] out = new Object[toIndex]; - System.arraycopy(array, fromIndex, out, fromIndex, length); - mergeSort(out, array, fromIndex, toIndex); + Object[] out = new Object[end]; + System.arraycopy(array, start, out, start, length); + mergeSort(out, array, start, end); } } @@ -2360,7 +2369,7 @@ } /** - * Swaps the elements at the specified positions in the specified array. + * Swaps the elements at the given indices in the array. * * @param a - * the index of one element to be swapped. @@ -2376,16 +2385,11 @@ } /** - * Sorts the specified range of the specified array of objects. The range to - * be sorted extends from index fromIndex, inclusive, to index toIndex, - * exclusive. (If fromIndex==toIndex, the range to be sorted is empty.) This - * sort is guaranteed to be stable: equal elements will not be reordered as - * a result of the sort. - * - * The sorting algorithm is a mergesort with exponential search (in which - * the merge is performed by exponential search). This algorithm offers - * guaranteed n*log(n) performance and in average case faster then any - * mergesort in which the merge is performed by linear search. + * Performs a sort on the section of the array between the given indices + * using a mergesort with exponential search algorithm (in which the merge + * is performed by exponential search). n*log(n) performance is guaranteed + * and in the average case it will be faster then any mergesort in which the + * merge is performed by linear search. * * @param in - * the array for sorting. @@ -2391,18 +2395,18 @@ * the array for sorting. * @param out - * the result, sorted array. - * @param fromIndex - - * the index of the first element (inclusive) to be sorted. - * @param toIndex - - * the index of the last element (exclusive) to be sorted. + * @param start + * the start index + * @param end + * the end index + 1 */ @SuppressWarnings("unchecked") - private static void mergeSort(Object[] in, Object[] out, int fromIndex, - int toIndex) { - int len = toIndex - fromIndex; + private static void mergeSort(Object[] in, Object[] out, int start, + int end) { + int len = end - start; // use insertion sort for small arrays if (len <= SIMPLE_LENGTH) { - for (int i = fromIndex + 1; i < toIndex; i++) { + for (int i = start + 1; i < end; i++) { Comparable current = (Comparable) out[i]; Object prev = out[i - 1]; if (current.compareTo(prev) < 0) { @@ -2409,7 +2413,7 @@ int j = i; do { out[j--] = prev; - } while (j > fromIndex + } while (j > start && current.compareTo(prev = out[j - 1]) < 0); out[j] = current; } @@ -2416,9 +2420,9 @@ } return; } - int med = (toIndex + fromIndex) >> 1; - mergeSort(out, in, fromIndex, med); - mergeSort(out, in, med, toIndex); + int med = (end + start) >> 1; + mergeSort(out, in, start, med); + mergeSort(out, in, med, end); // merging @@ -2424,25 +2428,25 @@ // if arrays are already sorted - no merge if (((Comparable) in[med - 1]).compareTo(in[med]) <= 0) { - System.arraycopy(in, fromIndex, out, fromIndex, len); + System.arraycopy(in, start, out, start, len); return; } - int r = med, i = fromIndex; + int r = med, i = start; // use merging with exponential search do { - Comparable fromVal = (Comparable) in[fromIndex]; + Comparable fromVal = (Comparable) in[start]; Comparable rVal = (Comparable) in[r]; if (fromVal.compareTo(rVal) <= 0) { - int l_1 = find(in, rVal, -1, fromIndex + 1, med - 1); - int toCopy = l_1 - fromIndex + 1; - System.arraycopy(in, fromIndex, out, i, toCopy); + int l_1 = find(in, rVal, -1, start + 1, med - 1); + int toCopy = l_1 - start + 1; + System.arraycopy(in, start, out, i, toCopy); i += toCopy; out[i++] = rVal; r++; - fromIndex = l_1 + 1; + start = l_1 + 1; } else { - int r_1 = find(in, fromVal, 0, r + 1, toIndex - 1); + int r_1 = find(in, fromVal, 0, r + 1, end - 1); int toCopy = r_1 - r + 1; System.arraycopy(in, r, out, i, toCopy); i += toCopy; @@ -2447,16 +2451,16 @@ System.arraycopy(in, r, out, i, toCopy); i += toCopy; out[i++] = fromVal; - fromIndex++; + start++; r = r_1 + 1; } - } while ((toIndex - r) > 0 && (med - fromIndex) > 0); + } while ((end - r) > 0 && (med - start) > 0); // copy rest of array - if ((toIndex - r) <= 0) { - System.arraycopy(in, fromIndex, out, i, med - fromIndex); + if ((end - r) <= 0) { + System.arraycopy(in, start, out, i, med - start); } else { - System.arraycopy(in, r, out, i, toIndex - r); + System.arraycopy(in, r, out, i, end - r); } } @@ -2461,16 +2465,11 @@ } /** - * Sorts the specified range of the specified array of objects. The range to - * be sorted extends from index fromIndex, inclusive, to index toIndex, - * exclusive. (If fromIndex==toIndex, the range to be sorted is empty.) This - * sort is guaranteed to be stable: equal elements will not be reordered as - * a result of the sort. - * - * The sorting algorithm is a mergesort with exponential search (in which - * the merge is performed by exponential search). This algorithm offers - * guaranteed n*log(n) performance and in average case faster then any - * mergesort in which the merge is performed by linear search. + * Performs a sort on the section of the array between the given indices + * using a mergesort with exponential search algorithm (in which the merge + * is performed by exponential search). n*log(n) performance is guaranteed + * and in the average case it will be faster then any mergesort in which the + * merge is performed by linear search. * * @param in - * the array for sorting. @@ -2476,10 +2475,10 @@ * the array for sorting. * @param out - * the result, sorted array. - * @param fromIndex - - * the index of the first element (inclusive) to be sorted. - * @param toIndex - - * the index of the last element (exclusive) to be sorted. + * @param start + * the start index + * @param end + * the end index + 1 * @param c - * the comparator to determine the order of the array. */ @@ -2484,12 +2483,12 @@ * the comparator to determine the order of the array. */ @SuppressWarnings("unchecked") - private static void mergeSort(Object[] in, Object[] out, int fromIndex, - int toIndex, Comparator c) { - int len = toIndex - fromIndex; + private static void mergeSort(Object[] in, Object[] out, int start, + int end, Comparator c) { + int len = end - start; // use insertion sort for small arrays if (len <= SIMPLE_LENGTH) { - for (int i = fromIndex + 1; i < toIndex; i++) { + for (int i = start + 1; i < end; i++) { Object current = out[i]; Object prev = out[i - 1]; if (c.compare(prev, current) > 0) { @@ -2496,7 +2495,7 @@ int j = i; do { out[j--] = prev; - } while (j > fromIndex + } while (j > start && (c.compare(prev = out[j - 1], current) > 0)); out[j] = current; } @@ -2503,9 +2502,9 @@ } return; } - int med = (toIndex + fromIndex) >> 1; - mergeSort(out, in, fromIndex, med, c); - mergeSort(out, in, med, toIndex, c); + int med = (end + start) >> 1; + mergeSort(out, in, start, med, c); + mergeSort(out, in, med, end, c); // merging @@ -2511,25 +2510,25 @@ // if arrays are already sorted - no merge if (c.compare(in[med - 1],in[med] ) <= 0) { - System.arraycopy(in, fromIndex, out, fromIndex, len); + System.arraycopy(in, start, out, start, len); return; } - int r = med, i = fromIndex; + int r = med, i = start; // use merging with exponential search do { - Object fromVal = in[fromIndex]; + Object fromVal = in[start]; Object rVal = in[r]; if (c.compare(fromVal, rVal) <= 0) { - int l_1 = find(in, rVal, -1, fromIndex + 1, med - 1, c); - int toCopy = l_1 - fromIndex + 1; - System.arraycopy(in, fromIndex, out, i, toCopy); + int l_1 = find(in, rVal, -1, start + 1, med - 1, c); + int toCopy = l_1 - start + 1; + System.arraycopy(in, start, out, i, toCopy); i += toCopy; out[i++] = rVal; r++; - fromIndex = l_1 + 1; + start = l_1 + 1; } else { - int r_1 = find(in, fromVal, 0, r + 1, toIndex - 1, c); + int r_1 = find(in, fromVal, 0, r + 1, end - 1, c); int toCopy = r_1 - r + 1; System.arraycopy(in, r, out, i, toCopy); i += toCopy; @@ -2534,16 +2533,16 @@ System.arraycopy(in, r, out, i, toCopy); i += toCopy; out[i++] = fromVal; - fromIndex++; + start++; r = r_1 + 1; } - } while ((toIndex - r) > 0 && (med - fromIndex) > 0); + } while ((end - r) > 0 && (med - start) > 0); // copy rest of array - if ((toIndex - r) <= 0) { - System.arraycopy(in, fromIndex, out, i, med - fromIndex); + if ((end - r) <= 0) { + System.arraycopy(in, start, out, i, med - start); } else { - System.arraycopy(in, r, out, i, toIndex - r); + System.arraycopy(in, r, out, i, end - r); } } @@ -2548,7 +2547,7 @@ } /** - * Finds the place of specified range of specified sorted array, where the + * Finds the place in the given range of specified sorted array, where the * element should be inserted for getting sorted array. Uses exponential * search algorithm. * @@ -2559,10 +2558,10 @@ * object to be inserted * * @param l - - * the index of the first element (inclusive) + * the start index * * @param r - - * the index of the last element (inclusive) + * the end index * * @param bnd - * possible values 0,-1. "-1" - val is located at index more then @@ -2597,7 +2596,7 @@ /** * Finds the place of specified range of specified sorted array, where the - * element should be inserted for getting sorted array. Uses expionential + * element should be inserted for getting sorted array. Uses exponential * search algorithm. * * @param arr - @@ -2602,16 +2601,12 @@ * * @param arr - * the array with already sorted range - * * @param val - * object to be inserted - * * @param l - - * the index of the first element (inclusive) - * + * the start index * @param r - - * the index of the last element (inclusive) - * + * the end index * @param bnd - * possible values 0,-1. "-1" - val is located at index more then * elements equals to val. "0" - val is located at index less @@ -2616,9 +2611,8 @@ * possible values 0,-1. "-1" - val is located at index more then * elements equals to val. "0" - val is located at index less * then elements equals to val. - * * @param c - - * the comparator to determine the order of the array. + * the comparator used to compare Objects */ @SuppressWarnings("unchecked") private static int find(Object[] arr, Object val, int bnd, int l, int r, @@ -2704,26 +2698,27 @@ } /** - * Sorts the specified range of the specified array of String. + * Performs a sort on the given String array. Elements will be re-ordered into + * ascending order. * * @param arr - - * the array to be sorted - * @param fromIndex - - * the index of the first element (inclusive) to be sorted. - * @param toIndex - - * the index of the last element (exclusive) to be sorted. + * the array to sort + * @param start - + * the start index + * @param end - + * the end index + 1 */ - private static void stableStringSort(String[] arr, int fromIndex, - int toIndex) { - stableStringSort(arr, arr, new String[toIndex], fromIndex, toIndex, 0); + private static void stableStringSort(String[] arr, int start, + int end) { + stableStringSort(arr, arr, new String[end], start, end, 0); } /** - * Sorts the specified range of the specified array of String. Use stable - * ternary quick sort algorithm. + * Performs a sort on the given String array. Elements will be re-ordered into + * ascending order. Uses a stable ternary quick sort algorithm. * * @param arr - - * the array to be sorted + * the array to sort * @param src - * auxiliary array * @param dst - @@ -2728,10 +2723,10 @@ * auxiliary array * @param dst - * auxiliary array - * @param fromIndex - - * the index of the first element (inclusive) to be sorted. - * @param toIndex - - * the index of the last element (exclusive) to be sorted. + * @param start - + * the start index + * @param end - + * the end index + 1 * @param chId - * index of char for current sorting */ @@ -2736,12 +2731,12 @@ * index of char for current sorting */ private static void stableStringSort(String[] arr, String[] src, - String[] dst, int fromIndex, int toIndex, int chId) { - int length = toIndex - fromIndex; + String[] dst, int start, int end, int chId) { + int length = end - start; // use insertion sort for small arrays if (length < SIMPLE_LENGTH) { if (src == arr) { - for (int i = fromIndex + 1; i < toIndex; i++) { + for (int i = start + 1; i < end; i++) { String current = arr[i]; String prev = arr[i - 1]; if (current.compareTo(prev) < 0) { @@ -2748,7 +2743,7 @@ int j = i; do { arr[j--] = prev; - } while (j > fromIndex + } while (j > start && current.compareTo(prev = arr[j - 1]) < 0); arr[j] = current; } @@ -2754,13 +2749,13 @@ } } } else { - int end = toIndex - 1; - dst[fromIndex] = src[end--]; - for (int i = fromIndex + 1; i < toIndex; i++, end--) { - String current = src[end]; + int actualEnd = end - 1; + dst[start] = src[actualEnd--]; + for (int i = start + 1; i < end; i++, actualEnd--) { + String current = src[actualEnd]; String prev; int j = i; - while (j > fromIndex + while (j > start && current.compareTo(prev = dst[j - 1]) < 0) { dst[j--] = prev; } @@ -2771,9 +2766,9 @@ } // Approximate median int s; - int mid = fromIndex + length / 2; - int lo = fromIndex; - int hi = toIndex - 1; + int mid = start + length / 2; + int lo = start; + int hi = end - 1; if (length > 40) { s = length / 8; lo = medChar(lo, lo + s, lo + s * 2, src, chId); @@ -2787,11 +2782,11 @@ // i - current element; int midVal = charAt(src[mid], chId); int a, b, c; - a = b = fromIndex; - c = toIndex - 1; + a = b = start; + c = end - 1; int cmp; - for (int i = fromIndex; i < toIndex; i++) { + for (int i = start; i < end; i++) { String el = src[i]; cmp = charAt(el, chId) - midVal; if (cmp < 0) { @@ -2806,15 +2801,15 @@ } } - s = b - fromIndex; + s = b - start; if (s > 0) { if (arr == src) { - System.arraycopy(dst, fromIndex, arr, a, s); + System.arraycopy(dst, start, arr, a, s); } else { - copySwap(dst, fromIndex, arr, a, s); + copySwap(dst, start, arr, a, s); } - if (b >= toIndex && midVal == -1) { + if (b >= end && midVal == -1) { return; } stableStringSort(arr, arr, arr == dst ? src : dst, a, a + s, @@ -2821,15 +2816,15 @@ chId + 1); } - s = a - fromIndex; + s = a - start; if (s > 0) { - stableStringSort(arr, src, dst, fromIndex, a, chId); + stableStringSort(arr, src, dst, start, a, chId); } c++; - s = toIndex - c; + s = end - c; if (s > 0) { - stableStringSort(arr, dst, src, c, toIndex, chId); + stableStringSort(arr, dst, src, c, end, chId); } } @@ -2834,14 +2829,16 @@ } /** - * Sorts the specified range in the array using the specified Comparator. + * Performs a sort on the section of the array between the given indices. + * Elements will be re-ordered into ascending order according to the given + * Comparator. * * @param array - * the Object array to be sorted + * the Object array to sort * @param start - * the start index to sort + * the start index * @param end - * the last + 1 index to sort + * the end index + 1 * @param comparator * the Comparator * @@ -2876,10 +2873,11 @@ } /** - * Sorts the specified array using the specified Comparator. + * Performs a sort on the given array. Elements will be re-ordered into + * ascending order according to the given Comparator. * * @param array - * the Object array to be sorted + * the Object array to sort * @param comparator * the Comparator * @@ -2892,10 +2890,11 @@ } /** - * Sorts the specified array in ascending order. + * Performs a sort on the given array. Elements will be re-ordered into + * ascending order. * * @param array - * the short array to be sorted + * the short array to sort */ public static void sort(short[] array) { sort(0, array.length, array); @@ -2902,14 +2901,15 @@ } /** - * Sorts the specified range in the array in ascending order. + * Performs a sort on the given array. Elements will be re-ordered into + * ascending order. * * @param array - * the short array to be sorted + * the short array to sort * @param start - * the start index to sort + * the start index * @param end - * the last + 1 index to sort + * the end index + 1 * * @exception IllegalArgumentException * when start > end