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