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