jdk/src/share/classes/java/util/Arrays.java
author igerasim
Sat, 08 Feb 2014 00:22:33 +0400
changeset 22954 617b9c3a2c65
parent 22285 6c67737a5ac0
child 23354 221bf0e3642d
permissions -rw-r--r--
8033943: Typo in the documentation for the class Arrays Reviewed-by: martin
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22285
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
     2
 * Copyright (c) 1997, 2014, 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
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    28
import java.lang.reflect.Array;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    29
import java.util.concurrent.ForkJoinPool;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    30
import java.util.function.BinaryOperator;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    31
import java.util.function.DoubleBinaryOperator;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    32
import java.util.function.IntBinaryOperator;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    33
import java.util.function.IntFunction;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    34
import java.util.function.IntToDoubleFunction;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    35
import java.util.function.IntToLongFunction;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    36
import java.util.function.IntUnaryOperator;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    37
import java.util.function.LongBinaryOperator;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    38
import java.util.stream.DoubleStream;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    39
import java.util.stream.IntStream;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    40
import java.util.stream.LongStream;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    41
import java.util.stream.Stream;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    42
import java.util.stream.StreamSupport;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * 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
    46
 * sorting and searching). This class also contains a static factory
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * that allows arrays to be viewed as lists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    49
 * <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
    50
 * if the specified array reference is null, except where noted.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <p>The documentation for the methods contained in this class includes
22954
617b9c3a2c65 8033943: Typo in the documentation for the class Arrays
igerasim
parents: 22285
diff changeset
    53
 * brief descriptions of the <i>implementations</i>. Such descriptions should
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * 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
    55
 * <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
    56
 * 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
    57
 * 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
    58
 * a MergeSort, but it does have to be <i>stable</i>.)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <p>This class is a member of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    64
 * @author Josh Bloch
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    65
 * @author Neal Gafter
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    66
 * @author John Rose
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    67
 * @since  1.2
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 */
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    69
public class Arrays {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
14925
72729557c226 8003981: Support Parallel Array Sorting - JEP 103
chegar
parents: 13795
diff changeset
    71
    /**
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    72
     * The minimum array length below which a parallel sorting
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    73
     * algorithm will not further partition the sorting task. Using
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    74
     * smaller sizes typically results in memory contention across
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    75
     * tasks that makes parallel speedups unlikely.
14925
72729557c226 8003981: Support Parallel Array Sorting - JEP 103
chegar
parents: 13795
diff changeset
    76
     */
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    77
    private static final int MIN_ARRAY_SORT_GRAN = 1 << 13;
14925
72729557c226 8003981: Support Parallel Array Sorting - JEP 103
chegar
parents: 13795
diff changeset
    78
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    // Suppresses default constructor, ensuring non-instantiability.
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    80
    private Arrays() {}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    82
    /**
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    83
     * A comparator that implements the natural ordering of a group of
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    84
     * mutually comparable elements. May be used when a supplied
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    85
     * comparator is null. To simplify code-sharing within underlying
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    86
     * implementations, the compare method only declares type Object
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    87
     * for its second argument.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    88
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    89
     * Arrays class implementor's note: It is an empirical matter
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    90
     * whether ComparableTimSort offers any performance benefit over
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    91
     * TimSort used with this comparator.  If not, you are better off
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    92
     * deleting or bypassing ComparableTimSort.  There is currently no
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    93
     * empirical case for separating them for parallel sorting, so all
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    94
     * public Object parallelSort methods use the same comparator
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    95
     * based implementation.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    96
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    97
    static final class NaturalOrder implements Comparator<Object> {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    98
        @SuppressWarnings("unchecked")
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    99
        public int compare(Object first, Object second) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   100
            return ((Comparable<Object>)first).compareTo(second);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   101
        }
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   102
        static final NaturalOrder INSTANCE = new NaturalOrder();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   103
    }
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   104
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   105
    /**
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   106
     * Checks that {@code fromIndex} and {@code toIndex} are in
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   107
     * the range and throws an exception if they aren't.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   108
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   109
    private static void rangeCheck(int arrayLength, int fromIndex, int toIndex) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   110
        if (fromIndex > toIndex) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   111
            throw new IllegalArgumentException(
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   112
                    "fromIndex(" + fromIndex + ") > toIndex(" + toIndex + ")");
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   113
        }
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   114
        if (fromIndex < 0) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   115
            throw new ArrayIndexOutOfBoundsException(fromIndex);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   116
        }
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   117
        if (toIndex > arrayLength) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   118
            throw new ArrayIndexOutOfBoundsException(toIndex);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   119
        }
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   120
    }
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   121
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   122
    /*
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   123
     * Sorting methods. Note that all public "sort" methods take the
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   124
     * same form: Performing argument checks if necessary, and then
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   125
     * expanding arguments into those required for the internal
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   126
     * implementation methods residing in other package-private
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   127
     * classes (except for legacyMergeSort, included in this class).
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   128
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /**
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   131
     * 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
   132
     *
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   133
     * <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
   134
     * 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
   135
     * 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
   136
     * 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
   137
     * faster than traditional (one-pivot) Quicksort implementations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @param a the array to be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    public static void sort(int[] a) {
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   142
        DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /**
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   146
     * Sorts the specified range of the array into ascending order. The range
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   147
     * to be sorted extends from the index {@code fromIndex}, inclusive, to
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   148
     * 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
   149
     * the range to be sorted is empty.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   151
     * <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
   152
     * 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
   153
     * 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
   154
     * 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
   155
     * faster than traditional (one-pivot) Quicksort implementations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @param a the array to be sorted
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   158
     * @param fromIndex the index of the first element, inclusive, to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   159
     * @param toIndex the index of the last element, exclusive, to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   160
     *
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   161
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   162
     * @throws ArrayIndexOutOfBoundsException
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   163
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    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
   166
        rangeCheck(a.length, fromIndex, toIndex);
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   167
        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    /**
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   171
     * 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
   172
     *
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   173
     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   174
     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   175
     * offers O(n log(n)) performance on many data sets that cause other
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   176
     * quicksorts to degrade to quadratic performance, and is typically
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   177
     * faster than traditional (one-pivot) Quicksort implementations.
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   178
     *
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   179
     * @param a the array to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   180
     */
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   181
    public static void sort(long[] a) {
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   182
        DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   183
    }
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   184
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   185
    /**
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   186
     * Sorts the specified range of the array into ascending order. The range
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   187
     * to be sorted extends from the index {@code fromIndex}, inclusive, to
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   188
     * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   189
     * the range to be sorted is empty.
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   190
     *
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   191
     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   192
     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   193
     * offers O(n log(n)) performance on many data sets that cause other
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   194
     * quicksorts to degrade to quadratic performance, and is typically
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   195
     * faster than traditional (one-pivot) Quicksort implementations.
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   196
     *
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   197
     * @param a the array to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   198
     * @param fromIndex the index of the first element, inclusive, to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   199
     * @param toIndex the index of the last element, exclusive, to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   200
     *
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   201
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   202
     * @throws ArrayIndexOutOfBoundsException
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   203
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   204
     */
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   205
    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
   206
        rangeCheck(a.length, fromIndex, toIndex);
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   207
        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   208
    }
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   209
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   210
    /**
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   211
     * Sorts the specified array into ascending numerical order.
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   212
     *
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   213
     * <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
   214
     * 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
   215
     * 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
   216
     * 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
   217
     * faster than traditional (one-pivot) Quicksort implementations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @param a the array to be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    public static void sort(short[] a) {
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   222
        DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    /**
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   226
     * Sorts the specified range of the array into ascending order. The range
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   227
     * to be sorted extends from the index {@code fromIndex}, inclusive, to
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   228
     * 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
   229
     * the range to be sorted is empty.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   231
     * <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
   232
     * 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
   233
     * 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
   234
     * 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
   235
     * faster than traditional (one-pivot) Quicksort implementations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * @param a the array to be sorted
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   238
     * @param fromIndex the index of the first element, inclusive, to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   239
     * @param toIndex the index of the last element, exclusive, to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   240
     *
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   241
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   242
     * @throws ArrayIndexOutOfBoundsException
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   243
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    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
   246
        rangeCheck(a.length, fromIndex, toIndex);
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   247
        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    /**
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   251
     * 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
   252
     *
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   253
     * <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
   254
     * 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
   255
     * 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
   256
     * 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
   257
     * faster than traditional (one-pivot) Quicksort implementations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * @param a the array to be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    public static void sort(char[] a) {
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   262
        DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    /**
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   266
     * Sorts the specified range of the array into ascending order. The range
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   267
     * to be sorted extends from the index {@code fromIndex}, inclusive, to
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   268
     * 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
   269
     * the range to be sorted is empty.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   271
     * <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
   272
     * 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
   273
     * 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
   274
     * 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
   275
     * faster than traditional (one-pivot) Quicksort implementations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * @param a the array to be sorted
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   278
     * @param fromIndex the index of the first element, inclusive, to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   279
     * @param toIndex the index of the last element, exclusive, to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   280
     *
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   281
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   282
     * @throws ArrayIndexOutOfBoundsException
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   283
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    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
   286
        rangeCheck(a.length, fromIndex, toIndex);
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   287
        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    /**
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   291
     * 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
   292
     *
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   293
     * <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
   294
     * 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
   295
     * 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
   296
     * 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
   297
     * faster than traditional (one-pivot) Quicksort implementations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * @param a the array to be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    public static void sort(byte[] a) {
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   302
        DualPivotQuicksort.sort(a, 0, a.length - 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    /**
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   306
     * Sorts the specified range of the array into ascending order. The range
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   307
     * to be sorted extends from the index {@code fromIndex}, inclusive, to
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   308
     * 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
   309
     * the range to be sorted is empty.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     *
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   311
     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   312
     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   313
     * offers O(n log(n)) performance on many data sets that cause other
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   314
     * quicksorts to degrade to quadratic performance, and is typically
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   315
     * faster than traditional (one-pivot) Quicksort implementations.
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   316
     *
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   317
     * @param a the array to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   318
     * @param fromIndex the index of the first element, inclusive, to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   319
     * @param toIndex the index of the last element, exclusive, to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   320
     *
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   321
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   322
     * @throws ArrayIndexOutOfBoundsException
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   323
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   324
     */
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   325
    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
   326
        rangeCheck(a.length, fromIndex, toIndex);
d229d56fd918 6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents: 5506
diff changeset
   327
        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1);
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   328
    }
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   329
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   330
    /**
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   331
     * Sorts the specified array into ascending numerical order.
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   332
     *
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   333
     * <p>The {@code <} relation does not provide a total order on all float
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   334
     * 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
   335
     * value compares neither less than, greater than, nor equal to any value,
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   336
     * even itself. This method uses the total order imposed by the method
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   337
     * {@link Float#compareTo}: {@code -0.0f} is treated as less than value
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   338
     * {@code 0.0f} and {@code Float.NaN} is considered greater than any
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   339
     * other value and all {@code Float.NaN} values are considered equal.
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   340
     *
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   341
     * <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
   342
     * 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
   343
     * 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
   344
     * 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
   345
     * faster than traditional (one-pivot) Quicksort implementations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * @param a the array to be sorted
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   348
     */
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   349
    public static void sort(float[] a) {
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   350
        DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   351
    }
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   352
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   353
    /**
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   354
     * Sorts the specified range of the array into ascending order. The range
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   355
     * to be sorted extends from the index {@code fromIndex}, inclusive, to
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   356
     * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   357
     * the range to be sorted is empty.
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   358
     *
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   359
     * <p>The {@code <} relation does not provide a total order on all float
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   360
     * 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
   361
     * value compares neither less than, greater than, nor equal to any value,
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   362
     * even itself. This method uses the total order imposed by the method
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   363
     * {@link Float#compareTo}: {@code -0.0f} is treated as less than value
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   364
     * {@code 0.0f} and {@code Float.NaN} is considered greater than any
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   365
     * other value and all {@code Float.NaN} values are considered equal.
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   366
     *
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   367
     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   368
     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   369
     * offers O(n log(n)) performance on many data sets that cause other
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   370
     * quicksorts to degrade to quadratic performance, and is typically
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   371
     * faster than traditional (one-pivot) Quicksort implementations.
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   372
     *
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   373
     * @param a the array to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   374
     * @param fromIndex the index of the first element, inclusive, to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   375
     * @param toIndex the index of the last element, exclusive, to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   376
     *
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   377
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   378
     * @throws ArrayIndexOutOfBoundsException
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   379
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     */
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   381
    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
   382
        rangeCheck(a.length, fromIndex, toIndex);
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   383
        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    /**
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   387
     * 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
   388
     *
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   389
     * <p>The {@code <} relation does not provide a total order on all double
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   390
     * 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
   391
     * value compares neither less than, greater than, nor equal to any value,
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   392
     * even itself. This method uses the total order imposed by the method
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   393
     * {@link Double#compareTo}: {@code -0.0d} is treated as less than value
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   394
     * {@code 0.0d} and {@code Double.NaN} is considered greater than any
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   395
     * 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
   396
     *
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   397
     * <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
   398
     * 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
   399
     * 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
   400
     * 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
   401
     * faster than traditional (one-pivot) Quicksort implementations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * @param a the array to be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    public static void sort(double[] a) {
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   406
        DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    /**
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   410
     * Sorts the specified range of the array into ascending order. The range
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   411
     * to be sorted extends from the index {@code fromIndex}, inclusive, to
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   412
     * 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
   413
     * the range to be sorted is empty.
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   414
     *
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   415
     * <p>The {@code <} relation does not provide a total order on all double
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   416
     * 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
   417
     * value compares neither less than, greater than, nor equal to any value,
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   418
     * even itself. This method uses the total order imposed by the method
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   419
     * {@link Double#compareTo}: {@code -0.0d} is treated as less than value
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   420
     * {@code 0.0d} and {@code Double.NaN} is considered greater than any
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   421
     * 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
   422
     *
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   423
     * <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
   424
     * 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
   425
     * 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
   426
     * 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
   427
     * faster than traditional (one-pivot) Quicksort implementations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * @param a the array to be sorted
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   430
     * @param fromIndex the index of the first element, inclusive, to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   431
     * @param toIndex the index of the last element, exclusive, to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   432
     *
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   433
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   434
     * @throws ArrayIndexOutOfBoundsException
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   435
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     */
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   437
    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
   438
        rangeCheck(a.length, fromIndex, toIndex);
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   439
        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   440
    }
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   441
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   442
    /**
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   443
     * Sorts the specified array into ascending numerical order.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   444
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   445
     * @implNote The sorting algorithm is a parallel sort-merge that breaks the
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   446
     * array into sub-arrays that are themselves sorted and then merged. When
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   447
     * the sub-array length reaches a minimum granularity, the sub-array is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   448
     * sorted using the appropriate {@link Arrays#sort(byte[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   449
     * method. If the length of the specified array is less than the minimum
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   450
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   451
     * Arrays#sort(byte[]) Arrays.sort} method. The algorithm requires a
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   452
     * working space no greater than the size of the original array. The
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   453
     * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   454
     * execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   455
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   456
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   457
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   458
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   459
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   460
    public static void parallelSort(byte[] a) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   461
        int n = a.length, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   462
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   463
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   464
            DualPivotQuicksort.sort(a, 0, n - 1);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   465
        else
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   466
            new ArraysParallelSortHelpers.FJByte.Sorter
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   467
                (null, a, new byte[n], 0, n, 0,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   468
                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   469
                 MIN_ARRAY_SORT_GRAN : g).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   470
    }
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   471
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   472
    /**
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   473
     * Sorts the specified range of the array into ascending numerical order.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   474
     * The range to be sorted extends from the index {@code fromIndex},
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   475
     * inclusive, to the index {@code toIndex}, exclusive. If
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   476
     * {@code fromIndex == toIndex}, the range to be sorted is empty.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   477
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   478
     * @implNote The sorting algorithm is a parallel sort-merge that breaks the
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   479
     * array into sub-arrays that are themselves sorted and then merged. When
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   480
     * the sub-array length reaches a minimum granularity, the sub-array is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   481
     * sorted using the appropriate {@link Arrays#sort(byte[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   482
     * method. If the length of the specified array is less than the minimum
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   483
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   484
     * Arrays#sort(byte[]) Arrays.sort} method. The algorithm requires a working
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   485
     * space no greater than the size of the specified range of the original
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   486
     * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   487
     * used to execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   488
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   489
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   490
     * @param fromIndex the index of the first element, inclusive, to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   491
     * @param toIndex the index of the last element, exclusive, to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   492
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   493
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   494
     * @throws ArrayIndexOutOfBoundsException
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   495
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   496
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   497
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   498
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   499
    public static void parallelSort(byte[] a, int fromIndex, int toIndex) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   500
        rangeCheck(a.length, fromIndex, toIndex);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   501
        int n = toIndex - fromIndex, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   502
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   503
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   504
            DualPivotQuicksort.sort(a, fromIndex, toIndex - 1);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   505
        else
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   506
            new ArraysParallelSortHelpers.FJByte.Sorter
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   507
                (null, a, new byte[n], fromIndex, n, 0,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   508
                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   509
                 MIN_ARRAY_SORT_GRAN : g).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   510
    }
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   511
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   512
    /**
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   513
     * Sorts the specified array into ascending numerical order.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   514
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   515
     * @implNote The sorting algorithm is a parallel sort-merge that breaks the
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   516
     * array into sub-arrays that are themselves sorted and then merged. When
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   517
     * the sub-array length reaches a minimum granularity, the sub-array is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   518
     * sorted using the appropriate {@link Arrays#sort(char[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   519
     * method. If the length of the specified array is less than the minimum
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   520
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   521
     * Arrays#sort(char[]) Arrays.sort} method. The algorithm requires a
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   522
     * working space no greater than the size of the original array. The
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   523
     * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   524
     * execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   525
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   526
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   527
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   528
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   529
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   530
    public static void parallelSort(char[] a) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   531
        int n = a.length, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   532
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   533
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   534
            DualPivotQuicksort.sort(a, 0, n - 1, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   535
        else
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   536
            new ArraysParallelSortHelpers.FJChar.Sorter
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   537
                (null, a, new char[n], 0, n, 0,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   538
                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   539
                 MIN_ARRAY_SORT_GRAN : g).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   540
    }
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   541
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   542
    /**
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   543
     * Sorts the specified range of the array into ascending numerical order.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   544
     * The range to be sorted extends from the index {@code fromIndex},
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   545
     * inclusive, to the index {@code toIndex}, exclusive. If
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   546
     * {@code fromIndex == toIndex}, the range to be sorted is empty.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   547
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   548
      @implNote The sorting algorithm is a parallel sort-merge that breaks the
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   549
     * array into sub-arrays that are themselves sorted and then merged. When
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   550
     * the sub-array length reaches a minimum granularity, the sub-array is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   551
     * sorted using the appropriate {@link Arrays#sort(char[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   552
     * method. If the length of the specified array is less than the minimum
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   553
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   554
     * Arrays#sort(char[]) Arrays.sort} method. The algorithm requires a working
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   555
     * space no greater than the size of the specified range of the original
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   556
     * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   557
     * used to execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   558
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   559
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   560
     * @param fromIndex the index of the first element, inclusive, to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   561
     * @param toIndex the index of the last element, exclusive, to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   562
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   563
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   564
     * @throws ArrayIndexOutOfBoundsException
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   565
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   566
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   567
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   568
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   569
    public static void parallelSort(char[] a, int fromIndex, int toIndex) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   570
        rangeCheck(a.length, fromIndex, toIndex);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   571
        int n = toIndex - fromIndex, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   572
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   573
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   574
            DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   575
        else
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   576
            new ArraysParallelSortHelpers.FJChar.Sorter
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   577
                (null, a, new char[n], fromIndex, n, 0,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   578
                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   579
                 MIN_ARRAY_SORT_GRAN : g).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   580
    }
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   581
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   582
    /**
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   583
     * Sorts the specified array into ascending numerical order.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   584
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   585
     * @implNote The sorting algorithm is a parallel sort-merge that breaks the
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   586
     * array into sub-arrays that are themselves sorted and then merged. When
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   587
     * the sub-array length reaches a minimum granularity, the sub-array is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   588
     * sorted using the appropriate {@link Arrays#sort(short[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   589
     * method. If the length of the specified array is less than the minimum
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   590
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   591
     * Arrays#sort(short[]) Arrays.sort} method. The algorithm requires a
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   592
     * working space no greater than the size of the original array. The
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   593
     * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   594
     * execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   595
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   596
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   597
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   598
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   599
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   600
    public static void parallelSort(short[] a) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   601
        int n = a.length, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   602
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   603
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   604
            DualPivotQuicksort.sort(a, 0, n - 1, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   605
        else
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   606
            new ArraysParallelSortHelpers.FJShort.Sorter
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   607
                (null, a, new short[n], 0, n, 0,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   608
                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   609
                 MIN_ARRAY_SORT_GRAN : g).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   610
    }
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   611
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   612
    /**
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   613
     * Sorts the specified range of the array into ascending numerical order.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   614
     * The range to be sorted extends from the index {@code fromIndex},
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   615
     * inclusive, to the index {@code toIndex}, exclusive. If
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   616
     * {@code fromIndex == toIndex}, the range to be sorted is empty.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   617
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   618
     * @implNote The sorting algorithm is a parallel sort-merge that breaks the
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   619
     * array into sub-arrays that are themselves sorted and then merged. When
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   620
     * the sub-array length reaches a minimum granularity, the sub-array is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   621
     * sorted using the appropriate {@link Arrays#sort(short[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   622
     * method. If the length of the specified array is less than the minimum
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   623
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   624
     * Arrays#sort(short[]) Arrays.sort} method. The algorithm requires a working
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   625
     * space no greater than the size of the specified range of the original
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   626
     * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   627
     * used to execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   628
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   629
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   630
     * @param fromIndex the index of the first element, inclusive, to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   631
     * @param toIndex the index of the last element, exclusive, to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   632
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   633
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   634
     * @throws ArrayIndexOutOfBoundsException
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   635
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   636
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   637
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   638
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   639
    public static void parallelSort(short[] a, int fromIndex, int toIndex) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   640
        rangeCheck(a.length, fromIndex, toIndex);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   641
        int n = toIndex - fromIndex, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   642
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   643
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   644
            DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   645
        else
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   646
            new ArraysParallelSortHelpers.FJShort.Sorter
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   647
                (null, a, new short[n], fromIndex, n, 0,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   648
                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   649
                 MIN_ARRAY_SORT_GRAN : g).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   650
    }
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   651
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   652
    /**
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   653
     * Sorts the specified array into ascending numerical order.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   654
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   655
     * @implNote The sorting algorithm is a parallel sort-merge that breaks the
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   656
     * array into sub-arrays that are themselves sorted and then merged. When
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   657
     * the sub-array length reaches a minimum granularity, the sub-array is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   658
     * sorted using the appropriate {@link Arrays#sort(int[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   659
     * method. If the length of the specified array is less than the minimum
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   660
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   661
     * Arrays#sort(int[]) Arrays.sort} method. The algorithm requires a
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   662
     * working space no greater than the size of the original array. The
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   663
     * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   664
     * execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   665
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   666
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   667
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   668
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   669
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   670
    public static void parallelSort(int[] a) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   671
        int n = a.length, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   672
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   673
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   674
            DualPivotQuicksort.sort(a, 0, n - 1, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   675
        else
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   676
            new ArraysParallelSortHelpers.FJInt.Sorter
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   677
                (null, a, new int[n], 0, n, 0,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   678
                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   679
                 MIN_ARRAY_SORT_GRAN : g).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   680
    }
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   681
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   682
    /**
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   683
     * Sorts the specified range of the array into ascending numerical order.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   684
     * The range to be sorted extends from the index {@code fromIndex},
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   685
     * inclusive, to the index {@code toIndex}, exclusive. If
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   686
     * {@code fromIndex == toIndex}, the range to be sorted is empty.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   687
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   688
     * @implNote The sorting algorithm is a parallel sort-merge that breaks the
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   689
     * array into sub-arrays that are themselves sorted and then merged. When
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   690
     * the sub-array length reaches a minimum granularity, the sub-array is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   691
     * sorted using the appropriate {@link Arrays#sort(int[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   692
     * method. If the length of the specified array is less than the minimum
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   693
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   694
     * Arrays#sort(int[]) Arrays.sort} method. The algorithm requires a working
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   695
     * space no greater than the size of the specified range of the original
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   696
     * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   697
     * used to execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   698
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   699
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   700
     * @param fromIndex the index of the first element, inclusive, to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   701
     * @param toIndex the index of the last element, exclusive, to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   702
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   703
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   704
     * @throws ArrayIndexOutOfBoundsException
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   705
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   706
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   707
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   708
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   709
    public static void parallelSort(int[] a, int fromIndex, int toIndex) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   710
        rangeCheck(a.length, fromIndex, toIndex);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   711
        int n = toIndex - fromIndex, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   712
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   713
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   714
            DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   715
        else
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   716
            new ArraysParallelSortHelpers.FJInt.Sorter
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   717
                (null, a, new int[n], fromIndex, n, 0,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   718
                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   719
                 MIN_ARRAY_SORT_GRAN : g).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   720
    }
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   721
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   722
    /**
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   723
     * Sorts the specified array into ascending numerical order.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   724
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   725
     * @implNote The sorting algorithm is a parallel sort-merge that breaks the
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   726
     * array into sub-arrays that are themselves sorted and then merged. When
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   727
     * the sub-array length reaches a minimum granularity, the sub-array is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   728
     * sorted using the appropriate {@link Arrays#sort(long[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   729
     * method. If the length of the specified array is less than the minimum
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   730
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   731
     * Arrays#sort(long[]) Arrays.sort} method. The algorithm requires a
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   732
     * working space no greater than the size of the original array. The
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   733
     * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   734
     * execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   735
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   736
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   737
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   738
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   739
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   740
    public static void parallelSort(long[] a) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   741
        int n = a.length, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   742
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   743
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   744
            DualPivotQuicksort.sort(a, 0, n - 1, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   745
        else
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   746
            new ArraysParallelSortHelpers.FJLong.Sorter
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   747
                (null, a, new long[n], 0, n, 0,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   748
                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   749
                 MIN_ARRAY_SORT_GRAN : g).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   750
    }
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   751
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   752
    /**
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   753
     * Sorts the specified range of the array into ascending numerical order.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   754
     * The range to be sorted extends from the index {@code fromIndex},
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   755
     * inclusive, to the index {@code toIndex}, exclusive. If
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   756
     * {@code fromIndex == toIndex}, the range to be sorted is empty.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   757
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   758
     * @implNote The sorting algorithm is a parallel sort-merge that breaks the
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   759
     * array into sub-arrays that are themselves sorted and then merged. When
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   760
     * the sub-array length reaches a minimum granularity, the sub-array is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   761
     * sorted using the appropriate {@link Arrays#sort(long[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   762
     * method. If the length of the specified array is less than the minimum
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   763
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   764
     * Arrays#sort(long[]) Arrays.sort} method. The algorithm requires a working
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   765
     * space no greater than the size of the specified range of the original
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   766
     * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   767
     * used to execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   768
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   769
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   770
     * @param fromIndex the index of the first element, inclusive, to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   771
     * @param toIndex the index of the last element, exclusive, to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   772
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   773
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   774
     * @throws ArrayIndexOutOfBoundsException
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   775
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   776
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   777
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   778
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   779
    public static void parallelSort(long[] a, int fromIndex, int toIndex) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   780
        rangeCheck(a.length, fromIndex, toIndex);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   781
        int n = toIndex - fromIndex, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   782
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   783
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   784
            DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   785
        else
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   786
            new ArraysParallelSortHelpers.FJLong.Sorter
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   787
                (null, a, new long[n], fromIndex, n, 0,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   788
                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   789
                 MIN_ARRAY_SORT_GRAN : g).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   790
    }
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   791
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   792
    /**
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   793
     * Sorts the specified array into ascending numerical order.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   794
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   795
     * <p>The {@code <} relation does not provide a total order on all float
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   796
     * values: {@code -0.0f == 0.0f} is {@code true} and a {@code Float.NaN}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   797
     * value compares neither less than, greater than, nor equal to any value,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   798
     * even itself. This method uses the total order imposed by the method
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   799
     * {@link Float#compareTo}: {@code -0.0f} is treated as less than value
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   800
     * {@code 0.0f} and {@code Float.NaN} is considered greater than any
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   801
     * other value and all {@code Float.NaN} values are considered equal.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   802
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   803
     * @implNote The sorting algorithm is a parallel sort-merge that breaks the
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   804
     * array into sub-arrays that are themselves sorted and then merged. When
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   805
     * the sub-array length reaches a minimum granularity, the sub-array is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   806
     * sorted using the appropriate {@link Arrays#sort(float[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   807
     * method. If the length of the specified array is less than the minimum
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   808
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   809
     * Arrays#sort(float[]) Arrays.sort} method. The algorithm requires a
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   810
     * working space no greater than the size of the original array. The
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   811
     * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   812
     * execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   813
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   814
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   815
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   816
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   817
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   818
    public static void parallelSort(float[] a) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   819
        int n = a.length, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   820
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   821
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   822
            DualPivotQuicksort.sort(a, 0, n - 1, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   823
        else
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   824
            new ArraysParallelSortHelpers.FJFloat.Sorter
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   825
                (null, a, new float[n], 0, n, 0,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   826
                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   827
                 MIN_ARRAY_SORT_GRAN : g).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   828
    }
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   829
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   830
    /**
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   831
     * Sorts the specified range of the array into ascending numerical order.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   832
     * The range to be sorted extends from the index {@code fromIndex},
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   833
     * inclusive, to the index {@code toIndex}, exclusive. If
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   834
     * {@code fromIndex == toIndex}, the range to be sorted is empty.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   835
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   836
     * <p>The {@code <} relation does not provide a total order on all float
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   837
     * values: {@code -0.0f == 0.0f} is {@code true} and a {@code Float.NaN}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   838
     * value compares neither less than, greater than, nor equal to any value,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   839
     * even itself. This method uses the total order imposed by the method
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   840
     * {@link Float#compareTo}: {@code -0.0f} is treated as less than value
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   841
     * {@code 0.0f} and {@code Float.NaN} is considered greater than any
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   842
     * other value and all {@code Float.NaN} values are considered equal.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   843
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   844
     * @implNote The sorting algorithm is a parallel sort-merge that breaks the
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   845
     * array into sub-arrays that are themselves sorted and then merged. When
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   846
     * the sub-array length reaches a minimum granularity, the sub-array is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   847
     * sorted using the appropriate {@link Arrays#sort(float[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   848
     * method. If the length of the specified array is less than the minimum
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   849
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   850
     * Arrays#sort(float[]) Arrays.sort} method. The algorithm requires a working
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   851
     * space no greater than the size of the specified range of the original
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   852
     * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   853
     * used to execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   854
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   855
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   856
     * @param fromIndex the index of the first element, inclusive, to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   857
     * @param toIndex the index of the last element, exclusive, to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   858
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   859
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   860
     * @throws ArrayIndexOutOfBoundsException
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   861
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   862
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   863
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   864
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   865
    public static void parallelSort(float[] a, int fromIndex, int toIndex) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   866
        rangeCheck(a.length, fromIndex, toIndex);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   867
        int n = toIndex - fromIndex, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   868
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   869
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   870
            DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   871
        else
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   872
            new ArraysParallelSortHelpers.FJFloat.Sorter
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   873
                (null, a, new float[n], fromIndex, n, 0,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   874
                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   875
                 MIN_ARRAY_SORT_GRAN : g).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   876
    }
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   877
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   878
    /**
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   879
     * Sorts the specified array into ascending numerical order.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   880
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   881
     * <p>The {@code <} relation does not provide a total order on all double
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   882
     * values: {@code -0.0d == 0.0d} is {@code true} and a {@code Double.NaN}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   883
     * value compares neither less than, greater than, nor equal to any value,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   884
     * even itself. This method uses the total order imposed by the method
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   885
     * {@link Double#compareTo}: {@code -0.0d} is treated as less than value
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   886
     * {@code 0.0d} and {@code Double.NaN} is considered greater than any
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   887
     * other value and all {@code Double.NaN} values are considered equal.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   888
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   889
     * @implNote The sorting algorithm is a parallel sort-merge that breaks the
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   890
     * array into sub-arrays that are themselves sorted and then merged. When
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   891
     * the sub-array length reaches a minimum granularity, the sub-array is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   892
     * sorted using the appropriate {@link Arrays#sort(double[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   893
     * method. If the length of the specified array is less than the minimum
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   894
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   895
     * Arrays#sort(double[]) Arrays.sort} method. The algorithm requires a
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   896
     * working space no greater than the size of the original array. The
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   897
     * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   898
     * execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   899
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   900
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   901
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   902
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   903
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   904
    public static void parallelSort(double[] a) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   905
        int n = a.length, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   906
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   907
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   908
            DualPivotQuicksort.sort(a, 0, n - 1, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   909
        else
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   910
            new ArraysParallelSortHelpers.FJDouble.Sorter
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   911
                (null, a, new double[n], 0, n, 0,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   912
                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   913
                 MIN_ARRAY_SORT_GRAN : g).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   914
    }
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   915
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   916
    /**
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   917
     * Sorts the specified range of the array into ascending numerical order.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   918
     * The range to be sorted extends from the index {@code fromIndex},
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   919
     * inclusive, to the index {@code toIndex}, exclusive. If
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   920
     * {@code fromIndex == toIndex}, the range to be sorted is empty.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   921
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   922
     * <p>The {@code <} relation does not provide a total order on all double
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   923
     * values: {@code -0.0d == 0.0d} is {@code true} and a {@code Double.NaN}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   924
     * value compares neither less than, greater than, nor equal to any value,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   925
     * even itself. This method uses the total order imposed by the method
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   926
     * {@link Double#compareTo}: {@code -0.0d} is treated as less than value
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   927
     * {@code 0.0d} and {@code Double.NaN} is considered greater than any
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   928
     * other value and all {@code Double.NaN} values are considered equal.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   929
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   930
     * @implNote The sorting algorithm is a parallel sort-merge that breaks the
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   931
     * array into sub-arrays that are themselves sorted and then merged. When
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   932
     * the sub-array length reaches a minimum granularity, the sub-array is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   933
     * sorted using the appropriate {@link Arrays#sort(double[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   934
     * method. If the length of the specified array is less than the minimum
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   935
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   936
     * Arrays#sort(double[]) Arrays.sort} method. The algorithm requires a working
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   937
     * space no greater than the size of the specified range of the original
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   938
     * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   939
     * used to execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   940
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   941
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   942
     * @param fromIndex the index of the first element, inclusive, to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   943
     * @param toIndex the index of the last element, exclusive, to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   944
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   945
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   946
     * @throws ArrayIndexOutOfBoundsException
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   947
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   948
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   949
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   950
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   951
    public static void parallelSort(double[] a, int fromIndex, int toIndex) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   952
        rangeCheck(a.length, fromIndex, toIndex);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   953
        int n = toIndex - fromIndex, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   954
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   955
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   956
            DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   957
        else
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   958
            new ArraysParallelSortHelpers.FJDouble.Sorter
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   959
                (null, a, new double[n], fromIndex, n, 0,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   960
                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   961
                 MIN_ARRAY_SORT_GRAN : g).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   962
    }
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   963
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   964
    /**
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   965
     * Sorts the specified array of objects into ascending order, according
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   966
     * to the {@linkplain Comparable natural ordering} of its elements.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   967
     * All elements in the array must implement the {@link Comparable}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   968
     * interface.  Furthermore, all elements in the array must be
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   969
     * <i>mutually comparable</i> (that is, {@code e1.compareTo(e2)} must
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   970
     * not throw a {@code ClassCastException} for any elements {@code e1}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   971
     * and {@code e2} in the array).
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   972
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   973
     * <p>This sort is guaranteed to be <i>stable</i>:  equal elements will
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   974
     * not be reordered as a result of the sort.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   975
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   976
     * @implNote The sorting algorithm is a parallel sort-merge that breaks the
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   977
     * array into sub-arrays that are themselves sorted and then merged. When
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   978
     * the sub-array length reaches a minimum granularity, the sub-array is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   979
     * sorted using the appropriate {@link Arrays#sort(Object[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   980
     * method. If the length of the specified array is less than the minimum
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   981
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   982
     * Arrays#sort(Object[]) Arrays.sort} method. The algorithm requires a
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   983
     * working space no greater than the size of the original array. The
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   984
     * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   985
     * execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   986
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
   987
     * @param <T> the class of the objects to be sorted
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   988
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   989
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   990
     * @throws ClassCastException if the array contains elements that are not
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   991
     *         <i>mutually comparable</i> (for example, strings and integers)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   992
     * @throws IllegalArgumentException (optional) if the natural
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   993
     *         ordering of the array elements is found to violate the
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   994
     *         {@link Comparable} contract
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   995
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   996
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   997
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   998
    @SuppressWarnings("unchecked")
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   999
    public static <T extends Comparable<? super T>> void parallelSort(T[] a) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1000
        int n = a.length, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1001
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1002
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1003
            TimSort.sort(a, 0, n, NaturalOrder.INSTANCE, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1004
        else
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 21278
diff changeset
  1005
            new ArraysParallelSortHelpers.FJObject.Sorter<>
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1006
                (null, a,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1007
                 (T[])Array.newInstance(a.getClass().getComponentType(), n),
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1008
                 0, n, 0, ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1009
                 MIN_ARRAY_SORT_GRAN : g, NaturalOrder.INSTANCE).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1010
    }
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1011
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1012
    /**
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1013
     * Sorts the specified range of the specified array of objects into
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1014
     * ascending order, according to the
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1015
     * {@linkplain Comparable natural ordering} of its
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1016
     * elements.  The range to be sorted extends from index
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1017
     * {@code fromIndex}, inclusive, to index {@code toIndex}, exclusive.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1018
     * (If {@code fromIndex==toIndex}, the range to be sorted is empty.)  All
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1019
     * elements in this range must implement the {@link Comparable}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1020
     * interface.  Furthermore, all elements in this range must be <i>mutually
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1021
     * comparable</i> (that is, {@code e1.compareTo(e2)} must not throw a
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1022
     * {@code ClassCastException} for any elements {@code e1} and
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1023
     * {@code e2} in the array).
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1024
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1025
     * <p>This sort is guaranteed to be <i>stable</i>:  equal elements will
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1026
     * not be reordered as a result of the sort.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1027
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1028
     * @implNote The sorting algorithm is a parallel sort-merge that breaks the
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1029
     * array into sub-arrays that are themselves sorted and then merged. When
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1030
     * the sub-array length reaches a minimum granularity, the sub-array is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1031
     * sorted using the appropriate {@link Arrays#sort(Object[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1032
     * method. If the length of the specified array is less than the minimum
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1033
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1034
     * Arrays#sort(Object[]) Arrays.sort} method. The algorithm requires a working
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1035
     * space no greater than the size of the specified range of the original
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1036
     * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1037
     * used to execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1038
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  1039
     * @param <T> the class of the objects to be sorted
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1040
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1041
     * @param fromIndex the index of the first element (inclusive) to be
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1042
     *        sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1043
     * @param toIndex the index of the last element (exclusive) to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1044
     * @throws IllegalArgumentException if {@code fromIndex > toIndex} or
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1045
     *         (optional) if the natural ordering of the array elements is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1046
     *         found to violate the {@link Comparable} contract
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1047
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1048
     *         {@code toIndex > a.length}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1049
     * @throws ClassCastException if the array contains elements that are
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1050
     *         not <i>mutually comparable</i> (for example, strings and
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1051
     *         integers).
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1052
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1053
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1054
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1055
    @SuppressWarnings("unchecked")
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1056
    public static <T extends Comparable<? super T>>
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1057
    void parallelSort(T[] a, int fromIndex, int toIndex) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1058
        rangeCheck(a.length, fromIndex, toIndex);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1059
        int n = toIndex - fromIndex, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1060
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1061
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1062
            TimSort.sort(a, fromIndex, toIndex, NaturalOrder.INSTANCE, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1063
        else
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 21278
diff changeset
  1064
            new ArraysParallelSortHelpers.FJObject.Sorter<>
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1065
                (null, a,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1066
                 (T[])Array.newInstance(a.getClass().getComponentType(), n),
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1067
                 fromIndex, n, 0, ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1068
                 MIN_ARRAY_SORT_GRAN : g, NaturalOrder.INSTANCE).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1069
    }
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1070
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1071
    /**
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1072
     * Sorts the specified array of objects according to the order induced by
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1073
     * the specified comparator.  All elements in the array must be
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1074
     * <i>mutually comparable</i> by the specified comparator (that is,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1075
     * {@code c.compare(e1, e2)} must not throw a {@code ClassCastException}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1076
     * for any elements {@code e1} and {@code e2} in the array).
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1077
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1078
     * <p>This sort is guaranteed to be <i>stable</i>:  equal elements will
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1079
     * not be reordered as a result of the sort.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1080
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1081
     * @implNote The sorting algorithm is a parallel sort-merge that breaks the
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1082
     * array into sub-arrays that are themselves sorted and then merged. When
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1083
     * the sub-array length reaches a minimum granularity, the sub-array is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1084
     * sorted using the appropriate {@link Arrays#sort(Object[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1085
     * method. If the length of the specified array is less than the minimum
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1086
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1087
     * Arrays#sort(Object[]) Arrays.sort} method. The algorithm requires a
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1088
     * working space no greater than the size of the original array. The
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1089
     * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1090
     * execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1091
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  1092
     * @param <T> the class of the objects to be sorted
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1093
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1094
     * @param cmp the comparator to determine the order of the array.  A
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1095
     *        {@code null} value indicates that the elements'
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1096
     *        {@linkplain Comparable natural ordering} should be used.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1097
     * @throws ClassCastException if the array contains elements that are
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1098
     *         not <i>mutually comparable</i> using the specified comparator
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1099
     * @throws IllegalArgumentException (optional) if the comparator is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1100
     *         found to violate the {@link java.util.Comparator} contract
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1101
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1102
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1103
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1104
    @SuppressWarnings("unchecked")
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1105
    public static <T> void parallelSort(T[] a, Comparator<? super T> cmp) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1106
        if (cmp == null)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1107
            cmp = NaturalOrder.INSTANCE;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1108
        int n = a.length, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1109
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1110
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1111
            TimSort.sort(a, 0, n, cmp, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1112
        else
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 21278
diff changeset
  1113
            new ArraysParallelSortHelpers.FJObject.Sorter<>
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1114
                (null, a,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1115
                 (T[])Array.newInstance(a.getClass().getComponentType(), n),
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1116
                 0, n, 0, ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1117
                 MIN_ARRAY_SORT_GRAN : g, cmp).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1118
    }
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1119
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1120
    /**
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1121
     * Sorts the specified range of the specified array of objects according
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1122
     * to the order induced by the specified comparator.  The range to be
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1123
     * sorted extends from index {@code fromIndex}, inclusive, to index
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1124
     * {@code toIndex}, exclusive.  (If {@code fromIndex==toIndex}, the
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1125
     * range to be sorted is empty.)  All elements in the range must be
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1126
     * <i>mutually comparable</i> by the specified comparator (that is,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1127
     * {@code c.compare(e1, e2)} must not throw a {@code ClassCastException}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1128
     * for any elements {@code e1} and {@code e2} in the range).
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1129
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1130
     * <p>This sort is guaranteed to be <i>stable</i>:  equal elements will
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1131
     * not be reordered as a result of the sort.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1132
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1133
     * @implNote The sorting algorithm is a parallel sort-merge that breaks the
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1134
     * array into sub-arrays that are themselves sorted and then merged. When
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1135
     * the sub-array length reaches a minimum granularity, the sub-array is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1136
     * sorted using the appropriate {@link Arrays#sort(Object[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1137
     * method. If the length of the specified array is less than the minimum
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1138
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1139
     * Arrays#sort(Object[]) Arrays.sort} method. The algorithm requires a working
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1140
     * space no greater than the size of the specified range of the original
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1141
     * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1142
     * used to execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1143
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  1144
     * @param <T> the class of the objects to be sorted
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1145
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1146
     * @param fromIndex the index of the first element (inclusive) to be
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1147
     *        sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1148
     * @param toIndex the index of the last element (exclusive) to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1149
     * @param cmp the comparator to determine the order of the array.  A
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1150
     *        {@code null} value indicates that the elements'
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1151
     *        {@linkplain Comparable natural ordering} should be used.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1152
     * @throws IllegalArgumentException if {@code fromIndex > toIndex} or
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1153
     *         (optional) if the natural ordering of the array elements is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1154
     *         found to violate the {@link Comparable} contract
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1155
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1156
     *         {@code toIndex > a.length}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1157
     * @throws ClassCastException if the array contains elements that are
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1158
     *         not <i>mutually comparable</i> (for example, strings and
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1159
     *         integers).
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1160
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1161
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1162
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1163
    @SuppressWarnings("unchecked")
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1164
    public static <T> void parallelSort(T[] a, int fromIndex, int toIndex,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1165
                                        Comparator<? super T> cmp) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1166
        rangeCheck(a.length, fromIndex, toIndex);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1167
        if (cmp == null)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1168
            cmp = NaturalOrder.INSTANCE;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1169
        int n = toIndex - fromIndex, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1170
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1171
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1172
            TimSort.sort(a, fromIndex, toIndex, cmp, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1173
        else
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 21278
diff changeset
  1174
            new ArraysParallelSortHelpers.FJObject.Sorter<>
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1175
                (null, a,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1176
                 (T[])Array.newInstance(a.getClass().getComponentType(), n),
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1177
                 fromIndex, n, 0, ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1178
                 MIN_ARRAY_SORT_GRAN : g, cmp).invoke();
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
  1179
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
  1181
    /*
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
  1182
     * Sorting of complex type arrays.
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
  1183
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1185
    /**
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1186
     * 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
  1187
     * 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
  1188
     * 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
  1189
     * 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
  1190
     */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1191
    static final class LegacyMergeSort {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1192
        private static final boolean userRequested =
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1193
            java.security.AccessController.doPrivileged(
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1194
                new sun.security.action.GetBooleanAction(
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1195
                    "java.util.Arrays.useLegacyMergeSort")).booleanValue();
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1196
    }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1197
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
    /**
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1199
     * 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
  1200
     * 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
  1201
     * 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
  1202
     * 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
  1203
     * <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
  1204
     * 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
  1205
     * and {@code e2} in the array).
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1206
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1207
     * <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
  1208
     * not be reordered as a result of the sort.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
     *
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1210
     * <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
  1211
     * 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
  1212
     * 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
  1213
     * 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
  1214
     * 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
  1215
     * implementation requires approximately n comparisons.  Temporary
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1216
     * 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
  1217
     * 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
  1218
     * arrays.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
     *
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1220
     * <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
  1221
     * 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
  1222
     * 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
  1223
     * 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
  1224
     * 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
  1225
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1226
     * <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
  1227
     * (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt">
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20492
diff changeset
  1228
     * TimSort</a>).  It uses techniques from Peter McIlroy's "Optimistic
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1229
     * 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
  1230
     * 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
  1231
     * January 1993.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
     * @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
  1234
     * @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
  1235
     *         <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
  1236
     * @throws IllegalArgumentException (optional) if the natural
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1237
     *         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
  1238
     *         {@link Comparable} contract
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
    public static void sort(Object[] a) {
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1241
        if (LegacyMergeSort.userRequested)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1242
            legacyMergeSort(a);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1243
        else
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1244
            ComparableTimSort.sort(a, 0, a.length, null, 0, 0);
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1245
    }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1246
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1247
    /** To be removed in a future release. */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1248
    private static void legacyMergeSort(Object[] a) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1249
        Object[] aux = a.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
        mergeSort(aux, a, 0, a.length, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
     * Sorts the specified range of the specified array of objects into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
     * ascending order, according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
     * {@linkplain Comparable natural ordering} of its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
     * 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
  1258
     * {@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
  1259
     * (If {@code fromIndex==toIndex}, the range to be sorted is empty.)  All
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
     * elements in this range must implement the {@link Comparable}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
     * 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
  1262
     * 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
  1263
     * {@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
  1264
     * {@code e2} in the array).
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1265
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1266
     * <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
  1267
     * not be reordered as a result of the sort.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
     *
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1269
     * <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
  1270
     * 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
  1271
     * 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
  1272
     * 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
  1273
     * 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
  1274
     * implementation requires approximately n comparisons.  Temporary
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1275
     * 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
  1276
     * 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
  1277
     * arrays.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
     *
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1279
     * <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
  1280
     * 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
  1281
     * 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
  1282
     * 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
  1283
     * 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
  1284
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1285
     * <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
  1286
     * (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt">
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20492
diff changeset
  1287
     * TimSort</a>).  It uses techniques from Peter McIlroy's "Optimistic
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1288
     * 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
  1289
     * 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
  1290
     * January 1993.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
     * @param a the array to be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
     *        sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
     * @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
  1296
     * @throws IllegalArgumentException if {@code fromIndex > toIndex} or
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1297
     *         (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
  1298
     *         found to violate the {@link Comparable} contract
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1299
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1300
     *         {@code toIndex > a.length}
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1301
     * @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
  1302
     *         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
  1303
     *         integers).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
    public static void sort(Object[] a, int fromIndex, int toIndex) {
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1306
        rangeCheck(a.length, fromIndex, toIndex);
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1307
        if (LegacyMergeSort.userRequested)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1308
            legacyMergeSort(a, fromIndex, toIndex);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1309
        else
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1310
            ComparableTimSort.sort(a, fromIndex, toIndex, null, 0, 0);
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1311
    }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1312
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1313
    /** To be removed in a future release. */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1314
    private static void legacyMergeSort(Object[] a,
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1315
                                        int fromIndex, int toIndex) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
        Object[] aux = copyOfRange(a, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
        mergeSort(aux, a, fromIndex, toIndex, -fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
     * 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
  1322
     * used in preference to mergesort.
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1323
     * To be removed in a future release.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
    private static final int INSERTIONSORT_THRESHOLD = 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
     * Src is the source array that starts at index 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
     * Dest is the (possibly larger) array destination with a possible offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
     * low is the index in dest to start sorting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
     * high is the end index in dest to end sorting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
     * 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
  1333
     * To be removed in a future release.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
     */
13795
73850c397272 7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents: 12448
diff changeset
  1335
    @SuppressWarnings({"unchecked", "rawtypes"})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
    private static void mergeSort(Object[] src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
                                  Object[] dest,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
                                  int low,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
                                  int high,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
                                  int off) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
        int length = high - low;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
        // Insertion sort on smallest arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
        if (length < INSERTIONSORT_THRESHOLD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
            for (int i=low; i<high; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
                for (int j=i; j>low &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
                         ((Comparable) dest[j-1]).compareTo(dest[j])>0; j--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
                    swap(dest, j, j-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
        // Recursively sort halves of dest into src
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
        int destLow  = low;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
        int destHigh = high;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
        low  += off;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
        high += off;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
        int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
        mergeSort(dest, src, low, mid, -off);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
        mergeSort(dest, src, mid, high, -off);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
        // If list is already sorted, just copy from src to dest.  This is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
        // optimization that results in faster sorts for nearly ordered lists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
        if (((Comparable)src[mid-1]).compareTo(src[mid]) <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
            System.arraycopy(src, low, dest, destLow, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
        // Merge sorted halves (now in src) into dest
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
        for(int i = destLow, p = low, q = mid; i < destHigh; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
            if (q >= high || p < mid && ((Comparable)src[p]).compareTo(src[q])<=0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
                dest[i] = src[p++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
                dest[i] = src[q++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
     * Swaps x[a] with x[b].
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
    private static void swap(Object[] x, int a, int b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
        Object t = x[a];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
        x[a] = x[b];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
        x[b] = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
     * Sorts the specified array of objects according to the order induced by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
     * the specified comparator.  All elements in the array must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
     * <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
  1390
     * {@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
  1391
     * 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
  1392
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1393
     * <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
  1394
     * not be reordered as a result of the sort.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
     *
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1396
     * <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
  1397
     * 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
  1398
     * 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
  1399
     * 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
  1400
     * 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
  1401
     * implementation requires approximately n comparisons.  Temporary
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1402
     * 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
  1403
     * 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
  1404
     * arrays.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
     *
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1406
     * <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
  1407
     * 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
  1408
     * 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
  1409
     * 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
  1410
     * 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
  1411
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1412
     * <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
  1413
     * (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt">
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20492
diff changeset
  1414
     * TimSort</a>).  It uses techniques from Peter McIlroy's "Optimistic
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1415
     * 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
  1416
     * 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
  1417
     * January 1993.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  1419
     * @param <T> the class of the objects to be sorted
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
     * @param a the array to be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
     * @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
  1422
     *        {@code null} value indicates that the elements'
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
     *        {@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
  1424
     * @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
  1425
     *         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
  1426
     * @throws IllegalArgumentException (optional) if the comparator is
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1427
     *         found to violate the {@link Comparator} contract
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
    public static <T> void sort(T[] a, Comparator<? super T> c) {
22285
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1430
        if (c == null) {
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1431
            sort(a);
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1432
        } else {
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1433
            if (LegacyMergeSort.userRequested)
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1434
                legacyMergeSort(a, c);
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1435
            else
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1436
                TimSort.sort(a, 0, a.length, c, null, 0, 0);
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1437
        }
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1438
    }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1439
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1440
    /** To be removed in a future release. */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1441
    private static <T> void legacyMergeSort(T[] a, Comparator<? super T> c) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1442
        T[] aux = a.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
        if (c==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
            mergeSort(aux, a, 0, a.length, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
            mergeSort(aux, a, 0, a.length, 0, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
     * Sorts the specified range of the specified array of objects according
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
     * 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
  1452
     * 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
  1453
     * {@code toIndex}, exclusive.  (If {@code fromIndex==toIndex}, the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
     * range to be sorted is empty.)  All elements in the range must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
     * <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
  1456
     * {@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
  1457
     * 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
  1458
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1459
     * <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
  1460
     * not be reordered as a result of the sort.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
     *
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1462
     * <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
  1463
     * 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
  1464
     * 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
  1465
     * 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
  1466
     * 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
  1467
     * implementation requires approximately n comparisons.  Temporary
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1468
     * 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
  1469
     * 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
  1470
     * arrays.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
     *
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1472
     * <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
  1473
     * 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
  1474
     * 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
  1475
     * 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
  1476
     * 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
  1477
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1478
     * <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
  1479
     * (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt">
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20492
diff changeset
  1480
     * TimSort</a>).  It uses techniques from Peter McIlroy's "Optimistic
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1481
     * 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
  1482
     * 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
  1483
     * January 1993.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  1485
     * @param <T> the class of the objects to be sorted
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
     * @param a the array to be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
     *        sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
     * @param toIndex the index of the last element (exclusive) to be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
     * @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
  1491
     *        {@code null} value indicates that the elements'
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
     *        {@linkplain Comparable natural ordering} should be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
     * @throws ClassCastException if the array contains elements that are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
     *         <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
  1495
     * @throws IllegalArgumentException if {@code fromIndex > toIndex} or
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1496
     *         (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
  1497
     *         {@link Comparator} contract
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1498
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1499
     *         {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
    public static <T> void sort(T[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
                                Comparator<? super T> c) {
22285
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1503
        if (c == null) {
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1504
            sort(a, fromIndex, toIndex);
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1505
        } else {
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1506
            rangeCheck(a.length, fromIndex, toIndex);
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1507
            if (LegacyMergeSort.userRequested)
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1508
                legacyMergeSort(a, fromIndex, toIndex, c);
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1509
            else
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1510
                TimSort.sort(a, fromIndex, toIndex, c, null, 0, 0);
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1511
        }
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1512
    }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1513
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1514
    /** To be removed in a future release. */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1515
    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
  1516
                                            Comparator<? super T> c) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1517
        T[] aux = copyOfRange(a, fromIndex, toIndex);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
        if (c==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
            mergeSort(aux, a, fromIndex, toIndex, -fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
            mergeSort(aux, a, fromIndex, toIndex, -fromIndex, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
     * Src is the source array that starts at index 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
     * Dest is the (possibly larger) array destination with a possible offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
     * low is the index in dest to start sorting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
     * high is the end index in dest to end sorting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
     * 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
  1530
     * To be removed in a future release.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
     */
13795
73850c397272 7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents: 12448
diff changeset
  1532
    @SuppressWarnings({"rawtypes", "unchecked"})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
    private static void mergeSort(Object[] src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
                                  Object[] dest,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
                                  int low, int high, int off,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
                                  Comparator c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
        int length = high - low;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
        // Insertion sort on smallest arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
        if (length < INSERTIONSORT_THRESHOLD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
            for (int i=low; i<high; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
                for (int j=i; j>low && c.compare(dest[j-1], dest[j])>0; j--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
                    swap(dest, j, j-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
        // Recursively sort halves of dest into src
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
        int destLow  = low;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
        int destHigh = high;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
        low  += off;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
        high += off;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
        int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
        mergeSort(dest, src, low, mid, -off, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
        mergeSort(dest, src, mid, high, -off, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
        // If list is already sorted, just copy from src to dest.  This is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
        // optimization that results in faster sorts for nearly ordered lists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
        if (c.compare(src[mid-1], src[mid]) <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
           System.arraycopy(src, low, dest, destLow, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
           return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
        // Merge sorted halves (now in src) into dest
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
        for(int i = destLow, p = low, q = mid; i < destHigh; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
            if (q >= high || p < mid && c.compare(src[p], src[q]) <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
                dest[i] = src[p++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
                dest[i] = src[q++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
18555
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1572
    // Parallel prefix
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1573
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1574
    /**
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1575
     * Cumulates, in parallel, each element of the given array in place,
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1576
     * using the supplied function. For example if the array initially
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1577
     * holds {@code [2, 1, 0, 3]} and the operation performs addition,
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1578
     * then upon return the array holds {@code [2, 3, 3, 6]}.
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1579
     * Parallel prefix computation is usually more efficient than
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1580
     * sequential loops for large arrays.
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1581
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  1582
     * @param <T> the class of the objects in the array
18555
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1583
     * @param array the array, which is modified in-place by this method
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1584
     * @param op a side-effect-free, associative function to perform the
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1585
     * cumulation
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1586
     * @throws NullPointerException if the specified array or function is null
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1587
     * @since 1.8
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1588
     */
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1589
    public static <T> void parallelPrefix(T[] array, BinaryOperator<T> op) {
20492
11418b1f61ff 8025067: Unconditionally throw NPE if null op provided to Arrays.parallelPrefix
mduigou
parents: 18829
diff changeset
  1590
        Objects.requireNonNull(op);
18555
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1591
        if (array.length > 0)
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1592
            new ArrayPrefixHelpers.CumulateTask<>
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1593
                    (null, op, array, 0, array.length).invoke();
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1594
    }
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1595
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1596
    /**
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1597
     * Performs {@link #parallelPrefix(Object[], BinaryOperator)}
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1598
     * for the given subrange of the array.
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1599
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  1600
     * @param <T> the class of the objects in the array
18555
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1601
     * @param array the array
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1602
     * @param fromIndex the index of the first element, inclusive
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1603
     * @param toIndex the index of the last element, exclusive
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1604
     * @param op a side-effect-free, associative function to perform the
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1605
     * cumulation
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1606
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1607
     * @throws ArrayIndexOutOfBoundsException
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1608
     *     if {@code fromIndex < 0} or {@code toIndex > array.length}
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1609
     * @throws NullPointerException if the specified array or function is null
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1610
     * @since 1.8
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1611
     */
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1612
    public static <T> void parallelPrefix(T[] array, int fromIndex,
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1613
                                          int toIndex, BinaryOperator<T> op) {
20492
11418b1f61ff 8025067: Unconditionally throw NPE if null op provided to Arrays.parallelPrefix
mduigou
parents: 18829
diff changeset
  1614
        Objects.requireNonNull(op);
18555
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1615
        rangeCheck(array.length, fromIndex, toIndex);
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1616
        if (fromIndex < toIndex)
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1617
            new ArrayPrefixHelpers.CumulateTask<>
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1618
                    (null, op, array, fromIndex, toIndex).invoke();
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1619
    }
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1620
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1621
    /**
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1622
     * Cumulates, in parallel, each element of the given array in place,
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1623
     * using the supplied function. For example if the array initially
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1624
     * holds {@code [2, 1, 0, 3]} and the operation performs addition,
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1625
     * then upon return the array holds {@code [2, 3, 3, 6]}.
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1626
     * Parallel prefix computation is usually more efficient than
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1627
     * sequential loops for large arrays.
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1628
     *
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1629
     * @param array the array, which is modified in-place by this method
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1630
     * @param op a side-effect-free, associative function to perform the
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1631
     * cumulation
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1632
     * @throws NullPointerException if the specified array or function is null
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1633
     * @since 1.8
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1634
     */
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1635
    public static void parallelPrefix(long[] array, LongBinaryOperator op) {
20492
11418b1f61ff 8025067: Unconditionally throw NPE if null op provided to Arrays.parallelPrefix
mduigou
parents: 18829
diff changeset
  1636
        Objects.requireNonNull(op);
18555
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1637
        if (array.length > 0)
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1638
            new ArrayPrefixHelpers.LongCumulateTask
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1639
                    (null, op, array, 0, array.length).invoke();
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1640
    }
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1641
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1642
    /**
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1643
     * Performs {@link #parallelPrefix(long[], LongBinaryOperator)}
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1644
     * for the given subrange of the array.
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1645
     *
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1646
     * @param array the array
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1647
     * @param fromIndex the index of the first element, inclusive
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1648
     * @param toIndex the index of the last element, exclusive
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1649
     * @param op a side-effect-free, associative function to perform the
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1650
     * cumulation
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1651
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1652
     * @throws ArrayIndexOutOfBoundsException
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1653
     *     if {@code fromIndex < 0} or {@code toIndex > array.length}
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1654
     * @throws NullPointerException if the specified array or function is null
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1655
     * @since 1.8
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1656
     */
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1657
    public static void parallelPrefix(long[] array, int fromIndex,
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1658
                                      int toIndex, LongBinaryOperator op) {
20492
11418b1f61ff 8025067: Unconditionally throw NPE if null op provided to Arrays.parallelPrefix
mduigou
parents: 18829
diff changeset
  1659
        Objects.requireNonNull(op);
18555
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1660
        rangeCheck(array.length, fromIndex, toIndex);
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1661
        if (fromIndex < toIndex)
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1662
            new ArrayPrefixHelpers.LongCumulateTask
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1663
                    (null, op, array, fromIndex, toIndex).invoke();
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1664
    }
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1665
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1666
    /**
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1667
     * Cumulates, in parallel, each element of the given array in place,
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1668
     * using the supplied function. For example if the array initially
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1669
     * holds {@code [2.0, 1.0, 0.0, 3.0]} and the operation performs addition,
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1670
     * then upon return the array holds {@code [2.0, 3.0, 3.0, 6.0]}.
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1671
     * Parallel prefix computation is usually more efficient than
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1672
     * sequential loops for large arrays.
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1673
     *
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1674
     * <p> Because floating-point operations may not be strictly associative,
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1675
     * the returned result may not be identical to the value that would be
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1676
     * obtained if the operation was performed sequentially.
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1677
     *
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1678
     * @param array the array, which is modified in-place by this method
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1679
     * @param op a side-effect-free function to perform the cumulation
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1680
     * @throws NullPointerException if the specified array or function is null
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1681
     * @since 1.8
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1682
     */
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1683
    public static void parallelPrefix(double[] array, DoubleBinaryOperator op) {
20492
11418b1f61ff 8025067: Unconditionally throw NPE if null op provided to Arrays.parallelPrefix
mduigou
parents: 18829
diff changeset
  1684
        Objects.requireNonNull(op);
18555
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1685
        if (array.length > 0)
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1686
            new ArrayPrefixHelpers.DoubleCumulateTask
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1687
                    (null, op, array, 0, array.length).invoke();
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1688
    }
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1689
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1690
    /**
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1691
     * Performs {@link #parallelPrefix(double[], DoubleBinaryOperator)}
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1692
     * for the given subrange of the array.
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1693
     *
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1694
     * @param array the array
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1695
     * @param fromIndex the index of the first element, inclusive
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1696
     * @param toIndex the index of the last element, exclusive
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1697
     * @param op a side-effect-free, associative function to perform the
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1698
     * cumulation
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1699
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1700
     * @throws ArrayIndexOutOfBoundsException
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1701
     *     if {@code fromIndex < 0} or {@code toIndex > array.length}
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1702
     * @throws NullPointerException if the specified array or function is null
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1703
     * @since 1.8
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1704
     */
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1705
    public static void parallelPrefix(double[] array, int fromIndex,
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1706
                                      int toIndex, DoubleBinaryOperator op) {
20492
11418b1f61ff 8025067: Unconditionally throw NPE if null op provided to Arrays.parallelPrefix
mduigou
parents: 18829
diff changeset
  1707
        Objects.requireNonNull(op);
18555
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1708
        rangeCheck(array.length, fromIndex, toIndex);
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1709
        if (fromIndex < toIndex)
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1710
            new ArrayPrefixHelpers.DoubleCumulateTask
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1711
                    (null, op, array, fromIndex, toIndex).invoke();
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1712
    }
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1713
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1714
    /**
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1715
     * Cumulates, in parallel, each element of the given array in place,
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1716
     * using the supplied function. For example if the array initially
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1717
     * holds {@code [2, 1, 0, 3]} and the operation performs addition,
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1718
     * then upon return the array holds {@code [2, 3, 3, 6]}.
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1719
     * Parallel prefix computation is usually more efficient than
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1720
     * sequential loops for large arrays.
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1721
     *
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1722
     * @param array the array, which is modified in-place by this method
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1723
     * @param op a side-effect-free, associative function to perform the
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1724
     * cumulation
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1725
     * @throws NullPointerException if the specified array or function is null
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1726
     * @since 1.8
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1727
     */
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1728
    public static void parallelPrefix(int[] array, IntBinaryOperator op) {
20492
11418b1f61ff 8025067: Unconditionally throw NPE if null op provided to Arrays.parallelPrefix
mduigou
parents: 18829
diff changeset
  1729
        Objects.requireNonNull(op);
18555
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1730
        if (array.length > 0)
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1731
            new ArrayPrefixHelpers.IntCumulateTask
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1732
                    (null, op, array, 0, array.length).invoke();
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1733
    }
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1734
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1735
    /**
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1736
     * Performs {@link #parallelPrefix(int[], IntBinaryOperator)}
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1737
     * for the given subrange of the array.
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1738
     *
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1739
     * @param array the array
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1740
     * @param fromIndex the index of the first element, inclusive
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1741
     * @param toIndex the index of the last element, exclusive
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1742
     * @param op a side-effect-free, associative function to perform the
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1743
     * cumulation
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1744
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1745
     * @throws ArrayIndexOutOfBoundsException
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1746
     *     if {@code fromIndex < 0} or {@code toIndex > array.length}
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1747
     * @throws NullPointerException if the specified array or function is null
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1748
     * @since 1.8
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1749
     */
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1750
    public static void parallelPrefix(int[] array, int fromIndex,
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1751
                                      int toIndex, IntBinaryOperator op) {
20492
11418b1f61ff 8025067: Unconditionally throw NPE if null op provided to Arrays.parallelPrefix
mduigou
parents: 18829
diff changeset
  1752
        Objects.requireNonNull(op);
18555
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1753
        rangeCheck(array.length, fromIndex, toIndex);
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1754
        if (fromIndex < toIndex)
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1755
            new ArrayPrefixHelpers.IntCumulateTask
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1756
                    (null, op, array, fromIndex, toIndex).invoke();
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1757
    }
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1758
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
    // Searching
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
     * Searches the specified array of longs for the specified value using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
     * binary search algorithm.  The array must be sorted (as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
     * by the {@link #sort(long[])} method) prior to making this call.  If it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
     * is not sorted, the results are undefined.  If the array contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
     * @return index of the search key, if it is contained in the array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
     *         element greater than the key, or <tt>a.length</tt> if all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
     *         elements in the array are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
    public static int binarySearch(long[] a, long key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
        return binarySearch0(a, 0, a.length, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
     * Searches a range of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
     * the specified array of longs for the specified value using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
     * binary search algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
     * The range must be sorted (as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
     * by the {@link #sort(long[], int, int)} method)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
     * prior to making this call.  If it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
     * is not sorted, the results are undefined.  If the range contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
     *          searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
     * @param toIndex the index of the last element (exclusive) to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
     * @return index of the search key, if it is contained in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
     *         within the specified range;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
     *         element in the range greater than the key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
     *         or <tt>toIndex</tt> if all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
     *         elements in the range are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
     *         if {@code fromIndex > toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
     * @throws ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
     *         if {@code fromIndex < 0 or toIndex > a.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
    public static int binarySearch(long[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
                                   long key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
        return binarySearch0(a, fromIndex, toIndex, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
    // Like public version, but without range checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
    private static int binarySearch0(long[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
                                     long key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
        int low = fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
        int high = toIndex - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
            int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
            long midVal = a[mid];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
            if (midVal < key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
                low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
            else if (midVal > key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
                high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
                return mid; // key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
        return -(low + 1);  // key not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
     * Searches the specified array of ints for the specified value using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
     * binary search algorithm.  The array must be sorted (as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
     * by the {@link #sort(int[])} method) prior to making this call.  If it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
     * is not sorted, the results are undefined.  If the array contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
     * @return index of the search key, if it is contained in the array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
     *         element greater than the key, or <tt>a.length</tt> if all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
     *         elements in the array are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
    public static int binarySearch(int[] a, int key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
        return binarySearch0(a, 0, a.length, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
     * Searches a range of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
     * the specified array of ints for the specified value using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
     * binary search algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
     * The range must be sorted (as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
     * by the {@link #sort(int[], int, int)} method)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
     * prior to making this call.  If it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
     * is not sorted, the results are undefined.  If the range contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
     *          searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
     * @param toIndex the index of the last element (exclusive) to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
     * @return index of the search key, if it is contained in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
     *         within the specified range;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
     *         element in the range greater than the key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
     *         or <tt>toIndex</tt> if all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
     *         elements in the range are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
     *         if {@code fromIndex > toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
     * @throws ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
     *         if {@code fromIndex < 0 or toIndex > a.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
    public static int binarySearch(int[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
                                   int key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
        return binarySearch0(a, fromIndex, toIndex, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
    // Like public version, but without range checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
    private static int binarySearch0(int[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
                                     int key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
        int low = fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
        int high = toIndex - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
            int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
            int midVal = a[mid];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
            if (midVal < key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
                low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
            else if (midVal > key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
                high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
                return mid; // key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
        return -(low + 1);  // key not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
     * Searches the specified array of shorts for the specified value using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
     * the binary search algorithm.  The array must be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
     * (as by the {@link #sort(short[])} method) prior to making this call.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
     * it is not sorted, the results are undefined.  If the array contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
     * @return index of the search key, if it is contained in the array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
     *         element greater than the key, or <tt>a.length</tt> if all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
     *         elements in the array are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
    public static int binarySearch(short[] a, short key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
        return binarySearch0(a, 0, a.length, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
     * Searches a range of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
     * the specified array of shorts for the specified value using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
     * the binary search algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
     * The range must be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
     * (as by the {@link #sort(short[], int, int)} method)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
     * prior to making this call.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
     * it is not sorted, the results are undefined.  If the range contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
     *          searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
     * @param toIndex the index of the last element (exclusive) to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
     * @return index of the search key, if it is contained in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
     *         within the specified range;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
     *         element in the range greater than the key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
     *         or <tt>toIndex</tt> if all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
     *         elements in the range are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
     *         if {@code fromIndex > toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
     * @throws ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
     *         if {@code fromIndex < 0 or toIndex > a.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
    public static int binarySearch(short[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
                                   short key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
        return binarySearch0(a, fromIndex, toIndex, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
    // Like public version, but without range checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
    private static int binarySearch0(short[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
                                     short key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
        int low = fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
        int high = toIndex - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
            int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
            short midVal = a[mid];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
            if (midVal < key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
                low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
            else if (midVal > key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
                high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
                return mid; // key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
        return -(low + 1);  // key not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
     * Searches the specified array of chars for the specified value using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
     * binary search algorithm.  The array must be sorted (as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
     * by the {@link #sort(char[])} method) prior to making this call.  If it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
     * is not sorted, the results are undefined.  If the array contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
     * @return index of the search key, if it is contained in the array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
     *         element greater than the key, or <tt>a.length</tt> if all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
     *         elements in the array are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
    public static int binarySearch(char[] a, char key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
        return binarySearch0(a, 0, a.length, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
     * Searches a range of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
     * the specified array of chars for the specified value using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
     * binary search algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
     * The range must be sorted (as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
     * by the {@link #sort(char[], int, int)} method)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
     * prior to making this call.  If it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
     * is not sorted, the results are undefined.  If the range contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
     *          searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
     * @param toIndex the index of the last element (exclusive) to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
     * @return index of the search key, if it is contained in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
     *         within the specified range;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
     *         element in the range greater than the key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
     *         or <tt>toIndex</tt> if all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
     *         elements in the range are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
     *         if {@code fromIndex > toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
     * @throws ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
     *         if {@code fromIndex < 0 or toIndex > a.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
    public static int binarySearch(char[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
                                   char key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
        return binarySearch0(a, fromIndex, toIndex, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
    // Like public version, but without range checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
    private static int binarySearch0(char[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
                                     char key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
        int low = fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
        int high = toIndex - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
            int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
            char midVal = a[mid];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
            if (midVal < key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
                low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
            else if (midVal > key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
                high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
                return mid; // key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
        return -(low + 1);  // key not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
     * Searches the specified array of bytes for the specified value using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
     * binary search algorithm.  The array must be sorted (as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
     * by the {@link #sort(byte[])} method) prior to making this call.  If it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
     * is not sorted, the results are undefined.  If the array contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
     * @return index of the search key, if it is contained in the array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
     *         element greater than the key, or <tt>a.length</tt> if all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
     *         elements in the array are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
    public static int binarySearch(byte[] a, byte key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
        return binarySearch0(a, 0, a.length, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
     * Searches a range of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
     * the specified array of bytes for the specified value using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
     * binary search algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
     * The range must be sorted (as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
     * by the {@link #sort(byte[], int, int)} method)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
     * prior to making this call.  If it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
     * is not sorted, the results are undefined.  If the range contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
     *          searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
     * @param toIndex the index of the last element (exclusive) to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
     * @return index of the search key, if it is contained in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
     *         within the specified range;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
     *         element in the range greater than the key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
     *         or <tt>toIndex</tt> if all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
     *         elements in the range are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
     *         if {@code fromIndex > toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
     * @throws ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
     *         if {@code fromIndex < 0 or toIndex > a.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
    public static int binarySearch(byte[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
                                   byte key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
        return binarySearch0(a, fromIndex, toIndex, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
    // Like public version, but without range checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
    private static int binarySearch0(byte[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
                                     byte key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
        int low = fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
        int high = toIndex - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
            int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
            byte midVal = a[mid];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
            if (midVal < key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
                low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
            else if (midVal > key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
                high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
                return mid; // key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
        return -(low + 1);  // key not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
     * Searches the specified array of doubles for the specified value using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
     * the binary search algorithm.  The array must be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
     * (as by the {@link #sort(double[])} method) prior to making this call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
     * If it is not sorted, the results are undefined.  If the array contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
     * one will be found.  This method considers all NaN values to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
     * equivalent and equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
     * @return index of the search key, if it is contained in the array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
     *         element greater than the key, or <tt>a.length</tt> if all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
     *         elements in the array are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
    public static int binarySearch(double[] a, double key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
        return binarySearch0(a, 0, a.length, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
     * Searches a range of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
     * the specified array of doubles for the specified value using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
     * the binary search algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
     * The range must be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
     * (as by the {@link #sort(double[], int, int)} method)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
     * prior to making this call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
     * If it is not sorted, the results are undefined.  If the range contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
     * one will be found.  This method considers all NaN values to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
     * equivalent and equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
     *          searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
     * @param toIndex the index of the last element (exclusive) to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
     * @return index of the search key, if it is contained in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
     *         within the specified range;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
     *         element in the range greater than the key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
     *         or <tt>toIndex</tt> if all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
     *         elements in the range are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
     *         if {@code fromIndex > toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
     * @throws ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
     *         if {@code fromIndex < 0 or toIndex > a.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
    public static int binarySearch(double[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
                                   double key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
        return binarySearch0(a, fromIndex, toIndex, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
    // Like public version, but without range checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
    private static int binarySearch0(double[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
                                     double key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
        int low = fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
        int high = toIndex - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
            int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
            double midVal = a[mid];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
            if (midVal < key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
                low = mid + 1;  // Neither val is NaN, thisVal is smaller
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
            else if (midVal > key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
                high = mid - 1; // Neither val is NaN, thisVal is larger
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
                long midBits = Double.doubleToLongBits(midVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
                long keyBits = Double.doubleToLongBits(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
                if (midBits == keyBits)     // Values are equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
                    return mid;             // Key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
                else if (midBits < keyBits) // (-0.0, 0.0) or (!NaN, NaN)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
                    low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
                else                        // (0.0, -0.0) or (NaN, !NaN)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
                    high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
        return -(low + 1);  // key not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
     * 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
  2259
     * 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
  2260
     * (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
  2261
     * it is not sorted, the results are undefined. If the array contains
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
     * 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
  2263
     * one will be found. This method considers all NaN values to be
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
     * equivalent and equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
     * @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
  2269
     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
     *         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
  2273
     *         elements in the array are less than the specified key. Note
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
    public static int binarySearch(float[] a, float key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
        return binarySearch0(a, 0, a.length, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
     * Searches a range of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
     * the specified array of floats for the specified value using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
     * the binary search algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
     * The range must be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
     * (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
  2287
     * prior to making this call. If
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
  2288
     * it is not sorted, the results are undefined. If the range contains
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
     * 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
  2290
     * one will be found. This method considers all NaN values to be
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
     * equivalent and equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
     *          searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
     * @param toIndex the index of the last element (exclusive) to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
     * @return index of the search key, if it is contained in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
     *         within the specified range;
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
  2300
     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
     *         element in the range greater than the key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
     *         or <tt>toIndex</tt> if all
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
  2305
     *         elements in the range are less than the specified key. Note
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
     *         if {@code fromIndex > toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
     * @throws ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
     *         if {@code fromIndex < 0 or toIndex > a.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
    public static int binarySearch(float[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
                                   float key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
        return binarySearch0(a, fromIndex, toIndex, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
    // Like public version, but without range checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
    private static int binarySearch0(float[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
                                     float key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
        int low = fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
        int high = toIndex - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
            int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
            float midVal = a[mid];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
            if (midVal < key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
                low = mid + 1;  // Neither val is NaN, thisVal is smaller
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
            else if (midVal > key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
                high = mid - 1; // Neither val is NaN, thisVal is larger
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
                int midBits = Float.floatToIntBits(midVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
                int keyBits = Float.floatToIntBits(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
                if (midBits == keyBits)     // Values are equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
                    return mid;             // Key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
                else if (midBits < keyBits) // (-0.0, 0.0) or (!NaN, NaN)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
                    low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
                else                        // (0.0, -0.0) or (NaN, !NaN)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
                    high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
        return -(low + 1);  // key not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
     * 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
  2350
     * search algorithm. The array must be sorted into ascending order
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
     * according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
     * {@linkplain Comparable natural ordering}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
     * of its elements (as by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
     * {@link #sort(Object[])} method) prior to making this call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
     * If it is not sorted, the results are undefined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
     * (If the array contains elements that are not mutually comparable (for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
     * example, strings and integers), it <i>cannot</i> be sorted according
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
     * to the natural ordering of its elements, hence results are undefined.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
     * If the array contains multiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
     * elements equal to the specified object, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
     * @return index of the search key, if it is contained in the array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
     *         element greater than the key, or <tt>a.length</tt> if all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
     *         elements in the array are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
     * @throws ClassCastException if the search key is not comparable to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
     *         elements of the array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
    public static int binarySearch(Object[] a, Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
        return binarySearch0(a, 0, a.length, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
     * Searches a range of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
     * the specified array for the specified object using the binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
     * search algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
     * The range must be sorted into ascending order
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
     * according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
     * {@linkplain Comparable natural ordering}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
     * of its elements (as by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
     * {@link #sort(Object[], int, int)} method) prior to making this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
     * call.  If it is not sorted, the results are undefined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
     * (If the range contains elements that are not mutually comparable (for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
     * example, strings and integers), it <i>cannot</i> be sorted according
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
     * to the natural ordering of its elements, hence results are undefined.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
     * If the range contains multiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
     * elements equal to the specified object, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
     *          searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
     * @param toIndex the index of the last element (exclusive) to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
     * @return index of the search key, if it is contained in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
     *         within the specified range;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
     *         element in the range greater than the key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
     *         or <tt>toIndex</tt> if all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
     *         elements in the range are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
     * @throws ClassCastException if the search key is not comparable to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
     *         elements of the array within the specified range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
     *         if {@code fromIndex > toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
     * @throws ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
     *         if {@code fromIndex < 0 or toIndex > a.length}
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 int binarySearch(Object[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
                                   Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
        return binarySearch0(a, fromIndex, toIndex, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
    // Like public version, but without range checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
    private static int binarySearch0(Object[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
                                     Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
        int low = fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2430
        int high = toIndex - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2433
            int mid = (low + high) >>> 1;
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  2434
            @SuppressWarnings("rawtypes")
14925
72729557c226 8003981: Support Parallel Array Sorting - JEP 103
chegar
parents: 13795
diff changeset
  2435
            Comparable midVal = (Comparable)a[mid];
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  2436
            @SuppressWarnings("unchecked")
14925
72729557c226 8003981: Support Parallel Array Sorting - JEP 103
chegar
parents: 13795
diff changeset
  2437
            int cmp = midVal.compareTo(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
            if (cmp < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
                low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2441
            else if (cmp > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2442
                high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2443
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2444
                return mid; // key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2445
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2446
        return -(low + 1);  // key not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2447
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2448
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2449
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2450
     * Searches the specified array for the specified object using the binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2451
     * search algorithm.  The array must be sorted into ascending order
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2452
     * according to the specified comparator (as by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2453
     * {@link #sort(Object[], Comparator) sort(T[], Comparator)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2454
     * method) prior to making this call.  If it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2455
     * not sorted, the results are undefined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2456
     * If the array contains multiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2457
     * elements equal to the specified object, there is no guarantee which one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2458
     * will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2459
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  2460
     * @param <T> the class of the objects in the array
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2461
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2462
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2463
     * @param c the comparator by which the array is ordered.  A
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2464
     *        <tt>null</tt> value indicates that the elements'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2465
     *        {@linkplain Comparable natural ordering} should be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2466
     * @return index of the search key, if it is contained in the array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2467
     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2468
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2469
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2470
     *         element greater than the key, or <tt>a.length</tt> if all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2471
     *         elements in the array are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2472
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2473
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2474
     * @throws ClassCastException if the array contains elements that are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2475
     *         <i>mutually comparable</i> using the specified comparator,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2476
     *         or the search key is not comparable to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2477
     *         elements of the array using this comparator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2478
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2479
    public static <T> int binarySearch(T[] a, T key, Comparator<? super T> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2480
        return binarySearch0(a, 0, a.length, key, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2481
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2482
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2483
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2484
     * Searches a range of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2485
     * the specified array for the specified object using the binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2486
     * search algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2487
     * The range must be sorted into ascending order
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2488
     * according to the specified comparator (as by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2489
     * {@link #sort(Object[], int, int, Comparator)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2490
     * sort(T[], int, int, Comparator)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2491
     * method) prior to making this call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2492
     * If it is not sorted, the results are undefined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2493
     * If the range contains multiple elements equal to the specified object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2494
     * there is no guarantee which one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2495
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  2496
     * @param <T> the class of the objects in the array
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2497
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2498
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2499
     *          searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2500
     * @param toIndex the index of the last element (exclusive) to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2501
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2502
     * @param c the comparator by which the array is ordered.  A
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2503
     *        <tt>null</tt> value indicates that the elements'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2504
     *        {@linkplain Comparable natural ordering} should be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2505
     * @return index of the search key, if it is contained in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2506
     *         within the specified range;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2507
     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2508
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2509
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2510
     *         element in the range greater than the key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2511
     *         or <tt>toIndex</tt> if all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2512
     *         elements in the range are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2513
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2514
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2515
     * @throws ClassCastException if the range contains elements that are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2516
     *         <i>mutually comparable</i> using the specified comparator,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2517
     *         or the search key is not comparable to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2518
     *         elements in the range using this comparator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2519
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2520
     *         if {@code fromIndex > toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2521
     * @throws ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2522
     *         if {@code fromIndex < 0 or toIndex > a.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2523
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2524
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2525
    public static <T> int binarySearch(T[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2526
                                       T key, Comparator<? super T> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2527
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2528
        return binarySearch0(a, fromIndex, toIndex, key, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2529
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2530
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2531
    // Like public version, but without range checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2532
    private static <T> int binarySearch0(T[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2533
                                         T key, Comparator<? super T> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2534
        if (c == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2535
            return binarySearch0(a, fromIndex, toIndex, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2536
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2537
        int low = fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2538
        int high = toIndex - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2539
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2540
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2541
            int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2542
            T midVal = a[mid];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2543
            int cmp = c.compare(midVal, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2544
            if (cmp < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2545
                low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2546
            else if (cmp > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2547
                high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2548
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2549
                return mid; // key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2550
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2551
        return -(low + 1);  // key not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2552
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2553
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2554
    // Equality Testing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2555
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2556
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2557
     * Returns <tt>true</tt> if the two specified arrays of longs are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2558
     * <i>equal</i> to one another.  Two arrays are considered equal if both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2559
     * arrays contain the same number of elements, and all corresponding pairs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2560
     * of elements in the two arrays are equal.  In other words, two arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2561
     * are equal if they contain the same elements in the same order.  Also,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2562
     * two array references are considered equal if both are <tt>null</tt>.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2563
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2564
     * @param a one array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2565
     * @param a2 the other array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2566
     * @return <tt>true</tt> if the two arrays are equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2567
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2568
    public static boolean equals(long[] a, long[] a2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2569
        if (a==a2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2570
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2571
        if (a==null || a2==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2572
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2573
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2574
        int length = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2575
        if (a2.length != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2576
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2577
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2578
        for (int i=0; i<length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2579
            if (a[i] != a2[i])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2580
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2581
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2582
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2583
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2584
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2585
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2586
     * Returns <tt>true</tt> if the two specified arrays of ints are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2587
     * <i>equal</i> to one another.  Two arrays are considered equal if both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2588
     * arrays contain the same number of elements, and all corresponding pairs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2589
     * of elements in the two arrays are equal.  In other words, two arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2590
     * are equal if they contain the same elements in the same order.  Also,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2591
     * two array references are considered equal if both are <tt>null</tt>.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2592
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2593
     * @param a one array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2594
     * @param a2 the other array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2595
     * @return <tt>true</tt> if the two arrays are equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2596
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2597
    public static boolean equals(int[] a, int[] a2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2598
        if (a==a2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2599
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2600
        if (a==null || a2==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2601
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2602
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2603
        int length = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2604
        if (a2.length != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2605
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2606
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2607
        for (int i=0; i<length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2608
            if (a[i] != a2[i])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2609
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2610
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2611
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2612
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2613
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2614
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2615
     * Returns <tt>true</tt> if the two specified arrays of shorts are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2616
     * <i>equal</i> to one another.  Two arrays are considered equal if both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2617
     * arrays contain the same number of elements, and all corresponding pairs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2618
     * of elements in the two arrays are equal.  In other words, two arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2619
     * are equal if they contain the same elements in the same order.  Also,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2620
     * two array references are considered equal if both are <tt>null</tt>.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2621
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2622
     * @param a one array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2623
     * @param a2 the other array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2624
     * @return <tt>true</tt> if the two arrays are equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2625
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2626
    public static boolean equals(short[] a, short a2[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2627
        if (a==a2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2628
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2629
        if (a==null || a2==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2630
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2631
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2632
        int length = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2633
        if (a2.length != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2634
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2635
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2636
        for (int i=0; i<length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2637
            if (a[i] != a2[i])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2638
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2639
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2640
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2641
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2642
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2643
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2644
     * Returns <tt>true</tt> if the two specified arrays of chars are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2645
     * <i>equal</i> to one another.  Two arrays are considered equal if both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2646
     * arrays contain the same number of elements, and all corresponding pairs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2647
     * of elements in the two arrays are equal.  In other words, two arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2648
     * are equal if they contain the same elements in the same order.  Also,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2649
     * two array references are considered equal if both are <tt>null</tt>.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2650
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2651
     * @param a one array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2652
     * @param a2 the other array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2653
     * @return <tt>true</tt> if the two arrays are equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2654
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2655
    public static boolean equals(char[] a, char[] a2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2656
        if (a==a2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2657
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2658
        if (a==null || a2==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2659
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2660
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2661
        int length = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2662
        if (a2.length != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2663
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2664
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2665
        for (int i=0; i<length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2666
            if (a[i] != a2[i])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2667
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2668
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2669
        return true;
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
     * Returns <tt>true</tt> if the two specified arrays of bytes are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2674
     * <i>equal</i> to one another.  Two arrays are considered equal if both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2675
     * arrays contain the same number of elements, and all corresponding pairs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2676
     * of elements in the two arrays are equal.  In other words, two arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2677
     * are equal if they contain the same elements in the same order.  Also,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2678
     * two array references are considered equal if both are <tt>null</tt>.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2679
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2680
     * @param a one array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2681
     * @param a2 the other array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2682
     * @return <tt>true</tt> if the two arrays are equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2683
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2684
    public static boolean equals(byte[] a, byte[] a2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2685
        if (a==a2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2686
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2687
        if (a==null || a2==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2688
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2689
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2690
        int length = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2691
        if (a2.length != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2692
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2693
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2694
        for (int i=0; i<length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2695
            if (a[i] != a2[i])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2696
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2697
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2698
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2699
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2700
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2701
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2702
     * Returns <tt>true</tt> if the two specified arrays of booleans are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2703
     * <i>equal</i> to one another.  Two arrays are considered equal if both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2704
     * arrays contain the same number of elements, and all corresponding pairs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2705
     * of elements in the two arrays are equal.  In other words, two arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2706
     * are equal if they contain the same elements in the same order.  Also,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2707
     * two array references are considered equal if both are <tt>null</tt>.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2708
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2709
     * @param a one array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2710
     * @param a2 the other array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2711
     * @return <tt>true</tt> if the two arrays are equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2712
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2713
    public static boolean equals(boolean[] a, boolean[] a2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2714
        if (a==a2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2715
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2716
        if (a==null || a2==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2717
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2718
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2719
        int length = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2720
        if (a2.length != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2721
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2722
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2723
        for (int i=0; i<length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2724
            if (a[i] != a2[i])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2725
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2726
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2727
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2728
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2729
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2730
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2731
     * Returns <tt>true</tt> if the two specified arrays of doubles are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2732
     * <i>equal</i> to one another.  Two arrays are considered equal if both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2733
     * arrays contain the same number of elements, and all corresponding pairs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2734
     * of elements in the two arrays are equal.  In other words, two arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2735
     * are equal if they contain the same elements in the same order.  Also,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2736
     * two array references are considered equal if both are <tt>null</tt>.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2737
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2738
     * Two doubles <tt>d1</tt> and <tt>d2</tt> are considered equal if:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2739
     * <pre>    <tt>new Double(d1).equals(new Double(d2))</tt></pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2740
     * (Unlike the <tt>==</tt> operator, this method considers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2741
     * <tt>NaN</tt> equals to itself, and 0.0d unequal to -0.0d.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2742
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2743
     * @param a one array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2744
     * @param a2 the other array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2745
     * @return <tt>true</tt> if the two arrays are equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2746
     * @see Double#equals(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2747
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2748
    public static boolean equals(double[] a, double[] a2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2749
        if (a==a2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2750
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2751
        if (a==null || a2==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2752
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2753
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2754
        int length = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2755
        if (a2.length != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2756
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2757
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2758
        for (int i=0; i<length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2759
            if (Double.doubleToLongBits(a[i])!=Double.doubleToLongBits(a2[i]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2760
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2761
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2762
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2763
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2764
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2765
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2766
     * Returns <tt>true</tt> if the two specified arrays of floats are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2767
     * <i>equal</i> to one another.  Two arrays are considered equal if both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2768
     * arrays contain the same number of elements, and all corresponding pairs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2769
     * of elements in the two arrays are equal.  In other words, two arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2770
     * are equal if they contain the same elements in the same order.  Also,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2771
     * two array references are considered equal if both are <tt>null</tt>.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2772
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2773
     * Two floats <tt>f1</tt> and <tt>f2</tt> are considered equal if:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2774
     * <pre>    <tt>new Float(f1).equals(new Float(f2))</tt></pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2775
     * (Unlike the <tt>==</tt> operator, this method considers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2776
     * <tt>NaN</tt> equals to itself, and 0.0f unequal to -0.0f.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2777
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2778
     * @param a one array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2779
     * @param a2 the other array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2780
     * @return <tt>true</tt> if the two arrays are equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2781
     * @see Float#equals(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2782
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2783
    public static boolean equals(float[] a, float[] a2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2784
        if (a==a2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2785
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2786
        if (a==null || a2==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2787
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2788
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2789
        int length = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2790
        if (a2.length != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2791
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2792
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2793
        for (int i=0; i<length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2794
            if (Float.floatToIntBits(a[i])!=Float.floatToIntBits(a2[i]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2795
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2796
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2797
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2798
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2799
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2800
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2801
     * Returns <tt>true</tt> if the two specified arrays of Objects are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2802
     * <i>equal</i> to one another.  The two arrays are considered equal if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2803
     * both arrays contain the same number of elements, and all corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2804
     * pairs of elements in the two arrays are equal.  Two objects <tt>e1</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2805
     * and <tt>e2</tt> are considered <i>equal</i> if <tt>(e1==null ? e2==null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2806
     * : e1.equals(e2))</tt>.  In other words, the two arrays are equal if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2807
     * they contain the same elements in the same order.  Also, two array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2808
     * references are considered equal if both are <tt>null</tt>.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2809
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2810
     * @param a one array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2811
     * @param a2 the other array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2812
     * @return <tt>true</tt> if the two arrays are equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2813
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2814
    public static boolean equals(Object[] a, Object[] a2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2815
        if (a==a2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2816
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2817
        if (a==null || a2==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2818
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2819
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2820
        int length = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2821
        if (a2.length != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2822
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2823
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2824
        for (int i=0; i<length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2825
            Object o1 = a[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2826
            Object o2 = a2[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2827
            if (!(o1==null ? o2==null : o1.equals(o2)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2828
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2829
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2830
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2831
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2832
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2833
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2834
    // Filling
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2835
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2836
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2837
     * Assigns the specified long value to each element of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2838
     * of longs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2839
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2840
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2841
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2842
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2843
    public static void fill(long[] a, long val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2844
        for (int i = 0, len = a.length; i < len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2845
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2846
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2847
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2848
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2849
     * Assigns the specified long value to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2850
     * range of the specified array of longs.  The range to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2851
     * extends from index <tt>fromIndex</tt>, inclusive, to index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2852
     * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2853
     * range to be filled is empty.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2854
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2855
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2856
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2857
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2858
     * @param toIndex the index of the last element (exclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2859
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2860
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2861
     * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2862
     * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2863
     *         <tt>toIndex &gt; a.length</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2864
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2865
    public static void fill(long[] a, int fromIndex, int toIndex, long val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2866
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2867
        for (int i = fromIndex; i < toIndex; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2868
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2869
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2870
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2871
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2872
     * Assigns the specified int value to each element of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2873
     * of ints.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2874
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2875
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2876
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2877
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2878
    public static void fill(int[] a, int val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2879
        for (int i = 0, len = a.length; i < len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2880
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2881
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2882
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2883
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2884
     * Assigns the specified int value to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2885
     * range of the specified array of ints.  The range to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2886
     * extends from index <tt>fromIndex</tt>, inclusive, to index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2887
     * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2888
     * range to be filled is empty.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2889
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2890
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2891
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2892
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2893
     * @param toIndex the index of the last element (exclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2894
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2895
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2896
     * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2897
     * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2898
     *         <tt>toIndex &gt; a.length</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2899
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2900
    public static void fill(int[] a, int fromIndex, int toIndex, int val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2901
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2902
        for (int i = fromIndex; i < toIndex; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2903
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2904
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2905
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2906
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2907
     * Assigns the specified short value to each element of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2908
     * of shorts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2909
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2910
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2911
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2912
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2913
    public static void fill(short[] a, short val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2914
        for (int i = 0, len = a.length; i < len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2915
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2916
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2917
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2918
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2919
     * Assigns the specified short value to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2920
     * range of the specified array of shorts.  The range to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2921
     * extends from index <tt>fromIndex</tt>, inclusive, to index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2922
     * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2923
     * range to be filled is empty.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2924
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2925
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2926
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2927
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2928
     * @param toIndex the index of the last element (exclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2929
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2930
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2931
     * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2932
     * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2933
     *         <tt>toIndex &gt; a.length</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2934
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2935
    public static void fill(short[] a, int fromIndex, int toIndex, short val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2936
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2937
        for (int i = fromIndex; i < toIndex; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2938
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2939
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2940
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2941
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2942
     * Assigns the specified char value to each element of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2943
     * of chars.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2944
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2945
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2946
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2947
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2948
    public static void fill(char[] a, char val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2949
        for (int i = 0, len = a.length; i < len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2950
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2951
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2952
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2953
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2954
     * Assigns the specified char value to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2955
     * range of the specified array of chars.  The range to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2956
     * extends from index <tt>fromIndex</tt>, inclusive, to index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2957
     * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2958
     * range to be filled is empty.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2959
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2960
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2961
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2962
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2963
     * @param toIndex the index of the last element (exclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2964
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2965
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2966
     * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2967
     * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2968
     *         <tt>toIndex &gt; a.length</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2969
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2970
    public static void fill(char[] a, int fromIndex, int toIndex, char val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2971
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2972
        for (int i = fromIndex; i < toIndex; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2973
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2974
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2975
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2976
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2977
     * Assigns the specified byte value to each element of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2978
     * of bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2979
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2980
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2981
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2982
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2983
    public static void fill(byte[] a, byte val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2984
        for (int i = 0, len = a.length; i < len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2985
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2986
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2987
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2988
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2989
     * Assigns the specified byte value to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2990
     * range of the specified array of bytes.  The range to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2991
     * extends from index <tt>fromIndex</tt>, inclusive, to index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2992
     * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2993
     * range to be filled is empty.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2994
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2995
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2996
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2997
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2998
     * @param toIndex the index of the last element (exclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2999
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3000
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3001
     * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3002
     * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3003
     *         <tt>toIndex &gt; a.length</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3004
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3005
    public static void fill(byte[] a, int fromIndex, int toIndex, byte val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3006
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3007
        for (int i = fromIndex; i < toIndex; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3008
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3009
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3010
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3011
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3012
     * Assigns the specified boolean value to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3013
     * array of booleans.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3014
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3015
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3016
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3017
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3018
    public static void fill(boolean[] a, boolean val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3019
        for (int i = 0, len = a.length; i < len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3020
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3021
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3022
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3023
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3024
     * Assigns the specified boolean value to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3025
     * range of the specified array of booleans.  The range to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3026
     * extends from index <tt>fromIndex</tt>, inclusive, to index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3027
     * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3028
     * range to be filled is empty.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3029
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3030
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3031
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3032
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3033
     * @param toIndex the index of the last element (exclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3034
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3035
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3036
     * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3037
     * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3038
     *         <tt>toIndex &gt; a.length</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3039
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3040
    public static void fill(boolean[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3041
                            boolean val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3042
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3043
        for (int i = fromIndex; i < toIndex; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3044
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3045
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3046
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3047
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3048
     * Assigns the specified double value to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3049
     * array of doubles.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3050
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3051
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3052
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3053
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3054
    public static void fill(double[] a, double val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3055
        for (int i = 0, len = a.length; i < len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3056
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3057
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3058
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3059
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3060
     * Assigns the specified double value to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3061
     * range of the specified array of doubles.  The range to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3062
     * extends from index <tt>fromIndex</tt>, inclusive, to index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3063
     * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3064
     * range to be filled is empty.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3065
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3066
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3067
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3068
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3069
     * @param toIndex the index of the last element (exclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3070
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3071
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3072
     * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3073
     * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3074
     *         <tt>toIndex &gt; a.length</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3075
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3076
    public static void fill(double[] a, int fromIndex, int toIndex,double val){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3077
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3078
        for (int i = fromIndex; i < toIndex; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3079
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3080
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3081
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3082
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3083
     * Assigns the specified float value to each element of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3084
     * of floats.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3085
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3086
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3087
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3088
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3089
    public static void fill(float[] a, float val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3090
        for (int i = 0, len = a.length; i < len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3091
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3092
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3093
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3094
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3095
     * Assigns the specified float value to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3096
     * range of the specified array of floats.  The range to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3097
     * extends from index <tt>fromIndex</tt>, inclusive, to index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3098
     * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3099
     * range to be filled is empty.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3100
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3101
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3102
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3103
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3104
     * @param toIndex the index of the last element (exclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3105
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3106
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3107
     * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3108
     * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3109
     *         <tt>toIndex &gt; a.length</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3110
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3111
    public static void fill(float[] a, int fromIndex, int toIndex, float val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3112
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3113
        for (int i = fromIndex; i < toIndex; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3114
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3116
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3118
     * Assigns the specified Object reference to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3119
     * array of Objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3120
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3121
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3122
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3123
     * @throws ArrayStoreException if the specified value is not of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3124
     *         runtime type that can be stored in the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3126
    public static void fill(Object[] a, Object val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3127
        for (int i = 0, len = a.length; i < len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3128
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3130
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3131
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3132
     * Assigns the specified Object reference to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3133
     * range of the specified array of Objects.  The range to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3134
     * extends from index <tt>fromIndex</tt>, inclusive, to index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3135
     * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3136
     * range to be filled is empty.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3137
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3138
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3139
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3140
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3141
     * @param toIndex the index of the last element (exclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3142
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3143
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3144
     * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3145
     * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3146
     *         <tt>toIndex &gt; a.length</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3147
     * @throws ArrayStoreException if the specified value is not of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3148
     *         runtime type that can be stored in the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3150
    public static void fill(Object[] a, int fromIndex, int toIndex, Object val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3151
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3152
        for (int i = fromIndex; i < toIndex; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3153
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3155
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
  3156
    // Cloning
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3157
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3159
     * Copies the specified array, truncating or padding with nulls (if necessary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3160
     * so the copy has the specified length.  For all indices that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3161
     * valid in both the original array and the copy, the two arrays will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3162
     * contain identical values.  For any indices that are valid in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3163
     * copy but not the original, the copy will contain <tt>null</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3164
     * Such indices will exist if and only if the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3165
     * is greater than that of the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3166
     * The resulting array is of exactly the same class as the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3167
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  3168
     * @param <T> the class of the objects in the array
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3169
     * @param original the array to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3170
     * @param newLength the length of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3171
     * @return a copy of the original array, truncated or padded with nulls
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3172
     *     to obtain the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3173
     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3174
     * @throws NullPointerException if <tt>original</tt> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3175
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3176
     */
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  3177
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3178
    public static <T> T[] copyOf(T[] original, int newLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3179
        return (T[]) copyOf(original, newLength, original.getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3181
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3182
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3183
     * Copies the specified array, truncating or padding with nulls (if necessary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3184
     * so the copy has the specified length.  For all indices that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3185
     * valid in both the original array and the copy, the two arrays will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3186
     * contain identical values.  For any indices that are valid in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3187
     * copy but not the original, the copy will contain <tt>null</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3188
     * Such indices will exist if and only if the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3189
     * is greater than that of the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3190
     * The resulting array is of the class <tt>newType</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3191
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  3192
     * @param <U> the class of the objects in the original array
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  3193
     * @param <T> the class of the objects in the returned array
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3194
     * @param original the array to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3195
     * @param newLength the length of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3196
     * @param newType the class of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3197
     * @return a copy of the original array, truncated or padded with nulls
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3198
     *     to obtain the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3199
     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3200
     * @throws NullPointerException if <tt>original</tt> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3201
     * @throws ArrayStoreException if an element copied from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3202
     *     <tt>original</tt> is not of a runtime type that can be stored in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3203
     *     an array of class <tt>newType</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3204
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3205
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3206
    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
  3207
        @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3208
        T[] copy = ((Object)newType == (Object)Object[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3209
            ? (T[]) new Object[newLength]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3210
            : (T[]) Array.newInstance(newType.getComponentType(), newLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3211
        System.arraycopy(original, 0, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3212
                         Math.min(original.length, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3213
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3215
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3216
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3217
     * Copies the specified array, truncating or padding with zeros (if necessary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3218
     * so the copy has the specified length.  For all indices that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3219
     * valid in both the original array and the copy, the two arrays will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3220
     * contain identical values.  For any indices that are valid in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3221
     * copy but not the original, the copy will contain <tt>(byte)0</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3222
     * Such indices will exist if and only if the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3223
     * is greater than that of the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3224
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3225
     * @param original the array to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3226
     * @param newLength the length of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3227
     * @return a copy of the original array, truncated or padded with zeros
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3228
     *     to obtain the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3229
     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3230
     * @throws NullPointerException if <tt>original</tt> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3231
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3232
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3233
    public static byte[] copyOf(byte[] original, int newLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3234
        byte[] copy = new byte[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3235
        System.arraycopy(original, 0, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3236
                         Math.min(original.length, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3237
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3238
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3239
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3240
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3241
     * Copies the specified array, truncating or padding with zeros (if necessary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3242
     * so the copy has the specified length.  For all indices that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3243
     * valid in both the original array and the copy, the two arrays will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3244
     * contain identical values.  For any indices that are valid in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3245
     * copy but not the original, the copy will contain <tt>(short)0</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3246
     * Such indices will exist if and only if the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3247
     * is greater than that of the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3248
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3249
     * @param original the array to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3250
     * @param newLength the length of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3251
     * @return a copy of the original array, truncated or padded with zeros
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3252
     *     to obtain the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3253
     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3254
     * @throws NullPointerException if <tt>original</tt> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3255
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3256
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3257
    public static short[] copyOf(short[] original, int newLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3258
        short[] copy = new short[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3259
        System.arraycopy(original, 0, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3260
                         Math.min(original.length, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3261
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3263
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3264
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3265
     * Copies the specified array, truncating or padding with zeros (if necessary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3266
     * so the copy has the specified length.  For all indices that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3267
     * valid in both the original array and the copy, the two arrays will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3268
     * contain identical values.  For any indices that are valid in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3269
     * copy but not the original, the copy will contain <tt>0</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3270
     * Such indices will exist if and only if the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3271
     * is greater than that of the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3272
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3273
     * @param original the array to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3274
     * @param newLength the length of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3275
     * @return a copy of the original array, truncated or padded with zeros
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3276
     *     to obtain the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3277
     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3278
     * @throws NullPointerException if <tt>original</tt> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3279
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3280
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3281
    public static int[] copyOf(int[] original, int newLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3282
        int[] copy = new int[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3283
        System.arraycopy(original, 0, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3284
                         Math.min(original.length, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3285
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3287
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3288
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3289
     * Copies the specified array, truncating or padding with zeros (if necessary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3290
     * so the copy has the specified length.  For all indices that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3291
     * valid in both the original array and the copy, the two arrays will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3292
     * contain identical values.  For any indices that are valid in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3293
     * copy but not the original, the copy will contain <tt>0L</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3294
     * Such indices will exist if and only if the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3295
     * is greater than that of the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3296
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3297
     * @param original the array to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3298
     * @param newLength the length of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3299
     * @return a copy of the original array, truncated or padded with zeros
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3300
     *     to obtain the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3301
     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3302
     * @throws NullPointerException if <tt>original</tt> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3303
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3304
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3305
    public static long[] copyOf(long[] original, int newLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3306
        long[] copy = new long[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3307
        System.arraycopy(original, 0, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3308
                         Math.min(original.length, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3309
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3311
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3312
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3313
     * Copies the specified array, truncating or padding with null characters (if necessary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3314
     * so the copy has the specified length.  For all indices that are valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3315
     * in both the original array and the copy, the two arrays will contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3316
     * identical values.  For any indices that are valid in the copy but not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3317
     * the original, the copy will contain <tt>'\\u000'</tt>.  Such indices
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3318
     * will exist if and only if the specified length is greater than that of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3319
     * the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3320
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3321
     * @param original the array to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3322
     * @param newLength the length of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3323
     * @return a copy of the original array, truncated or padded with null characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3324
     *     to obtain the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3325
     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3326
     * @throws NullPointerException if <tt>original</tt> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3327
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3328
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3329
    public static char[] copyOf(char[] original, int newLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3330
        char[] copy = new char[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3331
        System.arraycopy(original, 0, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3332
                         Math.min(original.length, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3333
        return copy;
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
     * Copies the specified array, truncating or padding with zeros (if necessary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3338
     * so the copy has the specified length.  For all indices that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3339
     * valid in both the original array and the copy, the two arrays will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3340
     * contain identical values.  For any indices that are valid in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3341
     * copy but not the original, the copy will contain <tt>0f</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3342
     * Such indices will exist if and only if the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3343
     * is greater than that of the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3344
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3345
     * @param original the array to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3346
     * @param newLength the length of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3347
     * @return a copy of the original array, truncated or padded with zeros
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3348
     *     to obtain the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3349
     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3350
     * @throws NullPointerException if <tt>original</tt> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3351
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3352
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3353
    public static float[] copyOf(float[] original, int newLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3354
        float[] copy = new float[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3355
        System.arraycopy(original, 0, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3356
                         Math.min(original.length, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3357
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3358
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3359
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3360
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3361
     * Copies the specified array, truncating or padding with zeros (if necessary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3362
     * so the copy has the specified length.  For all indices that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3363
     * valid in both the original array and the copy, the two arrays will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3364
     * contain identical values.  For any indices that are valid in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3365
     * copy but not the original, the copy will contain <tt>0d</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3366
     * Such indices will exist if and only if the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3367
     * is greater than that of the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3368
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3369
     * @param original the array to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3370
     * @param newLength the length of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3371
     * @return a copy of the original array, truncated or padded with zeros
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3372
     *     to obtain the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3373
     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3374
     * @throws NullPointerException if <tt>original</tt> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3375
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3376
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3377
    public static double[] copyOf(double[] original, int newLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3378
        double[] copy = new double[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3379
        System.arraycopy(original, 0, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3380
                         Math.min(original.length, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3381
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3382
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3383
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3384
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3385
     * Copies the specified array, truncating or padding with <tt>false</tt> (if necessary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3386
     * so the copy has the specified length.  For all indices that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3387
     * valid in both the original array and the copy, the two arrays will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3388
     * contain identical values.  For any indices that are valid in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3389
     * copy but not the original, the copy will contain <tt>false</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3390
     * Such indices will exist if and only if the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3391
     * is greater than that of the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3392
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3393
     * @param original the array to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3394
     * @param newLength the length of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3395
     * @return a copy of the original array, truncated or padded with false elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3396
     *     to obtain the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3397
     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3398
     * @throws NullPointerException if <tt>original</tt> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3399
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3400
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3401
    public static boolean[] copyOf(boolean[] original, int newLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3402
        boolean[] copy = new boolean[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3403
        System.arraycopy(original, 0, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3404
                         Math.min(original.length, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3405
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3406
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3407
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3408
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3409
     * Copies the specified range of the specified array into a new array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3410
     * The initial index of the range (<tt>from</tt>) must lie between zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3411
     * and <tt>original.length</tt>, inclusive.  The value at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3412
     * <tt>original[from]</tt> is placed into the initial element of the copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3413
     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3414
     * Values from subsequent elements in the original array are placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3415
     * subsequent elements in the copy.  The final index of the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3416
     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3417
     * may be greater than <tt>original.length</tt>, in which case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3418
     * <tt>null</tt> is placed in all elements of the copy whose index is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3419
     * greater than or equal to <tt>original.length - from</tt>.  The length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3420
     * of the returned array will be <tt>to - from</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3421
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3422
     * The resulting array is of exactly the same class as the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3423
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  3424
     * @param <T> the class of the objects in the array
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3425
     * @param original the array from which a range is to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3426
     * @param from the initial index of the range to be copied, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3427
     * @param to the final index of the range to be copied, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3428
     *     (This index may lie outside the array.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3429
     * @return a new array containing the specified range from the original array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3430
     *     truncated or padded with nulls to obtain the required length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3431
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3432
     *     or {@code from > original.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3433
     * @throws IllegalArgumentException if <tt>from &gt; to</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3434
     * @throws NullPointerException if <tt>original</tt> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3435
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3436
     */
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  3437
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3438
    public static <T> T[] copyOfRange(T[] original, int from, int to) {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  3439
        return copyOfRange(original, from, to, (Class<? extends T[]>) original.getClass());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3440
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3441
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3442
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3443
     * Copies the specified range of the specified array into a new array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3444
     * The initial index of the range (<tt>from</tt>) must lie between zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3445
     * and <tt>original.length</tt>, inclusive.  The value at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3446
     * <tt>original[from]</tt> is placed into the initial element of the copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3447
     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3448
     * Values from subsequent elements in the original array are placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3449
     * subsequent elements in the copy.  The final index of the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3450
     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3451
     * may be greater than <tt>original.length</tt>, in which case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3452
     * <tt>null</tt> is placed in all elements of the copy whose index is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3453
     * greater than or equal to <tt>original.length - from</tt>.  The length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3454
     * of the returned array will be <tt>to - from</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3455
     * The resulting array is of the class <tt>newType</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3456
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  3457
     * @param <U> the class of the objects in the original array
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  3458
     * @param <T> the class of the objects in the returned array
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3459
     * @param original the array from which a range is to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3460
     * @param from the initial index of the range to be copied, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3461
     * @param to the final index of the range to be copied, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3462
     *     (This index may lie outside the array.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3463
     * @param newType the class of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3464
     * @return a new array containing the specified range from the original array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3465
     *     truncated or padded with nulls to obtain the required length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3466
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3467
     *     or {@code from > original.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3468
     * @throws IllegalArgumentException if <tt>from &gt; to</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3469
     * @throws NullPointerException if <tt>original</tt> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3470
     * @throws ArrayStoreException if an element copied from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3471
     *     <tt>original</tt> is not of a runtime type that can be stored in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3472
     *     an array of class <tt>newType</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3473
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3474
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3475
    public static <T,U> T[] copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3476
        int newLength = to - from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3477
        if (newLength < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3478
            throw new IllegalArgumentException(from + " > " + to);
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  3479
        @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3480
        T[] copy = ((Object)newType == (Object)Object[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3481
            ? (T[]) new Object[newLength]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3482
            : (T[]) Array.newInstance(newType.getComponentType(), newLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3483
        System.arraycopy(original, from, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3484
                         Math.min(original.length - from, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3485
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3486
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3487
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3488
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3489
     * Copies the specified range of the specified array into a new array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3490
     * The initial index of the range (<tt>from</tt>) must lie between zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3491
     * and <tt>original.length</tt>, inclusive.  The value at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3492
     * <tt>original[from]</tt> is placed into the initial element of the copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3493
     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3494
     * Values from subsequent elements in the original array are placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3495
     * subsequent elements in the copy.  The final index of the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3496
     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3497
     * may be greater than <tt>original.length</tt>, in which case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3498
     * <tt>(byte)0</tt> is placed in all elements of the copy whose index is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3499
     * greater than or equal to <tt>original.length - from</tt>.  The length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3500
     * of the returned array will be <tt>to - from</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3501
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3502
     * @param original the array from which a range is to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3503
     * @param from the initial index of the range to be copied, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3504
     * @param to the final index of the range to be copied, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3505
     *     (This index may lie outside the array.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3506
     * @return a new array containing the specified range from the original array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3507
     *     truncated or padded with zeros to obtain the required length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3508
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3509
     *     or {@code from > original.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3510
     * @throws IllegalArgumentException if <tt>from &gt; to</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3511
     * @throws NullPointerException if <tt>original</tt> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3512
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3513
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3514
    public static byte[] copyOfRange(byte[] original, int from, int to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3515
        int newLength = to - from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3516
        if (newLength < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3517
            throw new IllegalArgumentException(from + " > " + to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3518
        byte[] copy = new byte[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3519
        System.arraycopy(original, from, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3520
                         Math.min(original.length - from, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3521
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3522
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3523
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3524
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3525
     * Copies the specified range of the specified array into a new array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3526
     * The initial index of the range (<tt>from</tt>) must lie between zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3527
     * and <tt>original.length</tt>, inclusive.  The value at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3528
     * <tt>original[from]</tt> is placed into the initial element of the copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3529
     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3530
     * Values from subsequent elements in the original array are placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3531
     * subsequent elements in the copy.  The final index of the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3532
     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3533
     * may be greater than <tt>original.length</tt>, in which case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3534
     * <tt>(short)0</tt> is placed in all elements of the copy whose index is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3535
     * greater than or equal to <tt>original.length - from</tt>.  The length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3536
     * of the returned array will be <tt>to - from</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3537
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3538
     * @param original the array from which a range is to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3539
     * @param from the initial index of the range to be copied, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3540
     * @param to the final index of the range to be copied, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3541
     *     (This index may lie outside the array.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3542
     * @return a new array containing the specified range from the original array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3543
     *     truncated or padded with zeros to obtain the required length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3544
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3545
     *     or {@code from > original.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3546
     * @throws IllegalArgumentException if <tt>from &gt; to</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3547
     * @throws NullPointerException if <tt>original</tt> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3548
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3549
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3550
    public static short[] copyOfRange(short[] original, int from, int to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3551
        int newLength = to - from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3552
        if (newLength < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3553
            throw new IllegalArgumentException(from + " > " + to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3554
        short[] copy = new short[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3555
        System.arraycopy(original, from, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3556
                         Math.min(original.length - from, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3557
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3558
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3559
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3560
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3561
     * Copies the specified range of the specified array into a new array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3562
     * The initial index of the range (<tt>from</tt>) must lie between zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3563
     * and <tt>original.length</tt>, inclusive.  The value at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3564
     * <tt>original[from]</tt> is placed into the initial element of the copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3565
     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3566
     * Values from subsequent elements in the original array are placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3567
     * subsequent elements in the copy.  The final index of the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3568
     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3569
     * may be greater than <tt>original.length</tt>, in which case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3570
     * <tt>0</tt> is placed in all elements of the copy whose index is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3571
     * greater than or equal to <tt>original.length - from</tt>.  The length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3572
     * of the returned array will be <tt>to - from</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3573
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3574
     * @param original the array from which a range is to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3575
     * @param from the initial index of the range to be copied, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3576
     * @param to the final index of the range to be copied, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3577
     *     (This index may lie outside the array.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3578
     * @return a new array containing the specified range from the original array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3579
     *     truncated or padded with zeros to obtain the required length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3580
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3581
     *     or {@code from > original.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3582
     * @throws IllegalArgumentException if <tt>from &gt; to</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3583
     * @throws NullPointerException if <tt>original</tt> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3584
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3585
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3586
    public static int[] copyOfRange(int[] original, int from, int to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3587
        int newLength = to - from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3588
        if (newLength < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3589
            throw new IllegalArgumentException(from + " > " + to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3590
        int[] copy = new int[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3591
        System.arraycopy(original, from, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3592
                         Math.min(original.length - from, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3593
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3594
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3595
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3596
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3597
     * Copies the specified range of the specified array into a new array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3598
     * The initial index of the range (<tt>from</tt>) must lie between zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3599
     * and <tt>original.length</tt>, inclusive.  The value at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3600
     * <tt>original[from]</tt> is placed into the initial element of the copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3601
     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3602
     * Values from subsequent elements in the original array are placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3603
     * subsequent elements in the copy.  The final index of the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3604
     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3605
     * may be greater than <tt>original.length</tt>, in which case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3606
     * <tt>0L</tt> is placed in all elements of the copy whose index is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3607
     * greater than or equal to <tt>original.length - from</tt>.  The length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3608
     * of the returned array will be <tt>to - from</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3609
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3610
     * @param original the array from which a range is to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3611
     * @param from the initial index of the range to be copied, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3612
     * @param to the final index of the range to be copied, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3613
     *     (This index may lie outside the array.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3614
     * @return a new array containing the specified range from the original array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3615
     *     truncated or padded with zeros to obtain the required length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3616
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3617
     *     or {@code from > original.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3618
     * @throws IllegalArgumentException if <tt>from &gt; to</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3619
     * @throws NullPointerException if <tt>original</tt> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3620
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3621
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3622
    public static long[] copyOfRange(long[] original, int from, int to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3623
        int newLength = to - from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3624
        if (newLength < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3625
            throw new IllegalArgumentException(from + " > " + to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3626
        long[] copy = new long[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3627
        System.arraycopy(original, from, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3628
                         Math.min(original.length - from, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3629
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3630
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3631
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3632
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3633
     * Copies the specified range of the specified array into a new array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3634
     * The initial index of the range (<tt>from</tt>) must lie between zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3635
     * and <tt>original.length</tt>, inclusive.  The value at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3636
     * <tt>original[from]</tt> is placed into the initial element of the copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3637
     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3638
     * Values from subsequent elements in the original array are placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3639
     * subsequent elements in the copy.  The final index of the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3640
     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3641
     * may be greater than <tt>original.length</tt>, in which case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3642
     * <tt>'\\u000'</tt> is placed in all elements of the copy whose index is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3643
     * greater than or equal to <tt>original.length - from</tt>.  The length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3644
     * of the returned array will be <tt>to - from</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3645
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3646
     * @param original the array from which a range is to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3647
     * @param from the initial index of the range to be copied, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3648
     * @param to the final index of the range to be copied, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3649
     *     (This index may lie outside the array.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3650
     * @return a new array containing the specified range from the original array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3651
     *     truncated or padded with null characters to obtain the required length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3652
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3653
     *     or {@code from > original.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3654
     * @throws IllegalArgumentException if <tt>from &gt; to</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3655
     * @throws NullPointerException if <tt>original</tt> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3656
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3657
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3658
    public static char[] copyOfRange(char[] original, int from, int to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3659
        int newLength = to - from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3660
        if (newLength < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3661
            throw new IllegalArgumentException(from + " > " + to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3662
        char[] copy = new char[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3663
        System.arraycopy(original, from, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3664
                         Math.min(original.length - from, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3665
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3666
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3667
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3668
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3669
     * Copies the specified range of the specified array into a new array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3670
     * The initial index of the range (<tt>from</tt>) must lie between zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3671
     * and <tt>original.length</tt>, inclusive.  The value at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3672
     * <tt>original[from]</tt> is placed into the initial element of the copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3673
     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3674
     * Values from subsequent elements in the original array are placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3675
     * subsequent elements in the copy.  The final index of the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3676
     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3677
     * may be greater than <tt>original.length</tt>, in which case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3678
     * <tt>0f</tt> is placed in all elements of the copy whose index is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3679
     * greater than or equal to <tt>original.length - from</tt>.  The length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3680
     * of the returned array will be <tt>to - from</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3681
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3682
     * @param original the array from which a range is to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3683
     * @param from the initial index of the range to be copied, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3684
     * @param to the final index of the range to be copied, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3685
     *     (This index may lie outside the array.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3686
     * @return a new array containing the specified range from the original array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3687
     *     truncated or padded with zeros to obtain the required length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3688
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3689
     *     or {@code from > original.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3690
     * @throws IllegalArgumentException if <tt>from &gt; to</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3691
     * @throws NullPointerException if <tt>original</tt> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3692
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3693
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3694
    public static float[] copyOfRange(float[] original, int from, int to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3695
        int newLength = to - from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3696
        if (newLength < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3697
            throw new IllegalArgumentException(from + " > " + to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3698
        float[] copy = new float[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3699
        System.arraycopy(original, from, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3700
                         Math.min(original.length - from, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3701
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3702
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3703
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3704
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3705
     * Copies the specified range of the specified array into a new array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3706
     * The initial index of the range (<tt>from</tt>) must lie between zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3707
     * and <tt>original.length</tt>, inclusive.  The value at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3708
     * <tt>original[from]</tt> is placed into the initial element of the copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3709
     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3710
     * Values from subsequent elements in the original array are placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3711
     * subsequent elements in the copy.  The final index of the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3712
     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3713
     * may be greater than <tt>original.length</tt>, in which case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3714
     * <tt>0d</tt> is placed in all elements of the copy whose index is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3715
     * greater than or equal to <tt>original.length - from</tt>.  The length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3716
     * of the returned array will be <tt>to - from</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3717
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3718
     * @param original the array from which a range is to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3719
     * @param from the initial index of the range to be copied, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3720
     * @param to the final index of the range to be copied, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3721
     *     (This index may lie outside the array.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3722
     * @return a new array containing the specified range from the original array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3723
     *     truncated or padded with zeros to obtain the required length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3724
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3725
     *     or {@code from > original.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3726
     * @throws IllegalArgumentException if <tt>from &gt; to</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3727
     * @throws NullPointerException if <tt>original</tt> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3728
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3729
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3730
    public static double[] copyOfRange(double[] original, int from, int to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3731
        int newLength = to - from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3732
        if (newLength < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3733
            throw new IllegalArgumentException(from + " > " + to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3734
        double[] copy = new double[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3735
        System.arraycopy(original, from, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3736
                         Math.min(original.length - from, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3737
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3738
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3739
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3740
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3741
     * Copies the specified range of the specified array into a new array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3742
     * The initial index of the range (<tt>from</tt>) must lie between zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3743
     * and <tt>original.length</tt>, inclusive.  The value at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3744
     * <tt>original[from]</tt> is placed into the initial element of the copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3745
     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3746
     * Values from subsequent elements in the original array are placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3747
     * subsequent elements in the copy.  The final index of the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3748
     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3749
     * may be greater than <tt>original.length</tt>, in which case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3750
     * <tt>false</tt> is placed in all elements of the copy whose index is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3751
     * greater than or equal to <tt>original.length - from</tt>.  The length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3752
     * of the returned array will be <tt>to - from</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3753
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3754
     * @param original the array from which a range is to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3755
     * @param from the initial index of the range to be copied, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3756
     * @param to the final index of the range to be copied, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3757
     *     (This index may lie outside the array.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3758
     * @return a new array containing the specified range from the original array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3759
     *     truncated or padded with false elements to obtain the required length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3760
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3761
     *     or {@code from > original.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3762
     * @throws IllegalArgumentException if <tt>from &gt; to</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3763
     * @throws NullPointerException if <tt>original</tt> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3764
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3765
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3766
    public static boolean[] copyOfRange(boolean[] original, int from, int to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3767
        int newLength = to - from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3768
        if (newLength < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3769
            throw new IllegalArgumentException(from + " > " + to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3770
        boolean[] copy = new boolean[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3771
        System.arraycopy(original, from, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3772
                         Math.min(original.length - from, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3773
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3774
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3775
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3776
    // Misc
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3777
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3778
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3779
     * Returns a fixed-size list backed by the specified array.  (Changes to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3780
     * the returned list "write through" to the array.)  This method acts
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3781
     * as bridge between array-based and collection-based APIs, in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3782
     * combination with {@link Collection#toArray}.  The returned list is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3783
     * serializable and implements {@link RandomAccess}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3784
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3785
     * <p>This method also provides a convenient way to create a fixed-size
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3786
     * list initialized to contain several elements:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3787
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3788
     *     List&lt;String&gt; stooges = Arrays.asList("Larry", "Moe", "Curly");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3789
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3790
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  3791
     * @param <T> the class of the objects in the array
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3792
     * @param a the array by which the list will be backed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3793
     * @return a list view of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3794
     */
8151
88b01a6d5f51 7006578: Project Coin: Retrofit JDK libraries with @SafeVarargs
darcy
parents: 7816
diff changeset
  3795
    @SafeVarargs
13795
73850c397272 7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents: 12448
diff changeset
  3796
    @SuppressWarnings("varargs")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3797
    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
  3798
        return new ArrayList<>(a);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3799
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3800
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3801
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3802
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3803
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3804
    private static class ArrayList<E> extends AbstractList<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3805
        implements RandomAccess, java.io.Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3806
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3807
        private static final long serialVersionUID = -2764017481108945198L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3808
        private final E[] a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3809
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3810
        ArrayList(E[] array) {
14925
72729557c226 8003981: Support Parallel Array Sorting - JEP 103
chegar
parents: 13795
diff changeset
  3811
            a = Objects.requireNonNull(array);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3812
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3813
14925
72729557c226 8003981: Support Parallel Array Sorting - JEP 103
chegar
parents: 13795
diff changeset
  3814
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3815
        public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3816
            return a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3817
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3818
14925
72729557c226 8003981: Support Parallel Array Sorting - JEP 103
chegar
parents: 13795
diff changeset
  3819
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3820
        public Object[] toArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3821
            return a.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3822
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3823
14925
72729557c226 8003981: Support Parallel Array Sorting - JEP 103
chegar
parents: 13795
diff changeset
  3824
        @Override
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  3825
        @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3826
        public <T> T[] toArray(T[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3827
            int size = size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3828
            if (a.length < size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3829
                return Arrays.copyOf(this.a, size,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3830
                                     (Class<? extends T[]>) a.getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3831
            System.arraycopy(this.a, 0, a, 0, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3832
            if (a.length > size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3833
                a[size] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3834
            return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3835
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3836
14925
72729557c226 8003981: Support Parallel Array Sorting - JEP 103
chegar
parents: 13795
diff changeset
  3837
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3838
        public E get(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3839
            return a[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3840
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3841
14925
72729557c226 8003981: Support Parallel Array Sorting - JEP 103
chegar
parents: 13795
diff changeset
  3842
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3843
        public E set(int index, E element) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3844
            E oldValue = a[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3845
            a[index] = element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3846
            return oldValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3847
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3848
14925
72729557c226 8003981: Support Parallel Array Sorting - JEP 103
chegar
parents: 13795
diff changeset
  3849
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3850
        public int indexOf(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3851
            if (o==null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3852
                for (int i=0; i<a.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3853
                    if (a[i]==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3854
                        return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3855
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3856
                for (int i=0; i<a.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3857
                    if (o.equals(a[i]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3858
                        return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3859
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3860
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3861
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3862
14925
72729557c226 8003981: Support Parallel Array Sorting - JEP 103
chegar
parents: 13795
diff changeset
  3863
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3864
        public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3865
            return indexOf(o) != -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3866
        }
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  3867
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  3868
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  3869
        public Spliterator<E> spliterator() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  3870
            return Spliterators.spliterator(a, Spliterator.ORDERED);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  3871
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3872
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3873
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3874
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3875
     * Returns a hash code based on the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3876
     * For any two <tt>long</tt> arrays <tt>a</tt> and <tt>b</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3877
     * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3878
     * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3879
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3880
     * <p>The value returned by this method is the same value that would be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3881
     * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3882
     * method on a {@link List} containing a sequence of {@link Long}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3883
     * instances representing the elements of <tt>a</tt> in the same order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3884
     * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3885
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3886
     * @param a the array whose hash value to compute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3887
     * @return a content-based hash code for <tt>a</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3888
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3889
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3890
    public static int hashCode(long a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3891
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3892
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3893
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3894
        int result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3895
        for (long element : a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3896
            int elementHash = (int)(element ^ (element >>> 32));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3897
            result = 31 * result + elementHash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3898
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3899
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3900
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3901
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3902
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3903
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3904
     * Returns a hash code based on the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3905
     * For any two non-null <tt>int</tt> arrays <tt>a</tt> and <tt>b</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3906
     * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3907
     * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3908
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3909
     * <p>The value returned by this method is the same value that would be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3910
     * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3911
     * method on a {@link List} containing a sequence of {@link Integer}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3912
     * instances representing the elements of <tt>a</tt> in the same order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3913
     * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3914
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3915
     * @param a the array whose hash value to compute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3916
     * @return a content-based hash code for <tt>a</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3917
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3918
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3919
    public static int hashCode(int a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3920
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3921
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3922
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3923
        int result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3924
        for (int element : a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3925
            result = 31 * result + element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3926
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3927
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3928
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3929
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3930
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3931
     * Returns a hash code based on the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3932
     * For any two <tt>short</tt> arrays <tt>a</tt> and <tt>b</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3933
     * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3934
     * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3935
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3936
     * <p>The value returned by this method is the same value that would be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3937
     * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3938
     * method on a {@link List} containing a sequence of {@link Short}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3939
     * instances representing the elements of <tt>a</tt> in the same order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3940
     * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3941
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3942
     * @param a the array whose hash value to compute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3943
     * @return a content-based hash code for <tt>a</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3944
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3945
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3946
    public static int hashCode(short a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3947
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3948
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3949
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3950
        int result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3951
        for (short element : a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3952
            result = 31 * result + element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3953
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3954
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3955
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3956
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3957
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3958
     * Returns a hash code based on the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3959
     * For any two <tt>char</tt> arrays <tt>a</tt> and <tt>b</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3960
     * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3961
     * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3962
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3963
     * <p>The value returned by this method is the same value that would be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3964
     * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3965
     * method on a {@link List} containing a sequence of {@link Character}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3966
     * instances representing the elements of <tt>a</tt> in the same order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3967
     * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3968
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3969
     * @param a the array whose hash value to compute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3970
     * @return a content-based hash code for <tt>a</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3971
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3972
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3973
    public static int hashCode(char a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3974
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3975
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3976
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3977
        int result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3978
        for (char element : a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3979
            result = 31 * result + element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3980
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3981
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3982
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3983
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3984
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3985
     * Returns a hash code based on the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3986
     * For any two <tt>byte</tt> arrays <tt>a</tt> and <tt>b</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3987
     * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3988
     * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3989
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3990
     * <p>The value returned by this method is the same value that would be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3991
     * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3992
     * method on a {@link List} containing a sequence of {@link Byte}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3993
     * instances representing the elements of <tt>a</tt> in the same order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3994
     * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3995
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3996
     * @param a the array whose hash value to compute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3997
     * @return a content-based hash code for <tt>a</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3998
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3999
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4000
    public static int hashCode(byte a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4001
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4002
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4003
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4004
        int result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4005
        for (byte element : a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4006
            result = 31 * result + element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4007
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4008
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4009
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4010
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4011
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4012
     * Returns a hash code based on the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4013
     * For any two <tt>boolean</tt> arrays <tt>a</tt> and <tt>b</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4014
     * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4015
     * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4016
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4017
     * <p>The value returned by this method is the same value that would be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4018
     * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4019
     * method on a {@link List} containing a sequence of {@link Boolean}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4020
     * instances representing the elements of <tt>a</tt> in the same order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4021
     * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4022
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4023
     * @param a the array whose hash value to compute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4024
     * @return a content-based hash code for <tt>a</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4025
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4026
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4027
    public static int hashCode(boolean a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4028
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4029
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4030
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4031
        int result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4032
        for (boolean element : a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4033
            result = 31 * result + (element ? 1231 : 1237);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4034
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4035
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4036
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4037
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4038
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4039
     * Returns a hash code based on the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4040
     * For any two <tt>float</tt> arrays <tt>a</tt> and <tt>b</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4041
     * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4042
     * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4043
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4044
     * <p>The value returned by this method is the same value that would be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4045
     * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4046
     * method on a {@link List} containing a sequence of {@link Float}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4047
     * instances representing the elements of <tt>a</tt> in the same order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4048
     * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4049
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4050
     * @param a the array whose hash value to compute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4051
     * @return a content-based hash code for <tt>a</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4052
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4053
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4054
    public static int hashCode(float a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4055
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4056
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4057
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4058
        int result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4059
        for (float element : a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4060
            result = 31 * result + Float.floatToIntBits(element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4061
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4062
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4063
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4064
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4065
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4066
     * Returns a hash code based on the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4067
     * For any two <tt>double</tt> arrays <tt>a</tt> and <tt>b</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4068
     * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4069
     * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4070
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4071
     * <p>The value returned by this method is the same value that would be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4072
     * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4073
     * method on a {@link List} containing a sequence of {@link Double}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4074
     * instances representing the elements of <tt>a</tt> in the same order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4075
     * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4076
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4077
     * @param a the array whose hash value to compute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4078
     * @return a content-based hash code for <tt>a</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4079
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4080
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4081
    public static int hashCode(double a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4082
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4083
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4084
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4085
        int result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4086
        for (double element : a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4087
            long bits = Double.doubleToLongBits(element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4088
            result = 31 * result + (int)(bits ^ (bits >>> 32));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4089
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4090
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4091
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4092
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4093
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4094
     * Returns a hash code based on the contents of the specified array.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4095
     * the array contains other arrays as elements, the hash code is based on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4096
     * their identities rather than their contents.  It is therefore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4097
     * acceptable to invoke this method on an array that contains itself as an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4098
     * element,  either directly or indirectly through one or more levels of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4099
     * arrays.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4100
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4101
     * <p>For any two arrays <tt>a</tt> and <tt>b</tt> such that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4102
     * <tt>Arrays.equals(a, b)</tt>, it is also the case that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4103
     * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4104
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4105
     * <p>The value returned by this method is equal to the value that would
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4106
     * be returned by <tt>Arrays.asList(a).hashCode()</tt>, unless <tt>a</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4107
     * is <tt>null</tt>, in which case <tt>0</tt> is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4108
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4109
     * @param a the array whose content-based hash code to compute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4110
     * @return a content-based hash code for <tt>a</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4111
     * @see #deepHashCode(Object[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4112
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4114
    public static int hashCode(Object a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4115
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4116
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4117
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4118
        int result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4119
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4120
        for (Object element : a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4121
            result = 31 * result + (element == null ? 0 : element.hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4122
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4123
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4125
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4126
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4127
     * Returns a hash code based on the "deep contents" of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4128
     * array.  If the array contains other arrays as elements, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4129
     * hash code is based on their contents and so on, ad infinitum.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4130
     * It is therefore unacceptable to invoke this method on an array that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4131
     * contains itself as an element, either directly or indirectly through
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4132
     * one or more levels of arrays.  The behavior of such an invocation is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4133
     * undefined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4134
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4135
     * <p>For any two arrays <tt>a</tt> and <tt>b</tt> such that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4136
     * <tt>Arrays.deepEquals(a, b)</tt>, it is also the case that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4137
     * <tt>Arrays.deepHashCode(a) == Arrays.deepHashCode(b)</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4138
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4139
     * <p>The computation of the value returned by this method is similar to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4140
     * that of the value returned by {@link List#hashCode()} on a list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4141
     * containing the same elements as <tt>a</tt> in the same order, with one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4142
     * difference: If an element <tt>e</tt> of <tt>a</tt> is itself an array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4143
     * its hash code is computed not by calling <tt>e.hashCode()</tt>, but as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4144
     * by calling the appropriate overloading of <tt>Arrays.hashCode(e)</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4145
     * if <tt>e</tt> is an array of a primitive type, or as by calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4146
     * <tt>Arrays.deepHashCode(e)</tt> recursively if <tt>e</tt> is an array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4147
     * of a reference type.  If <tt>a</tt> is <tt>null</tt>, this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4148
     * returns 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4149
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4150
     * @param a the array whose deep-content-based hash code to compute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4151
     * @return a deep-content-based hash code for <tt>a</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4152
     * @see #hashCode(Object[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4153
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4155
    public static int deepHashCode(Object a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4156
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4157
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4158
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4159
        int result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4160
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4161
        for (Object element : a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4162
            int elementHash = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4163
            if (element instanceof Object[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4164
                elementHash = deepHashCode((Object[]) element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4165
            else if (element instanceof byte[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4166
                elementHash = hashCode((byte[]) element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4167
            else if (element instanceof short[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4168
                elementHash = hashCode((short[]) element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4169
            else if (element instanceof int[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4170
                elementHash = hashCode((int[]) element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4171
            else if (element instanceof long[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4172
                elementHash = hashCode((long[]) element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4173
            else if (element instanceof char[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4174
                elementHash = hashCode((char[]) element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4175
            else if (element instanceof float[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4176
                elementHash = hashCode((float[]) element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4177
            else if (element instanceof double[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4178
                elementHash = hashCode((double[]) element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4179
            else if (element instanceof boolean[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4180
                elementHash = hashCode((boolean[]) element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4181
            else if (element != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4182
                elementHash = element.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4183
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4184
            result = 31 * result + elementHash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4186
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4187
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4189
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4190
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4191
     * Returns <tt>true</tt> if the two specified arrays are <i>deeply
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4192
     * equal</i> to one another.  Unlike the {@link #equals(Object[],Object[])}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4193
     * method, this method is appropriate for use with nested arrays of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4194
     * arbitrary depth.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4195
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4196
     * <p>Two array references are considered deeply equal if both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4197
     * are <tt>null</tt>, or if they refer to arrays that contain the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4198
     * number of elements and all corresponding pairs of elements in the two
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4199
     * arrays are deeply equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4200
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4201
     * <p>Two possibly <tt>null</tt> elements <tt>e1</tt> and <tt>e2</tt> are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4202
     * deeply equal if any of the following conditions hold:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4203
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4204
     *    <li> <tt>e1</tt> and <tt>e2</tt> are both arrays of object reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4205
     *         types, and <tt>Arrays.deepEquals(e1, e2) would return true</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4206
     *    <li> <tt>e1</tt> and <tt>e2</tt> are arrays of the same primitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4207
     *         type, and the appropriate overloading of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4208
     *         <tt>Arrays.equals(e1, e2)</tt> would return true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4209
     *    <li> <tt>e1 == e2</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4210
     *    <li> <tt>e1.equals(e2)</tt> would return true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4211
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4212
     * Note that this definition permits <tt>null</tt> elements at any depth.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4213
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4214
     * <p>If either of the specified arrays contain themselves as elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4215
     * either directly or indirectly through one or more levels of arrays,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4216
     * the behavior of this method is undefined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4217
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4218
     * @param a1 one array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4219
     * @param a2 the other array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4220
     * @return <tt>true</tt> if the two arrays are equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4221
     * @see #equals(Object[],Object[])
4165
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4222
     * @see Objects#deepEquals(Object, Object)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4223
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4224
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4225
    public static boolean deepEquals(Object[] a1, Object[] a2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4226
        if (a1 == a2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4227
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4228
        if (a1 == null || a2==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4229
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4230
        int length = a1.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4231
        if (a2.length != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4232
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4233
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4234
        for (int i = 0; i < length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4235
            Object e1 = a1[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4236
            Object e2 = a2[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4237
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4238
            if (e1 == e2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4239
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4240
            if (e1 == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4241
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4242
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4243
            // 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
  4244
            boolean eq = deepEquals0(e1, e2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4245
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4246
            if (!eq)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4247
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4249
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4250
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4251
4165
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4252
    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
  4253
        assert e1 != null;
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4254
        boolean eq;
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4255
        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
  4256
            eq = deepEquals ((Object[]) e1, (Object[]) e2);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4257
        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
  4258
            eq = equals((byte[]) e1, (byte[]) e2);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4259
        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
  4260
            eq = equals((short[]) e1, (short[]) e2);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4261
        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
  4262
            eq = equals((int[]) e1, (int[]) e2);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4263
        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
  4264
            eq = equals((long[]) e1, (long[]) e2);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4265
        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
  4266
            eq = equals((char[]) e1, (char[]) e2);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4267
        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
  4268
            eq = equals((float[]) e1, (float[]) e2);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4269
        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
  4270
            eq = equals((double[]) e1, (double[]) e2);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4271
        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
  4272
            eq = equals((boolean[]) e1, (boolean[]) e2);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4273
        else
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4274
            eq = e1.equals(e2);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4275
        return eq;
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4276
    }
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4277
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4278
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4279
     * Returns a string representation of the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4280
     * The string representation consists of a list of the array's elements,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4281
     * enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4282
     * separated by the characters <tt>", "</tt> (a comma followed by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4283
     * space).  Elements are converted to strings as by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4284
     * <tt>String.valueOf(long)</tt>.  Returns <tt>"null"</tt> if <tt>a</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4285
     * is <tt>null</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4286
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4287
     * @param a the array whose string representation to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4288
     * @return a string representation of <tt>a</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4289
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4290
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4291
    public static String toString(long[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4292
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4293
            return "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4294
        int iMax = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4295
        if (iMax == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4296
            return "[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4297
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4298
        StringBuilder b = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4299
        b.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4300
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4301
            b.append(a[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4302
            if (i == iMax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4303
                return b.append(']').toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4304
            b.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4306
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4307
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4308
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4309
     * Returns a string representation of the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4310
     * The string representation consists of a list of the array's elements,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4311
     * enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4312
     * separated by the characters <tt>", "</tt> (a comma followed by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4313
     * space).  Elements are converted to strings as by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4314
     * <tt>String.valueOf(int)</tt>.  Returns <tt>"null"</tt> if <tt>a</tt> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4315
     * <tt>null</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4316
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4317
     * @param a the array whose string representation to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4318
     * @return a string representation of <tt>a</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4319
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4320
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4321
    public static String toString(int[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4322
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4323
            return "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4324
        int iMax = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4325
        if (iMax == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4326
            return "[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4327
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4328
        StringBuilder b = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4329
        b.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4330
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4331
            b.append(a[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4332
            if (i == iMax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4333
                return b.append(']').toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4334
            b.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4335
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4337
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4338
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4339
     * Returns a string representation of the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4340
     * The string representation consists of a list of the array's elements,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4341
     * enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4342
     * separated by the characters <tt>", "</tt> (a comma followed by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4343
     * space).  Elements are converted to strings as by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4344
     * <tt>String.valueOf(short)</tt>.  Returns <tt>"null"</tt> if <tt>a</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4345
     * is <tt>null</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4346
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4347
     * @param a the array whose string representation to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4348
     * @return a string representation of <tt>a</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4349
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4350
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4351
    public static String toString(short[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4352
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4353
            return "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4354
        int iMax = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4355
        if (iMax == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4356
            return "[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4357
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4358
        StringBuilder b = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4359
        b.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4360
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4361
            b.append(a[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4362
            if (i == iMax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4363
                return b.append(']').toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4364
            b.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4365
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4366
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4367
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4368
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4369
     * Returns a string representation of the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4370
     * The string representation consists of a list of the array's elements,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4371
     * enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4372
     * separated by the characters <tt>", "</tt> (a comma followed by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4373
     * space).  Elements are converted to strings as by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4374
     * <tt>String.valueOf(char)</tt>.  Returns <tt>"null"</tt> if <tt>a</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4375
     * is <tt>null</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4376
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4377
     * @param a the array whose string representation to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4378
     * @return a string representation of <tt>a</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4379
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4380
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4381
    public static String toString(char[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4382
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4383
            return "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4384
        int iMax = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4385
        if (iMax == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4386
            return "[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4387
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4388
        StringBuilder b = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4389
        b.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4390
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4391
            b.append(a[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4392
            if (i == iMax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4393
                return b.append(']').toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4394
            b.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4395
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4396
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4397
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4398
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4399
     * Returns a string representation of the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4400
     * The string representation consists of a list of the array's elements,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4401
     * enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4402
     * are separated by the characters <tt>", "</tt> (a comma followed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4403
     * by a space).  Elements are converted to strings as by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4404
     * <tt>String.valueOf(byte)</tt>.  Returns <tt>"null"</tt> if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4405
     * <tt>a</tt> is <tt>null</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4406
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4407
     * @param a the array whose string representation to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4408
     * @return a string representation of <tt>a</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4409
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4410
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4411
    public static String toString(byte[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4412
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4413
            return "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4414
        int iMax = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4415
        if (iMax == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4416
            return "[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4417
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4418
        StringBuilder b = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4419
        b.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4420
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4421
            b.append(a[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4422
            if (i == iMax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4423
                return b.append(']').toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4424
            b.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4425
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4426
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4427
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4428
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4429
     * Returns a string representation of the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4430
     * The string representation consists of a list of the array's elements,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4431
     * enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4432
     * separated by the characters <tt>", "</tt> (a comma followed by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4433
     * space).  Elements are converted to strings as by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4434
     * <tt>String.valueOf(boolean)</tt>.  Returns <tt>"null"</tt> if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4435
     * <tt>a</tt> is <tt>null</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4436
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4437
     * @param a the array whose string representation to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4438
     * @return a string representation of <tt>a</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4439
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4440
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4441
    public static String toString(boolean[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4442
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4443
            return "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4444
        int iMax = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4445
        if (iMax == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4446
            return "[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4447
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4448
        StringBuilder b = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4449
        b.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4450
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4451
            b.append(a[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4452
            if (i == iMax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4453
                return b.append(']').toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4454
            b.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4455
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4456
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4457
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4458
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4459
     * Returns a string representation of the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4460
     * The string representation consists of a list of the array's elements,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4461
     * enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4462
     * separated by the characters <tt>", "</tt> (a comma followed by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4463
     * space).  Elements are converted to strings as by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4464
     * <tt>String.valueOf(float)</tt>.  Returns <tt>"null"</tt> if <tt>a</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4465
     * is <tt>null</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4466
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4467
     * @param a the array whose string representation to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4468
     * @return a string representation of <tt>a</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4469
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4470
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4471
    public static String toString(float[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4472
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4473
            return "null";
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
  4474
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4475
        int iMax = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4476
        if (iMax == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4477
            return "[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4478
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4479
        StringBuilder b = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4480
        b.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4481
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4482
            b.append(a[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4483
            if (i == iMax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4484
                return b.append(']').toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4485
            b.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4486
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4487
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4488
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4489
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4490
     * Returns a string representation of the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4491
     * The string representation consists of a list of the array's elements,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4492
     * enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4493
     * separated by the characters <tt>", "</tt> (a comma followed by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4494
     * space).  Elements are converted to strings as by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4495
     * <tt>String.valueOf(double)</tt>.  Returns <tt>"null"</tt> if <tt>a</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4496
     * is <tt>null</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4497
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4498
     * @param a the array whose string representation to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4499
     * @return a string representation of <tt>a</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4500
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4501
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4502
    public static String toString(double[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4503
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4504
            return "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4505
        int iMax = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4506
        if (iMax == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4507
            return "[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4508
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4509
        StringBuilder b = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4510
        b.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4511
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4512
            b.append(a[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4513
            if (i == iMax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4514
                return b.append(']').toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4515
            b.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4516
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4517
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4518
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4519
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4520
     * Returns a string representation of the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4521
     * If the array contains other arrays as elements, they are converted to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4522
     * strings by the {@link Object#toString} method inherited from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4523
     * <tt>Object</tt>, which describes their <i>identities</i> rather than
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4524
     * their contents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4525
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4526
     * <p>The value returned by this method is equal to the value that would
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4527
     * be returned by <tt>Arrays.asList(a).toString()</tt>, unless <tt>a</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4528
     * is <tt>null</tt>, in which case <tt>"null"</tt> is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4529
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4530
     * @param a the array whose string representation to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4531
     * @return a string representation of <tt>a</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4532
     * @see #deepToString(Object[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4533
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4534
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4535
    public static String toString(Object[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4536
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4537
            return "null";
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
  4538
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4539
        int iMax = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4540
        if (iMax == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4541
            return "[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4542
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4543
        StringBuilder b = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4544
        b.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4545
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4546
            b.append(String.valueOf(a[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4547
            if (i == iMax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4548
                return b.append(']').toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4549
            b.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4550
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4551
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4552
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4553
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4554
     * Returns a string representation of the "deep contents" of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4555
     * array.  If the array contains other arrays as elements, the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4556
     * representation contains their contents and so on.  This method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4557
     * designed for converting multidimensional arrays to strings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4558
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4559
     * <p>The string representation consists of a list of the array's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4560
     * elements, enclosed in square brackets (<tt>"[]"</tt>).  Adjacent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4561
     * elements are separated by the characters <tt>", "</tt> (a comma
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4562
     * followed by a space).  Elements are converted to strings as by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4563
     * <tt>String.valueOf(Object)</tt>, unless they are themselves
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4564
     * arrays.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4565
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4566
     * <p>If an element <tt>e</tt> is an array of a primitive type, it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4567
     * converted to a string as by invoking the appropriate overloading of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4568
     * <tt>Arrays.toString(e)</tt>.  If an element <tt>e</tt> is an array of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4569
     * reference type, it is converted to a string as by invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4570
     * this method recursively.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4571
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4572
     * <p>To avoid infinite recursion, if the specified array contains itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4573
     * as an element, or contains an indirect reference to itself through one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4574
     * or more levels of arrays, the self-reference is converted to the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4575
     * <tt>"[...]"</tt>.  For example, an array containing only a reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4576
     * to itself would be rendered as <tt>"[[...]]"</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4577
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4578
     * <p>This method returns <tt>"null"</tt> if the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4579
     * is <tt>null</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4580
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4581
     * @param a the array whose string representation to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4582
     * @return a string representation of <tt>a</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4583
     * @see #toString(Object[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4584
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4585
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4586
    public static String deepToString(Object[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4587
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4588
            return "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4589
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4590
        int bufLen = 20 * a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4591
        if (a.length != 0 && bufLen <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4592
            bufLen = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4593
        StringBuilder buf = new StringBuilder(bufLen);
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 21278
diff changeset
  4594
        deepToString(a, buf, new HashSet<>());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4595
        return buf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4596
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4597
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4598
    private static void deepToString(Object[] a, StringBuilder buf,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4599
                                     Set<Object[]> dejaVu) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4600
        if (a == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4601
            buf.append("null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4602
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4603
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4604
        int iMax = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4605
        if (iMax == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4606
            buf.append("[]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4607
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4608
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4609
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4610
        dejaVu.add(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4611
        buf.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4612
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4613
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4614
            Object element = a[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4615
            if (element == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4616
                buf.append("null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4617
            } else {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  4618
                Class<?> eClass = element.getClass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4619
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4620
                if (eClass.isArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4621
                    if (eClass == byte[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4622
                        buf.append(toString((byte[]) element));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4623
                    else if (eClass == short[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4624
                        buf.append(toString((short[]) element));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4625
                    else if (eClass == int[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4626
                        buf.append(toString((int[]) element));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4627
                    else if (eClass == long[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4628
                        buf.append(toString((long[]) element));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4629
                    else if (eClass == char[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4630
                        buf.append(toString((char[]) element));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4631
                    else if (eClass == float[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4632
                        buf.append(toString((float[]) element));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4633
                    else if (eClass == double[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4634
                        buf.append(toString((double[]) element));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4635
                    else if (eClass == boolean[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4636
                        buf.append(toString((boolean[]) element));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4637
                    else { // element is an array of object references
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4638
                        if (dejaVu.contains(element))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4639
                            buf.append("[...]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4640
                        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4641
                            deepToString((Object[])element, buf, dejaVu);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4642
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4643
                } else {  // element is non-null and not an array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4644
                    buf.append(element.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4645
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4646
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4647
            if (i == iMax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4648
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4649
            buf.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4650
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4651
        buf.append(']');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4652
        dejaVu.remove(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4653
    }
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4654
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4655
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4656
    /**
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4657
     * Set all elements of the specified array, using the provided
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4658
     * generator function to compute each element.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4659
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4660
     * <p>If the generator function throws an exception, it is relayed to
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4661
     * the caller and the array is left in an indeterminate state.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4662
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4663
     * @param <T> type of elements of the array
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4664
     * @param array array to be initialized
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4665
     * @param generator a function accepting an index and producing the desired
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4666
     *        value for that position
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4667
     * @throws NullPointerException if the generator is null
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4668
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4669
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4670
    public static <T> void setAll(T[] array, IntFunction<? extends T> generator) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4671
        Objects.requireNonNull(generator);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4672
        for (int i = 0; i < array.length; i++)
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4673
            array[i] = generator.apply(i);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4674
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4675
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4676
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4677
     * Set all elements of the specified array, in parallel, using the
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4678
     * provided generator function to compute each element.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4679
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4680
     * <p>If the generator function throws an exception, an unchecked exception
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4681
     * is thrown from {@code parallelSetAll} and the array is left in an
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4682
     * indeterminate state.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4683
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4684
     * @param <T> type of elements of the array
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4685
     * @param array array to be initialized
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4686
     * @param generator a function accepting an index and producing the desired
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4687
     *        value for that position
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4688
     * @throws NullPointerException if the generator is null
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4689
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4690
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4691
    public static <T> void parallelSetAll(T[] array, IntFunction<? extends T> generator) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4692
        Objects.requireNonNull(generator);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4693
        IntStream.range(0, array.length).parallel().forEach(i -> { array[i] = generator.apply(i); });
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4694
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4695
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4696
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4697
     * Set all elements of the specified array, using the provided
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4698
     * generator function to compute each element.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4699
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4700
     * <p>If the generator function throws an exception, it is relayed to
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4701
     * the caller and the array is left in an indeterminate state.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4702
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4703
     * @param array array to be initialized
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4704
     * @param generator a function accepting an index and producing the desired
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4705
     *        value for that position
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4706
     * @throws NullPointerException if the generator is null
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4707
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4708
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4709
    public static void setAll(int[] array, IntUnaryOperator generator) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4710
        Objects.requireNonNull(generator);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4711
        for (int i = 0; i < array.length; i++)
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4712
            array[i] = generator.applyAsInt(i);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4713
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4714
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4715
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4716
     * Set all elements of the specified array, in parallel, using the
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4717
     * provided generator function to compute each element.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4718
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4719
     * <p>If the generator function throws an exception, an unchecked exception
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4720
     * is thrown from {@code parallelSetAll} and the array is left in an
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4721
     * indeterminate state.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4722
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4723
     * @param array array to be initialized
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4724
     * @param generator a function accepting an index and producing the desired
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4725
     * value for that position
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4726
     * @throws NullPointerException if the generator is null
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4727
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4728
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4729
    public static void parallelSetAll(int[] array, IntUnaryOperator generator) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4730
        Objects.requireNonNull(generator);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4731
        IntStream.range(0, array.length).parallel().forEach(i -> { array[i] = generator.applyAsInt(i); });
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4732
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4733
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4734
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4735
     * Set all elements of the specified array, using the provided
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4736
     * generator function to compute each element.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4737
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4738
     * <p>If the generator function throws an exception, it is relayed to
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4739
     * the caller and the array is left in an indeterminate state.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4740
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4741
     * @param array array to be initialized
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4742
     * @param generator a function accepting an index and producing the desired
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4743
     *        value for that position
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4744
     * @throws NullPointerException if the generator is null
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4745
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4746
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4747
    public static void setAll(long[] array, IntToLongFunction generator) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4748
        Objects.requireNonNull(generator);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4749
        for (int i = 0; i < array.length; i++)
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4750
            array[i] = generator.applyAsLong(i);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4751
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4752
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4753
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4754
     * Set all elements of the specified array, in parallel, using the
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4755
     * provided generator function to compute each element.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4756
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4757
     * <p>If the generator function throws an exception, an unchecked exception
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4758
     * is thrown from {@code parallelSetAll} and the array is left in an
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4759
     * indeterminate state.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4760
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4761
     * @param array array to be initialized
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4762
     * @param generator a function accepting an index and producing the desired
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4763
     *        value for that position
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4764
     * @throws NullPointerException if the generator is null
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4765
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4766
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4767
    public static void parallelSetAll(long[] array, IntToLongFunction generator) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4768
        Objects.requireNonNull(generator);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4769
        IntStream.range(0, array.length).parallel().forEach(i -> { array[i] = generator.applyAsLong(i); });
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4770
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4771
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4772
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4773
     * Set all elements of the specified array, using the provided
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4774
     * generator function to compute each element.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4775
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4776
     * <p>If the generator function throws an exception, it is relayed to
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4777
     * the caller and the array is left in an indeterminate state.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4778
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4779
     * @param array array to be initialized
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4780
     * @param generator a function accepting an index and producing the desired
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4781
     *        value for that position
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4782
     * @throws NullPointerException if the generator is null
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4783
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4784
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4785
    public static void setAll(double[] array, IntToDoubleFunction generator) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4786
        Objects.requireNonNull(generator);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4787
        for (int i = 0; i < array.length; i++)
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4788
            array[i] = generator.applyAsDouble(i);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4789
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4790
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4791
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4792
     * Set all elements of the specified array, in parallel, using the
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4793
     * provided generator function to compute each element.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4794
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4795
     * <p>If the generator function throws an exception, an unchecked exception
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4796
     * is thrown from {@code parallelSetAll} and the array is left in an
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4797
     * indeterminate state.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4798
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4799
     * @param array array to be initialized
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4800
     * @param generator a function accepting an index and producing the desired
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4801
     *        value for that position
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4802
     * @throws NullPointerException if the generator is null
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4803
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4804
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4805
    public static void parallelSetAll(double[] array, IntToDoubleFunction generator) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4806
        Objects.requireNonNull(generator);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4807
        IntStream.range(0, array.length).parallel().forEach(i -> { array[i] = generator.applyAsDouble(i); });
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4808
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4809
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4810
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4811
     * Returns a {@link Spliterator} covering all of the specified array.
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4812
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4813
     * <p>The spliterator reports {@link Spliterator#SIZED},
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4814
     * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4815
     * {@link Spliterator#IMMUTABLE}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4816
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4817
     * @param <T> type of elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4818
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4819
     * @return a spliterator for the array elements
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4820
     * @since 1.8
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4821
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4822
    public static <T> Spliterator<T> spliterator(T[] array) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4823
        return Spliterators.spliterator(array,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4824
                                        Spliterator.ORDERED | Spliterator.IMMUTABLE);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4825
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4826
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4827
    /**
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4828
     * Returns a {@link Spliterator} covering the specified range of the
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4829
     * specified array.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4830
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4831
     * <p>The spliterator reports {@link Spliterator#SIZED},
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4832
     * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4833
     * {@link Spliterator#IMMUTABLE}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4834
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4835
     * @param <T> type of elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4836
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4837
     * @param startInclusive the first index to cover, inclusive
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4838
     * @param endExclusive index immediately past the last index to cover
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4839
     * @return a spliterator for the array elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4840
     * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4841
     *         negative, {@code endExclusive} is less than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4842
     *         {@code startInclusive}, or {@code endExclusive} is greater than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4843
     *         the array size
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4844
     * @since 1.8
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4845
     */
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4846
    public static <T> Spliterator<T> spliterator(T[] array, int startInclusive, int endExclusive) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4847
        return Spliterators.spliterator(array, startInclusive, endExclusive,
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4848
                                        Spliterator.ORDERED | Spliterator.IMMUTABLE);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4849
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4850
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4851
    /**
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4852
     * Returns a {@link Spliterator.OfInt} covering all of the specified array.
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4853
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4854
     * <p>The spliterator reports {@link Spliterator#SIZED},
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4855
     * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4856
     * {@link Spliterator#IMMUTABLE}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4857
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4858
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4859
     * @return a spliterator for the array elements
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4860
     * @since 1.8
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4861
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4862
    public static Spliterator.OfInt spliterator(int[] array) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4863
        return Spliterators.spliterator(array,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4864
                                        Spliterator.ORDERED | Spliterator.IMMUTABLE);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4865
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4866
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4867
    /**
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4868
     * Returns a {@link Spliterator.OfInt} covering the specified range of the
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4869
     * specified array.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4870
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4871
     * <p>The spliterator reports {@link Spliterator#SIZED},
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4872
     * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4873
     * {@link Spliterator#IMMUTABLE}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4874
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4875
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4876
     * @param startInclusive the first index to cover, inclusive
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4877
     * @param endExclusive index immediately past the last index to cover
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4878
     * @return a spliterator for the array elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4879
     * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4880
     *         negative, {@code endExclusive} is less than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4881
     *         {@code startInclusive}, or {@code endExclusive} is greater than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4882
     *         the array size
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4883
     * @since 1.8
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4884
     */
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4885
    public static Spliterator.OfInt spliterator(int[] array, int startInclusive, int endExclusive) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4886
        return Spliterators.spliterator(array, startInclusive, endExclusive,
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4887
                                        Spliterator.ORDERED | Spliterator.IMMUTABLE);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4888
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4889
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4890
    /**
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4891
     * Returns a {@link Spliterator.OfLong} covering all of the specified array.
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4892
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4893
     * <p>The spliterator reports {@link Spliterator#SIZED},
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4894
     * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4895
     * {@link Spliterator#IMMUTABLE}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4896
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4897
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4898
     * @return the spliterator for the array elements
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4899
     * @since 1.8
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4900
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4901
    public static Spliterator.OfLong spliterator(long[] array) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4902
        return Spliterators.spliterator(array,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4903
                                        Spliterator.ORDERED | Spliterator.IMMUTABLE);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4904
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4905
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4906
    /**
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4907
     * Returns a {@link Spliterator.OfLong} covering the specified range of the
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4908
     * specified array.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4909
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4910
     * <p>The spliterator reports {@link Spliterator#SIZED},
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4911
     * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4912
     * {@link Spliterator#IMMUTABLE}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4913
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4914
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4915
     * @param startInclusive the first index to cover, inclusive
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4916
     * @param endExclusive index immediately past the last index to cover
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4917
     * @return a spliterator for the array elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4918
     * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4919
     *         negative, {@code endExclusive} is less than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4920
     *         {@code startInclusive}, or {@code endExclusive} is greater than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4921
     *         the array size
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4922
     * @since 1.8
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4923
     */
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4924
    public static Spliterator.OfLong spliterator(long[] array, int startInclusive, int endExclusive) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4925
        return Spliterators.spliterator(array, startInclusive, endExclusive,
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4926
                                        Spliterator.ORDERED | Spliterator.IMMUTABLE);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4927
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4928
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4929
    /**
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4930
     * Returns a {@link Spliterator.OfDouble} covering all of the specified
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4931
     * array.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4932
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4933
     * <p>The spliterator reports {@link Spliterator#SIZED},
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4934
     * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4935
     * {@link Spliterator#IMMUTABLE}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4936
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4937
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4938
     * @return a spliterator for the array elements
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4939
     * @since 1.8
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4940
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4941
    public static Spliterator.OfDouble spliterator(double[] array) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4942
        return Spliterators.spliterator(array,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4943
                                        Spliterator.ORDERED | Spliterator.IMMUTABLE);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4944
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4945
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4946
    /**
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4947
     * Returns a {@link Spliterator.OfDouble} covering the specified range of
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4948
     * the specified array.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4949
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4950
     * <p>The spliterator reports {@link Spliterator#SIZED},
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4951
     * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4952
     * {@link Spliterator#IMMUTABLE}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4953
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4954
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4955
     * @param startInclusive the first index to cover, inclusive
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4956
     * @param endExclusive index immediately past the last index to cover
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4957
     * @return a spliterator for the array elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4958
     * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4959
     *         negative, {@code endExclusive} is less than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4960
     *         {@code startInclusive}, or {@code endExclusive} is greater than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4961
     *         the array size
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4962
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4963
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4964
    public static Spliterator.OfDouble spliterator(double[] array, int startInclusive, int endExclusive) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4965
        return Spliterators.spliterator(array, startInclusive, endExclusive,
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4966
                                        Spliterator.ORDERED | Spliterator.IMMUTABLE);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4967
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4968
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4969
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4970
     * Returns a sequential {@link Stream} with the specified array as its
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4971
     * source.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4972
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4973
     * @param <T> The type of the array elements
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4974
     * @param array The array, assumed to be unmodified during use
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4975
     * @return a {@code Stream} for the array
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4976
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4977
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4978
    public static <T> Stream<T> stream(T[] array) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4979
        return stream(array, 0, array.length);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4980
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4981
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4982
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4983
     * Returns a sequential {@link Stream} with the specified range of the
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4984
     * specified array as its source.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4985
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4986
     * @param <T> the type of the array elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4987
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4988
     * @param startInclusive the first index to cover, inclusive
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4989
     * @param endExclusive index immediately past the last index to cover
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4990
     * @return a {@code Stream} for the array range
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4991
     * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4992
     *         negative, {@code endExclusive} is less than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4993
     *         {@code startInclusive}, or {@code endExclusive} is greater than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4994
     *         the array size
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4995
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4996
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4997
    public static <T> Stream<T> stream(T[] array, int startInclusive, int endExclusive) {
18822
4b6be7c19547 8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents: 18555
diff changeset
  4998
        return StreamSupport.stream(spliterator(array, startInclusive, endExclusive), false);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  4999
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5000
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5001
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5002
     * Returns a sequential {@link IntStream} with the specified array as its
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5003
     * source.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5004
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5005
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5006
     * @return an {@code IntStream} for the array
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5007
     * @since 1.8
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5008
     */
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5009
    public static IntStream stream(int[] array) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5010
        return stream(array, 0, array.length);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5011
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5012
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5013
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5014
     * Returns a sequential {@link IntStream} with the specified range of the
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5015
     * specified array as its source.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5016
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5017
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5018
     * @param startInclusive the first index to cover, inclusive
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5019
     * @param endExclusive index immediately past the last index to cover
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5020
     * @return an {@code IntStream} for the array range
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5021
     * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5022
     *         negative, {@code endExclusive} is less than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5023
     *         {@code startInclusive}, or {@code endExclusive} is greater than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5024
     *         the array size
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5025
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5026
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5027
    public static IntStream stream(int[] array, int startInclusive, int endExclusive) {
18822
4b6be7c19547 8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents: 18555
diff changeset
  5028
        return StreamSupport.intStream(spliterator(array, startInclusive, endExclusive), false);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5029
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5030
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5031
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5032
     * Returns a sequential {@link LongStream} with the specified array as its
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5033
     * source.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5034
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5035
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5036
     * @return a {@code LongStream} for the array
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5037
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5038
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5039
    public static LongStream stream(long[] array) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5040
        return stream(array, 0, array.length);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5041
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5042
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5043
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5044
     * Returns a sequential {@link LongStream} with the specified range of the
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5045
     * specified array as its source.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5046
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5047
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5048
     * @param startInclusive the first index to cover, inclusive
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5049
     * @param endExclusive index immediately past the last index to cover
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5050
     * @return a {@code LongStream} for the array range
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5051
     * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5052
     *         negative, {@code endExclusive} is less than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5053
     *         {@code startInclusive}, or {@code endExclusive} is greater than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5054
     *         the array size
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5055
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5056
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5057
    public static LongStream stream(long[] array, int startInclusive, int endExclusive) {
18822
4b6be7c19547 8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents: 18555
diff changeset
  5058
        return StreamSupport.longStream(spliterator(array, startInclusive, endExclusive), false);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5059
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5060
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5061
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5062
     * Returns a sequential {@link DoubleStream} with the specified array as its
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5063
     * source.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5064
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5065
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5066
     * @return a {@code DoubleStream} for the array
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5067
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5068
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5069
    public static DoubleStream stream(double[] array) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5070
        return stream(array, 0, array.length);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5071
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5072
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5073
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5074
     * Returns a sequential {@link DoubleStream} with the specified range of the
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5075
     * specified array as its source.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5076
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5077
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5078
     * @param startInclusive the first index to cover, inclusive
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5079
     * @param endExclusive index immediately past the last index to cover
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5080
     * @return a {@code DoubleStream} for the array range
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5081
     * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5082
     *         negative, {@code endExclusive} is less than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5083
     *         {@code startInclusive}, or {@code endExclusive} is greater than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5084
     *         the array size
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5085
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5086
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5087
    public static DoubleStream stream(double[] array, int startInclusive, int endExclusive) {
18822
4b6be7c19547 8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents: 18555
diff changeset
  5088
        return StreamSupport.doubleStream(spliterator(array, startInclusive, endExclusive), false);
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5089
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5090
}