jdk/src/share/classes/java/util/Arrays.java
author alanb
Thu, 29 Oct 2009 11:18:37 +0000
changeset 4170 a94a6faf44e6
parent 4165 7cd799c224da
child 4233 9b594a48d0f4
permissions -rw-r--r--
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation Reviewed-by: jjb Contributed-by: vladimir.yaroslavskiy@sun.com, joshua.bloch@google.com, jbentley@avaya.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
     2
 * Copyright 1997-2009 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.lang.reflect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * This class contains various methods for manipulating arrays (such as
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    32
 * sorting and searching). This class also contains a static factory
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * that allows arrays to be viewed as lists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    35
 * <p>The methods in this class all throw a {@code NullPointerException},
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    36
 * if the specified array reference is null, except where noted.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <p>The documentation for the methods contained in this class includes
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    39
 * briefs description of the <i>implementations</i>. Such descriptions should
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * be regarded as <i>implementation notes</i>, rather than parts of the
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    41
 * <i>specification</i>. Implementors should feel free to substitute other
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    42
 * algorithms, so long as the specification itself is adhered to. (For
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    43
 * example, the algorithm used by {@code sort(Object[])} does not have to be
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    44
 * a MergeSort, but it does have to be <i>stable</i>.)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <p>This class is a member of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    50
 * @author Josh Bloch
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    51
 * @author Neal Gafter
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    52
 * @author John Rose
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    53
 * @since  1.2
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 */
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    55
public class Arrays {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    // Suppresses default constructor, ensuring non-instantiability.
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    58
    private Arrays() {}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    // Sorting
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /**
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    63
     * Sorts the specified array into ascending numerical order.
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    64
     *
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    65
     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort,
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    66
     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    67
     * offers O(n log(n)) performance on many data sets that cause other
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    68
     * quicksorts to degrade to quadratic performance, and is typically
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    69
     * faster than traditional (one-pivot) Quicksort implementations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * @param a the array to be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public static void sort(long[] a) {
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    74
        sort(a, 0, a.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    78
     * Sorts the specified range of the specified array into ascending order. The
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    79
     * range of to be sorted extends from the index {@code fromIndex}, inclusive,
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    80
     * to the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    81
     * the range to be sorted is empty.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     *
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    83
     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort,
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    84
     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    85
     * offers O(n log(n)) performance on many data sets that cause other
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    86
     * quicksorts to degrade to quadratic performance, and is typically
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    87
     * faster than traditional (one-pivot) Quicksort implementations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @param a the array to be sorted
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    90
     * @param fromIndex the index of the first element, inclusively, to be sorted
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    91
     * @param toIndex the index of the last element, exclusively, to be sorted
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    92
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    93
     * @throws ArrayIndexOutOfBoundsException
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    94
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public static void sort(long[] a, int fromIndex, int toIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        rangeCheck(a.length, fromIndex, toIndex);
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    98
        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   102
     * Sorts the specified array into ascending numerical order.
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   103
     *
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   104
     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort,
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   105
     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   106
     * offers O(n log(n)) performance on many data sets that cause other
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   107
     * quicksorts to degrade to quadratic performance, and is typically
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   108
     * faster than traditional (one-pivot) Quicksort implementations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @param a the array to be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    public static void sort(int[] a) {
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   113
        sort(a, 0, a.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /**
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   117
     * Sorts the specified range of the specified array into ascending order. The
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   118
     * range of to be sorted extends from the index {@code fromIndex}, inclusive,
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   119
     * to the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   120
     * the range to be sorted is empty.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   122
     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort,
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   123
     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   124
     * offers O(n log(n)) performance on many data sets that cause other
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   125
     * quicksorts to degrade to quadratic performance, and is typically
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   126
     * faster than traditional (one-pivot) Quicksort implementations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @param a the array to be sorted
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   129
     * @param fromIndex the index of the first element, inclusively, to be sorted
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   130
     * @param toIndex the index of the last element, exclusively, to be sorted
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   131
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   132
     * @throws ArrayIndexOutOfBoundsException
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   133
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    public static void sort(int[] a, int fromIndex, int toIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        rangeCheck(a.length, fromIndex, toIndex);
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   137
        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    /**
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   141
     * Sorts the specified array into ascending numerical order.
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   142
     *
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   143
     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort,
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   144
     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   145
     * offers O(n log(n)) performance on many data sets that cause other
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   146
     * quicksorts to degrade to quadratic performance, and is typically
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   147
     * faster than traditional (one-pivot) Quicksort implementations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @param a the array to be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public static void sort(short[] a) {
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   152
        sort(a, 0, a.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /**
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   156
     * Sorts the specified range of the specified array into ascending order. The
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   157
     * range of to be sorted extends from the index {@code fromIndex}, inclusive,
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   158
     * to the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   159
     * the range to be sorted is empty.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   161
     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort,
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   162
     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   163
     * offers O(n log(n)) performance on many data sets that cause other
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   164
     * quicksorts to degrade to quadratic performance, and is typically
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   165
     * faster than traditional (one-pivot) Quicksort implementations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @param a the array to be sorted
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   168
     * @param fromIndex the index of the first element, inclusively, to be sorted
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   169
     * @param toIndex the index of the last element, exclusively, to be sorted
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   170
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   171
     * @throws ArrayIndexOutOfBoundsException
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   172
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    public static void sort(short[] a, int fromIndex, int toIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        rangeCheck(a.length, fromIndex, toIndex);
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   176
        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    /**
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   180
     * Sorts the specified array into ascending numerical order.
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   181
     *
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   182
     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort,
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   183
     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   184
     * offers O(n log(n)) performance on many data sets that cause other
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   185
     * quicksorts to degrade to quadratic performance, and is typically
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   186
     * faster than traditional (one-pivot) Quicksort implementations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @param a the array to be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    public static void sort(char[] a) {
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   191
        sort(a, 0, a.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    /**
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   195
     * Sorts the specified range of the specified array into ascending order. The
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   196
     * range of to be sorted extends from the index {@code fromIndex}, inclusive,
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   197
     * to the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   198
     * the range to be sorted is empty.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   200
     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort,
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   201
     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   202
     * offers O(n log(n)) performance on many data sets that cause other
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   203
     * quicksorts to degrade to quadratic performance, and is typically
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   204
     * faster than traditional (one-pivot) Quicksort implementations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * @param a the array to be sorted
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   207
     * @param fromIndex the index of the first element, inclusively, to be sorted
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   208
     * @param toIndex the index of the last element, exclusively, to be sorted
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   209
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   210
     * @throws ArrayIndexOutOfBoundsException
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   211
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    public static void sort(char[] a, int fromIndex, int toIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        rangeCheck(a.length, fromIndex, toIndex);
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   215
        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    /**
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   219
     * Sorts the specified array into ascending numerical order.
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   220
     *
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   221
     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort,
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   222
     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   223
     * offers O(n log(n)) performance on many data sets that cause other
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   224
     * quicksorts to degrade to quadratic performance, and is typically
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   225
     * faster than traditional (one-pivot) Quicksort implementations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * @param a the array to be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    public static void sort(byte[] a) {
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   230
        sort(a, 0, a.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    /**
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   234
     * Sorts the specified range of the specified array into ascending order. The
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   235
     * range of to be sorted extends from the index {@code fromIndex}, inclusive,
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   236
     * to the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   237
     * the range to be sorted is empty.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     *
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   239
     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort,
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   240
     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   241
     * offers O(n log(n)) performance on many data sets that cause other
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   242
     * quicksorts to degrade to quadratic performance, and is typically
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   243
     * faster than traditional (one-pivot) Quicksort implementations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * @param a the array to be sorted
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   246
     * @param fromIndex the index of the first element, inclusively, to be sorted
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   247
     * @param toIndex the index of the last element, exclusively, to be sorted
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   248
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   249
     * @throws ArrayIndexOutOfBoundsException
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   250
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    public static void sort(byte[] a, int fromIndex, int toIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        rangeCheck(a.length, fromIndex, toIndex);
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   254
        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    /**
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   258
     * Sorts the specified array into ascending numerical order.
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   259
     *
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   260
     * <p>The {@code <} relation does not provide a total order on
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * all floating-point values; although they are distinct numbers
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   262
     * {@code -0.0d == 0.0d} is {@code true} and a NaN value compares
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   263
     * neither less than, greater than, nor equal to any floating-point
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   264
     * value, even itself. To allow the sort to proceed, instead of using
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   265
     * the {@code <} relation to determine ascending numerical order,
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   266
     * this method uses the total order imposed by {@link Double#compareTo}.
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   267
     * This ordering differs from the {@code <} relation in that {@code -0.0d}
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   268
     * is treated as less than {@code 0.0d} and NaN is considered greater than
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   269
     * any other floating-point value. For the purposes of sorting, all NaN
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   270
     * values are considered equivalent and equal.
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   271
     *
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   272
     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort,
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   273
     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   274
     * offers O(n log(n)) performance on many data sets that cause other
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   275
     * quicksorts to degrade to quadratic performance, and is typically
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   276
     * faster than traditional (one-pivot) Quicksort implementations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @param a the array to be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    public static void sort(double[] a) {
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   281
        sort(a, 0, a.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    /**
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   285
     * Sorts the specified range of the specified array into ascending order. The
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   286
     * range of to be sorted extends from the index {@code fromIndex}, inclusive,
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   287
     * to the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   288
     * the range to be sorted is empty.
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   289
     *
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   290
     * <p>The {@code <} relation does not provide a total order on
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * all floating-point values; although they are distinct numbers
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   292
     * {@code -0.0d == 0.0d} is {@code true} and a NaN value compares
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   293
     * neither less than, greater than, nor equal to any floating-point
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   294
     * value, even itself. To allow the sort to proceed, instead of using
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   295
     * the {@code <} relation to determine ascending numerical order,
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   296
     * this method uses the total order imposed by {@link Double#compareTo}.
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   297
     * This ordering differs from the {@code <} relation in that {@code -0.0d}
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   298
     * is treated as less than {@code 0.0d} and NaN is considered greater than
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   299
     * any other floating-point value. For the purposes of sorting, all NaN
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   300
     * values are considered equivalent and equal.
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   301
     *
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   302
     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort,
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   303
     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   304
     * offers O(n log(n)) performance on many data sets that cause other
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   305
     * quicksorts to degrade to quadratic performance, and is typically
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   306
     * faster than traditional (one-pivot) Quicksort implementations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * @param a the array to be sorted
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   309
     * @param fromIndex the index of the first element, inclusively, to be sorted
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   310
     * @param toIndex the index of the last element, exclusively, to be sorted
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   311
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   312
     * @throws ArrayIndexOutOfBoundsException
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   313
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     */
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   315
    public static void sort(double[] a, int fromIndex, int toIndex) {
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   316
        rangeCheck(a.length, fromIndex, toIndex);
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   317
        sortNegZeroAndNaN(a, fromIndex, toIndex);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   320
    private static void sortNegZeroAndNaN(double[] a, int fromIndex, int toIndex) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        final long NEG_ZERO_BITS = Double.doubleToLongBits(-0.0d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
         * The sort is done in three phases to avoid the expense of using
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   324
         * NaN and -0.0d aware comparisons during the main sort.
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   325
         *
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   326
         * Preprocessing phase: move any NaN's to end of array, count the
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   327
         * number of -0.0d's, and turn them into 0.0d's.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        int numNegZeros = 0;
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   330
        int i = fromIndex;
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   331
        int n = toIndex;
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   332
        double temp;
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   333
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   334
        while (i < n) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            if (a[i] != a[i]) {
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   336
                n--;
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   337
                temp = a[i];
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   338
                a[i] = a[n];
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   339
                a[n] = temp;
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   340
            }
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   341
            else {
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   342
                if (a[i] == 0 && Double.doubleToLongBits(a[i]) == NEG_ZERO_BITS) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                    a[i] = 0.0d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                    numNegZeros++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        // Main sort phase: quicksort everything but the NaN's
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   350
        DualPivotQuicksort.sort(a, fromIndex, n - 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   352
        // Postprocessing phase: change 0.0d's to -0.0d's as required
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        if (numNegZeros != 0) {
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   354
            int j = binarySearch0(a, fromIndex, n, 0.0d); // position of ANY zero
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   355
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                j--;
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   358
            }
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   359
            while (j >= fromIndex && a[j] == 0.0d);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            // j is now one less than the index of the FIRST zero
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   362
            for (int k = 0; k < numNegZeros; k++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                a[++j] = -0.0d;
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   364
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   368
    /**
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   369
     * Sorts the specified array into ascending numerical order.
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   370
     *
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   371
     * <p>The {@code <} relation does not provide a total order on
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   372
     * all floating-point values; although they are distinct numbers
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   373
     * {@code -0.0f == 0.0f} is {@code true} and a NaN value compares
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   374
     * neither less than, greater than, nor equal to any floating-point
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   375
     * value, even itself. To allow the sort to proceed, instead of using
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   376
     * the {@code <} relation to determine ascending numerical order,
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   377
     * this method uses the total order imposed by {@link Float#compareTo}.
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   378
     * This ordering differs from the {@code <} relation in that {@code -0.0f}
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   379
     * is treated as less than {@code 0.0f} and NaN is considered greater than
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   380
     * any other floating-point value. For the purposes of sorting, all NaN
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   381
     * values are considered equivalent and equal.
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   382
     *
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   383
     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort,
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   384
     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   385
     * offers O(n log(n)) performance on many data sets that cause other
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   386
     * quicksorts to degrade to quadratic performance, and is typically
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   387
     * faster than traditional (one-pivot) Quicksort implementations.
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   388
     *
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   389
     * @param a the array to be sorted
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   390
     */
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   391
    public static void sort(float[] a) {
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   392
        sort(a, 0, a.length);
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   393
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   395
    /**
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   396
     * Sorts the specified range of the specified array into ascending order. The
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   397
     * range of to be sorted extends from the index {@code fromIndex}, inclusive,
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   398
     * to the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   399
     * the range to be sorted is empty.
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   400
     *
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   401
     * <p>The {@code <} relation does not provide a total order on
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   402
     * all floating-point values; although they are distinct numbers
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   403
     * {@code -0.0f == 0.0f} is {@code true} and a NaN value compares
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   404
     * neither less than, greater than, nor equal to any floating-point
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   405
     * value, even itself. To allow the sort to proceed, instead of using
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   406
     * the {@code <} relation to determine ascending numerical order,
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   407
     * this method uses the total order imposed by {@link Float#compareTo}.
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   408
     * This ordering differs from the {@code <} relation in that {@code -0.0f}
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   409
     * is treated as less than {@code 0.0f} and NaN is considered greater than
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   410
     * any other floating-point value. For the purposes of sorting, all NaN
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   411
     * values are considered equivalent and equal.
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   412
     *
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   413
     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort,
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   414
     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   415
     * offers O(n log(n)) performance on many data sets that cause other
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   416
     * quicksorts to degrade to quadratic performance, and is typically
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   417
     * faster than traditional (one-pivot) Quicksort implementations.
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   418
     *
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   419
     * @param a the array to be sorted
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   420
     * @param fromIndex the index of the first element, inclusively, to be sorted
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   421
     * @param toIndex the index of the last element, exclusively, to be sorted
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   422
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   423
     * @throws ArrayIndexOutOfBoundsException
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   424
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   425
     */
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   426
    public static void sort(float[] a, int fromIndex, int toIndex) {
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   427
        rangeCheck(a.length, fromIndex, toIndex);
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   428
        sortNegZeroAndNaN(a, fromIndex, toIndex);
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   429
    }
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   430
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   431
    private static void sortNegZeroAndNaN(float[] a, int fromIndex, int toIndex) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        final int NEG_ZERO_BITS = Float.floatToIntBits(-0.0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
         * The sort is done in three phases to avoid the expense of using
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   435
         * NaN and -0.0f aware comparisons during the main sort.
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   436
         *
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   437
         * Preprocessing phase: move any NaN's to end of array, count the
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   438
         * number of -0.0f's, and turn them into 0.0f's.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        int numNegZeros = 0;
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   441
        int i = fromIndex;
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   442
        int n = toIndex;
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   443
        float temp;
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   444
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   445
        while (i < n) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            if (a[i] != a[i]) {
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   447
                n--;
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   448
                temp = a[i];
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   449
                a[i] = a[n];
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   450
                a[n] = temp;
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   451
            }
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   452
            else {
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   453
                if (a[i] == 0 && Float.floatToIntBits(a[i]) == NEG_ZERO_BITS) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                    a[i] = 0.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                    numNegZeros++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        // Main sort phase: quicksort everything but the NaN's
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   461
        DualPivotQuicksort.sort(a, fromIndex, n - 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   463
        // Postprocessing phase: change 0.0f's to -0.0f's as required
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        if (numNegZeros != 0) {
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   465
            int j = binarySearch0(a, fromIndex, n, 0.0f); // position of ANY zero
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   466
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                j--;
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   469
            }
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   470
            while (j >= fromIndex && a[j] == 0.0f);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            // j is now one less than the index of the FIRST zero
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   473
            for (int k = 0; k < numNegZeros; k++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                a[++j] = -0.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   479
    /**
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   480
     * Old merge sort implementation can be selected (for
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   481
     * compatibility with broken comparators) using a system property.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   482
     * Cannot be a static boolean in the enclosing class due to
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   483
     * circular dependencies. To be removed in a future release.
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   484
     */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   485
    static final class LegacyMergeSort {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   486
        private static final boolean userRequested =
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   487
            java.security.AccessController.doPrivileged(
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   488
                new sun.security.action.GetBooleanAction(
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   489
                    "java.util.Arrays.useLegacyMergeSort")).booleanValue();
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   490
    }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   491
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   492
    /*
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   493
     * If this platform has an optimizing VM, check whether ComparableTimSort
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   494
     * offers any performance benefit over TimSort in conjunction with a
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   495
     * comparator that returns:
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   496
     *    {@code ((Comparable)first).compareTo(Second)}.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   497
     * If not, you are better off deleting ComparableTimSort to
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   498
     * eliminate the code duplication.  In other words, the commented
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   499
     * out code below is the preferable implementation for sorting
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   500
     * arrays of Comparables if it offers sufficient performance.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   501
     */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   502
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   503
//    /**
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   504
//     * A comparator that implements the natural ordering of a group of
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   505
//     * mutually comparable elements.  Using this comparator saves us
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   506
//     * from duplicating most of the code in this file (one version for
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   507
//     * Comparables, one for explicit Comparators).
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   508
//     */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   509
//    private static final Comparator<Object> NATURAL_ORDER =
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   510
//            new Comparator<Object>() {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   511
//        @SuppressWarnings("unchecked")
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   512
//        public int compare(Object first, Object second) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   513
//            return ((Comparable<Object>)first).compareTo(second);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   514
//        }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   515
//    };
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   516
//
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   517
//    public static void sort(Object[] a) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   518
//        sort(a, 0, a.length, NATURAL_ORDER);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   519
//    }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   520
//
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   521
//    public static void sort(Object[] a, int fromIndex, int toIndex) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   522
//        sort(a, fromIndex, toIndex, NATURAL_ORDER);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   523
//    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    /**
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   526
     * Sorts the specified array of objects into ascending order, according
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   527
     * to the {@linkplain Comparable natural ordering} of its elements.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   528
     * All elements in the array must implement the {@link Comparable}
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   529
     * interface.  Furthermore, all elements in the array must be
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   530
     * <i>mutually comparable</i> (that is, {@code e1.compareTo(e2)} must
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   531
     * not throw a {@code ClassCastException} for any elements {@code e1}
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   532
     * and {@code e2} in the array).
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   533
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   534
     * <p>This sort is guaranteed to be <i>stable</i>:  equal elements will
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   535
     * not be reordered as a result of the sort.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     *
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   537
     * <p>Implementation note: This implementation is a stable, adaptive,
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   538
     * iterative mergesort that requires far fewer than n lg(n) comparisons
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   539
     * when the input array is partially sorted, while offering the
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   540
     * performance of a traditional mergesort when the input array is
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   541
     * randomly ordered.  If the input array is nearly sorted, the
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   542
     * implementation requires approximately n comparisons.  Temporary
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   543
     * storage requirements vary from a small constant for nearly sorted
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   544
     * input arrays to n/2 object references for randomly ordered input
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   545
     * arrays.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     *
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   547
     * <p>The implementation takes equal advantage of ascending and
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   548
     * descending order in its input array, and can take advantage of
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   549
     * ascending and descending order in different parts of the the same
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   550
     * input array.  It is well-suited to merging two or more sorted arrays:
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   551
     * simply concatenate the arrays and sort the resulting array.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   552
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   553
     * <p>The implementation was adapted from Tim Peters's list sort for Python
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   554
     * (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt">
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   555
     * TimSort</a>).  It uses techiques from Peter McIlroy's "Optimistic
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   556
     * Sorting and Information Theoretic Complexity", in Proceedings of the
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   557
     * Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474,
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   558
     * January 1993.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * @param a the array to be sorted
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   561
     * @throws ClassCastException if the array contains elements that are not
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   562
     *         <i>mutually comparable</i> (for example, strings and integers)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   563
     * @throws IllegalArgumentException (optional) if the natural
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   564
     *         ordering of the array elements is found to violate the
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   565
     *         {@link Comparable} contract
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    public static void sort(Object[] a) {
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   568
        if (LegacyMergeSort.userRequested)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   569
            legacyMergeSort(a);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   570
        else
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   571
            ComparableTimSort.sort(a);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   572
    }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   573
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   574
    /** To be removed in a future release. */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   575
    private static void legacyMergeSort(Object[] a) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   576
        Object[] aux = a.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        mergeSort(aux, a, 0, a.length, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * Sorts the specified range of the specified array of objects into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * ascending order, according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * {@linkplain Comparable natural ordering} of its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * elements.  The range to be sorted extends from index
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   585
     * {@code fromIndex}, inclusive, to index {@code toIndex}, exclusive.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   586
     * (If {@code fromIndex==toIndex}, the range to be sorted is empty.)  All
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * elements in this range must implement the {@link Comparable}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * interface.  Furthermore, all elements in this range must be <i>mutually
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   589
     * comparable</i> (that is, {@code e1.compareTo(e2)} must not throw a
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   590
     * {@code ClassCastException} for any elements {@code e1} and
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   591
     * {@code e2} in the array).
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   592
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   593
     * <p>This sort is guaranteed to be <i>stable</i>:  equal elements will
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   594
     * not be reordered as a result of the sort.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     *
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   596
     * <p>Implementation note: This implementation is a stable, adaptive,
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   597
     * iterative mergesort that requires far fewer than n lg(n) comparisons
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   598
     * when the input array is partially sorted, while offering the
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   599
     * performance of a traditional mergesort when the input array is
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   600
     * randomly ordered.  If the input array is nearly sorted, the
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   601
     * implementation requires approximately n comparisons.  Temporary
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   602
     * storage requirements vary from a small constant for nearly sorted
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   603
     * input arrays to n/2 object references for randomly ordered input
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   604
     * arrays.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     *
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   606
     * <p>The implementation takes equal advantage of ascending and
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   607
     * descending order in its input array, and can take advantage of
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   608
     * ascending and descending order in different parts of the the same
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   609
     * input array.  It is well-suited to merging two or more sorted arrays:
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   610
     * simply concatenate the arrays and sort the resulting array.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   611
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   612
     * <p>The implementation was adapted from Tim Peters's list sort for Python
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   613
     * (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt">
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   614
     * TimSort</a>).  It uses techiques from Peter McIlroy's "Optimistic
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   615
     * Sorting and Information Theoretic Complexity", in Proceedings of the
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   616
     * Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474,
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   617
     * January 1993.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * @param a the array to be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     *        sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * @param toIndex the index of the last element (exclusive) to be sorted
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   623
     * @throws IllegalArgumentException if {@code fromIndex > toIndex} or
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   624
     *         (optional) if the natural ordering of the array elements is
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   625
     *         found to violate the {@link Comparable} contract
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   626
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   627
     *         {@code toIndex > a.length}
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   628
     * @throws ClassCastException if the array contains elements that are
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   629
     *         not <i>mutually comparable</i> (for example, strings and
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   630
     *         integers).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    public static void sort(Object[] a, int fromIndex, int toIndex) {
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   633
        if (LegacyMergeSort.userRequested)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   634
            legacyMergeSort(a, fromIndex, toIndex);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   635
        else
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   636
            ComparableTimSort.sort(a, fromIndex, toIndex);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   637
    }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   638
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   639
    /** To be removed in a future release. */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   640
    private static void legacyMergeSort(Object[] a,
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   641
                                        int fromIndex, int toIndex) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        Object[] aux = copyOfRange(a, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        mergeSort(aux, a, fromIndex, toIndex, -fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * Tuning parameter: list size at or below which insertion sort will be
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   649
     * used in preference to mergesort.
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   650
     * To be removed in a future release.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    private static final int INSERTIONSORT_THRESHOLD = 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * Src is the source array that starts at index 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * Dest is the (possibly larger) array destination with a possible offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * low is the index in dest to start sorting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * high is the end index in dest to end sorting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * off is the offset to generate corresponding low, high in src
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   660
     * To be removed in a future release.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    private static void mergeSort(Object[] src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                                  Object[] dest,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                                  int low,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                                  int high,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                                  int off) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        int length = high - low;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        // Insertion sort on smallest arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        if (length < INSERTIONSORT_THRESHOLD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            for (int i=low; i<high; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                for (int j=i; j>low &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                         ((Comparable) dest[j-1]).compareTo(dest[j])>0; j--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                    swap(dest, j, j-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        // Recursively sort halves of dest into src
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        int destLow  = low;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        int destHigh = high;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        low  += off;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        high += off;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        mergeSort(dest, src, low, mid, -off);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        mergeSort(dest, src, mid, high, -off);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        // If list is already sorted, just copy from src to dest.  This is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        // optimization that results in faster sorts for nearly ordered lists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        if (((Comparable)src[mid-1]).compareTo(src[mid]) <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            System.arraycopy(src, low, dest, destLow, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        // Merge sorted halves (now in src) into dest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        for(int i = destLow, p = low, q = mid; i < destHigh; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            if (q >= high || p < mid && ((Comparable)src[p]).compareTo(src[q])<=0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                dest[i] = src[p++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                dest[i] = src[q++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     * Swaps x[a] with x[b].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    private static void swap(Object[] x, int a, int b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        Object t = x[a];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        x[a] = x[b];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        x[b] = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * Sorts the specified array of objects according to the order induced by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * the specified comparator.  All elements in the array must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * <i>mutually comparable</i> by the specified comparator (that is,
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   716
     * {@code c.compare(e1, e2)} must not throw a {@code ClassCastException}
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   717
     * for any elements {@code e1} and {@code e2} in the array).
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   718
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   719
     * <p>This sort is guaranteed to be <i>stable</i>:  equal elements will
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   720
     * not be reordered as a result of the sort.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     *
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   722
     * <p>Implementation note: This implementation is a stable, adaptive,
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   723
     * iterative mergesort that requires far fewer than n lg(n) comparisons
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   724
     * when the input array is partially sorted, while offering the
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   725
     * performance of a traditional mergesort when the input array is
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   726
     * randomly ordered.  If the input array is nearly sorted, the
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   727
     * implementation requires approximately n comparisons.  Temporary
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   728
     * storage requirements vary from a small constant for nearly sorted
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   729
     * input arrays to n/2 object references for randomly ordered input
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   730
     * arrays.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     *
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   732
     * <p>The implementation takes equal advantage of ascending and
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   733
     * descending order in its input array, and can take advantage of
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   734
     * ascending and descending order in different parts of the the same
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   735
     * input array.  It is well-suited to merging two or more sorted arrays:
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   736
     * simply concatenate the arrays and sort the resulting array.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   737
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   738
     * <p>The implementation was adapted from Tim Peters's list sort for Python
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   739
     * (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt">
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   740
     * TimSort</a>).  It uses techiques from Peter McIlroy's "Optimistic
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   741
     * Sorting and Information Theoretic Complexity", in Proceedings of the
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   742
     * Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474,
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   743
     * January 1993.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     * @param a the array to be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     * @param c the comparator to determine the order of the array.  A
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   747
     *        {@code null} value indicates that the elements'
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     *        {@linkplain Comparable natural ordering} should be used.
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   749
     * @throws ClassCastException if the array contains elements that are
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   750
     *         not <i>mutually comparable</i> using the specified comparator
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   751
     * @throws IllegalArgumentException (optional) if the comparator is
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   752
     *         found to violate the {@link Comparator} contract
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
    public static <T> void sort(T[] a, Comparator<? super T> c) {
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   755
        if (LegacyMergeSort.userRequested)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   756
            legacyMergeSort(a, c);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   757
        else
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   758
            TimSort.sort(a, c);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   759
    }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   760
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   761
    /** To be removed in a future release. */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   762
    private static <T> void legacyMergeSort(T[] a, Comparator<? super T> c) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   763
        T[] aux = a.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        if (c==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            mergeSort(aux, a, 0, a.length, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
            mergeSort(aux, a, 0, a.length, 0, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     * Sorts the specified range of the specified array of objects according
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     * to the order induced by the specified comparator.  The range to be
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   773
     * sorted extends from index {@code fromIndex}, inclusive, to index
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   774
     * {@code toIndex}, exclusive.  (If {@code fromIndex==toIndex}, the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     * range to be sorted is empty.)  All elements in the range must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     * <i>mutually comparable</i> by the specified comparator (that is,
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   777
     * {@code c.compare(e1, e2)} must not throw a {@code ClassCastException}
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   778
     * for any elements {@code e1} and {@code e2} in the range).
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   779
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   780
     * <p>This sort is guaranteed to be <i>stable</i>:  equal elements will
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   781
     * not be reordered as a result of the sort.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     *
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   783
     * <p>Implementation note: This implementation is a stable, adaptive,
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   784
     * iterative mergesort that requires far fewer than n lg(n) comparisons
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   785
     * when the input array is partially sorted, while offering the
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   786
     * performance of a traditional mergesort when the input array is
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   787
     * randomly ordered.  If the input array is nearly sorted, the
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   788
     * implementation requires approximately n comparisons.  Temporary
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   789
     * storage requirements vary from a small constant for nearly sorted
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   790
     * input arrays to n/2 object references for randomly ordered input
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   791
     * arrays.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     *
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   793
     * <p>The implementation takes equal advantage of ascending and
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   794
     * descending order in its input array, and can take advantage of
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   795
     * ascending and descending order in different parts of the the same
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   796
     * input array.  It is well-suited to merging two or more sorted arrays:
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   797
     * simply concatenate the arrays and sort the resulting array.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   798
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   799
     * <p>The implementation was adapted from Tim Peters's list sort for Python
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   800
     * (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt">
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   801
     * TimSort</a>).  It uses techiques from Peter McIlroy's "Optimistic
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   802
     * Sorting and Information Theoretic Complexity", in Proceedings of the
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   803
     * Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474,
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   804
     * January 1993.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * @param a the array to be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     *        sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * @param toIndex the index of the last element (exclusive) to be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     * @param c the comparator to determine the order of the array.  A
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   811
     *        {@code null} value indicates that the elements'
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     *        {@linkplain Comparable natural ordering} should be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     * @throws ClassCastException if the array contains elements that are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     *         <i>mutually comparable</i> using the specified comparator.
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   815
     * @throws IllegalArgumentException if {@code fromIndex > toIndex} or
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   816
     *         (optional) if the comparator is found to violate the
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   817
     *         {@link Comparator} contract
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   818
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   819
     *         {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
    public static <T> void sort(T[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                                Comparator<? super T> c) {
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   823
        if (LegacyMergeSort.userRequested)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   824
            legacyMergeSort(a, fromIndex, toIndex, c);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   825
        else
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   826
            TimSort.sort(a, fromIndex, toIndex, c);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   827
    }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   828
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   829
    /** To be removed in a future release. */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   830
    private static <T> void legacyMergeSort(T[] a, int fromIndex, int toIndex,
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   831
                                            Comparator<? super T> c) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        rangeCheck(a.length, fromIndex, toIndex);
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   833
        T[] aux = copyOfRange(a, fromIndex, toIndex);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        if (c==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
            mergeSort(aux, a, fromIndex, toIndex, -fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
            mergeSort(aux, a, fromIndex, toIndex, -fromIndex, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     * Src is the source array that starts at index 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     * Dest is the (possibly larger) array destination with a possible offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     * low is the index in dest to start sorting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     * high is the end index in dest to end sorting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     * off is the offset into src corresponding to low in dest
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
   846
     * To be removed in a future release.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
    private static void mergeSort(Object[] src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
                                  Object[] dest,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                                  int low, int high, int off,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                                  Comparator c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        int length = high - low;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        // Insertion sort on smallest arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        if (length < INSERTIONSORT_THRESHOLD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
            for (int i=low; i<high; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
                for (int j=i; j>low && c.compare(dest[j-1], dest[j])>0; j--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
                    swap(dest, j, j-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        // Recursively sort halves of dest into src
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        int destLow  = low;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        int destHigh = high;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        low  += off;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        high += off;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        mergeSort(dest, src, low, mid, -off, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        mergeSort(dest, src, mid, high, -off, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        // If list is already sorted, just copy from src to dest.  This is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
        // optimization that results in faster sorts for nearly ordered lists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
        if (c.compare(src[mid-1], src[mid]) <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
           System.arraycopy(src, low, dest, destLow, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
           return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
        // Merge sorted halves (now in src) into dest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        for(int i = destLow, p = low, q = mid; i < destHigh; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
            if (q >= high || p < mid && c.compare(src[p], src[q]) <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
                dest[i] = src[p++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
                dest[i] = src[q++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
    /**
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   888
     * Checks that {@code fromIndex} and {@code toIndex} are in
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   889
     * the range and throws an appropriate exception, if they aren't.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     */
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   891
    private static void rangeCheck(int length, int fromIndex, int toIndex) {
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   892
        if (fromIndex > toIndex) {
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   893
            throw new IllegalArgumentException(
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   894
                "fromIndex(" + fromIndex + ") > toIndex(" + toIndex + ")");
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   895
        }
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   896
        if (fromIndex < 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
            throw new ArrayIndexOutOfBoundsException(fromIndex);
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   898
        }
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   899
        if (toIndex > length) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
            throw new ArrayIndexOutOfBoundsException(toIndex);
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   901
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
    // Searching
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     * Searches the specified array of longs for the specified value using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     * binary search algorithm.  The array must be sorted (as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     * by the {@link #sort(long[])} method) prior to making this call.  If it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     * is not sorted, the results are undefined.  If the array contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     * @return index of the search key, if it is contained in the array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     *         element greater than the key, or <tt>a.length</tt> if all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     *         elements in the array are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
    public static int binarySearch(long[] a, long key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        return binarySearch0(a, 0, a.length, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     * Searches a range of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     * the specified array of longs for the specified value using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     * binary search algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     * The range must be sorted (as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
     * by the {@link #sort(long[], int, int)} method)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
     * prior to making this call.  If it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     * is not sorted, the results are undefined.  If the range contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     *          searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     * @param toIndex the index of the last element (exclusive) to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     * @return index of the search key, if it is contained in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     *         within the specified range;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     *         element in the range greater than the key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     *         or <tt>toIndex</tt> if all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     *         elements in the range are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     *         if {@code fromIndex > toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     * @throws ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     *         if {@code fromIndex < 0 or toIndex > a.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
    public static int binarySearch(long[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
                                   long key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
        return binarySearch0(a, fromIndex, toIndex, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
    // Like public version, but without range checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
    private static int binarySearch0(long[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
                                     long key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        int low = fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
        int high = toIndex - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
            int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
            long midVal = a[mid];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
            if (midVal < key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
                low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
            else if (midVal > key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
                high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
                return mid; // key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        return -(low + 1);  // key not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     * Searches the specified array of ints for the specified value using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     * binary search algorithm.  The array must be sorted (as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     * by the {@link #sort(int[])} method) prior to making this call.  If it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     * is not sorted, the results are undefined.  If the array contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
     * @return index of the search key, if it is contained in the array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
     *         element greater than the key, or <tt>a.length</tt> if all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     *         elements in the array are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
    public static int binarySearch(int[] a, int key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
        return binarySearch0(a, 0, a.length, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     * Searches a range of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
     * the specified array of ints for the specified value using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
     * binary search algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     * The range must be sorted (as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     * by the {@link #sort(int[], int, int)} method)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
     * prior to making this call.  If it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     * is not sorted, the results are undefined.  If the range contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
     *          searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     * @param toIndex the index of the last element (exclusive) to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     * @return index of the search key, if it is contained in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     *         within the specified range;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     *         element in the range greater than the key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     *         or <tt>toIndex</tt> if all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     *         elements in the range are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
     *         if {@code fromIndex > toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
     * @throws ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
     *         if {@code fromIndex < 0 or toIndex > a.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
    public static int binarySearch(int[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
                                   int key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
        return binarySearch0(a, fromIndex, toIndex, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
    // Like public version, but without range checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
    private static int binarySearch0(int[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
                                     int key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
        int low = fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
        int high = toIndex - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
            int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
            int midVal = a[mid];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
            if (midVal < key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
                low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
            else if (midVal > key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
                high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
                return mid; // key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        return -(low + 1);  // key not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
     * Searches the specified array of shorts for the specified value using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
     * the binary search algorithm.  The array must be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
     * (as by the {@link #sort(short[])} method) prior to making this call.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
     * it is not sorted, the results are undefined.  If the array contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
     * @return index of the search key, if it is contained in the array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
     *         element greater than the key, or <tt>a.length</tt> if all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
     *         elements in the array are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
    public static int binarySearch(short[] a, short key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
        return binarySearch0(a, 0, a.length, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
     * Searches a range of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
     * the specified array of shorts for the specified value using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
     * the binary search algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
     * The range must be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     * (as by the {@link #sort(short[], int, int)} method)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
     * prior to making this call.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
     * it is not sorted, the results are undefined.  If the range contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
     *          searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
     * @param toIndex the index of the last element (exclusive) to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
     * @return index of the search key, if it is contained in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
     *         within the specified range;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
     *         element in the range greater than the key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
     *         or <tt>toIndex</tt> if all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
     *         elements in the range are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
     *         if {@code fromIndex > toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
     * @throws ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
     *         if {@code fromIndex < 0 or toIndex > a.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
    public static int binarySearch(short[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
                                   short key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
        return binarySearch0(a, fromIndex, toIndex, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
    // Like public version, but without range checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
    private static int binarySearch0(short[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
                                     short key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
        int low = fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
        int high = toIndex - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
            int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
            short midVal = a[mid];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
            if (midVal < key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
                low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
            else if (midVal > key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
                high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
                return mid; // key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
        return -(low + 1);  // key not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
     * Searches the specified array of chars for the specified value using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
     * binary search algorithm.  The array must be sorted (as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
     * by the {@link #sort(char[])} method) prior to making this call.  If it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
     * is not sorted, the results are undefined.  If the array contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     * @return index of the search key, if it is contained in the array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
     *         element greater than the key, or <tt>a.length</tt> if all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
     *         elements in the array are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
    public static int binarySearch(char[] a, char key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
        return binarySearch0(a, 0, a.length, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
     * Searches a range of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
     * the specified array of chars for the specified value using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
     * binary search algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
     * The range must be sorted (as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
     * by the {@link #sort(char[], int, int)} method)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
     * prior to making this call.  If it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
     * is not sorted, the results are undefined.  If the range contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
     *          searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
     * @param toIndex the index of the last element (exclusive) to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
     * @return index of the search key, if it is contained in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
     *         within the specified range;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
     *         element in the range greater than the key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
     *         or <tt>toIndex</tt> if all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
     *         elements in the range are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
     *         if {@code fromIndex > toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
     * @throws ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
     *         if {@code fromIndex < 0 or toIndex > a.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
    public static int binarySearch(char[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
                                   char key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
        return binarySearch0(a, fromIndex, toIndex, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
    // Like public version, but without range checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
    private static int binarySearch0(char[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
                                     char key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
        int low = fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
        int high = toIndex - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
            int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
            char midVal = a[mid];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
            if (midVal < key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
                low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
            else if (midVal > key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
                high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
                return mid; // key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
        return -(low + 1);  // key not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
     * Searches the specified array of bytes for the specified value using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
     * binary search algorithm.  The array must be sorted (as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
     * by the {@link #sort(byte[])} method) prior to making this call.  If it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
     * is not sorted, the results are undefined.  If the array contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
     * @return index of the search key, if it is contained in the array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
     *         element greater than the key, or <tt>a.length</tt> if all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
     *         elements in the array are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
    public static int binarySearch(byte[] a, byte key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
        return binarySearch0(a, 0, a.length, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
     * Searches a range of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
     * the specified array of bytes for the specified value using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
     * binary search algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
     * The range must be sorted (as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
     * by the {@link #sort(byte[], int, int)} method)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
     * prior to making this call.  If it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
     * is not sorted, the results are undefined.  If the range contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
     *          searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
     * @param toIndex the index of the last element (exclusive) to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
     * @return index of the search key, if it is contained in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
     *         within the specified range;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
     *         element in the range greater than the key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
     *         or <tt>toIndex</tt> if all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
     *         elements in the range are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
     *         if {@code fromIndex > toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
     * @throws ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
     *         if {@code fromIndex < 0 or toIndex > a.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
    public static int binarySearch(byte[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
                                   byte key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
        return binarySearch0(a, fromIndex, toIndex, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
    // Like public version, but without range checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
    private static int binarySearch0(byte[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
                                     byte key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
        int low = fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
        int high = toIndex - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
            int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
            byte midVal = a[mid];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
            if (midVal < key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
                low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
            else if (midVal > key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
                high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
                return mid; // key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
        return -(low + 1);  // key not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
     * Searches the specified array of doubles for the specified value using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
     * the binary search algorithm.  The array must be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
     * (as by the {@link #sort(double[])} method) prior to making this call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
     * If it is not sorted, the results are undefined.  If the array contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
     * one will be found.  This method considers all NaN values to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
     * equivalent and equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
     * @return index of the search key, if it is contained in the array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
     *         element greater than the key, or <tt>a.length</tt> if all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
     *         elements in the array are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
    public static int binarySearch(double[] a, double key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
        return binarySearch0(a, 0, a.length, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
     * Searches a range of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
     * the specified array of doubles for the specified value using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
     * the binary search algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
     * The range must be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
     * (as by the {@link #sort(double[], int, int)} method)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
     * prior to making this call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
     * If it is not sorted, the results are undefined.  If the range contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
     * one will be found.  This method considers all NaN values to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
     * equivalent and equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
     *          searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
     * @param toIndex the index of the last element (exclusive) to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
     * @return index of the search key, if it is contained in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
     *         within the specified range;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
     *         element in the range greater than the key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
     *         or <tt>toIndex</tt> if all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
     *         elements in the range are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
     *         if {@code fromIndex > toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
     * @throws ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
     *         if {@code fromIndex < 0 or toIndex > a.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
    public static int binarySearch(double[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
                                   double key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
        return binarySearch0(a, fromIndex, toIndex, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
    // Like public version, but without range checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
    private static int binarySearch0(double[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
                                     double key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
        int low = fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
        int high = toIndex - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
            int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
            double midVal = a[mid];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
            if (midVal < key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
                low = mid + 1;  // Neither val is NaN, thisVal is smaller
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
            else if (midVal > key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
                high = mid - 1; // Neither val is NaN, thisVal is larger
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
                long midBits = Double.doubleToLongBits(midVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
                long keyBits = Double.doubleToLongBits(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
                if (midBits == keyBits)     // Values are equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
                    return mid;             // Key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
                else if (midBits < keyBits) // (-0.0, 0.0) or (!NaN, NaN)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
                    low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
                else                        // (0.0, -0.0) or (NaN, !NaN)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
                    high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
        return -(low + 1);  // key not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
     * Searches the specified array of floats for the specified value using
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
  1404
     * the binary search algorithm. The array must be sorted
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
  1405
     * (as by the {@link #sort(float[])} method) prior to making this call. If
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
  1406
     * it is not sorted, the results are undefined. If the array contains
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
     * multiple elements with the specified value, there is no guarantee which
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
  1408
     * one will be found. This method considers all NaN values to be
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
     * equivalent and equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
     * @return index of the search key, if it is contained in the array;
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
  1414
     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
     *         element greater than the key, or <tt>a.length</tt> if all
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
  1418
     *         elements in the array are less than the specified key. Note
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
    public static int binarySearch(float[] a, float key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
        return binarySearch0(a, 0, a.length, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
     * Searches a range of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
     * the specified array of floats for the specified value using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
     * the binary search algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
     * The range must be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
     * (as by the {@link #sort(float[], int, int)} method)
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
  1432
     * prior to making this call. If
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
  1433
     * it is not sorted, the results are undefined. If the range contains
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
     * multiple elements with the specified value, there is no guarantee which
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
  1435
     * one will be found. This method considers all NaN values to be
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
     * equivalent and equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
     *          searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
     * @param toIndex the index of the last element (exclusive) to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
     * @return index of the search key, if it is contained in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
     *         within the specified range;
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
  1445
     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
     *         element in the range greater than the key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
     *         or <tt>toIndex</tt> if all
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
  1450
     *         elements in the range are less than the specified key. Note
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
     *         if {@code fromIndex > toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
     * @throws ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
     *         if {@code fromIndex < 0 or toIndex > a.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
    public static int binarySearch(float[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
                                   float key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
        return binarySearch0(a, fromIndex, toIndex, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
    // Like public version, but without range checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
    private static int binarySearch0(float[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
                                     float key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
        int low = fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
        int high = toIndex - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
            int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
            float midVal = a[mid];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
            if (midVal < key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
                low = mid + 1;  // Neither val is NaN, thisVal is smaller
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
            else if (midVal > key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
                high = mid - 1; // Neither val is NaN, thisVal is larger
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
                int midBits = Float.floatToIntBits(midVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
                int keyBits = Float.floatToIntBits(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
                if (midBits == keyBits)     // Values are equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
                    return mid;             // Key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
                else if (midBits < keyBits) // (-0.0, 0.0) or (!NaN, NaN)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
                    low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
                else                        // (0.0, -0.0) or (NaN, !NaN)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
                    high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
        return -(low + 1);  // key not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
     * Searches the specified array for the specified object using the binary
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
  1495
     * search algorithm. The array must be sorted into ascending order
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
     * according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
     * {@linkplain Comparable natural ordering}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
     * of its elements (as by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
     * {@link #sort(Object[])} method) prior to making this call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
     * If it is not sorted, the results are undefined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
     * (If the array contains elements that are not mutually comparable (for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
     * example, strings and integers), it <i>cannot</i> be sorted according
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
     * to the natural ordering of its elements, hence results are undefined.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
     * If the array contains multiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
     * elements equal to the specified object, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
     * @return index of the search key, if it is contained in the array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
     *         element greater than the key, or <tt>a.length</tt> if all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
     *         elements in the array are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
     * @throws ClassCastException if the search key is not comparable to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
     *         elements of the array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
    public static int binarySearch(Object[] a, Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
        return binarySearch0(a, 0, a.length, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
     * Searches a range of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
     * the specified array for the specified object using the binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
     * search algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
     * The range must be sorted into ascending order
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
     * according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
     * {@linkplain Comparable natural ordering}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
     * of its elements (as by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
     * {@link #sort(Object[], int, int)} method) prior to making this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
     * call.  If it is not sorted, the results are undefined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
     * (If the range contains elements that are not mutually comparable (for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
     * example, strings and integers), it <i>cannot</i> be sorted according
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
     * to the natural ordering of its elements, hence results are undefined.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
     * If the range contains multiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
     * elements equal to the specified object, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
     *          searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
     * @param toIndex the index of the last element (exclusive) to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
     * @return index of the search key, if it is contained in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
     *         within the specified range;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
     *         element in the range greater than the key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
     *         or <tt>toIndex</tt> if all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
     *         elements in the range are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
     * @throws ClassCastException if the search key is not comparable to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
     *         elements of the array within the specified range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
     *         if {@code fromIndex > toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
     * @throws ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
     *         if {@code fromIndex < 0 or toIndex > a.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
    public static int binarySearch(Object[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
                                   Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
        return binarySearch0(a, fromIndex, toIndex, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
    // Like public version, but without range checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
    private static int binarySearch0(Object[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
                                     Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
        int low = fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
        int high = toIndex - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
            int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
            Comparable midVal = (Comparable)a[mid];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
            int cmp = midVal.compareTo(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
            if (cmp < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
                low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
            else if (cmp > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
                high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
                return mid; // key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
        return -(low + 1);  // key not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
     * Searches the specified array for the specified object using the binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
     * search algorithm.  The array must be sorted into ascending order
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
     * according to the specified comparator (as by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
     * {@link #sort(Object[], Comparator) sort(T[], Comparator)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
     * method) prior to making this call.  If it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
     * not sorted, the results are undefined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
     * If the array contains multiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
     * elements equal to the specified object, there is no guarantee which one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
     * will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
     * @param c the comparator by which the array is ordered.  A
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
     *        <tt>null</tt> value indicates that the elements'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
     *        {@linkplain Comparable natural ordering} should be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
     * @return index of the search key, if it is contained in the array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
     *         element greater than the key, or <tt>a.length</tt> if all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
     *         elements in the array are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
     * @throws ClassCastException if the array contains elements that are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
     *         <i>mutually comparable</i> using the specified comparator,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
     *         or the search key is not comparable to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
     *         elements of the array using this comparator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
    public static <T> int binarySearch(T[] a, T key, Comparator<? super T> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
        return binarySearch0(a, 0, a.length, key, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
     * Searches a range of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
     * the specified array for the specified object using the binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
     * search algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
     * The range must be sorted into ascending order
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
     * according to the specified comparator (as by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
     * {@link #sort(Object[], int, int, Comparator)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
     * sort(T[], int, int, Comparator)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
     * method) prior to making this call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
     * If it is not sorted, the results are undefined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
     * If the range contains multiple elements equal to the specified object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
     * there is no guarantee which one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
     *          searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
     * @param toIndex the index of the last element (exclusive) to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
     * @param c the comparator by which the array is ordered.  A
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
     *        <tt>null</tt> value indicates that the elements'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
     *        {@linkplain Comparable natural ordering} should be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
     * @return index of the search key, if it is contained in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
     *         within the specified range;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
     *         element in the range greater than the key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
     *         or <tt>toIndex</tt> if all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
     *         elements in the range are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
     * @throws ClassCastException if the range contains elements that are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
     *         <i>mutually comparable</i> using the specified comparator,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
     *         or the search key is not comparable to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
     *         elements in the range using this comparator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
     *         if {@code fromIndex > toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
     * @throws ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
     *         if {@code fromIndex < 0 or toIndex > a.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
    public static <T> int binarySearch(T[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
                                       T key, Comparator<? super T> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
        return binarySearch0(a, fromIndex, toIndex, key, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
    // Like public version, but without range checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
    private static <T> int binarySearch0(T[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
                                         T key, Comparator<? super T> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
        if (c == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
            return binarySearch0(a, fromIndex, toIndex, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
        int low = fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
        int high = toIndex - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
            int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
            T midVal = a[mid];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
            int cmp = c.compare(midVal, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
            if (cmp < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
                low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
            else if (cmp > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
                high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
                return mid; // key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
        return -(low + 1);  // key not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
    // Equality Testing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
     * Returns <tt>true</tt> if the two specified arrays of longs are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
     * <i>equal</i> to one another.  Two arrays are considered equal if both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
     * arrays contain the same number of elements, and all corresponding pairs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
     * of elements in the two arrays are equal.  In other words, two arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
     * are equal if they contain the same elements in the same order.  Also,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
     * two array references are considered equal if both are <tt>null</tt>.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
     * @param a one array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
     * @param a2 the other array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
     * @return <tt>true</tt> if the two arrays are equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
    public static boolean equals(long[] a, long[] a2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
        if (a==a2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
        if (a==null || a2==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
        int length = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
        if (a2.length != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
        for (int i=0; i<length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
            if (a[i] != a2[i])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
     * Returns <tt>true</tt> if the two specified arrays of ints are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
     * <i>equal</i> to one another.  Two arrays are considered equal if both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
     * arrays contain the same number of elements, and all corresponding pairs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
     * of elements in the two arrays are equal.  In other words, two arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
     * are equal if they contain the same elements in the same order.  Also,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
     * two array references are considered equal if both are <tt>null</tt>.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
     * @param a one array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
     * @param a2 the other array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
     * @return <tt>true</tt> if the two arrays are equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
    public static boolean equals(int[] a, int[] a2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
        if (a==a2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
        if (a==null || a2==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
        int length = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
        if (a2.length != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
        for (int i=0; i<length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
            if (a[i] != a2[i])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
     * Returns <tt>true</tt> if the two specified arrays of shorts are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
     * <i>equal</i> to one another.  Two arrays are considered equal if both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
     * arrays contain the same number of elements, and all corresponding pairs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
     * of elements in the two arrays are equal.  In other words, two arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
     * are equal if they contain the same elements in the same order.  Also,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
     * two array references are considered equal if both are <tt>null</tt>.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
     * @param a one array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
     * @param a2 the other array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
     * @return <tt>true</tt> if the two arrays are equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
    public static boolean equals(short[] a, short a2[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
        if (a==a2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
        if (a==null || a2==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
        int length = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
        if (a2.length != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
        for (int i=0; i<length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
            if (a[i] != a2[i])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
     * Returns <tt>true</tt> if the two specified arrays of chars are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
     * <i>equal</i> to one another.  Two arrays are considered equal if both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
     * arrays contain the same number of elements, and all corresponding pairs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
     * of elements in the two arrays are equal.  In other words, two arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
     * are equal if they contain the same elements in the same order.  Also,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
     * two array references are considered equal if both are <tt>null</tt>.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
     * @param a one array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
     * @param a2 the other array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
     * @return <tt>true</tt> if the two arrays are equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
    public static boolean equals(char[] a, char[] a2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
        if (a==a2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
        if (a==null || a2==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
        int length = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
        if (a2.length != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
        for (int i=0; i<length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
            if (a[i] != a2[i])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
     * Returns <tt>true</tt> if the two specified arrays of bytes are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
     * <i>equal</i> to one another.  Two arrays are considered equal if both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
     * arrays contain the same number of elements, and all corresponding pairs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
     * of elements in the two arrays are equal.  In other words, two arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
     * are equal if they contain the same elements in the same order.  Also,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
     * two array references are considered equal if both are <tt>null</tt>.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
     * @param a one array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
     * @param a2 the other array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
     * @return <tt>true</tt> if the two arrays are equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
    public static boolean equals(byte[] a, byte[] a2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
        if (a==a2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
        if (a==null || a2==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
        int length = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
        if (a2.length != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
        for (int i=0; i<length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
            if (a[i] != a2[i])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
     * Returns <tt>true</tt> if the two specified arrays of booleans are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
     * <i>equal</i> to one another.  Two arrays are considered equal if both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
     * arrays contain the same number of elements, and all corresponding pairs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
     * of elements in the two arrays are equal.  In other words, two arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
     * are equal if they contain the same elements in the same order.  Also,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
     * two array references are considered equal if both are <tt>null</tt>.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
     * @param a one array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
     * @param a2 the other array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
     * @return <tt>true</tt> if the two arrays are equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
    public static boolean equals(boolean[] a, boolean[] a2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
        if (a==a2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
        if (a==null || a2==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
        int length = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
        if (a2.length != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
        for (int i=0; i<length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
            if (a[i] != a2[i])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
     * Returns <tt>true</tt> if the two specified arrays of doubles are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
     * <i>equal</i> to one another.  Two arrays are considered equal if both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
     * arrays contain the same number of elements, and all corresponding pairs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
     * of elements in the two arrays are equal.  In other words, two arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
     * are equal if they contain the same elements in the same order.  Also,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
     * two array references are considered equal if both are <tt>null</tt>.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
     * Two doubles <tt>d1</tt> and <tt>d2</tt> are considered equal if:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
     * <pre>    <tt>new Double(d1).equals(new Double(d2))</tt></pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
     * (Unlike the <tt>==</tt> operator, this method considers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
     * <tt>NaN</tt> equals to itself, and 0.0d unequal to -0.0d.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
     * @param a one array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
     * @param a2 the other array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
     * @return <tt>true</tt> if the two arrays are equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
     * @see Double#equals(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
    public static boolean equals(double[] a, double[] a2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
        if (a==a2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
        if (a==null || a2==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
        int length = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
        if (a2.length != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
        for (int i=0; i<length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
            if (Double.doubleToLongBits(a[i])!=Double.doubleToLongBits(a2[i]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
     * Returns <tt>true</tt> if the two specified arrays of floats are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
     * <i>equal</i> to one another.  Two arrays are considered equal if both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
     * arrays contain the same number of elements, and all corresponding pairs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
     * of elements in the two arrays are equal.  In other words, two arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
     * are equal if they contain the same elements in the same order.  Also,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
     * two array references are considered equal if both are <tt>null</tt>.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
     * Two floats <tt>f1</tt> and <tt>f2</tt> are considered equal if:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
     * <pre>    <tt>new Float(f1).equals(new Float(f2))</tt></pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
     * (Unlike the <tt>==</tt> operator, this method considers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
     * <tt>NaN</tt> equals to itself, and 0.0f unequal to -0.0f.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
     * @param a one array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
     * @param a2 the other array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
     * @return <tt>true</tt> if the two arrays are equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
     * @see Float#equals(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
    public static boolean equals(float[] a, float[] a2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
        if (a==a2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
        if (a==null || a2==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
        int length = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
        if (a2.length != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
        for (int i=0; i<length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
            if (Float.floatToIntBits(a[i])!=Float.floatToIntBits(a2[i]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
     * Returns <tt>true</tt> if the two specified arrays of Objects are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
     * <i>equal</i> to one another.  The two arrays are considered equal if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
     * both arrays contain the same number of elements, and all corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
     * pairs of elements in the two arrays are equal.  Two objects <tt>e1</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
     * and <tt>e2</tt> are considered <i>equal</i> if <tt>(e1==null ? e2==null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
     * : e1.equals(e2))</tt>.  In other words, the two arrays are equal if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
     * they contain the same elements in the same order.  Also, two array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
     * references are considered equal if both are <tt>null</tt>.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
     * @param a one array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
     * @param a2 the other array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
     * @return <tt>true</tt> if the two arrays are equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
    public static boolean equals(Object[] a, Object[] a2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
        if (a==a2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
        if (a==null || a2==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
        int length = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
        if (a2.length != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
        for (int i=0; i<length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
            Object o1 = a[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
            Object o2 = a2[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
            if (!(o1==null ? o2==null : o1.equals(o2)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
    // Filling
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
     * Assigns the specified long value to each element of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
     * of longs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
    public static void fill(long[] a, long val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
        for (int i = 0, len = a.length; i < len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
     * Assigns the specified long value to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
     * range of the specified array of longs.  The range to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
     * extends from index <tt>fromIndex</tt>, inclusive, to index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
     * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
     * range to be filled is empty.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
     * @param toIndex the index of the last element (exclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
     * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
     * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
     *         <tt>toIndex &gt; a.length</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
    public static void fill(long[] a, int fromIndex, int toIndex, long val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
        for (int i = fromIndex; i < toIndex; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
     * Assigns the specified int value to each element of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
     * of ints.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
    public static void fill(int[] a, int val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
        for (int i = 0, len = a.length; i < len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
     * Assigns the specified int value to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
     * range of the specified array of ints.  The range to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
     * extends from index <tt>fromIndex</tt>, inclusive, to index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
     * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
     * range to be filled is empty.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
     * @param toIndex the index of the last element (exclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
     * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
     * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
     *         <tt>toIndex &gt; a.length</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
    public static void fill(int[] a, int fromIndex, int toIndex, int val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
        for (int i = fromIndex; i < toIndex; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
     * Assigns the specified short value to each element of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
     * of shorts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
    public static void fill(short[] a, short val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
        for (int i = 0, len = a.length; i < len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
     * Assigns the specified short value to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
     * range of the specified array of shorts.  The range to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
     * extends from index <tt>fromIndex</tt>, inclusive, to index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
     * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
     * range to be filled is empty.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
     * @param toIndex the index of the last element (exclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
     * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
     * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
     *         <tt>toIndex &gt; a.length</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
    public static void fill(short[] a, int fromIndex, int toIndex, short val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
        for (int i = fromIndex; i < toIndex; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
     * Assigns the specified char value to each element of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
     * of chars.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
    public static void fill(char[] a, char val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
        for (int i = 0, len = a.length; i < len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
     * Assigns the specified char value to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
     * range of the specified array of chars.  The range to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
     * extends from index <tt>fromIndex</tt>, inclusive, to index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
     * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
     * range to be filled is empty.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
     * @param toIndex the index of the last element (exclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
     * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
     * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
     *         <tt>toIndex &gt; a.length</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
    public static void fill(char[] a, int fromIndex, int toIndex, char val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
        for (int i = fromIndex; i < toIndex; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
     * Assigns the specified byte value to each element of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
     * of bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
    public static void fill(byte[] a, byte val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
        for (int i = 0, len = a.length; i < len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
     * Assigns the specified byte value to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
     * range of the specified array of bytes.  The range to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
     * extends from index <tt>fromIndex</tt>, inclusive, to index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
     * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
     * range to be filled is empty.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
     * @param toIndex the index of the last element (exclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
     * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
     * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
     *         <tt>toIndex &gt; a.length</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
    public static void fill(byte[] a, int fromIndex, int toIndex, byte val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
        for (int i = fromIndex; i < toIndex; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
     * Assigns the specified boolean value to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
     * array of booleans.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
    public static void fill(boolean[] a, boolean val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
        for (int i = 0, len = a.length; i < len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
     * Assigns the specified boolean value to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
     * range of the specified array of booleans.  The range to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
     * extends from index <tt>fromIndex</tt>, inclusive, to index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
     * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
     * range to be filled is empty.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
     * @param toIndex the index of the last element (exclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
     * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
     * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
     *         <tt>toIndex &gt; a.length</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
    public static void fill(boolean[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
                            boolean val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
        for (int i = fromIndex; i < toIndex; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
     * Assigns the specified double value to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
     * array of doubles.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
    public static void fill(double[] a, double val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
        for (int i = 0, len = a.length; i < len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
     * Assigns the specified double value to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
     * range of the specified array of doubles.  The range to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
     * extends from index <tt>fromIndex</tt>, inclusive, to index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
     * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
     * range to be filled is empty.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
     * @param toIndex the index of the last element (exclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
     * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
     * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
     *         <tt>toIndex &gt; a.length</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
    public static void fill(double[] a, int fromIndex, int toIndex,double val){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
        for (int i = fromIndex; i < toIndex; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
     * Assigns the specified float value to each element of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
     * of floats.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
    public static void fill(float[] a, float val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
        for (int i = 0, len = a.length; i < len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
     * Assigns the specified float value to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
     * range of the specified array of floats.  The range to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
     * extends from index <tt>fromIndex</tt>, inclusive, to index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
     * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
     * range to be filled is empty.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
     * @param toIndex the index of the last element (exclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
     * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
     * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
     *         <tt>toIndex &gt; a.length</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
    public static void fill(float[] a, int fromIndex, int toIndex, float val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
        for (int i = fromIndex; i < toIndex; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
     * Assigns the specified Object reference to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
     * array of Objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
     * @throws ArrayStoreException if the specified value is not of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
     *         runtime type that can be stored in the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
    public static void fill(Object[] a, Object val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
        for (int i = 0, len = a.length; i < len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
     * Assigns the specified Object reference to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
     * range of the specified array of Objects.  The range to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
     * extends from index <tt>fromIndex</tt>, inclusive, to index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
     * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
     * range to be filled is empty.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
     * @param toIndex the index of the last element (exclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
     * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
     * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
     *         <tt>toIndex &gt; a.length</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
     * @throws ArrayStoreException if the specified value is not of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
     *         runtime type that can be stored in the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
    public static void fill(Object[] a, int fromIndex, int toIndex, Object val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
        for (int i = fromIndex; i < toIndex; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
  2297
    // Cloning
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
     * Copies the specified array, truncating or padding with nulls (if necessary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
     * so the copy has the specified length.  For all indices that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
     * valid in both the original array and the copy, the two arrays will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
     * contain identical values.  For any indices that are valid in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
     * copy but not the original, the copy will contain <tt>null</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
     * Such indices will exist if and only if the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
     * is greater than that of the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
     * The resulting array is of exactly the same class as the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
     * @param original the array to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
     * @param newLength the length of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
     * @return a copy of the original array, truncated or padded with nulls
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
     *     to obtain the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
     * @throws NullPointerException if <tt>original</tt> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
    public static <T> T[] copyOf(T[] original, int newLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
        return (T[]) copyOf(original, newLength, original.getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
     * Copies the specified array, truncating or padding with nulls (if necessary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
     * so the copy has the specified length.  For all indices that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
     * valid in both the original array and the copy, the two arrays will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
     * contain identical values.  For any indices that are valid in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
     * copy but not the original, the copy will contain <tt>null</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
     * Such indices will exist if and only if the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
     * is greater than that of the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
     * The resulting array is of the class <tt>newType</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
     * @param original the array to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
     * @param newLength the length of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
     * @param newType the class of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
     * @return a copy of the original array, truncated or padded with nulls
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
     *     to obtain the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
     * @throws NullPointerException if <tt>original</tt> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
     * @throws ArrayStoreException if an element copied from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
     *     <tt>original</tt> is not of a runtime type that can be stored in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
     *     an array of class <tt>newType</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
    public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
        T[] copy = ((Object)newType == (Object)Object[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
            ? (T[]) new Object[newLength]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
            : (T[]) Array.newInstance(newType.getComponentType(), newLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
        System.arraycopy(original, 0, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
                         Math.min(original.length, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
     * Copies the specified array, truncating or padding with zeros (if necessary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
     * so the copy has the specified length.  For all indices that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
     * valid in both the original array and the copy, the two arrays will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
     * contain identical values.  For any indices that are valid in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
     * copy but not the original, the copy will contain <tt>(byte)0</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
     * Such indices will exist if and only if the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
     * is greater than that of the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
     * @param original the array to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
     * @param newLength the length of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
     * @return a copy of the original array, truncated or padded with zeros
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
     *     to obtain the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
     * @throws NullPointerException if <tt>original</tt> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
    public static byte[] copyOf(byte[] original, int newLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
        byte[] copy = new byte[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
        System.arraycopy(original, 0, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
                         Math.min(original.length, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
     * Copies the specified array, truncating or padding with zeros (if necessary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
     * so the copy has the specified length.  For all indices that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
     * valid in both the original array and the copy, the two arrays will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
     * contain identical values.  For any indices that are valid in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
     * copy but not the original, the copy will contain <tt>(short)0</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
     * Such indices will exist if and only if the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
     * is greater than that of the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
     * @param original the array to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
     * @param newLength the length of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
     * @return a copy of the original array, truncated or padded with zeros
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
     *     to obtain the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
     * @throws NullPointerException if <tt>original</tt> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
    public static short[] copyOf(short[] original, int newLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
        short[] copy = new short[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
        System.arraycopy(original, 0, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
                         Math.min(original.length, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
     * Copies the specified array, truncating or padding with zeros (if necessary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
     * so the copy has the specified length.  For all indices that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
     * valid in both the original array and the copy, the two arrays will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
     * contain identical values.  For any indices that are valid in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
     * copy but not the original, the copy will contain <tt>0</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
     * Such indices will exist if and only if the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
     * is greater than that of the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
     * @param original the array to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
     * @param newLength the length of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
     * @return a copy of the original array, truncated or padded with zeros
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
     *     to obtain the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
     * @throws NullPointerException if <tt>original</tt> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
    public static int[] copyOf(int[] original, int newLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2418
        int[] copy = new int[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
        System.arraycopy(original, 0, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2420
                         Math.min(original.length, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
     * Copies the specified array, truncating or padding with zeros (if necessary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
     * so the copy has the specified length.  For all indices that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
     * valid in both the original array and the copy, the two arrays will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
     * contain identical values.  For any indices that are valid in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
     * copy but not the original, the copy will contain <tt>0L</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2430
     * Such indices will exist if and only if the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
     * is greater than that of the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2433
     * @param original the array to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
     * @param newLength the length of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
     * @return a copy of the original array, truncated or padded with zeros
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
     *     to obtain the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
     * @throws NullPointerException if <tt>original</tt> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2441
    public static long[] copyOf(long[] original, int newLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2442
        long[] copy = new long[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2443
        System.arraycopy(original, 0, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2444
                         Math.min(original.length, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2445
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2446
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2447
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2448
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2449
     * Copies the specified array, truncating or padding with null characters (if necessary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2450
     * so the copy has the specified length.  For all indices that are valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2451
     * in both the original array and the copy, the two arrays will contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2452
     * identical values.  For any indices that are valid in the copy but not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2453
     * the original, the copy will contain <tt>'\\u000'</tt>.  Such indices
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2454
     * will exist if and only if the specified length is greater than that of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2455
     * the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2456
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2457
     * @param original the array to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2458
     * @param newLength the length of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2459
     * @return a copy of the original array, truncated or padded with null characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2460
     *     to obtain the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2461
     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2462
     * @throws NullPointerException if <tt>original</tt> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2463
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2464
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2465
    public static char[] copyOf(char[] original, int newLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2466
        char[] copy = new char[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2467
        System.arraycopy(original, 0, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2468
                         Math.min(original.length, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2469
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2470
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2471
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2472
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2473
     * Copies the specified array, truncating or padding with zeros (if necessary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2474
     * so the copy has the specified length.  For all indices that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2475
     * valid in both the original array and the copy, the two arrays will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2476
     * contain identical values.  For any indices that are valid in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2477
     * copy but not the original, the copy will contain <tt>0f</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2478
     * Such indices will exist if and only if the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2479
     * is greater than that of the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2480
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2481
     * @param original the array to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2482
     * @param newLength the length of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2483
     * @return a copy of the original array, truncated or padded with zeros
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2484
     *     to obtain the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2485
     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2486
     * @throws NullPointerException if <tt>original</tt> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2487
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2488
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2489
    public static float[] copyOf(float[] original, int newLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2490
        float[] copy = new float[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2491
        System.arraycopy(original, 0, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2492
                         Math.min(original.length, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2493
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2494
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2495
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2496
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2497
     * Copies the specified array, truncating or padding with zeros (if necessary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2498
     * so the copy has the specified length.  For all indices that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2499
     * valid in both the original array and the copy, the two arrays will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2500
     * contain identical values.  For any indices that are valid in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2501
     * copy but not the original, the copy will contain <tt>0d</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2502
     * Such indices will exist if and only if the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2503
     * is greater than that of the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2504
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2505
     * @param original the array to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2506
     * @param newLength the length of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2507
     * @return a copy of the original array, truncated or padded with zeros
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2508
     *     to obtain the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2509
     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2510
     * @throws NullPointerException if <tt>original</tt> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2511
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2512
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2513
    public static double[] copyOf(double[] original, int newLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2514
        double[] copy = new double[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2515
        System.arraycopy(original, 0, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2516
                         Math.min(original.length, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2517
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2518
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2519
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2520
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2521
     * Copies the specified array, truncating or padding with <tt>false</tt> (if necessary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2522
     * so the copy has the specified length.  For all indices that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2523
     * valid in both the original array and the copy, the two arrays will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2524
     * contain identical values.  For any indices that are valid in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2525
     * copy but not the original, the copy will contain <tt>false</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2526
     * Such indices will exist if and only if the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2527
     * is greater than that of the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2528
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2529
     * @param original the array to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2530
     * @param newLength the length of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2531
     * @return a copy of the original array, truncated or padded with false elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2532
     *     to obtain the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2533
     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2534
     * @throws NullPointerException if <tt>original</tt> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2535
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2536
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2537
    public static boolean[] copyOf(boolean[] original, int newLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2538
        boolean[] copy = new boolean[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2539
        System.arraycopy(original, 0, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2540
                         Math.min(original.length, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2541
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2542
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2543
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2544
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2545
     * Copies the specified range of the specified array into a new array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2546
     * The initial index of the range (<tt>from</tt>) must lie between zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2547
     * and <tt>original.length</tt>, inclusive.  The value at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2548
     * <tt>original[from]</tt> is placed into the initial element of the copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2549
     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2550
     * Values from subsequent elements in the original array are placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2551
     * subsequent elements in the copy.  The final index of the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2552
     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2553
     * may be greater than <tt>original.length</tt>, in which case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2554
     * <tt>null</tt> is placed in all elements of the copy whose index is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2555
     * greater than or equal to <tt>original.length - from</tt>.  The length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2556
     * of the returned array will be <tt>to - from</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2557
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2558
     * The resulting array is of exactly the same class as the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2559
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2560
     * @param original the array from which a range is to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2561
     * @param from the initial index of the range to be copied, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2562
     * @param to the final index of the range to be copied, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2563
     *     (This index may lie outside the array.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2564
     * @return a new array containing the specified range from the original array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2565
     *     truncated or padded with nulls to obtain the required length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2566
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2567
     *     or {@code from > original.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2568
     * @throws IllegalArgumentException if <tt>from &gt; to</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2569
     * @throws NullPointerException if <tt>original</tt> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2570
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2571
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2572
    public static <T> T[] copyOfRange(T[] original, int from, int to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2573
        return copyOfRange(original, from, to, (Class<T[]>) original.getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2574
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2575
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2576
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2577
     * Copies the specified range of the specified array into a new array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2578
     * The initial index of the range (<tt>from</tt>) must lie between zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2579
     * and <tt>original.length</tt>, inclusive.  The value at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2580
     * <tt>original[from]</tt> is placed into the initial element of the copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2581
     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2582
     * Values from subsequent elements in the original array are placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2583
     * subsequent elements in the copy.  The final index of the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2584
     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2585
     * may be greater than <tt>original.length</tt>, in which case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2586
     * <tt>null</tt> is placed in all elements of the copy whose index is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2587
     * greater than or equal to <tt>original.length - from</tt>.  The length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2588
     * of the returned array will be <tt>to - from</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2589
     * The resulting array is of the class <tt>newType</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2590
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2591
     * @param original the array from which a range is to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2592
     * @param from the initial index of the range to be copied, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2593
     * @param to the final index of the range to be copied, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2594
     *     (This index may lie outside the array.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2595
     * @param newType the class of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2596
     * @return a new array containing the specified range from the original array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2597
     *     truncated or padded with nulls to obtain the required length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2598
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2599
     *     or {@code from > original.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2600
     * @throws IllegalArgumentException if <tt>from &gt; to</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2601
     * @throws NullPointerException if <tt>original</tt> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2602
     * @throws ArrayStoreException if an element copied from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2603
     *     <tt>original</tt> is not of a runtime type that can be stored in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2604
     *     an array of class <tt>newType</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2605
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2606
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2607
    public static <T,U> T[] copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2608
        int newLength = to - from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2609
        if (newLength < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2610
            throw new IllegalArgumentException(from + " > " + to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2611
        T[] copy = ((Object)newType == (Object)Object[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2612
            ? (T[]) new Object[newLength]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2613
            : (T[]) Array.newInstance(newType.getComponentType(), newLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2614
        System.arraycopy(original, from, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2615
                         Math.min(original.length - from, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2616
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2617
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2618
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2619
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2620
     * Copies the specified range of the specified array into a new array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2621
     * The initial index of the range (<tt>from</tt>) must lie between zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2622
     * and <tt>original.length</tt>, inclusive.  The value at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2623
     * <tt>original[from]</tt> is placed into the initial element of the copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2624
     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2625
     * Values from subsequent elements in the original array are placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2626
     * subsequent elements in the copy.  The final index of the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2627
     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2628
     * may be greater than <tt>original.length</tt>, in which case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2629
     * <tt>(byte)0</tt> is placed in all elements of the copy whose index is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2630
     * greater than or equal to <tt>original.length - from</tt>.  The length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2631
     * of the returned array will be <tt>to - from</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2632
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2633
     * @param original the array from which a range is to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2634
     * @param from the initial index of the range to be copied, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2635
     * @param to the final index of the range to be copied, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2636
     *     (This index may lie outside the array.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2637
     * @return a new array containing the specified range from the original array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2638
     *     truncated or padded with zeros to obtain the required length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2639
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2640
     *     or {@code from > original.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2641
     * @throws IllegalArgumentException if <tt>from &gt; to</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2642
     * @throws NullPointerException if <tt>original</tt> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2643
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2644
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2645
    public static byte[] copyOfRange(byte[] original, int from, int to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2646
        int newLength = to - from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2647
        if (newLength < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2648
            throw new IllegalArgumentException(from + " > " + to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2649
        byte[] copy = new byte[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2650
        System.arraycopy(original, from, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2651
                         Math.min(original.length - from, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2652
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2653
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2654
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2655
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2656
     * Copies the specified range of the specified array into a new array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2657
     * The initial index of the range (<tt>from</tt>) must lie between zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2658
     * and <tt>original.length</tt>, inclusive.  The value at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2659
     * <tt>original[from]</tt> is placed into the initial element of the copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2660
     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2661
     * Values from subsequent elements in the original array are placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2662
     * subsequent elements in the copy.  The final index of the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2663
     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2664
     * may be greater than <tt>original.length</tt>, in which case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2665
     * <tt>(short)0</tt> is placed in all elements of the copy whose index is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2666
     * greater than or equal to <tt>original.length - from</tt>.  The length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2667
     * of the returned array will be <tt>to - from</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2668
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2669
     * @param original the array from which a range is to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2670
     * @param from the initial index of the range to be copied, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2671
     * @param to the final index of the range to be copied, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2672
     *     (This index may lie outside the array.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2673
     * @return a new array containing the specified range from the original array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2674
     *     truncated or padded with zeros to obtain the required length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2675
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2676
     *     or {@code from > original.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2677
     * @throws IllegalArgumentException if <tt>from &gt; to</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2678
     * @throws NullPointerException if <tt>original</tt> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2679
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2680
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2681
    public static short[] copyOfRange(short[] original, int from, int to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2682
        int newLength = to - from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2683
        if (newLength < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2684
            throw new IllegalArgumentException(from + " > " + to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2685
        short[] copy = new short[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2686
        System.arraycopy(original, from, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2687
                         Math.min(original.length - from, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2688
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2689
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2690
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2691
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2692
     * Copies the specified range of the specified array into a new array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2693
     * The initial index of the range (<tt>from</tt>) must lie between zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2694
     * and <tt>original.length</tt>, inclusive.  The value at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2695
     * <tt>original[from]</tt> is placed into the initial element of the copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2696
     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2697
     * Values from subsequent elements in the original array are placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2698
     * subsequent elements in the copy.  The final index of the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2699
     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2700
     * may be greater than <tt>original.length</tt>, in which case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2701
     * <tt>0</tt> is placed in all elements of the copy whose index is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2702
     * greater than or equal to <tt>original.length - from</tt>.  The length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2703
     * of the returned array will be <tt>to - from</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2704
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2705
     * @param original the array from which a range is to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2706
     * @param from the initial index of the range to be copied, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2707
     * @param to the final index of the range to be copied, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2708
     *     (This index may lie outside the array.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2709
     * @return a new array containing the specified range from the original array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2710
     *     truncated or padded with zeros to obtain the required length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2711
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2712
     *     or {@code from > original.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2713
     * @throws IllegalArgumentException if <tt>from &gt; to</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2714
     * @throws NullPointerException if <tt>original</tt> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2715
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2716
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2717
    public static int[] copyOfRange(int[] original, int from, int to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2718
        int newLength = to - from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2719
        if (newLength < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2720
            throw new IllegalArgumentException(from + " > " + to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2721
        int[] copy = new int[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2722
        System.arraycopy(original, from, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2723
                         Math.min(original.length - from, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2724
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2725
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2726
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2727
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2728
     * Copies the specified range of the specified array into a new array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2729
     * The initial index of the range (<tt>from</tt>) must lie between zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2730
     * and <tt>original.length</tt>, inclusive.  The value at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2731
     * <tt>original[from]</tt> is placed into the initial element of the copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2732
     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2733
     * Values from subsequent elements in the original array are placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2734
     * subsequent elements in the copy.  The final index of the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2735
     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2736
     * may be greater than <tt>original.length</tt>, in which case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2737
     * <tt>0L</tt> is placed in all elements of the copy whose index is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2738
     * greater than or equal to <tt>original.length - from</tt>.  The length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2739
     * of the returned array will be <tt>to - from</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2740
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2741
     * @param original the array from which a range is to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2742
     * @param from the initial index of the range to be copied, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2743
     * @param to the final index of the range to be copied, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2744
     *     (This index may lie outside the array.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2745
     * @return a new array containing the specified range from the original array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2746
     *     truncated or padded with zeros to obtain the required length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2747
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2748
     *     or {@code from > original.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2749
     * @throws IllegalArgumentException if <tt>from &gt; to</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2750
     * @throws NullPointerException if <tt>original</tt> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2751
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2752
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2753
    public static long[] copyOfRange(long[] original, int from, int to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2754
        int newLength = to - from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2755
        if (newLength < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2756
            throw new IllegalArgumentException(from + " > " + to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2757
        long[] copy = new long[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2758
        System.arraycopy(original, from, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2759
                         Math.min(original.length - from, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2760
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2761
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2762
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2763
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2764
     * Copies the specified range of the specified array into a new array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2765
     * The initial index of the range (<tt>from</tt>) must lie between zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2766
     * and <tt>original.length</tt>, inclusive.  The value at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2767
     * <tt>original[from]</tt> is placed into the initial element of the copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2768
     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2769
     * Values from subsequent elements in the original array are placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2770
     * subsequent elements in the copy.  The final index of the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2771
     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2772
     * may be greater than <tt>original.length</tt>, in which case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2773
     * <tt>'\\u000'</tt> is placed in all elements of the copy whose index is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2774
     * greater than or equal to <tt>original.length - from</tt>.  The length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2775
     * of the returned array will be <tt>to - from</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2776
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2777
     * @param original the array from which a range is to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2778
     * @param from the initial index of the range to be copied, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2779
     * @param to the final index of the range to be copied, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2780
     *     (This index may lie outside the array.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2781
     * @return a new array containing the specified range from the original array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2782
     *     truncated or padded with null characters to obtain the required length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2783
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2784
     *     or {@code from > original.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2785
     * @throws IllegalArgumentException if <tt>from &gt; to</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2786
     * @throws NullPointerException if <tt>original</tt> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2787
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2788
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2789
    public static char[] copyOfRange(char[] original, int from, int to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2790
        int newLength = to - from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2791
        if (newLength < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2792
            throw new IllegalArgumentException(from + " > " + to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2793
        char[] copy = new char[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2794
        System.arraycopy(original, from, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2795
                         Math.min(original.length - from, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2796
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2797
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2798
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2799
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2800
     * Copies the specified range of the specified array into a new array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2801
     * The initial index of the range (<tt>from</tt>) must lie between zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2802
     * and <tt>original.length</tt>, inclusive.  The value at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2803
     * <tt>original[from]</tt> is placed into the initial element of the copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2804
     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2805
     * Values from subsequent elements in the original array are placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2806
     * subsequent elements in the copy.  The final index of the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2807
     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2808
     * may be greater than <tt>original.length</tt>, in which case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2809
     * <tt>0f</tt> is placed in all elements of the copy whose index is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2810
     * greater than or equal to <tt>original.length - from</tt>.  The length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2811
     * of the returned array will be <tt>to - from</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2812
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2813
     * @param original the array from which a range is to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2814
     * @param from the initial index of the range to be copied, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2815
     * @param to the final index of the range to be copied, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2816
     *     (This index may lie outside the array.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2817
     * @return a new array containing the specified range from the original array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2818
     *     truncated or padded with zeros to obtain the required length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2819
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2820
     *     or {@code from > original.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2821
     * @throws IllegalArgumentException if <tt>from &gt; to</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2822
     * @throws NullPointerException if <tt>original</tt> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2823
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2824
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2825
    public static float[] copyOfRange(float[] original, int from, int to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2826
        int newLength = to - from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2827
        if (newLength < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2828
            throw new IllegalArgumentException(from + " > " + to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2829
        float[] copy = new float[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2830
        System.arraycopy(original, from, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2831
                         Math.min(original.length - from, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2832
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2833
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2834
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2835
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2836
     * Copies the specified range of the specified array into a new array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2837
     * The initial index of the range (<tt>from</tt>) must lie between zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2838
     * and <tt>original.length</tt>, inclusive.  The value at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2839
     * <tt>original[from]</tt> is placed into the initial element of the copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2840
     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2841
     * Values from subsequent elements in the original array are placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2842
     * subsequent elements in the copy.  The final index of the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2843
     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2844
     * may be greater than <tt>original.length</tt>, in which case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2845
     * <tt>0d</tt> is placed in all elements of the copy whose index is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2846
     * greater than or equal to <tt>original.length - from</tt>.  The length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2847
     * of the returned array will be <tt>to - from</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2848
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2849
     * @param original the array from which a range is to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2850
     * @param from the initial index of the range to be copied, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2851
     * @param to the final index of the range to be copied, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2852
     *     (This index may lie outside the array.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2853
     * @return a new array containing the specified range from the original array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2854
     *     truncated or padded with zeros to obtain the required length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2855
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2856
     *     or {@code from > original.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2857
     * @throws IllegalArgumentException if <tt>from &gt; to</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2858
     * @throws NullPointerException if <tt>original</tt> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2859
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2860
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2861
    public static double[] copyOfRange(double[] original, int from, int to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2862
        int newLength = to - from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2863
        if (newLength < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2864
            throw new IllegalArgumentException(from + " > " + to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2865
        double[] copy = new double[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2866
        System.arraycopy(original, from, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2867
                         Math.min(original.length - from, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2868
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2869
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2870
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2871
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2872
     * Copies the specified range of the specified array into a new array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2873
     * The initial index of the range (<tt>from</tt>) must lie between zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2874
     * and <tt>original.length</tt>, inclusive.  The value at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2875
     * <tt>original[from]</tt> is placed into the initial element of the copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2876
     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2877
     * Values from subsequent elements in the original array are placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2878
     * subsequent elements in the copy.  The final index of the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2879
     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2880
     * may be greater than <tt>original.length</tt>, in which case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2881
     * <tt>false</tt> is placed in all elements of the copy whose index is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2882
     * greater than or equal to <tt>original.length - from</tt>.  The length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2883
     * of the returned array will be <tt>to - from</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2884
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2885
     * @param original the array from which a range is to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2886
     * @param from the initial index of the range to be copied, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2887
     * @param to the final index of the range to be copied, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2888
     *     (This index may lie outside the array.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2889
     * @return a new array containing the specified range from the original array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2890
     *     truncated or padded with false elements to obtain the required length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2891
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2892
     *     or {@code from > original.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2893
     * @throws IllegalArgumentException if <tt>from &gt; to</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2894
     * @throws NullPointerException if <tt>original</tt> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2895
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2896
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2897
    public static boolean[] copyOfRange(boolean[] original, int from, int to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2898
        int newLength = to - from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2899
        if (newLength < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2900
            throw new IllegalArgumentException(from + " > " + to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2901
        boolean[] copy = new boolean[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2902
        System.arraycopy(original, from, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2903
                         Math.min(original.length - from, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2904
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2905
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2906
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2907
    // Misc
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2908
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2909
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2910
     * Returns a fixed-size list backed by the specified array.  (Changes to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2911
     * the returned list "write through" to the array.)  This method acts
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2912
     * as bridge between array-based and collection-based APIs, in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2913
     * combination with {@link Collection#toArray}.  The returned list is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2914
     * serializable and implements {@link RandomAccess}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2915
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2916
     * <p>This method also provides a convenient way to create a fixed-size
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2917
     * list initialized to contain several elements:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2918
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2919
     *     List&lt;String&gt; stooges = Arrays.asList("Larry", "Moe", "Curly");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2920
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2921
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2922
     * @param a the array by which the list will be backed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2923
     * @return a list view of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2924
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2925
    public static <T> List<T> asList(T... a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2926
        return new ArrayList<T>(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2927
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2928
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2929
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2930
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2931
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2932
    private static class ArrayList<E> extends AbstractList<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2933
        implements RandomAccess, java.io.Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2934
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2935
        private static final long serialVersionUID = -2764017481108945198L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2936
        private final E[] a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2937
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2938
        ArrayList(E[] array) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2939
            if (array==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2940
                throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2941
            a = array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2942
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2943
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2944
        public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2945
            return a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2946
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2947
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2948
        public Object[] toArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2949
            return a.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2950
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2951
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2952
        public <T> T[] toArray(T[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2953
            int size = size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2954
            if (a.length < size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2955
                return Arrays.copyOf(this.a, size,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2956
                                     (Class<? extends T[]>) a.getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2957
            System.arraycopy(this.a, 0, a, 0, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2958
            if (a.length > size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2959
                a[size] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2960
            return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2961
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2962
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2963
        public E get(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2964
            return a[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2965
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2966
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2967
        public E set(int index, E element) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2968
            E oldValue = a[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2969
            a[index] = element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2970
            return oldValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2971
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2972
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2973
        public int indexOf(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2974
            if (o==null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2975
                for (int i=0; i<a.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2976
                    if (a[i]==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2977
                        return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2978
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2979
                for (int i=0; i<a.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2980
                    if (o.equals(a[i]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2981
                        return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2982
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2983
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2984
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2985
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2986
        public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2987
            return indexOf(o) != -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2988
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2989
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2990
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2991
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2992
     * Returns a hash code based on the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2993
     * For any two <tt>long</tt> arrays <tt>a</tt> and <tt>b</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2994
     * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2995
     * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2996
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2997
     * <p>The value returned by this method is the same value that would be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2998
     * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2999
     * method on a {@link List} containing a sequence of {@link Long}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3000
     * instances representing the elements of <tt>a</tt> in the same order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3001
     * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3002
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3003
     * @param a the array whose hash value to compute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3004
     * @return a content-based hash code for <tt>a</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3005
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3006
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3007
    public static int hashCode(long a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3008
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3009
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3010
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3011
        int result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3012
        for (long element : a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3013
            int elementHash = (int)(element ^ (element >>> 32));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3014
            result = 31 * result + elementHash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3015
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3016
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3017
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3018
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3019
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3020
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3021
     * Returns a hash code based on the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3022
     * For any two non-null <tt>int</tt> arrays <tt>a</tt> and <tt>b</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3023
     * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3024
     * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3025
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3026
     * <p>The value returned by this method is the same value that would be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3027
     * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3028
     * method on a {@link List} containing a sequence of {@link Integer}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3029
     * instances representing the elements of <tt>a</tt> in the same order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3030
     * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3031
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3032
     * @param a the array whose hash value to compute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3033
     * @return a content-based hash code for <tt>a</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3034
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3035
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3036
    public static int hashCode(int a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3037
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3038
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3039
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3040
        int result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3041
        for (int element : a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3042
            result = 31 * result + element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3043
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3044
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3045
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3046
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3047
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3048
     * Returns a hash code based on the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3049
     * For any two <tt>short</tt> arrays <tt>a</tt> and <tt>b</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3050
     * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3051
     * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3052
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3053
     * <p>The value returned by this method is the same value that would be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3054
     * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3055
     * method on a {@link List} containing a sequence of {@link Short}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3056
     * instances representing the elements of <tt>a</tt> in the same order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3057
     * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3058
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3059
     * @param a the array whose hash value to compute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3060
     * @return a content-based hash code for <tt>a</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3061
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3062
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3063
    public static int hashCode(short a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3064
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3065
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3066
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3067
        int result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3068
        for (short element : a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3069
            result = 31 * result + element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3070
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3071
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3072
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3073
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3074
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3075
     * Returns a hash code based on the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3076
     * For any two <tt>char</tt> arrays <tt>a</tt> and <tt>b</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3077
     * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3078
     * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3079
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3080
     * <p>The value returned by this method is the same value that would be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3081
     * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3082
     * method on a {@link List} containing a sequence of {@link Character}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3083
     * instances representing the elements of <tt>a</tt> in the same order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3084
     * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3085
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3086
     * @param a the array whose hash value to compute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3087
     * @return a content-based hash code for <tt>a</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3088
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3089
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3090
    public static int hashCode(char a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3091
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3092
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3093
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3094
        int result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3095
        for (char element : a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3096
            result = 31 * result + element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3097
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3098
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3099
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3100
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3102
     * Returns a hash code based on the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3103
     * For any two <tt>byte</tt> arrays <tt>a</tt> and <tt>b</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3104
     * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3105
     * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3106
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3107
     * <p>The value returned by this method is the same value that would be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3108
     * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3109
     * method on a {@link List} containing a sequence of {@link Byte}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3110
     * instances representing the elements of <tt>a</tt> in the same order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3111
     * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3112
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3113
     * @param a the array whose hash value to compute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3114
     * @return a content-based hash code for <tt>a</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3115
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3117
    public static int hashCode(byte a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3118
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3119
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3120
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3121
        int result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3122
        for (byte element : a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3123
            result = 31 * result + element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3124
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3125
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3127
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3129
     * Returns a hash code based on the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3130
     * For any two <tt>boolean</tt> arrays <tt>a</tt> and <tt>b</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3131
     * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3132
     * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3133
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3134
     * <p>The value returned by this method is the same value that would be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3135
     * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3136
     * method on a {@link List} containing a sequence of {@link Boolean}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3137
     * instances representing the elements of <tt>a</tt> in the same order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3138
     * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3139
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3140
     * @param a the array whose hash value to compute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3141
     * @return a content-based hash code for <tt>a</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3142
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3143
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3144
    public static int hashCode(boolean a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3145
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3146
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3147
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3148
        int result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3149
        for (boolean element : a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3150
            result = 31 * result + (element ? 1231 : 1237);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3151
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3152
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3154
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3155
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3156
     * Returns a hash code based on the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3157
     * For any two <tt>float</tt> arrays <tt>a</tt> and <tt>b</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3158
     * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3159
     * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3161
     * <p>The value returned by this method is the same value that would be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3162
     * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3163
     * method on a {@link List} containing a sequence of {@link Float}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3164
     * instances representing the elements of <tt>a</tt> in the same order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3165
     * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3166
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3167
     * @param a the array whose hash value to compute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3168
     * @return a content-based hash code for <tt>a</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3169
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3171
    public static int hashCode(float a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3172
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3173
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3174
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3175
        int result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3176
        for (float element : a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3177
            result = 31 * result + Float.floatToIntBits(element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3178
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3179
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3181
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3182
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3183
     * Returns a hash code based on the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3184
     * For any two <tt>double</tt> arrays <tt>a</tt> and <tt>b</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3185
     * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3186
     * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3187
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3188
     * <p>The value returned by this method is the same value that would be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3189
     * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3190
     * method on a {@link List} containing a sequence of {@link Double}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3191
     * instances representing the elements of <tt>a</tt> in the same order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3192
     * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3193
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3194
     * @param a the array whose hash value to compute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3195
     * @return a content-based hash code for <tt>a</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3196
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3197
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3198
    public static int hashCode(double a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3199
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3200
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3201
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3202
        int result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3203
        for (double element : a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3204
            long bits = Double.doubleToLongBits(element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3205
            result = 31 * result + (int)(bits ^ (bits >>> 32));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3207
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3209
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3210
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3211
     * Returns a hash code based on the contents of the specified array.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3212
     * the array contains other arrays as elements, the hash code is based on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3213
     * their identities rather than their contents.  It is therefore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3214
     * acceptable to invoke this method on an array that contains itself as an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3215
     * element,  either directly or indirectly through one or more levels of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3216
     * arrays.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3217
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3218
     * <p>For any two arrays <tt>a</tt> and <tt>b</tt> such that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3219
     * <tt>Arrays.equals(a, b)</tt>, it is also the case that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3220
     * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3221
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3222
     * <p>The value returned by this method is equal to the value that would
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3223
     * be returned by <tt>Arrays.asList(a).hashCode()</tt>, unless <tt>a</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3224
     * is <tt>null</tt>, in which case <tt>0</tt> is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3225
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3226
     * @param a the array whose content-based hash code to compute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3227
     * @return a content-based hash code for <tt>a</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3228
     * @see #deepHashCode(Object[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3229
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3230
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3231
    public static int hashCode(Object a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3232
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3233
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3234
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3235
        int result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3236
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3237
        for (Object element : a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3238
            result = 31 * result + (element == null ? 0 : element.hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3239
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3240
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3242
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3243
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3244
     * Returns a hash code based on the "deep contents" of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3245
     * array.  If the array contains other arrays as elements, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3246
     * hash code is based on their contents and so on, ad infinitum.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3247
     * It is therefore unacceptable to invoke this method on an array that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3248
     * contains itself as an element, either directly or indirectly through
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3249
     * one or more levels of arrays.  The behavior of such an invocation is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3250
     * undefined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3251
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3252
     * <p>For any two arrays <tt>a</tt> and <tt>b</tt> such that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3253
     * <tt>Arrays.deepEquals(a, b)</tt>, it is also the case that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3254
     * <tt>Arrays.deepHashCode(a) == Arrays.deepHashCode(b)</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3255
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3256
     * <p>The computation of the value returned by this method is similar to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3257
     * that of the value returned by {@link List#hashCode()} on a list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3258
     * containing the same elements as <tt>a</tt> in the same order, with one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3259
     * difference: If an element <tt>e</tt> of <tt>a</tt> is itself an array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3260
     * its hash code is computed not by calling <tt>e.hashCode()</tt>, but as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3261
     * by calling the appropriate overloading of <tt>Arrays.hashCode(e)</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3262
     * if <tt>e</tt> is an array of a primitive type, or as by calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3263
     * <tt>Arrays.deepHashCode(e)</tt> recursively if <tt>e</tt> is an array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3264
     * of a reference type.  If <tt>a</tt> is <tt>null</tt>, this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3265
     * returns 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3266
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3267
     * @param a the array whose deep-content-based hash code to compute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3268
     * @return a deep-content-based hash code for <tt>a</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3269
     * @see #hashCode(Object[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3270
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3271
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3272
    public static int deepHashCode(Object a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3273
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3274
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3275
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3276
        int result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3277
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3278
        for (Object element : a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3279
            int elementHash = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3280
            if (element instanceof Object[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3281
                elementHash = deepHashCode((Object[]) element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3282
            else if (element instanceof byte[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3283
                elementHash = hashCode((byte[]) element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3284
            else if (element instanceof short[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3285
                elementHash = hashCode((short[]) element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3286
            else if (element instanceof int[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3287
                elementHash = hashCode((int[]) element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3288
            else if (element instanceof long[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3289
                elementHash = hashCode((long[]) element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3290
            else if (element instanceof char[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3291
                elementHash = hashCode((char[]) element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3292
            else if (element instanceof float[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3293
                elementHash = hashCode((float[]) element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3294
            else if (element instanceof double[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3295
                elementHash = hashCode((double[]) element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3296
            else if (element instanceof boolean[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3297
                elementHash = hashCode((boolean[]) element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3298
            else if (element != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3299
                elementHash = element.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3300
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3301
            result = 31 * result + elementHash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3302
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3303
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3304
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3306
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3307
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3308
     * Returns <tt>true</tt> if the two specified arrays are <i>deeply
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3309
     * equal</i> to one another.  Unlike the {@link #equals(Object[],Object[])}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3310
     * method, this method is appropriate for use with nested arrays of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3311
     * arbitrary depth.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3312
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3313
     * <p>Two array references are considered deeply equal if both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3314
     * are <tt>null</tt>, or if they refer to arrays that contain the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3315
     * number of elements and all corresponding pairs of elements in the two
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3316
     * arrays are deeply equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3317
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3318
     * <p>Two possibly <tt>null</tt> elements <tt>e1</tt> and <tt>e2</tt> are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3319
     * deeply equal if any of the following conditions hold:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3320
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3321
     *    <li> <tt>e1</tt> and <tt>e2</tt> are both arrays of object reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3322
     *         types, and <tt>Arrays.deepEquals(e1, e2) would return true</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3323
     *    <li> <tt>e1</tt> and <tt>e2</tt> are arrays of the same primitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3324
     *         type, and the appropriate overloading of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3325
     *         <tt>Arrays.equals(e1, e2)</tt> would return true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3326
     *    <li> <tt>e1 == e2</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3327
     *    <li> <tt>e1.equals(e2)</tt> would return true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3328
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3329
     * Note that this definition permits <tt>null</tt> elements at any depth.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3330
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3331
     * <p>If either of the specified arrays contain themselves as elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3332
     * either directly or indirectly through one or more levels of arrays,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3333
     * the behavior of this method is undefined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3334
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3335
     * @param a1 one array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3336
     * @param a2 the other array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3337
     * @return <tt>true</tt> if the two arrays are equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3338
     * @see #equals(Object[],Object[])
4165
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  3339
     * @see Objects#deepEquals(Object, Object)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3340
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3341
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3342
    public static boolean deepEquals(Object[] a1, Object[] a2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3343
        if (a1 == a2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3344
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3345
        if (a1 == null || a2==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3346
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3347
        int length = a1.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3348
        if (a2.length != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3349
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3350
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3351
        for (int i = 0; i < length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3352
            Object e1 = a1[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3353
            Object e2 = a2[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3354
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3355
            if (e1 == e2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3356
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3357
            if (e1 == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3358
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3359
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3360
            // Figure out whether the two elements are equal
4165
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  3361
            boolean eq = deepEquals0(e1, e2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3362
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3363
            if (!eq)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3364
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3365
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3366
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3367
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3368
4165
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  3369
    static boolean deepEquals0(Object e1, Object e2) {
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  3370
        assert e1 != null;
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  3371
        boolean eq;
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  3372
        if (e1 instanceof Object[] && e2 instanceof Object[])
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  3373
            eq = deepEquals ((Object[]) e1, (Object[]) e2);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  3374
        else if (e1 instanceof byte[] && e2 instanceof byte[])
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  3375
            eq = equals((byte[]) e1, (byte[]) e2);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  3376
        else if (e1 instanceof short[] && e2 instanceof short[])
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  3377
            eq = equals((short[]) e1, (short[]) e2);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  3378
        else if (e1 instanceof int[] && e2 instanceof int[])
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  3379
            eq = equals((int[]) e1, (int[]) e2);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  3380
        else if (e1 instanceof long[] && e2 instanceof long[])
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  3381
            eq = equals((long[]) e1, (long[]) e2);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  3382
        else if (e1 instanceof char[] && e2 instanceof char[])
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  3383
            eq = equals((char[]) e1, (char[]) e2);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  3384
        else if (e1 instanceof float[] && e2 instanceof float[])
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  3385
            eq = equals((float[]) e1, (float[]) e2);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  3386
        else if (e1 instanceof double[] && e2 instanceof double[])
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  3387
            eq = equals((double[]) e1, (double[]) e2);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  3388
        else if (e1 instanceof boolean[] && e2 instanceof boolean[])
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  3389
            eq = equals((boolean[]) e1, (boolean[]) e2);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  3390
        else
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  3391
            eq = e1.equals(e2);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  3392
        return eq;
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  3393
    }
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  3394
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3395
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3396
     * Returns a string representation of the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3397
     * The string representation consists of a list of the array's elements,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3398
     * enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3399
     * separated by the characters <tt>", "</tt> (a comma followed by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3400
     * space).  Elements are converted to strings as by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3401
     * <tt>String.valueOf(long)</tt>.  Returns <tt>"null"</tt> if <tt>a</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3402
     * is <tt>null</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3403
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3404
     * @param a the array whose string representation to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3405
     * @return a string representation of <tt>a</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3406
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3407
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3408
    public static String toString(long[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3409
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3410
            return "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3411
        int iMax = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3412
        if (iMax == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3413
            return "[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3414
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3415
        StringBuilder b = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3416
        b.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3417
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3418
            b.append(a[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3419
            if (i == iMax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3420
                return b.append(']').toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3421
            b.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3422
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3423
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3424
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3425
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3426
     * Returns a string representation of the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3427
     * The string representation consists of a list of the array's elements,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3428
     * enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3429
     * separated by the characters <tt>", "</tt> (a comma followed by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3430
     * space).  Elements are converted to strings as by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3431
     * <tt>String.valueOf(int)</tt>.  Returns <tt>"null"</tt> if <tt>a</tt> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3432
     * <tt>null</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3433
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3434
     * @param a the array whose string representation to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3435
     * @return a string representation of <tt>a</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3436
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3437
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3438
    public static String toString(int[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3439
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3440
            return "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3441
        int iMax = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3442
        if (iMax == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3443
            return "[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3444
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3445
        StringBuilder b = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3446
        b.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3447
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3448
            b.append(a[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3449
            if (i == iMax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3450
                return b.append(']').toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3451
            b.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3452
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3453
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3454
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3455
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3456
     * Returns a string representation of the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3457
     * The string representation consists of a list of the array's elements,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3458
     * enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3459
     * separated by the characters <tt>", "</tt> (a comma followed by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3460
     * space).  Elements are converted to strings as by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3461
     * <tt>String.valueOf(short)</tt>.  Returns <tt>"null"</tt> if <tt>a</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3462
     * is <tt>null</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3463
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3464
     * @param a the array whose string representation to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3465
     * @return a string representation of <tt>a</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3466
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3467
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3468
    public static String toString(short[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3469
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3470
            return "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3471
        int iMax = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3472
        if (iMax == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3473
            return "[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3474
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3475
        StringBuilder b = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3476
        b.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3477
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3478
            b.append(a[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3479
            if (i == iMax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3480
                return b.append(']').toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3481
            b.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3482
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3483
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3484
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3485
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3486
     * Returns a string representation of the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3487
     * The string representation consists of a list of the array's elements,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3488
     * enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3489
     * separated by the characters <tt>", "</tt> (a comma followed by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3490
     * space).  Elements are converted to strings as by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3491
     * <tt>String.valueOf(char)</tt>.  Returns <tt>"null"</tt> if <tt>a</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3492
     * is <tt>null</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3493
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3494
     * @param a the array whose string representation to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3495
     * @return a string representation of <tt>a</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3496
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3497
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3498
    public static String toString(char[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3499
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3500
            return "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3501
        int iMax = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3502
        if (iMax == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3503
            return "[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3504
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3505
        StringBuilder b = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3506
        b.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3507
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3508
            b.append(a[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3509
            if (i == iMax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3510
                return b.append(']').toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3511
            b.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3512
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3513
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3514
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3515
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3516
     * Returns a string representation of the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3517
     * The string representation consists of a list of the array's elements,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3518
     * enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3519
     * are separated by the characters <tt>", "</tt> (a comma followed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3520
     * by a space).  Elements are converted to strings as by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3521
     * <tt>String.valueOf(byte)</tt>.  Returns <tt>"null"</tt> if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3522
     * <tt>a</tt> is <tt>null</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3523
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3524
     * @param a the array whose string representation to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3525
     * @return a string representation of <tt>a</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3526
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3527
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3528
    public static String toString(byte[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3529
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3530
            return "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3531
        int iMax = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3532
        if (iMax == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3533
            return "[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3534
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3535
        StringBuilder b = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3536
        b.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3537
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3538
            b.append(a[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3539
            if (i == iMax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3540
                return b.append(']').toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3541
            b.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3542
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3543
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3544
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3545
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3546
     * Returns a string representation of the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3547
     * The string representation consists of a list of the array's elements,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3548
     * enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3549
     * separated by the characters <tt>", "</tt> (a comma followed by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3550
     * space).  Elements are converted to strings as by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3551
     * <tt>String.valueOf(boolean)</tt>.  Returns <tt>"null"</tt> if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3552
     * <tt>a</tt> is <tt>null</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3553
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3554
     * @param a the array whose string representation to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3555
     * @return a string representation of <tt>a</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3556
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3557
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3558
    public static String toString(boolean[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3559
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3560
            return "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3561
        int iMax = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3562
        if (iMax == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3563
            return "[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3564
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3565
        StringBuilder b = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3566
        b.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3567
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3568
            b.append(a[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3569
            if (i == iMax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3570
                return b.append(']').toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3571
            b.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3572
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3573
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3574
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3575
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3576
     * Returns a string representation of the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3577
     * The string representation consists of a list of the array's elements,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3578
     * enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3579
     * separated by the characters <tt>", "</tt> (a comma followed by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3580
     * space).  Elements are converted to strings as by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3581
     * <tt>String.valueOf(float)</tt>.  Returns <tt>"null"</tt> if <tt>a</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3582
     * is <tt>null</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3583
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3584
     * @param a the array whose string representation to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3585
     * @return a string representation of <tt>a</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3586
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3587
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3588
    public static String toString(float[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3589
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3590
            return "null";
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
  3591
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3592
        int iMax = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3593
        if (iMax == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3594
            return "[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3595
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3596
        StringBuilder b = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3597
        b.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3598
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3599
            b.append(a[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3600
            if (i == iMax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3601
                return b.append(']').toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3602
            b.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3603
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3604
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3605
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3606
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3607
     * Returns a string representation of the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3608
     * The string representation consists of a list of the array's elements,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3609
     * enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3610
     * separated by the characters <tt>", "</tt> (a comma followed by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3611
     * space).  Elements are converted to strings as by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3612
     * <tt>String.valueOf(double)</tt>.  Returns <tt>"null"</tt> if <tt>a</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3613
     * is <tt>null</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3614
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3615
     * @param a the array whose string representation to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3616
     * @return a string representation of <tt>a</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3617
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3618
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3619
    public static String toString(double[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3620
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3621
            return "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3622
        int iMax = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3623
        if (iMax == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3624
            return "[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3625
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3626
        StringBuilder b = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3627
        b.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3628
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3629
            b.append(a[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3630
            if (i == iMax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3631
                return b.append(']').toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3632
            b.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3633
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3634
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3635
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3636
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3637
     * Returns a string representation of the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3638
     * If the array contains other arrays as elements, they are converted to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3639
     * strings by the {@link Object#toString} method inherited from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3640
     * <tt>Object</tt>, which describes their <i>identities</i> rather than
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3641
     * their contents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3642
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3643
     * <p>The value returned by this method is equal to the value that would
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3644
     * be returned by <tt>Arrays.asList(a).toString()</tt>, unless <tt>a</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3645
     * is <tt>null</tt>, in which case <tt>"null"</tt> is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3646
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3647
     * @param a the array whose string representation to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3648
     * @return a string representation of <tt>a</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3649
     * @see #deepToString(Object[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3650
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3651
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3652
    public static String toString(Object[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3653
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3654
            return "null";
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
  3655
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3656
        int iMax = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3657
        if (iMax == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3658
            return "[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3659
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3660
        StringBuilder b = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3661
        b.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3662
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3663
            b.append(String.valueOf(a[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3664
            if (i == iMax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3665
                return b.append(']').toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3666
            b.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3667
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3668
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3669
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3670
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3671
     * Returns a string representation of the "deep contents" of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3672
     * array.  If the array contains other arrays as elements, the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3673
     * representation contains their contents and so on.  This method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3674
     * designed for converting multidimensional arrays to strings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3675
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3676
     * <p>The string representation consists of a list of the array's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3677
     * elements, enclosed in square brackets (<tt>"[]"</tt>).  Adjacent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3678
     * elements are separated by the characters <tt>", "</tt> (a comma
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3679
     * followed by a space).  Elements are converted to strings as by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3680
     * <tt>String.valueOf(Object)</tt>, unless they are themselves
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3681
     * arrays.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3682
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3683
     * <p>If an element <tt>e</tt> is an array of a primitive type, it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3684
     * converted to a string as by invoking the appropriate overloading of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3685
     * <tt>Arrays.toString(e)</tt>.  If an element <tt>e</tt> is an array of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3686
     * reference type, it is converted to a string as by invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3687
     * this method recursively.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3688
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3689
     * <p>To avoid infinite recursion, if the specified array contains itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3690
     * as an element, or contains an indirect reference to itself through one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3691
     * or more levels of arrays, the self-reference is converted to the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3692
     * <tt>"[...]"</tt>.  For example, an array containing only a reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3693
     * to itself would be rendered as <tt>"[[...]]"</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3694
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3695
     * <p>This method returns <tt>"null"</tt> if the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3696
     * is <tt>null</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3697
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3698
     * @param a the array whose string representation to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3699
     * @return a string representation of <tt>a</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3700
     * @see #toString(Object[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3701
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3702
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3703
    public static String deepToString(Object[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3704
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3705
            return "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3706
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3707
        int bufLen = 20 * a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3708
        if (a.length != 0 && bufLen <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3709
            bufLen = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3710
        StringBuilder buf = new StringBuilder(bufLen);
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  3711
        deepToString(a, buf, new HashSet<Object[]>());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3712
        return buf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3713
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3714
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3715
    private static void deepToString(Object[] a, StringBuilder buf,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3716
                                     Set<Object[]> dejaVu) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3717
        if (a == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3718
            buf.append("null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3719
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3720
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3721
        int iMax = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3722
        if (iMax == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3723
            buf.append("[]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3724
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3725
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3726
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3727
        dejaVu.add(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3728
        buf.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3729
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3730
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3731
            Object element = a[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3732
            if (element == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3733
                buf.append("null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3734
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3735
                Class eClass = element.getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3736
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3737
                if (eClass.isArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3738
                    if (eClass == byte[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3739
                        buf.append(toString((byte[]) element));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3740
                    else if (eClass == short[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3741
                        buf.append(toString((short[]) element));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3742
                    else if (eClass == int[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3743
                        buf.append(toString((int[]) element));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3744
                    else if (eClass == long[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3745
                        buf.append(toString((long[]) element));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3746
                    else if (eClass == char[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3747
                        buf.append(toString((char[]) element));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3748
                    else if (eClass == float[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3749
                        buf.append(toString((float[]) element));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3750
                    else if (eClass == double[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3751
                        buf.append(toString((double[]) element));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3752
                    else if (eClass == boolean[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3753
                        buf.append(toString((boolean[]) element));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3754
                    else { // element is an array of object references
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3755
                        if (dejaVu.contains(element))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3756
                            buf.append("[...]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3757
                        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3758
                            deepToString((Object[])element, buf, dejaVu);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3759
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3760
                } else {  // element is non-null and not an array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3761
                    buf.append(element.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3762
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3763
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3764
            if (i == iMax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3765
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3766
            buf.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3767
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3768
        buf.append(']');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3769
        dejaVu.remove(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3770
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3771
}