src/java.base/share/classes/java/util/Arrays.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 58520 e036ee8bae56
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 51884
diff changeset
     2
 * Copyright (c) 1997, 2019, 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
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
    28
import jdk.internal.HotSpotIntrinsicCandidate;
48356
29e165bdf669 8193085: Vectorize the nio Buffer equals and compareTo implementations
psandoz
parents: 47216
diff changeset
    29
import jdk.internal.util.ArraysSupport;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
    30
51884
2ee7e1b7ba66 7033681: Arrays.asList methods needs better documentation
smarks
parents: 49955
diff changeset
    31
import java.io.Serializable;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    32
import java.lang.reflect.Array;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    33
import java.util.concurrent.ForkJoinPool;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    34
import java.util.function.BinaryOperator;
23354
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
    35
import java.util.function.Consumer;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    36
import java.util.function.DoubleBinaryOperator;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    37
import java.util.function.IntBinaryOperator;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    38
import java.util.function.IntFunction;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    39
import java.util.function.IntToDoubleFunction;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    40
import java.util.function.IntToLongFunction;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    41
import java.util.function.IntUnaryOperator;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    42
import java.util.function.LongBinaryOperator;
23354
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
    43
import java.util.function.UnaryOperator;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    44
import java.util.stream.DoubleStream;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    45
import java.util.stream.IntStream;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    46
import java.util.stream.LongStream;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    47
import java.util.stream.Stream;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    48
import java.util.stream.StreamSupport;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * 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
    52
 * sorting and searching). This class also contains a static factory
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * that allows arrays to be viewed as lists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    55
 * <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
    56
 * if the specified array reference is null, except where noted.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <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
    59
 * brief descriptions of the <i>implementations</i>. Such descriptions should
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * 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
    61
 * <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
    62
 * 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
    63
 * 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
    64
 * a MergeSort, but it does have to be <i>stable</i>.)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <p>This class is a member of the
49433
b6671a111395 8199465: {@docRoot} references need to be updated to reflect new module/package structure
jjg
parents: 48356
diff changeset
    67
 * <a href="{@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework">
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    70
 * @author Josh Bloch
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    71
 * @author Neal Gafter
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    72
 * @author John Rose
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    73
 * @since  1.2
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 */
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    75
public class Arrays {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
14925
72729557c226 8003981: Support Parallel Array Sorting - JEP 103
chegar
parents: 13795
diff changeset
    77
    /**
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    78
     * The minimum array length below which a parallel sorting
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    79
     * algorithm will not further partition the sorting task. Using
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    80
     * smaller sizes typically results in memory contention across
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    81
     * tasks that makes parallel speedups unlikely.
14925
72729557c226 8003981: Support Parallel Array Sorting - JEP 103
chegar
parents: 13795
diff changeset
    82
     */
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    83
    private static final int MIN_ARRAY_SORT_GRAN = 1 << 13;
14925
72729557c226 8003981: Support Parallel Array Sorting - JEP 103
chegar
parents: 13795
diff changeset
    84
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    // Suppresses default constructor, ensuring non-instantiability.
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    86
    private Arrays() {}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
17712
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
     * A comparator that implements the natural ordering of a group of
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    90
     * mutually comparable elements. May be used when a supplied
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    91
     * comparator is null. To simplify code-sharing within underlying
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    92
     * implementations, the compare method only declares type Object
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    93
     * for its second argument.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    94
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    95
     * Arrays class implementor's note: It is an empirical matter
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    96
     * whether ComparableTimSort offers any performance benefit over
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    97
     * TimSort used with this comparator.  If not, you are better off
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    98
     * deleting or bypassing ComparableTimSort.  There is currently no
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    99
     * empirical case for separating them for parallel sorting, so all
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   100
     * public Object parallelSort methods use the same comparator
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   101
     * based implementation.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   102
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   103
    static final class NaturalOrder implements Comparator<Object> {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   104
        @SuppressWarnings("unchecked")
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   105
        public int compare(Object first, Object second) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   106
            return ((Comparable<Object>)first).compareTo(second);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   107
        }
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   108
        static final NaturalOrder INSTANCE = new NaturalOrder();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   109
    }
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   110
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   111
    /**
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   112
     * Checks that {@code fromIndex} and {@code toIndex} are in
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   113
     * the range and throws an exception if they aren't.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   114
     */
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
   115
    static void rangeCheck(int arrayLength, int fromIndex, int toIndex) {
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   116
        if (fromIndex > toIndex) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   117
            throw new IllegalArgumentException(
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   118
                    "fromIndex(" + fromIndex + ") > toIndex(" + 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
        if (fromIndex < 0) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   121
            throw new ArrayIndexOutOfBoundsException(fromIndex);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   122
        }
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   123
        if (toIndex > arrayLength) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   124
            throw new ArrayIndexOutOfBoundsException(toIndex);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   125
        }
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   126
    }
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   127
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   128
    /*
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   129
     * Sorting methods. Note that all public "sort" methods take the
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   130
     * same form: Performing argument checks if necessary, and then
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   131
     * expanding arguments into those required for the internal
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   132
     * implementation methods residing in other package-private
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   133
     * classes (except for legacyMergeSort, included in this class).
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   134
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /**
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   137
     * 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
   138
     *
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   139
     * <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
   140
     * 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
   141
     * 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
   142
     * 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
   143
     * faster than traditional (one-pivot) Quicksort implementations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @param a the array to be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public static void sort(int[] a) {
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   148
        DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    /**
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   152
     * Sorts the specified range of the array into ascending order. The range
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   153
     * to be sorted extends from the index {@code fromIndex}, inclusive, to
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   154
     * 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
   155
     * the range to be sorted is empty.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   157
     * <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
   158
     * 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
   159
     * 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
   160
     * 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
   161
     * faster than traditional (one-pivot) Quicksort implementations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @param a the array to be sorted
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   164
     * @param fromIndex the index of the first element, inclusive, to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   165
     * @param toIndex the index of the last element, exclusive, to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   166
     *
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   167
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   168
     * @throws ArrayIndexOutOfBoundsException
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   169
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    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
   172
        rangeCheck(a.length, fromIndex, toIndex);
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   173
        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    /**
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   177
     * 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
   178
     *
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   179
     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   180
     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   181
     * offers O(n log(n)) performance on many data sets that cause other
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   182
     * quicksorts to degrade to quadratic performance, and is typically
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   183
     * faster than traditional (one-pivot) Quicksort implementations.
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   184
     *
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   185
     * @param a the array to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   186
     */
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   187
    public static void sort(long[] a) {
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   188
        DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   189
    }
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   190
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   191
    /**
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   192
     * Sorts the specified range of the array into ascending order. The range
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   193
     * to be sorted extends from the index {@code fromIndex}, inclusive, to
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   194
     * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   195
     * the range to be sorted is empty.
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   196
     *
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   197
     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   198
     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   199
     * offers O(n log(n)) performance on many data sets that cause other
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   200
     * quicksorts to degrade to quadratic performance, and is typically
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   201
     * faster than traditional (one-pivot) Quicksort implementations.
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   202
     *
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   203
     * @param a the array to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   204
     * @param fromIndex the index of the first element, inclusive, to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   205
     * @param toIndex the index of the last element, exclusive, to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   206
     *
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   207
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   208
     * @throws ArrayIndexOutOfBoundsException
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   209
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   210
     */
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   211
    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
   212
        rangeCheck(a.length, fromIndex, toIndex);
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   213
        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   214
    }
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   215
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   216
    /**
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   217
     * Sorts the specified array into ascending numerical order.
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   218
     *
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   219
     * <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
   220
     * 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
   221
     * 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
   222
     * 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
   223
     * faster than traditional (one-pivot) Quicksort implementations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @param a the array to be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    public static void sort(short[] a) {
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   228
        DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    /**
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   232
     * Sorts the specified range of the array into ascending order. The range
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   233
     * to be sorted extends from the index {@code fromIndex}, inclusive, to
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   234
     * 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
   235
     * the range to be sorted is empty.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     *
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   237
     * <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
   238
     * 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
   239
     * 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
   240
     * 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
   241
     * faster than traditional (one-pivot) Quicksort implementations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @param a the array to be sorted
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   244
     * @param fromIndex the index of the first element, inclusive, to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   245
     * @param toIndex the index of the last element, exclusive, to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   246
     *
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   247
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   248
     * @throws ArrayIndexOutOfBoundsException
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   249
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    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
   252
        rangeCheck(a.length, fromIndex, toIndex);
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   253
        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    /**
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   257
     * 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
   258
     *
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   259
     * <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
   260
     * 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
   261
     * 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
   262
     * 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
   263
     * faster than traditional (one-pivot) Quicksort implementations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * @param a the array to be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    public static void sort(char[] a) {
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   268
        DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    /**
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   272
     * Sorts the specified range of the array into ascending order. The range
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   273
     * to be sorted extends from the index {@code fromIndex}, inclusive, to
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   274
     * 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
   275
     * the range to be sorted is empty.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     *
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   277
     * <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
   278
     * 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
   279
     * 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
   280
     * 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
   281
     * faster than traditional (one-pivot) Quicksort implementations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * @param a the array to be sorted
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   284
     * @param fromIndex the index of the first element, inclusive, to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   285
     * @param toIndex the index of the last element, exclusive, to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   286
     *
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   287
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   288
     * @throws ArrayIndexOutOfBoundsException
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   289
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    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
   292
        rangeCheck(a.length, fromIndex, toIndex);
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   293
        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    /**
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   297
     * 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
   298
     *
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   299
     * <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
   300
     * 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
   301
     * 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
   302
     * 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
   303
     * faster than traditional (one-pivot) Quicksort implementations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * @param a the array to be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    public static void sort(byte[] a) {
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   308
        DualPivotQuicksort.sort(a, 0, a.length - 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    /**
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   312
     * Sorts the specified range of the array into ascending order. The range
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   313
     * to be sorted extends from the index {@code fromIndex}, inclusive, to
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   314
     * 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
   315
     * the range to be sorted is empty.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     *
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   317
     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   318
     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   319
     * offers O(n log(n)) performance on many data sets that cause other
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   320
     * quicksorts to degrade to quadratic performance, and is typically
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   321
     * faster than traditional (one-pivot) Quicksort implementations.
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   322
     *
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   323
     * @param a the array to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   324
     * @param fromIndex the index of the first element, inclusive, to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   325
     * @param toIndex the index of the last element, exclusive, to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   326
     *
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   327
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   328
     * @throws ArrayIndexOutOfBoundsException
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   329
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   330
     */
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   331
    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
   332
        rangeCheck(a.length, fromIndex, toIndex);
d229d56fd918 6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents: 5506
diff changeset
   333
        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1);
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   334
    }
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   335
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   336
    /**
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   337
     * Sorts the specified array into ascending numerical order.
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   338
     *
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   339
     * <p>The {@code <} relation does not provide a total order on all float
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   340
     * 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
   341
     * value compares neither less than, greater than, nor equal to any value,
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   342
     * even itself. This method uses the total order imposed by the method
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   343
     * {@link Float#compareTo}: {@code -0.0f} is treated as less than value
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   344
     * {@code 0.0f} and {@code Float.NaN} is considered greater than any
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   345
     * other value and all {@code Float.NaN} values are considered equal.
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   346
     *
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   347
     * <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
   348
     * 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
   349
     * 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
   350
     * 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
   351
     * faster than traditional (one-pivot) Quicksort implementations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * @param a the array to be sorted
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   354
     */
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   355
    public static void sort(float[] a) {
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   356
        DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   357
    }
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   358
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   359
    /**
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   360
     * Sorts the specified range of the array into ascending order. The range
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   361
     * to be sorted extends from the index {@code fromIndex}, inclusive, to
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   362
     * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   363
     * the range to be sorted is empty.
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   364
     *
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   365
     * <p>The {@code <} relation does not provide a total order on all float
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   366
     * 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
   367
     * value compares neither less than, greater than, nor equal to any value,
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   368
     * even itself. This method uses the total order imposed by the method
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   369
     * {@link Float#compareTo}: {@code -0.0f} is treated as less than value
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   370
     * {@code 0.0f} and {@code Float.NaN} is considered greater than any
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   371
     * other value and all {@code Float.NaN} values are considered equal.
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   372
     *
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   373
     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   374
     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   375
     * offers O(n log(n)) performance on many data sets that cause other
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   376
     * quicksorts to degrade to quadratic performance, and is typically
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   377
     * faster than traditional (one-pivot) Quicksort implementations.
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   378
     *
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   379
     * @param a the array to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   380
     * @param fromIndex the index of the first element, inclusive, to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   381
     * @param toIndex the index of the last element, exclusive, to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   382
     *
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   383
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   384
     * @throws ArrayIndexOutOfBoundsException
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   385
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     */
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   387
    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
   388
        rangeCheck(a.length, fromIndex, toIndex);
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   389
        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    /**
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   393
     * 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
   394
     *
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   395
     * <p>The {@code <} relation does not provide a total order on all double
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   396
     * 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
   397
     * value compares neither less than, greater than, nor equal to any value,
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   398
     * even itself. This method uses the total order imposed by the method
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   399
     * {@link Double#compareTo}: {@code -0.0d} is treated as less than value
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   400
     * {@code 0.0d} and {@code Double.NaN} is considered greater than any
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   401
     * 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
   402
     *
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   403
     * <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
   404
     * 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
   405
     * 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
   406
     * 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
   407
     * faster than traditional (one-pivot) Quicksort implementations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * @param a the array to be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    public static void sort(double[] a) {
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   412
        DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    /**
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   416
     * Sorts the specified range of the array into ascending order. The range
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   417
     * to be sorted extends from the index {@code fromIndex}, inclusive, to
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   418
     * 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
   419
     * the range to be sorted is empty.
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   420
     *
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   421
     * <p>The {@code <} relation does not provide a total order on all double
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   422
     * 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
   423
     * value compares neither less than, greater than, nor equal to any value,
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   424
     * even itself. This method uses the total order imposed by the method
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   425
     * {@link Double#compareTo}: {@code -0.0d} is treated as less than value
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   426
     * {@code 0.0d} and {@code Double.NaN} is considered greater than any
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   427
     * 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
   428
     *
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   429
     * <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
   430
     * 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
   431
     * 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
   432
     * 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
   433
     * faster than traditional (one-pivot) Quicksort implementations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * @param a the array to be sorted
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   436
     * @param fromIndex the index of the first element, inclusive, to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   437
     * @param toIndex the index of the last element, exclusive, to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   438
     *
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   439
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   440
     * @throws ArrayIndexOutOfBoundsException
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   441
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     */
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   443
    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
   444
        rangeCheck(a.length, fromIndex, toIndex);
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   445
        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   446
    }
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   447
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   448
    /**
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   449
     * Sorts the specified array into ascending numerical order.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   450
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   451
     * @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
   452
     * 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
   453
     * 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
   454
     * sorted using the appropriate {@link Arrays#sort(byte[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   455
     * 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
   456
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   457
     * Arrays#sort(byte[]) Arrays.sort} method. The algorithm requires a
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   458
     * 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
   459
     * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   460
     * execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   461
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   462
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   463
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   464
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   465
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   466
    public static void parallelSort(byte[] a) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   467
        int n = a.length, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   468
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   469
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   470
            DualPivotQuicksort.sort(a, 0, n - 1);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   471
        else
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   472
            new ArraysParallelSortHelpers.FJByte.Sorter
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   473
                (null, a, new byte[n], 0, n, 0,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   474
                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   475
                 MIN_ARRAY_SORT_GRAN : g).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   476
    }
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
    /**
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   479
     * Sorts the specified range of the array into ascending numerical order.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   480
     * The range to be sorted extends from the index {@code fromIndex},
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   481
     * inclusive, to the index {@code toIndex}, exclusive. If
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   482
     * {@code fromIndex == toIndex}, the range to be sorted is empty.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   483
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   484
     * @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
   485
     * 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
   486
     * 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
   487
     * sorted using the appropriate {@link Arrays#sort(byte[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   488
     * 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
   489
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   490
     * Arrays#sort(byte[]) Arrays.sort} method. The algorithm requires a working
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   491
     * 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
   492
     * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   493
     * used to execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   494
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   495
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   496
     * @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
   497
     * @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
   498
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   499
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   500
     * @throws ArrayIndexOutOfBoundsException
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   501
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   502
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   503
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   504
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   505
    public static void parallelSort(byte[] a, int fromIndex, int toIndex) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   506
        rangeCheck(a.length, fromIndex, toIndex);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   507
        int n = toIndex - fromIndex, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   508
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   509
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   510
            DualPivotQuicksort.sort(a, fromIndex, toIndex - 1);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   511
        else
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   512
            new ArraysParallelSortHelpers.FJByte.Sorter
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   513
                (null, a, new byte[n], fromIndex, n, 0,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   514
                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   515
                 MIN_ARRAY_SORT_GRAN : g).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   516
    }
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   517
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   518
    /**
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   519
     * Sorts the specified array into ascending numerical order.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   520
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   521
     * @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
   522
     * 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
   523
     * 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
   524
     * sorted using the appropriate {@link Arrays#sort(char[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   525
     * 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
   526
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   527
     * Arrays#sort(char[]) Arrays.sort} method. The algorithm requires a
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   528
     * 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
   529
     * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   530
     * execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   531
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   532
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   533
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   534
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   535
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   536
    public static void parallelSort(char[] a) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   537
        int n = a.length, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   538
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   539
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   540
            DualPivotQuicksort.sort(a, 0, n - 1, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   541
        else
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   542
            new ArraysParallelSortHelpers.FJChar.Sorter
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   543
                (null, a, new char[n], 0, n, 0,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   544
                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   545
                 MIN_ARRAY_SORT_GRAN : g).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   546
    }
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
    /**
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   549
     * Sorts the specified range of the array into ascending numerical order.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   550
     * The range to be sorted extends from the index {@code fromIndex},
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   551
     * inclusive, to the index {@code toIndex}, exclusive. If
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   552
     * {@code fromIndex == toIndex}, the range to be sorted is empty.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   553
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   554
      @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
   555
     * 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
   556
     * 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
   557
     * sorted using the appropriate {@link Arrays#sort(char[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   558
     * 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
   559
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   560
     * Arrays#sort(char[]) Arrays.sort} method. The algorithm requires a working
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   561
     * 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
   562
     * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   563
     * used to execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   564
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   565
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   566
     * @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
   567
     * @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
   568
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   569
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   570
     * @throws ArrayIndexOutOfBoundsException
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   571
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   572
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   573
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   574
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   575
    public static void parallelSort(char[] a, int fromIndex, int toIndex) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   576
        rangeCheck(a.length, fromIndex, toIndex);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   577
        int n = toIndex - fromIndex, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   578
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   579
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   580
            DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   581
        else
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   582
            new ArraysParallelSortHelpers.FJChar.Sorter
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   583
                (null, a, new char[n], fromIndex, n, 0,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   584
                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   585
                 MIN_ARRAY_SORT_GRAN : g).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   586
    }
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   587
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   588
    /**
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   589
     * Sorts the specified array into ascending numerical order.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   590
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   591
     * @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
   592
     * 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
   593
     * 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
   594
     * sorted using the appropriate {@link Arrays#sort(short[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   595
     * 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
   596
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   597
     * Arrays#sort(short[]) Arrays.sort} method. The algorithm requires a
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   598
     * 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
   599
     * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   600
     * execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   601
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   602
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   603
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   604
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   605
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   606
    public static void parallelSort(short[] a) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   607
        int n = a.length, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   608
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   609
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   610
            DualPivotQuicksort.sort(a, 0, n - 1, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   611
        else
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   612
            new ArraysParallelSortHelpers.FJShort.Sorter
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   613
                (null, a, new short[n], 0, n, 0,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   614
                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   615
                 MIN_ARRAY_SORT_GRAN : g).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   616
    }
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
    /**
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   619
     * Sorts the specified range of the array into ascending numerical order.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   620
     * The range to be sorted extends from the index {@code fromIndex},
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   621
     * inclusive, to the index {@code toIndex}, exclusive. If
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   622
     * {@code fromIndex == toIndex}, the range to be sorted is empty.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   623
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   624
     * @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
   625
     * 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
   626
     * 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
   627
     * sorted using the appropriate {@link Arrays#sort(short[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   628
     * 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
   629
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   630
     * Arrays#sort(short[]) Arrays.sort} method. The algorithm requires a working
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   631
     * 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
   632
     * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   633
     * used to execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   634
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   635
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   636
     * @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
   637
     * @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
   638
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   639
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   640
     * @throws ArrayIndexOutOfBoundsException
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   641
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   642
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   643
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   644
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   645
    public static void parallelSort(short[] a, int fromIndex, int toIndex) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   646
        rangeCheck(a.length, fromIndex, toIndex);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   647
        int n = toIndex - fromIndex, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   648
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   649
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   650
            DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   651
        else
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   652
            new ArraysParallelSortHelpers.FJShort.Sorter
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   653
                (null, a, new short[n], fromIndex, n, 0,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   654
                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   655
                 MIN_ARRAY_SORT_GRAN : g).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   656
    }
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   657
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   658
    /**
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   659
     * Sorts the specified array into ascending numerical order.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   660
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   661
     * @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
   662
     * 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
   663
     * 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
   664
     * sorted using the appropriate {@link Arrays#sort(int[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   665
     * 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
   666
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   667
     * Arrays#sort(int[]) Arrays.sort} method. The algorithm requires a
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   668
     * 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
   669
     * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   670
     * execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   671
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   672
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   673
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   674
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   675
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   676
    public static void parallelSort(int[] a) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   677
        int n = a.length, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   678
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   679
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   680
            DualPivotQuicksort.sort(a, 0, n - 1, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   681
        else
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   682
            new ArraysParallelSortHelpers.FJInt.Sorter
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   683
                (null, a, new int[n], 0, n, 0,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   684
                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   685
                 MIN_ARRAY_SORT_GRAN : g).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   686
    }
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
    /**
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   689
     * Sorts the specified range of the array into ascending numerical order.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   690
     * The range to be sorted extends from the index {@code fromIndex},
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   691
     * inclusive, to the index {@code toIndex}, exclusive. If
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   692
     * {@code fromIndex == toIndex}, the range to be sorted is empty.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   693
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   694
     * @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
   695
     * 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
   696
     * 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
   697
     * sorted using the appropriate {@link Arrays#sort(int[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   698
     * 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
   699
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   700
     * Arrays#sort(int[]) Arrays.sort} method. The algorithm requires a working
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   701
     * 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
   702
     * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   703
     * used to execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   704
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   705
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   706
     * @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
   707
     * @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
   708
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   709
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   710
     * @throws ArrayIndexOutOfBoundsException
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   711
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   712
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   713
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   714
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   715
    public static void parallelSort(int[] a, int fromIndex, int toIndex) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   716
        rangeCheck(a.length, fromIndex, toIndex);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   717
        int n = toIndex - fromIndex, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   718
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   719
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   720
            DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   721
        else
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   722
            new ArraysParallelSortHelpers.FJInt.Sorter
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   723
                (null, a, new int[n], fromIndex, n, 0,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   724
                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   725
                 MIN_ARRAY_SORT_GRAN : g).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   726
    }
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   727
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   728
    /**
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   729
     * Sorts the specified array into ascending numerical order.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   730
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   731
     * @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
   732
     * 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
   733
     * 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
   734
     * sorted using the appropriate {@link Arrays#sort(long[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   735
     * 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
   736
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   737
     * Arrays#sort(long[]) Arrays.sort} method. The algorithm requires a
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   738
     * 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
   739
     * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   740
     * execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   741
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   742
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   743
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   744
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   745
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   746
    public static void parallelSort(long[] a) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   747
        int n = a.length, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   748
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   749
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   750
            DualPivotQuicksort.sort(a, 0, n - 1, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   751
        else
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   752
            new ArraysParallelSortHelpers.FJLong.Sorter
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   753
                (null, a, new long[n], 0, n, 0,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   754
                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   755
                 MIN_ARRAY_SORT_GRAN : g).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   756
    }
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
    /**
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   759
     * Sorts the specified range of the array into ascending numerical order.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   760
     * The range to be sorted extends from the index {@code fromIndex},
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   761
     * inclusive, to the index {@code toIndex}, exclusive. If
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   762
     * {@code fromIndex == toIndex}, the range to be sorted is empty.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   763
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   764
     * @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
   765
     * 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
   766
     * 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
   767
     * sorted using the appropriate {@link Arrays#sort(long[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   768
     * 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
   769
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   770
     * Arrays#sort(long[]) Arrays.sort} method. The algorithm requires a working
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   771
     * 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
   772
     * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   773
     * used to execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   774
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   775
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   776
     * @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
   777
     * @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
   778
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   779
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   780
     * @throws ArrayIndexOutOfBoundsException
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   781
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   782
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   783
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   784
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   785
    public static void parallelSort(long[] a, int fromIndex, int toIndex) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   786
        rangeCheck(a.length, fromIndex, toIndex);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   787
        int n = toIndex - fromIndex, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   788
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   789
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   790
            DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   791
        else
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   792
            new ArraysParallelSortHelpers.FJLong.Sorter
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   793
                (null, a, new long[n], fromIndex, n, 0,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   794
                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   795
                 MIN_ARRAY_SORT_GRAN : g).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   796
    }
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   797
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   798
    /**
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   799
     * Sorts the specified array into ascending numerical order.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   800
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   801
     * <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
   802
     * 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
   803
     * 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
   804
     * 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
   805
     * {@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
   806
     * {@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
   807
     * other value and all {@code Float.NaN} values are considered equal.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   808
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   809
     * @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
   810
     * 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
   811
     * 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
   812
     * sorted using the appropriate {@link Arrays#sort(float[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   813
     * 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
   814
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   815
     * Arrays#sort(float[]) Arrays.sort} method. The algorithm requires a
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   816
     * 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
   817
     * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   818
     * execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   819
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   820
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   821
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   822
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   823
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   824
    public static void parallelSort(float[] a) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   825
        int n = a.length, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   826
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   827
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   828
            DualPivotQuicksort.sort(a, 0, n - 1, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   829
        else
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   830
            new ArraysParallelSortHelpers.FJFloat.Sorter
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   831
                (null, a, new float[n], 0, n, 0,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   832
                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   833
                 MIN_ARRAY_SORT_GRAN : g).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   834
    }
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
    /**
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   837
     * Sorts the specified range of the array into ascending numerical order.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   838
     * The range to be sorted extends from the index {@code fromIndex},
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   839
     * inclusive, to the index {@code toIndex}, exclusive. If
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   840
     * {@code fromIndex == toIndex}, the range to be sorted is empty.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   841
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   842
     * <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
   843
     * 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
   844
     * 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
   845
     * 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
   846
     * {@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
   847
     * {@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
   848
     * other value and all {@code Float.NaN} values are considered equal.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   849
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   850
     * @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
   851
     * 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
   852
     * 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
   853
     * sorted using the appropriate {@link Arrays#sort(float[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   854
     * 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
   855
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   856
     * Arrays#sort(float[]) Arrays.sort} method. The algorithm requires a working
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   857
     * 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
   858
     * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   859
     * used to execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   860
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   861
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   862
     * @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
   863
     * @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
   864
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   865
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   866
     * @throws ArrayIndexOutOfBoundsException
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   867
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   868
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   869
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   870
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   871
    public static void parallelSort(float[] a, int fromIndex, int toIndex) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   872
        rangeCheck(a.length, fromIndex, toIndex);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   873
        int n = toIndex - fromIndex, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   874
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   875
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   876
            DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   877
        else
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   878
            new ArraysParallelSortHelpers.FJFloat.Sorter
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   879
                (null, a, new float[n], fromIndex, n, 0,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   880
                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   881
                 MIN_ARRAY_SORT_GRAN : g).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   882
    }
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   883
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   884
    /**
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   885
     * Sorts the specified array into ascending numerical order.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   886
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   887
     * <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
   888
     * 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
   889
     * 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
   890
     * 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
   891
     * {@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
   892
     * {@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
   893
     * other value and all {@code Double.NaN} values are considered equal.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   894
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   895
     * @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
   896
     * 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
   897
     * 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
   898
     * sorted using the appropriate {@link Arrays#sort(double[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   899
     * 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
   900
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   901
     * Arrays#sort(double[]) Arrays.sort} method. The algorithm requires a
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   902
     * 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
   903
     * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   904
     * execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   905
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   906
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   907
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   908
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   909
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   910
    public static void parallelSort(double[] a) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   911
        int n = a.length, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   912
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   913
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   914
            DualPivotQuicksort.sort(a, 0, n - 1, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   915
        else
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   916
            new ArraysParallelSortHelpers.FJDouble.Sorter
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   917
                (null, a, new double[n], 0, n, 0,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   918
                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   919
                 MIN_ARRAY_SORT_GRAN : g).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   920
    }
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
    /**
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   923
     * Sorts the specified range of the array into ascending numerical order.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   924
     * The range to be sorted extends from the index {@code fromIndex},
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   925
     * inclusive, to the index {@code toIndex}, exclusive. If
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   926
     * {@code fromIndex == toIndex}, the range to be sorted is empty.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   927
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   928
     * <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
   929
     * 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
   930
     * 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
   931
     * 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
   932
     * {@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
   933
     * {@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
   934
     * other value and all {@code Double.NaN} values are considered equal.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   935
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   936
     * @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
   937
     * 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
   938
     * 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
   939
     * sorted using the appropriate {@link Arrays#sort(double[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   940
     * 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
   941
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   942
     * Arrays#sort(double[]) Arrays.sort} method. The algorithm requires a working
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   943
     * 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
   944
     * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   945
     * used to execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   946
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   947
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   948
     * @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
   949
     * @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
   950
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   951
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   952
     * @throws ArrayIndexOutOfBoundsException
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   953
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   954
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   955
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   956
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   957
    public static void parallelSort(double[] a, int fromIndex, int toIndex) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   958
        rangeCheck(a.length, fromIndex, toIndex);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   959
        int n = toIndex - fromIndex, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   960
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   961
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   962
            DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   963
        else
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   964
            new ArraysParallelSortHelpers.FJDouble.Sorter
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   965
                (null, a, new double[n], fromIndex, n, 0,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   966
                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   967
                 MIN_ARRAY_SORT_GRAN : g).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   968
    }
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   969
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   970
    /**
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   971
     * Sorts the specified array of objects into ascending order, according
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   972
     * to the {@linkplain Comparable natural ordering} of its elements.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   973
     * All elements in the array must implement the {@link Comparable}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   974
     * interface.  Furthermore, all elements in the array must be
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   975
     * <i>mutually comparable</i> (that is, {@code e1.compareTo(e2)} must
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   976
     * not throw a {@code ClassCastException} for any elements {@code e1}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   977
     * and {@code e2} in the array).
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   978
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   979
     * <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
   980
     * not be reordered as a result of the sort.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   981
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   982
     * @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
   983
     * 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
   984
     * 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
   985
     * sorted using the appropriate {@link Arrays#sort(Object[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   986
     * 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
   987
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   988
     * Arrays#sort(Object[]) Arrays.sort} method. The algorithm requires a
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   989
     * 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
   990
     * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   991
     * execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   992
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
   993
     * @param <T> the class of the objects to be sorted
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   994
     * @param a the array to be sorted
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
     * @throws ClassCastException if the array contains elements that are not
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   997
     *         <i>mutually comparable</i> (for example, strings and integers)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   998
     * @throws IllegalArgumentException (optional) if the natural
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   999
     *         ordering of the array elements is found to violate the
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1000
     *         {@link Comparable} contract
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1001
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1002
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1003
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1004
    @SuppressWarnings("unchecked")
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1005
    public static <T extends Comparable<? super T>> void parallelSort(T[] a) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1006
        int n = a.length, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1007
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1008
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1009
            TimSort.sort(a, 0, n, NaturalOrder.INSTANCE, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1010
        else
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 21278
diff changeset
  1011
            new ArraysParallelSortHelpers.FJObject.Sorter<>
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1012
                (null, a,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1013
                 (T[])Array.newInstance(a.getClass().getComponentType(), n),
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1014
                 0, n, 0, ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1015
                 MIN_ARRAY_SORT_GRAN : g, NaturalOrder.INSTANCE).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1016
    }
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1017
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1018
    /**
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1019
     * Sorts the specified range of the specified array of objects into
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1020
     * ascending order, according to the
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1021
     * {@linkplain Comparable natural ordering} of its
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1022
     * elements.  The range to be sorted extends from index
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1023
     * {@code fromIndex}, inclusive, to index {@code toIndex}, exclusive.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1024
     * (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
  1025
     * elements in this range must implement the {@link Comparable}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1026
     * interface.  Furthermore, all elements in this range must be <i>mutually
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1027
     * 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
  1028
     * {@code ClassCastException} for any elements {@code e1} and
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1029
     * {@code e2} in the array).
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1030
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1031
     * <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
  1032
     * not be reordered as a result of the sort.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1033
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1034
     * @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
  1035
     * 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
  1036
     * 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
  1037
     * sorted using the appropriate {@link Arrays#sort(Object[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1038
     * 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
  1039
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1040
     * Arrays#sort(Object[]) Arrays.sort} method. The algorithm requires a working
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1041
     * 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
  1042
     * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1043
     * used to execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1044
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  1045
     * @param <T> the class of the objects to be sorted
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1046
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1047
     * @param fromIndex the index of the first element (inclusive) to be
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1048
     *        sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1049
     * @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
  1050
     * @throws IllegalArgumentException if {@code fromIndex > toIndex} or
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1051
     *         (optional) if the natural ordering of the array elements is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1052
     *         found to violate the {@link Comparable} contract
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1053
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1054
     *         {@code toIndex > a.length}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1055
     * @throws ClassCastException if the array contains elements that are
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1056
     *         not <i>mutually comparable</i> (for example, strings and
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1057
     *         integers).
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1058
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1059
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1060
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1061
    @SuppressWarnings("unchecked")
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1062
    public static <T extends Comparable<? super T>>
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1063
    void parallelSort(T[] a, int fromIndex, int toIndex) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1064
        rangeCheck(a.length, fromIndex, toIndex);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1065
        int n = toIndex - fromIndex, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1066
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1067
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1068
            TimSort.sort(a, fromIndex, toIndex, NaturalOrder.INSTANCE, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1069
        else
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 21278
diff changeset
  1070
            new ArraysParallelSortHelpers.FJObject.Sorter<>
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1071
                (null, a,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1072
                 (T[])Array.newInstance(a.getClass().getComponentType(), n),
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1073
                 fromIndex, n, 0, ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1074
                 MIN_ARRAY_SORT_GRAN : g, NaturalOrder.INSTANCE).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1075
    }
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1076
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
     * 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
  1079
     * the specified comparator.  All elements in the array must be
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1080
     * <i>mutually comparable</i> by the specified comparator (that is,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1081
     * {@code c.compare(e1, e2)} must not throw a {@code ClassCastException}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1082
     * for any elements {@code e1} and {@code e2} in the array).
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1083
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1084
     * <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
  1085
     * not be reordered as a result of the sort.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1086
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1087
     * @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
  1088
     * 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
  1089
     * 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
  1090
     * sorted using the appropriate {@link Arrays#sort(Object[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1091
     * 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
  1092
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1093
     * Arrays#sort(Object[]) Arrays.sort} method. The algorithm requires a
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1094
     * 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
  1095
     * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1096
     * execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1097
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  1098
     * @param <T> the class of the objects to be sorted
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1099
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1100
     * @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
  1101
     *        {@code null} value indicates that the elements'
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1102
     *        {@linkplain Comparable natural ordering} should be used.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1103
     * @throws ClassCastException if the array contains elements that are
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1104
     *         not <i>mutually comparable</i> using the specified comparator
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1105
     * @throws IllegalArgumentException (optional) if the comparator is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1106
     *         found to violate the {@link java.util.Comparator} contract
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1107
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1108
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1109
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1110
    @SuppressWarnings("unchecked")
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1111
    public static <T> void parallelSort(T[] a, Comparator<? super T> cmp) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1112
        if (cmp == null)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1113
            cmp = NaturalOrder.INSTANCE;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1114
        int n = a.length, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1115
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1116
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1117
            TimSort.sort(a, 0, n, cmp, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1118
        else
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 21278
diff changeset
  1119
            new ArraysParallelSortHelpers.FJObject.Sorter<>
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1120
                (null, a,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1121
                 (T[])Array.newInstance(a.getClass().getComponentType(), n),
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1122
                 0, n, 0, ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1123
                 MIN_ARRAY_SORT_GRAN : g, cmp).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1124
    }
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1125
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1126
    /**
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1127
     * Sorts the specified range of the specified array of objects according
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1128
     * 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
  1129
     * sorted extends from index {@code fromIndex}, inclusive, to index
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1130
     * {@code toIndex}, exclusive.  (If {@code fromIndex==toIndex}, the
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1131
     * 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
  1132
     * <i>mutually comparable</i> by the specified comparator (that is,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1133
     * {@code c.compare(e1, e2)} must not throw a {@code ClassCastException}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1134
     * for any elements {@code e1} and {@code e2} in the range).
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1135
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1136
     * <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
  1137
     * not be reordered as a result of the sort.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1138
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1139
     * @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
  1140
     * 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
  1141
     * 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
  1142
     * sorted using the appropriate {@link Arrays#sort(Object[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1143
     * 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
  1144
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1145
     * Arrays#sort(Object[]) Arrays.sort} method. The algorithm requires a working
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1146
     * 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
  1147
     * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1148
     * used to execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1149
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  1150
     * @param <T> the class of the objects to be sorted
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1151
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1152
     * @param fromIndex the index of the first element (inclusive) to be
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1153
     *        sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1154
     * @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
  1155
     * @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
  1156
     *        {@code null} value indicates that the elements'
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1157
     *        {@linkplain Comparable natural ordering} should be used.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1158
     * @throws IllegalArgumentException if {@code fromIndex > toIndex} or
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1159
     *         (optional) if the natural ordering of the array elements is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1160
     *         found to violate the {@link Comparable} contract
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1161
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1162
     *         {@code toIndex > a.length}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1163
     * @throws ClassCastException if the array contains elements that are
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1164
     *         not <i>mutually comparable</i> (for example, strings and
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1165
     *         integers).
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1166
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1167
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1168
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1169
    @SuppressWarnings("unchecked")
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1170
    public static <T> void parallelSort(T[] a, int fromIndex, int toIndex,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1171
                                        Comparator<? super T> cmp) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1172
        rangeCheck(a.length, fromIndex, toIndex);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1173
        if (cmp == null)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1174
            cmp = NaturalOrder.INSTANCE;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1175
        int n = toIndex - fromIndex, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1176
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1177
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1178
            TimSort.sort(a, fromIndex, toIndex, cmp, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1179
        else
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 21278
diff changeset
  1180
            new ArraysParallelSortHelpers.FJObject.Sorter<>
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1181
                (null, a,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1182
                 (T[])Array.newInstance(a.getClass().getComponentType(), n),
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1183
                 fromIndex, n, 0, ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1184
                 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
  1185
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
  1187
    /*
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
  1188
     * Sorting of complex type arrays.
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
  1189
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1191
    /**
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1192
     * 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
  1193
     * 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
  1194
     * 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
  1195
     * 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
  1196
     */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1197
    static final class LegacyMergeSort {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1198
        private static final boolean userRequested =
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1199
            java.security.AccessController.doPrivileged(
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1200
                new sun.security.action.GetBooleanAction(
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1201
                    "java.util.Arrays.useLegacyMergeSort")).booleanValue();
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1202
    }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1203
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
    /**
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1205
     * 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
  1206
     * 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
  1207
     * 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
  1208
     * 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
  1209
     * <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
  1210
     * 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
  1211
     * and {@code e2} in the array).
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1212
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1213
     * <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
  1214
     * not be reordered as a result of the sort.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
     *
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1216
     * <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
  1217
     * 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
  1218
     * 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
  1219
     * 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
  1220
     * 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
  1221
     * implementation requires approximately n comparisons.  Temporary
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1222
     * 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
  1223
     * 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
  1224
     * arrays.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
     *
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1226
     * <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
  1227
     * descending order in its input array, and can take advantage of
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 26451
diff changeset
  1228
     * ascending and descending order in different parts of the same
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1229
     * 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
  1230
     * 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
  1231
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1232
     * <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
  1233
     * (<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
  1234
     * 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
  1235
     * 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
  1236
     * 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
  1237
     * January 1993.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
     * @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
  1240
     * @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
  1241
     *         <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
  1242
     * @throws IllegalArgumentException (optional) if the natural
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1243
     *         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
  1244
     *         {@link Comparable} contract
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
    public static void sort(Object[] a) {
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1247
        if (LegacyMergeSort.userRequested)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1248
            legacyMergeSort(a);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1249
        else
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1250
            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
  1251
    }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1252
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1253
    /** To be removed in a future release. */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1254
    private static void legacyMergeSort(Object[] a) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1255
        Object[] aux = a.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
        mergeSort(aux, a, 0, a.length, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
     * Sorts the specified range of the specified array of objects into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
     * ascending order, according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
     * {@linkplain Comparable natural ordering} of its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
     * 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
  1264
     * {@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
  1265
     * (If {@code fromIndex==toIndex}, the range to be sorted is empty.)  All
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
     * elements in this range must implement the {@link Comparable}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
     * 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
  1268
     * 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
  1269
     * {@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
  1270
     * {@code e2} in the array).
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1271
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1272
     * <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
  1273
     * not be reordered as a result of the sort.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
     *
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1275
     * <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
  1276
     * 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
  1277
     * 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
  1278
     * 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
  1279
     * 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
  1280
     * implementation requires approximately n comparisons.  Temporary
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1281
     * 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
  1282
     * 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
  1283
     * arrays.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
     *
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1285
     * <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
  1286
     * descending order in its input array, and can take advantage of
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 26451
diff changeset
  1287
     * ascending and descending order in different parts of the same
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1288
     * 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
  1289
     * 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
  1290
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1291
     * <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
  1292
     * (<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
  1293
     * 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
  1294
     * 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
  1295
     * 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
  1296
     * January 1993.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
     * @param a the array to be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
     *        sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
     * @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
  1302
     * @throws IllegalArgumentException if {@code fromIndex > toIndex} or
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1303
     *         (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
  1304
     *         found to violate the {@link Comparable} contract
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1305
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1306
     *         {@code toIndex > a.length}
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1307
     * @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
  1308
     *         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
  1309
     *         integers).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
    public static void sort(Object[] a, int fromIndex, int toIndex) {
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1312
        rangeCheck(a.length, fromIndex, toIndex);
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1313
        if (LegacyMergeSort.userRequested)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1314
            legacyMergeSort(a, fromIndex, toIndex);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1315
        else
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1316
            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
  1317
    }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1318
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1319
    /** To be removed in a future release. */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1320
    private static void legacyMergeSort(Object[] a,
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1321
                                        int fromIndex, int toIndex) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
        Object[] aux = copyOfRange(a, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
        mergeSort(aux, a, fromIndex, toIndex, -fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
     * 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
  1328
     * used in preference to mergesort.
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1329
     * To be removed in a future release.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
    private static final int INSERTIONSORT_THRESHOLD = 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
     * Src is the source array that starts at index 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
     * Dest is the (possibly larger) array destination with a possible offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
     * low is the index in dest to start sorting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
     * high is the end index in dest to end sorting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
     * 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
  1339
     * To be removed in a future release.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
     */
13795
73850c397272 7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents: 12448
diff changeset
  1341
    @SuppressWarnings({"unchecked", "rawtypes"})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
    private static void mergeSort(Object[] src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
                                  Object[] dest,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
                                  int low,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
                                  int high,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
                                  int off) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
        int length = high - low;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
        // Insertion sort on smallest arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
        if (length < INSERTIONSORT_THRESHOLD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
            for (int i=low; i<high; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
                for (int j=i; j>low &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
                         ((Comparable) dest[j-1]).compareTo(dest[j])>0; j--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
                    swap(dest, j, j-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
        // Recursively sort halves of dest into src
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
        int destLow  = low;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
        int destHigh = high;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
        low  += off;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
        high += off;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
        int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
        mergeSort(dest, src, low, mid, -off);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
        mergeSort(dest, src, mid, high, -off);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
        // If list is already sorted, just copy from src to dest.  This is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
        // optimization that results in faster sorts for nearly ordered lists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
        if (((Comparable)src[mid-1]).compareTo(src[mid]) <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
            System.arraycopy(src, low, dest, destLow, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
        // Merge sorted halves (now in src) into dest
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
        for(int i = destLow, p = low, q = mid; i < destHigh; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
            if (q >= high || p < mid && ((Comparable)src[p]).compareTo(src[q])<=0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
                dest[i] = src[p++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
                dest[i] = src[q++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
     * Swaps x[a] with x[b].
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
    private static void swap(Object[] x, int a, int b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
        Object t = x[a];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
        x[a] = x[b];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
        x[b] = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
     * Sorts the specified array of objects according to the order induced by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
     * the specified comparator.  All elements in the array must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
     * <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
  1396
     * {@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
  1397
     * 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
  1398
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1399
     * <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
  1400
     * not be reordered as a result of the sort.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
     *
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1402
     * <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
  1403
     * 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
  1404
     * 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
  1405
     * 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
  1406
     * 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
  1407
     * implementation requires approximately n comparisons.  Temporary
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1408
     * 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
  1409
     * 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
  1410
     * arrays.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
     *
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1412
     * <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
  1413
     * descending order in its input array, and can take advantage of
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 26451
diff changeset
  1414
     * ascending and descending order in different parts of the same
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1415
     * 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
  1416
     * 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
  1417
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1418
     * <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
  1419
     * (<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
  1420
     * 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
  1421
     * 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
  1422
     * 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
  1423
     * January 1993.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  1425
     * @param <T> the class of the objects to be sorted
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
     * @param a the array to be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
     * @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
  1428
     *        {@code null} value indicates that the elements'
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
     *        {@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
  1430
     * @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
  1431
     *         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
  1432
     * @throws IllegalArgumentException (optional) if the comparator is
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1433
     *         found to violate the {@link Comparator} contract
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
    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
  1436
        if (c == null) {
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1437
            sort(a);
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1438
        } else {
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1439
            if (LegacyMergeSort.userRequested)
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1440
                legacyMergeSort(a, c);
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1441
            else
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1442
                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
  1443
        }
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1444
    }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1445
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1446
    /** To be removed in a future release. */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1447
    private static <T> void legacyMergeSort(T[] a, Comparator<? super T> c) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1448
        T[] aux = a.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
        if (c==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
            mergeSort(aux, a, 0, a.length, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
            mergeSort(aux, a, 0, a.length, 0, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
     * Sorts the specified range of the specified array of objects according
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
     * 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
  1458
     * 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
  1459
     * {@code toIndex}, exclusive.  (If {@code fromIndex==toIndex}, the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
     * range to be sorted is empty.)  All elements in the range must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
     * <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
  1462
     * {@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
  1463
     * 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
  1464
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1465
     * <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
  1466
     * not be reordered as a result of the sort.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
     *
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1468
     * <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
  1469
     * 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
  1470
     * 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
  1471
     * 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
  1472
     * 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
  1473
     * implementation requires approximately n comparisons.  Temporary
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1474
     * 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
  1475
     * 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
  1476
     * arrays.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
     *
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1478
     * <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
  1479
     * descending order in its input array, and can take advantage of
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 26451
diff changeset
  1480
     * ascending and descending order in different parts of the same
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1481
     * 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
  1482
     * 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
  1483
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1484
     * <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
  1485
     * (<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
  1486
     * 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
  1487
     * 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
  1488
     * 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
  1489
     * January 1993.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  1491
     * @param <T> the class of the objects to be sorted
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
     * @param a the array to be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
     *        sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
     * @param toIndex the index of the last element (exclusive) to be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
     * @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
  1497
     *        {@code null} value indicates that the elements'
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
     *        {@linkplain Comparable natural ordering} should be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
     * @throws ClassCastException if the array contains elements that are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
     *         <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
  1501
     * @throws IllegalArgumentException if {@code fromIndex > toIndex} or
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1502
     *         (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
  1503
     *         {@link Comparator} contract
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1504
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1505
     *         {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
    public static <T> void sort(T[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
                                Comparator<? super T> c) {
22285
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1509
        if (c == null) {
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1510
            sort(a, fromIndex, toIndex);
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1511
        } else {
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1512
            rangeCheck(a.length, fromIndex, toIndex);
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1513
            if (LegacyMergeSort.userRequested)
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1514
                legacyMergeSort(a, fromIndex, toIndex, c);
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1515
            else
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1516
                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
  1517
        }
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1518
    }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1519
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1520
    /** To be removed in a future release. */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1521
    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
  1522
                                            Comparator<? super T> c) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1523
        T[] aux = copyOfRange(a, fromIndex, toIndex);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
        if (c==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
            mergeSort(aux, a, fromIndex, toIndex, -fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
            mergeSort(aux, a, fromIndex, toIndex, -fromIndex, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
     * Src is the source array that starts at index 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
     * Dest is the (possibly larger) array destination with a possible offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
     * low is the index in dest to start sorting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
     * high is the end index in dest to end sorting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
     * 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
  1536
     * To be removed in a future release.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
     */
13795
73850c397272 7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents: 12448
diff changeset
  1538
    @SuppressWarnings({"rawtypes", "unchecked"})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
    private static void mergeSort(Object[] src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
                                  Object[] dest,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
                                  int low, int high, int off,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
                                  Comparator c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
        int length = high - low;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
        // Insertion sort on smallest arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
        if (length < INSERTIONSORT_THRESHOLD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
            for (int i=low; i<high; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
                for (int j=i; j>low && c.compare(dest[j-1], dest[j])>0; j--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
                    swap(dest, j, j-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
        // Recursively sort halves of dest into src
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
        int destLow  = low;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
        int destHigh = high;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
        low  += off;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
        high += off;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
        int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
        mergeSort(dest, src, low, mid, -off, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
        mergeSort(dest, src, mid, high, -off, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
        // If list is already sorted, just copy from src to dest.  This is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
        // optimization that results in faster sorts for nearly ordered lists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
        if (c.compare(src[mid-1], src[mid]) <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
           System.arraycopy(src, low, dest, destLow, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
           return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
        // Merge sorted halves (now in src) into dest
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
        for(int i = destLow, p = low, q = mid; i < destHigh; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
            if (q >= high || p < mid && c.compare(src[p], src[q]) <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
                dest[i] = src[p++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
                dest[i] = src[q++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
18555
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1578
    // Parallel prefix
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1579
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1580
    /**
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1581
     * 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
  1582
     * 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
  1583
     * 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
  1584
     * 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
  1585
     * Parallel prefix computation is usually more efficient than
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1586
     * sequential loops for large arrays.
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1587
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  1588
     * @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
  1589
     * @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
  1590
     * @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
  1591
     * cumulation
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1592
     * @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
  1593
     * @since 1.8
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
    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
  1596
        Objects.requireNonNull(op);
18555
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1597
        if (array.length > 0)
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1598
            new ArrayPrefixHelpers.CumulateTask<>
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1599
                    (null, op, array, 0, array.length).invoke();
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1600
    }
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1601
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1602
    /**
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1603
     * Performs {@link #parallelPrefix(Object[], BinaryOperator)}
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1604
     * for the given subrange of the array.
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1605
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  1606
     * @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
  1607
     * @param array the array
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1608
     * @param fromIndex the index of the first element, inclusive
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1609
     * @param toIndex the index of the last element, exclusive
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1610
     * @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
  1611
     * cumulation
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1612
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1613
     * @throws ArrayIndexOutOfBoundsException
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1614
     *     if {@code fromIndex < 0} or {@code toIndex > array.length}
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1615
     * @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
  1616
     * @since 1.8
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1617
     */
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1618
    public static <T> void parallelPrefix(T[] array, int fromIndex,
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1619
                                          int toIndex, BinaryOperator<T> op) {
20492
11418b1f61ff 8025067: Unconditionally throw NPE if null op provided to Arrays.parallelPrefix
mduigou
parents: 18829
diff changeset
  1620
        Objects.requireNonNull(op);
18555
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1621
        rangeCheck(array.length, fromIndex, toIndex);
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1622
        if (fromIndex < toIndex)
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1623
            new ArrayPrefixHelpers.CumulateTask<>
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1624
                    (null, op, array, fromIndex, toIndex).invoke();
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1625
    }
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1626
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1627
    /**
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1628
     * 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
  1629
     * 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
  1630
     * 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
  1631
     * 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
  1632
     * Parallel prefix computation is usually more efficient than
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1633
     * sequential loops for large arrays.
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
     * @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
  1636
     * @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
  1637
     * cumulation
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1638
     * @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
  1639
     * @since 1.8
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
    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
  1642
        Objects.requireNonNull(op);
18555
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1643
        if (array.length > 0)
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1644
            new ArrayPrefixHelpers.LongCumulateTask
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1645
                    (null, op, array, 0, array.length).invoke();
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1646
    }
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1647
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1648
    /**
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1649
     * Performs {@link #parallelPrefix(long[], LongBinaryOperator)}
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1650
     * for the given subrange of the array.
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1651
     *
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1652
     * @param array the array
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1653
     * @param fromIndex the index of the first element, inclusive
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1654
     * @param toIndex the index of the last element, exclusive
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1655
     * @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
  1656
     * cumulation
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1657
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1658
     * @throws ArrayIndexOutOfBoundsException
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1659
     *     if {@code fromIndex < 0} or {@code toIndex > array.length}
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1660
     * @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
  1661
     * @since 1.8
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1662
     */
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1663
    public static void parallelPrefix(long[] array, int fromIndex,
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1664
                                      int toIndex, LongBinaryOperator op) {
20492
11418b1f61ff 8025067: Unconditionally throw NPE if null op provided to Arrays.parallelPrefix
mduigou
parents: 18829
diff changeset
  1665
        Objects.requireNonNull(op);
18555
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1666
        rangeCheck(array.length, fromIndex, toIndex);
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1667
        if (fromIndex < toIndex)
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1668
            new ArrayPrefixHelpers.LongCumulateTask
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1669
                    (null, op, array, fromIndex, toIndex).invoke();
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1670
    }
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1671
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1672
    /**
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1673
     * 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
  1674
     * 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
  1675
     * 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
  1676
     * 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
  1677
     * Parallel prefix computation is usually more efficient than
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1678
     * sequential loops for large arrays.
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1679
     *
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1680
     * <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
  1681
     * 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
  1682
     * obtained if the operation was performed sequentially.
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1683
     *
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1684
     * @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
  1685
     * @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
  1686
     * @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
  1687
     * @since 1.8
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
    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
  1690
        Objects.requireNonNull(op);
18555
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1691
        if (array.length > 0)
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1692
            new ArrayPrefixHelpers.DoubleCumulateTask
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1693
                    (null, op, array, 0, array.length).invoke();
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1694
    }
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1695
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1696
    /**
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1697
     * Performs {@link #parallelPrefix(double[], DoubleBinaryOperator)}
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1698
     * for the given subrange of the array.
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1699
     *
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1700
     * @param array the array
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1701
     * @param fromIndex the index of the first element, inclusive
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1702
     * @param toIndex the index of the last element, exclusive
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1703
     * @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
  1704
     * cumulation
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1705
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1706
     * @throws ArrayIndexOutOfBoundsException
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1707
     *     if {@code fromIndex < 0} or {@code toIndex > array.length}
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1708
     * @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
  1709
     * @since 1.8
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1710
     */
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1711
    public static void parallelPrefix(double[] array, int fromIndex,
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1712
                                      int toIndex, DoubleBinaryOperator op) {
20492
11418b1f61ff 8025067: Unconditionally throw NPE if null op provided to Arrays.parallelPrefix
mduigou
parents: 18829
diff changeset
  1713
        Objects.requireNonNull(op);
18555
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1714
        rangeCheck(array.length, fromIndex, toIndex);
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1715
        if (fromIndex < toIndex)
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1716
            new ArrayPrefixHelpers.DoubleCumulateTask
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1717
                    (null, op, array, fromIndex, toIndex).invoke();
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1718
    }
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1719
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1720
    /**
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1721
     * 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
  1722
     * 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
  1723
     * 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
  1724
     * 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
  1725
     * Parallel prefix computation is usually more efficient than
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1726
     * sequential loops for large arrays.
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
     * @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
  1729
     * @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
  1730
     * cumulation
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1731
     * @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
  1732
     * @since 1.8
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
    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
  1735
        Objects.requireNonNull(op);
18555
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1736
        if (array.length > 0)
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1737
            new ArrayPrefixHelpers.IntCumulateTask
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1738
                    (null, op, array, 0, array.length).invoke();
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1739
    }
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1740
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1741
    /**
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1742
     * Performs {@link #parallelPrefix(int[], IntBinaryOperator)}
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1743
     * for the given subrange of the array.
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1744
     *
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1745
     * @param array the array
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1746
     * @param fromIndex the index of the first element, inclusive
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1747
     * @param toIndex the index of the last element, exclusive
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1748
     * @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
  1749
     * cumulation
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1750
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1751
     * @throws ArrayIndexOutOfBoundsException
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1752
     *     if {@code fromIndex < 0} or {@code toIndex > array.length}
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1753
     * @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
  1754
     * @since 1.8
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1755
     */
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1756
    public static void parallelPrefix(int[] array, int fromIndex,
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1757
                                      int toIndex, IntBinaryOperator op) {
20492
11418b1f61ff 8025067: Unconditionally throw NPE if null op provided to Arrays.parallelPrefix
mduigou
parents: 18829
diff changeset
  1758
        Objects.requireNonNull(op);
18555
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1759
        rangeCheck(array.length, fromIndex, toIndex);
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1760
        if (fromIndex < toIndex)
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1761
            new ArrayPrefixHelpers.IntCumulateTask
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1762
                    (null, op, array, fromIndex, toIndex).invoke();
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1763
    }
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1764
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
    // Searching
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
     * Searches the specified array of longs for the specified value using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
     * binary search algorithm.  The array must be sorted (as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
     * by the {@link #sort(long[])} method) prior to making this call.  If it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
     * is not sorted, the results are undefined.  If the array contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
     * @return index of the search key, if it is contained in the array;
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  1778
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
     *         key would be inserted into the array: the index of the first
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  1781
     *         element greater than the key, or {@code a.length} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
     *         elements in the array are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
    public static int binarySearch(long[] a, long key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
        return binarySearch0(a, 0, a.length, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
     * Searches a range of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
     * the specified array of longs for the specified value using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
     * binary search algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
     * The range must be sorted (as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
     * by the {@link #sort(long[], int, int)} method)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
     * prior to making this call.  If it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
     * is not sorted, the results are undefined.  If the range contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
     *          searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
     * @param toIndex the index of the last element (exclusive) to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
     * @return index of the search key, if it is contained in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
     *         within the specified range;
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  1808
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
     *         element in the range greater than the key,
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  1812
     *         or {@code toIndex} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
     *         elements in the range are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
     *         if {@code fromIndex > toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
     * @throws ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
     *         if {@code fromIndex < 0 or toIndex > a.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
    public static int binarySearch(long[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
                                   long key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
        return binarySearch0(a, fromIndex, toIndex, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
    // Like public version, but without range checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
    private static int binarySearch0(long[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
                                     long key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
        int low = fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
        int high = toIndex - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
            int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
            long midVal = a[mid];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
            if (midVal < key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
                low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
            else if (midVal > key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
                high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
                return mid; // key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
        return -(low + 1);  // key not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
     * Searches the specified array of ints for the specified value using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
     * binary search algorithm.  The array must be sorted (as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
     * by the {@link #sort(int[])} method) prior to making this call.  If it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
     * is not sorted, the results are undefined.  If the array contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
     * @return index of the search key, if it is contained in the array;
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  1859
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
     *         key would be inserted into the array: the index of the first
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  1862
     *         element greater than the key, or {@code a.length} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
     *         elements in the array are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
    public static int binarySearch(int[] a, int key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
        return binarySearch0(a, 0, a.length, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
     * Searches a range of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
     * the specified array of ints for the specified value using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
     * binary search algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
     * The range must be sorted (as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
     * by the {@link #sort(int[], int, int)} method)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
     * prior to making this call.  If it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
     * is not sorted, the results are undefined.  If the range contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
     *          searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
     * @param toIndex the index of the last element (exclusive) to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
     * @return index of the search key, if it is contained in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
     *         within the specified range;
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  1889
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
     *         element in the range greater than the key,
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  1893
     *         or {@code toIndex} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
     *         elements in the range are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
     *         if {@code fromIndex > toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
     * @throws ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
     *         if {@code fromIndex < 0 or toIndex > a.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
    public static int binarySearch(int[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
                                   int key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
        return binarySearch0(a, fromIndex, toIndex, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
    // Like public version, but without range checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
    private static int binarySearch0(int[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
                                     int key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
        int low = fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
        int high = toIndex - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
            int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
            int midVal = a[mid];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
            if (midVal < key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
                low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
            else if (midVal > key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
                high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
                return mid; // key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
        return -(low + 1);  // key not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
     * Searches the specified array of shorts for the specified value using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
     * the binary search algorithm.  The array must be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
     * (as by the {@link #sort(short[])} method) prior to making this call.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
     * it is not sorted, the results are undefined.  If the array contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
     * @return index of the search key, if it is contained in the array;
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  1940
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
     *         key would be inserted into the array: the index of the first
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  1943
     *         element greater than the key, or {@code a.length} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
     *         elements in the array are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
    public static int binarySearch(short[] a, short key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
        return binarySearch0(a, 0, a.length, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
     * Searches a range of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
     * the specified array of shorts for the specified value using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
     * the binary search algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
     * The range must be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
     * (as by the {@link #sort(short[], int, int)} method)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
     * prior to making this call.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
     * it is not sorted, the results are undefined.  If the range contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
     *          searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
     * @param toIndex the index of the last element (exclusive) to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
     * @return index of the search key, if it is contained in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
     *         within the specified range;
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  1970
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
     *         element in the range greater than the key,
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  1974
     *         or {@code toIndex} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
     *         elements in the range are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
     *         if {@code fromIndex > toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
     * @throws ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
     *         if {@code fromIndex < 0 or toIndex > a.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
    public static int binarySearch(short[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
                                   short key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
        return binarySearch0(a, fromIndex, toIndex, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
    // Like public version, but without range checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
    private static int binarySearch0(short[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
                                     short key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
        int low = fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
        int high = toIndex - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
            int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
            short midVal = a[mid];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
            if (midVal < key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
                low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
            else if (midVal > key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
                high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
                return mid; // key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
        return -(low + 1);  // key not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
     * Searches the specified array of chars for the specified value using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
     * binary search algorithm.  The array must be sorted (as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
     * by the {@link #sort(char[])} method) prior to making this call.  If it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
     * is not sorted, the results are undefined.  If the array contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
     * @return index of the search key, if it is contained in the array;
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2021
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
     *         key would be inserted into the array: the index of the first
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2024
     *         element greater than the key, or {@code a.length} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
     *         elements in the array are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
    public static int binarySearch(char[] a, char key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
        return binarySearch0(a, 0, a.length, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
     * Searches a range of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
     * the specified array of chars for the specified value using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
     * binary search algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
     * The range must be sorted (as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
     * by the {@link #sort(char[], int, int)} method)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
     * prior to making this call.  If it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
     * is not sorted, the results are undefined.  If the range contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
     *          searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
     * @param toIndex the index of the last element (exclusive) to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
     * @return index of the search key, if it is contained in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
     *         within the specified range;
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2051
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
     *         element in the range greater than the key,
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2055
     *         or {@code toIndex} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
     *         elements in the range are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
     *         if {@code fromIndex > toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
     * @throws ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
     *         if {@code fromIndex < 0 or toIndex > a.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
    public static int binarySearch(char[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
                                   char key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
        return binarySearch0(a, fromIndex, toIndex, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
    // Like public version, but without range checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
    private static int binarySearch0(char[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
                                     char key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
        int low = fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
        int high = toIndex - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
            int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
            char midVal = a[mid];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
            if (midVal < key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
                low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
            else if (midVal > key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
                high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
                return mid; // key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
        return -(low + 1);  // key not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
     * Searches the specified array of bytes for the specified value using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
     * binary search algorithm.  The array must be sorted (as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
     * by the {@link #sort(byte[])} method) prior to making this call.  If it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
     * is not sorted, the results are undefined.  If the array contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
     * @return index of the search key, if it is contained in the array;
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2102
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
     *         key would be inserted into the array: the index of the first
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2105
     *         element greater than the key, or {@code a.length} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
     *         elements in the array are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
    public static int binarySearch(byte[] a, byte key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
        return binarySearch0(a, 0, a.length, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
     * Searches a range of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
     * the specified array of bytes for the specified value using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
     * binary search algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
     * The range must be sorted (as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
     * by the {@link #sort(byte[], int, int)} method)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
     * prior to making this call.  If it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
     * is not sorted, the results are undefined.  If the range contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
     *          searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
     * @param toIndex the index of the last element (exclusive) to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
     * @return index of the search key, if it is contained in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
     *         within the specified range;
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2132
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
     *         element in the range greater than the key,
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2136
     *         or {@code toIndex} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
     *         elements in the range are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
     *         if {@code fromIndex > toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
     * @throws ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
     *         if {@code fromIndex < 0 or toIndex > a.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
    public static int binarySearch(byte[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
                                   byte key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
        return binarySearch0(a, fromIndex, toIndex, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
    // Like public version, but without range checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
    private static int binarySearch0(byte[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
                                     byte key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
        int low = fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
        int high = toIndex - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
            int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
            byte midVal = a[mid];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
            if (midVal < key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
                low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
            else if (midVal > key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
                high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
                return mid; // key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
        return -(low + 1);  // key not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
     * Searches the specified array of doubles for the specified value using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
     * the binary search algorithm.  The array must be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
     * (as by the {@link #sort(double[])} method) prior to making this call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
     * If it is not sorted, the results are undefined.  If the array contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
     * one will be found.  This method considers all NaN values to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
     * equivalent and equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
     * @return index of the search key, if it is contained in the array;
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2184
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
     *         key would be inserted into the array: the index of the first
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2187
     *         element greater than the key, or {@code a.length} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
     *         elements in the array are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
    public static int binarySearch(double[] a, double key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
        return binarySearch0(a, 0, a.length, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
     * Searches a range of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
     * the specified array of doubles for the specified value using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
     * the binary search algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
     * The range must be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
     * (as by the {@link #sort(double[], int, int)} method)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
     * prior to making this call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
     * If it is not sorted, the results are undefined.  If the range contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
     * one will be found.  This method considers all NaN values to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
     * equivalent and equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
     *          searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
     * @param toIndex the index of the last element (exclusive) to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
     * @return index of the search key, if it is contained in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
     *         within the specified range;
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2215
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
     *         element in the range greater than the key,
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2219
     *         or {@code toIndex} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
     *         elements in the range are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
     *         if {@code fromIndex > toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
     * @throws ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
     *         if {@code fromIndex < 0 or toIndex > a.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
    public static int binarySearch(double[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
                                   double key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
        return binarySearch0(a, fromIndex, toIndex, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
    // Like public version, but without range checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
    private static int binarySearch0(double[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
                                     double key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
        int low = fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
        int high = toIndex - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
            int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
            double midVal = a[mid];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
            if (midVal < key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
                low = mid + 1;  // Neither val is NaN, thisVal is smaller
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
            else if (midVal > key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
                high = mid - 1; // Neither val is NaN, thisVal is larger
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
                long midBits = Double.doubleToLongBits(midVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
                long keyBits = Double.doubleToLongBits(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
                if (midBits == keyBits)     // Values are equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
                    return mid;             // Key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
                else if (midBits < keyBits) // (-0.0, 0.0) or (!NaN, NaN)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
                    low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
                else                        // (0.0, -0.0) or (NaN, !NaN)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
                    high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
        return -(low + 1);  // key not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
     * 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
  2265
     * 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
  2266
     * (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
  2267
     * it is not sorted, the results are undefined. If the array contains
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
     * 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
  2269
     * one will be found. This method considers all NaN values to be
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
     * equivalent and equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
     * @return index of the search key, if it is contained in the array;
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2275
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
     *         key would be inserted into the array: the index of the first
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2278
     *         element greater than the key, or {@code a.length} if all
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
  2279
     *         elements in the array are less than the specified key. Note
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
    public static int binarySearch(float[] a, float key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
        return binarySearch0(a, 0, a.length, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
     * Searches a range of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
     * the specified array of floats for the specified value using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
     * the binary search algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
     * The range must be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
     * (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
  2293
     * prior to making this call. If
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
  2294
     * it is not sorted, the results are undefined. If the range contains
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
     * 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
  2296
     * one will be found. This method considers all NaN values to be
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
     * equivalent and equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
     *          searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
     * @param toIndex the index of the last element (exclusive) to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
     * @return index of the search key, if it is contained in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
     *         within the specified range;
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2306
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
     *         element in the range greater than the key,
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2310
     *         or {@code toIndex} if all
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
  2311
     *         elements in the range are less than the specified key. Note
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
     *         if {@code fromIndex > toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
     * @throws ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
     *         if {@code fromIndex < 0 or toIndex > a.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
    public static int binarySearch(float[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
                                   float key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
        return binarySearch0(a, fromIndex, toIndex, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
    // Like public version, but without range checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
    private static int binarySearch0(float[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
                                     float key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
        int low = fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
        int high = toIndex - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
            int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
            float midVal = a[mid];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
            if (midVal < key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
                low = mid + 1;  // Neither val is NaN, thisVal is smaller
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
            else if (midVal > key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
                high = mid - 1; // Neither val is NaN, thisVal is larger
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
                int midBits = Float.floatToIntBits(midVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
                int keyBits = Float.floatToIntBits(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
                if (midBits == keyBits)     // Values are equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
                    return mid;             // Key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
                else if (midBits < keyBits) // (-0.0, 0.0) or (!NaN, NaN)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
                    low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
                else                        // (0.0, -0.0) or (NaN, !NaN)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
                    high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
        return -(low + 1);  // key not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
     * 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
  2356
     * search algorithm. The array must be sorted into ascending order
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
     * according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
     * {@linkplain Comparable natural ordering}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
     * of its elements (as by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
     * {@link #sort(Object[])} method) prior to making this call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
     * If it is not sorted, the results are undefined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
     * (If the array contains elements that are not mutually comparable (for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
     * example, strings and integers), it <i>cannot</i> be sorted according
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
     * to the natural ordering of its elements, hence results are undefined.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
     * If the array contains multiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
     * elements equal to the specified object, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
     * @return index of the search key, if it is contained in the array;
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2372
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
     *         key would be inserted into the array: the index of the first
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2375
     *         element greater than the key, or {@code a.length} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
     *         elements in the array are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
     * @throws ClassCastException if the search key is not comparable to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
     *         elements of the array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
    public static int binarySearch(Object[] a, Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
        return binarySearch0(a, 0, a.length, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
     * Searches a range of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
     * the specified array for the specified object using the binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
     * search algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
     * The range must be sorted into ascending order
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
     * according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
     * {@linkplain Comparable natural ordering}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
     * of its elements (as by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
     * {@link #sort(Object[], int, int)} method) prior to making this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
     * call.  If it is not sorted, the results are undefined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
     * (If the range contains elements that are not mutually comparable (for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
     * example, strings and integers), it <i>cannot</i> be sorted according
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
     * to the natural ordering of its elements, hence results are undefined.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
     * If the range contains multiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
     * elements equal to the specified object, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
     *          searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
     * @param toIndex the index of the last element (exclusive) to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
     * @return index of the search key, if it is contained in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
     *         within the specified range;
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2410
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
     *         element in the range greater than the key,
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2414
     *         or {@code toIndex} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
     *         elements in the range are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2418
     * @throws ClassCastException if the search key is not comparable to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
     *         elements of the array within the specified range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2420
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
     *         if {@code fromIndex > toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
     * @throws ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
     *         if {@code fromIndex < 0 or toIndex > a.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
    public static int binarySearch(Object[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
                                   Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
        return binarySearch0(a, fromIndex, toIndex, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2430
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
    // Like public version, but without range checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2433
    private static int binarySearch0(Object[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
                                     Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
        int low = fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
        int high = toIndex - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
            int mid = (low + high) >>> 1;
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  2440
            @SuppressWarnings("rawtypes")
14925
72729557c226 8003981: Support Parallel Array Sorting - JEP 103
chegar
parents: 13795
diff changeset
  2441
            Comparable midVal = (Comparable)a[mid];
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  2442
            @SuppressWarnings("unchecked")
14925
72729557c226 8003981: Support Parallel Array Sorting - JEP 103
chegar
parents: 13795
diff changeset
  2443
            int cmp = midVal.compareTo(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2444
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2445
            if (cmp < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2446
                low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2447
            else if (cmp > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2448
                high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2449
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2450
                return mid; // key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2451
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2452
        return -(low + 1);  // key not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2453
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2454
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2455
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2456
     * Searches the specified array for the specified object using the binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2457
     * search algorithm.  The array must be sorted into ascending order
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2458
     * according to the specified comparator (as by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2459
     * {@link #sort(Object[], Comparator) sort(T[], Comparator)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2460
     * method) prior to making this call.  If it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2461
     * not sorted, the results are undefined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2462
     * If the array contains multiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2463
     * elements equal to the specified object, there is no guarantee which one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2464
     * will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2465
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  2466
     * @param <T> the class of the objects in the array
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2467
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2468
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2469
     * @param c the comparator by which the array is ordered.  A
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2470
     *        {@code null} value indicates that the elements'
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2471
     *        {@linkplain Comparable natural ordering} should be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2472
     * @return index of the search key, if it is contained in the array;
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2473
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2474
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2475
     *         key would be inserted into the array: the index of the first
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2476
     *         element greater than the key, or {@code a.length} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2477
     *         elements in the array are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2478
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2479
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2480
     * @throws ClassCastException if the array contains elements that are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2481
     *         <i>mutually comparable</i> using the specified comparator,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2482
     *         or the search key is not comparable to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2483
     *         elements of the array using this comparator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2484
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2485
    public static <T> int binarySearch(T[] a, T key, Comparator<? super T> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2486
        return binarySearch0(a, 0, a.length, key, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2487
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2488
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2489
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2490
     * Searches a range of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2491
     * the specified array for the specified object using the binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2492
     * search algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2493
     * The range must be sorted into ascending order
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2494
     * according to the specified comparator (as by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2495
     * {@link #sort(Object[], int, int, Comparator)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2496
     * sort(T[], int, int, Comparator)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2497
     * method) prior to making this call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2498
     * If it is not sorted, the results are undefined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2499
     * If the range contains multiple elements equal to the specified object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2500
     * there is no guarantee which one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2501
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  2502
     * @param <T> the class of the objects in the array
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2503
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2504
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2505
     *          searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2506
     * @param toIndex the index of the last element (exclusive) to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2507
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2508
     * @param c the comparator by which the array is ordered.  A
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2509
     *        {@code null} value indicates that the elements'
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2510
     *        {@linkplain Comparable natural ordering} should be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2511
     * @return index of the search key, if it is contained in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2512
     *         within the specified range;
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2513
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2514
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2515
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2516
     *         element in the range greater than the key,
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2517
     *         or {@code toIndex} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2518
     *         elements in the range are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2519
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2520
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2521
     * @throws ClassCastException if the range contains elements that are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2522
     *         <i>mutually comparable</i> using the specified comparator,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2523
     *         or the search key is not comparable to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2524
     *         elements in the range using this comparator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2525
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2526
     *         if {@code fromIndex > toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2527
     * @throws ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2528
     *         if {@code fromIndex < 0 or toIndex > a.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2529
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2530
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2531
    public static <T> int binarySearch(T[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2532
                                       T key, Comparator<? super T> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2533
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2534
        return binarySearch0(a, fromIndex, toIndex, key, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2535
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2536
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2537
    // Like public version, but without range checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2538
    private static <T> int binarySearch0(T[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2539
                                         T key, Comparator<? super T> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2540
        if (c == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2541
            return binarySearch0(a, fromIndex, toIndex, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2542
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2543
        int low = fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2544
        int high = toIndex - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2545
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2546
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2547
            int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2548
            T midVal = a[mid];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2549
            int cmp = c.compare(midVal, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2550
            if (cmp < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2551
                low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2552
            else if (cmp > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2553
                high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2554
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2555
                return mid; // key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2556
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2557
        return -(low + 1);  // key not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2558
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2559
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2560
    // Equality Testing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2561
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2562
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2563
     * Returns {@code true} if the two specified arrays of longs are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2564
     * <i>equal</i> to one another.  Two arrays are considered equal if both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2565
     * arrays contain the same number of elements, and all corresponding pairs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2566
     * of elements in the two arrays are equal.  In other words, two arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2567
     * are equal if they contain the same elements in the same order.  Also,
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2568
     * two array references are considered equal if both are {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2569
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2570
     * @param a one array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2571
     * @param a2 the other array to be tested for equality
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2572
     * @return {@code true} if the two arrays are equal
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2573
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2574
    public static boolean equals(long[] a, long[] a2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2575
        if (a==a2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2576
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2577
        if (a==null || a2==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2578
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2579
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2580
        int length = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2581
        if (a2.length != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2582
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2583
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2584
        return ArraysSupport.mismatch(a, a2, length) < 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2585
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2586
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2587
    /**
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2588
     * Returns true if the two specified arrays of longs, over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2589
     * ranges, are <i>equal</i> to one another.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2590
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2591
     * <p>Two arrays are considered equal if the number of elements covered by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2592
     * each range is the same, and all corresponding pairs of elements over the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2593
     * specified ranges in the two arrays are equal.  In other words, two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2594
     * are equal if they contain, over the specified ranges, the same elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2595
     * in the same order.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2596
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2597
     * @param a the first array to be tested for equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2598
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2599
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2600
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2601
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2602
     * @param b the second array to be tested fro equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2603
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2604
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2605
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2606
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2607
     * @return {@code true} if the two arrays, over the specified ranges, are
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2608
     *         equal
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2609
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2610
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2611
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2612
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2613
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2614
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2615
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2616
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2617
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2618
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2619
    public static boolean equals(long[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2620
                                 long[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2621
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2622
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2623
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2624
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2625
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2626
        if (aLength != bLength)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2627
            return false;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2628
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2629
        return ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2630
                                      b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2631
                                      aLength) < 0;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2632
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2633
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2634
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2635
     * Returns {@code true} if the two specified arrays of ints are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2636
     * <i>equal</i> to one another.  Two arrays are considered equal if both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2637
     * arrays contain the same number of elements, and all corresponding pairs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2638
     * of elements in the two arrays are equal.  In other words, two arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2639
     * are equal if they contain the same elements in the same order.  Also,
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2640
     * two array references are considered equal if both are {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2641
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2642
     * @param a one array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2643
     * @param a2 the other array to be tested for equality
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2644
     * @return {@code true} if the two arrays are equal
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2645
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2646
    public static boolean equals(int[] a, int[] a2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2647
        if (a==a2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2648
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2649
        if (a==null || a2==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2650
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2651
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2652
        int length = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2653
        if (a2.length != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2654
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2655
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2656
        return ArraysSupport.mismatch(a, a2, length) < 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2657
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2658
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2659
    /**
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2660
     * Returns true if the two specified arrays of ints, over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2661
     * ranges, are <i>equal</i> to one another.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2662
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2663
     * <p>Two arrays are considered equal if the number of elements covered by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2664
     * each range is the same, and all corresponding pairs of elements over the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2665
     * specified ranges in the two arrays are equal.  In other words, two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2666
     * are equal if they contain, over the specified ranges, the same elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2667
     * in the same order.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2668
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2669
     * @param a the first array to be tested for equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2670
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2671
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2672
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2673
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2674
     * @param b the second array to be tested fro equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2675
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2676
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2677
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2678
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2679
     * @return {@code true} if the two arrays, over the specified ranges, are
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2680
     *         equal
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2681
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2682
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2683
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2684
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2685
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2686
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2687
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2688
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2689
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2690
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2691
    public static boolean equals(int[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2692
                                 int[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2693
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2694
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2695
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2696
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2697
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2698
        if (aLength != bLength)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2699
            return false;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2700
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2701
        return ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2702
                                      b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2703
                                      aLength) < 0;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2704
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2705
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2706
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2707
     * Returns {@code true} if the two specified arrays of shorts are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2708
     * <i>equal</i> to one another.  Two arrays are considered equal if both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2709
     * arrays contain the same number of elements, and all corresponding pairs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2710
     * of elements in the two arrays are equal.  In other words, two arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2711
     * are equal if they contain the same elements in the same order.  Also,
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2712
     * two array references are considered equal if both are {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2713
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2714
     * @param a one array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2715
     * @param a2 the other array to be tested for equality
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2716
     * @return {@code true} if the two arrays are equal
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2717
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2718
    public static boolean equals(short[] a, short a2[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2719
        if (a==a2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2720
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2721
        if (a==null || a2==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2722
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2723
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2724
        int length = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2725
        if (a2.length != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2726
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2727
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2728
        return ArraysSupport.mismatch(a, a2, length) < 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2729
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2730
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2731
    /**
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2732
     * Returns true if the two specified arrays of shorts, over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2733
     * ranges, are <i>equal</i> to one another.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2734
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2735
     * <p>Two arrays are considered equal if the number of elements covered by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2736
     * each range is the same, and all corresponding pairs of elements over the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2737
     * specified ranges in the two arrays are equal.  In other words, two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2738
     * are equal if they contain, over the specified ranges, the same elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2739
     * in the same order.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2740
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2741
     * @param a the first array to be tested for equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2742
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2743
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2744
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2745
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2746
     * @param b the second array to be tested fro equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2747
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2748
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2749
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2750
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2751
     * @return {@code true} if the two arrays, over the specified ranges, are
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2752
     *         equal
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2753
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2754
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2755
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2756
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2757
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2758
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2759
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2760
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2761
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2762
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2763
    public static boolean equals(short[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2764
                                 short[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2765
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2766
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2767
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2768
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2769
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2770
        if (aLength != bLength)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2771
            return false;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2772
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2773
        return ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2774
                                      b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2775
                                      aLength) < 0;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2776
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2777
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2778
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2779
     * Returns {@code true} if the two specified arrays of chars are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2780
     * <i>equal</i> to one another.  Two arrays are considered equal if both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2781
     * arrays contain the same number of elements, and all corresponding pairs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2782
     * of elements in the two arrays are equal.  In other words, two arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2783
     * are equal if they contain the same elements in the same order.  Also,
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2784
     * two array references are considered equal if both are {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2785
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2786
     * @param a one array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2787
     * @param a2 the other array to be tested for equality
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2788
     * @return {@code true} if the two arrays are equal
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2789
     */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 29223
diff changeset
  2790
    @HotSpotIntrinsicCandidate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2791
    public static boolean equals(char[] a, char[] a2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2792
        if (a==a2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2793
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2794
        if (a==null || a2==null)
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
        int length = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2798
        if (a2.length != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2799
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2800
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2801
        return ArraysSupport.mismatch(a, a2, length) < 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2802
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2803
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2804
    /**
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2805
     * Returns true if the two specified arrays of chars, over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2806
     * ranges, are <i>equal</i> to one another.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2807
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2808
     * <p>Two arrays are considered equal if the number of elements covered by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2809
     * each range is the same, and all corresponding pairs of elements over the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2810
     * specified ranges in the two arrays are equal.  In other words, two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2811
     * are equal if they contain, over the specified ranges, the same elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2812
     * in the same order.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2813
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2814
     * @param a the first array to be tested for equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2815
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2816
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2817
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2818
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2819
     * @param b the second array to be tested fro equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2820
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2821
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2822
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2823
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2824
     * @return {@code true} if the two arrays, over the specified ranges, are
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2825
     *         equal
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2826
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2827
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2828
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2829
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2830
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2831
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2832
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2833
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2834
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2835
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2836
    public static boolean equals(char[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2837
                                 char[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2838
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2839
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2840
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2841
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2842
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2843
        if (aLength != bLength)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2844
            return false;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2845
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2846
        return ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2847
                                      b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2848
                                      aLength) < 0;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2849
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2850
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2851
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2852
     * Returns {@code true} if the two specified arrays of bytes are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2853
     * <i>equal</i> to one another.  Two arrays are considered equal if both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2854
     * arrays contain the same number of elements, and all corresponding pairs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2855
     * of elements in the two arrays are equal.  In other words, two arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2856
     * are equal if they contain the same elements in the same order.  Also,
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2857
     * two array references are considered equal if both are {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2858
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2859
     * @param a one array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2860
     * @param a2 the other array to be tested for equality
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2861
     * @return {@code true} if the two arrays are equal
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2862
     */
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32108
diff changeset
  2863
    @HotSpotIntrinsicCandidate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2864
    public static boolean equals(byte[] a, byte[] a2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2865
        if (a==a2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2866
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2867
        if (a==null || a2==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2868
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2869
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2870
        int length = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2871
        if (a2.length != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2872
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2873
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2874
        return ArraysSupport.mismatch(a, a2, length) < 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2875
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2876
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2877
    /**
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2878
     * Returns true if the two specified arrays of bytes, over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2879
     * ranges, are <i>equal</i> to one another.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2880
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2881
     * <p>Two arrays are considered equal if the number of elements covered by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2882
     * each range is the same, and all corresponding pairs of elements over the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2883
     * specified ranges in the two arrays are equal.  In other words, two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2884
     * are equal if they contain, over the specified ranges, the same elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2885
     * in the same order.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2886
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2887
     * @param a the first array to be tested for equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2888
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2889
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2890
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2891
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2892
     * @param b the second array to be tested fro equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2893
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2894
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2895
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2896
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2897
     * @return {@code true} if the two arrays, over the specified ranges, are
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2898
     *         equal
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2899
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2900
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2901
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2902
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2903
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2904
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2905
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2906
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2907
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2908
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2909
    public static boolean equals(byte[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2910
                                 byte[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2911
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2912
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2913
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2914
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2915
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2916
        if (aLength != bLength)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2917
            return false;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2918
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2919
        return ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2920
                                      b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2921
                                      aLength) < 0;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2922
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2923
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2924
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2925
     * Returns {@code true} if the two specified arrays of booleans are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2926
     * <i>equal</i> to one another.  Two arrays are considered equal if both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2927
     * arrays contain the same number of elements, and all corresponding pairs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2928
     * of elements in the two arrays are equal.  In other words, two arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2929
     * are equal if they contain the same elements in the same order.  Also,
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2930
     * two array references are considered equal if both are {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2931
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2932
     * @param a one array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2933
     * @param a2 the other array to be tested for equality
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2934
     * @return {@code true} if the two arrays are equal
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2935
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2936
    public static boolean equals(boolean[] a, boolean[] a2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2937
        if (a==a2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2938
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2939
        if (a==null || a2==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2940
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2941
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2942
        int length = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2943
        if (a2.length != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2944
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2945
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2946
        return ArraysSupport.mismatch(a, a2, length) < 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2947
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2948
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2949
    /**
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2950
     * Returns true if the two specified arrays of booleans, over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2951
     * ranges, are <i>equal</i> to one another.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2952
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2953
     * <p>Two arrays are considered equal if the number of elements covered by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2954
     * each range is the same, and all corresponding pairs of elements over the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2955
     * specified ranges in the two arrays are equal.  In other words, two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2956
     * are equal if they contain, over the specified ranges, the same elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2957
     * in the same order.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2958
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2959
     * @param a the first array to be tested for equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2960
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2961
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2962
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2963
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2964
     * @param b the second array to be tested fro equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2965
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2966
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2967
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2968
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2969
     * @return {@code true} if the two arrays, over the specified ranges, are
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2970
     *         equal
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2971
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2972
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2973
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2974
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2975
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2976
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2977
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2978
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2979
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2980
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2981
    public static boolean equals(boolean[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2982
                                 boolean[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2983
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2984
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2985
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2986
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2987
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2988
        if (aLength != bLength)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2989
            return false;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2990
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2991
        return ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2992
                                      b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2993
                                      aLength) < 0;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2994
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2995
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2996
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2997
     * Returns {@code true} if the two specified arrays of doubles are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2998
     * <i>equal</i> to one another.  Two arrays are considered equal if both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2999
     * arrays contain the same number of elements, and all corresponding pairs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3000
     * of elements in the two arrays are equal.  In other words, two arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3001
     * are equal if they contain the same elements in the same order.  Also,
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3002
     * two array references are considered equal if both are {@code null}.
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3003
     *
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3004
     * Two doubles {@code d1} and {@code d2} are considered equal if:
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3005
     * <pre>    {@code new Double(d1).equals(new Double(d2))}</pre>
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3006
     * (Unlike the {@code ==} operator, this method considers
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3007
     * {@code NaN} equals to itself, and 0.0d unequal to -0.0d.)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3008
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3009
     * @param a one array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3010
     * @param a2 the other array to be tested for equality
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3011
     * @return {@code true} if the two arrays are equal
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3012
     * @see Double#equals(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3013
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3014
    public static boolean equals(double[] a, double[] a2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3015
        if (a==a2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3016
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3017
        if (a==null || a2==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3018
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3019
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3020
        int length = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3021
        if (a2.length != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3022
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3023
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  3024
        return ArraysSupport.mismatch(a, a2, length) < 0;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3025
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3026
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3027
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3028
     * Returns true if the two specified arrays of doubles, over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3029
     * ranges, are <i>equal</i> to one another.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3030
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3031
     * <p>Two arrays are considered equal if the number of elements covered by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3032
     * each range is the same, and all corresponding pairs of elements over the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3033
     * specified ranges in the two arrays are equal.  In other words, two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3034
     * are equal if they contain, over the specified ranges, the same elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3035
     * in the same order.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3036
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3037
     * <p>Two doubles {@code d1} and {@code d2} are considered equal if:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3038
     * <pre>    {@code new Double(d1).equals(new Double(d2))}</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3039
     * (Unlike the {@code ==} operator, this method considers
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3040
     * {@code NaN} equals to itself, and 0.0d unequal to -0.0d.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3041
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3042
     * @param a the first array to be tested for equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3043
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3044
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3045
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3046
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3047
     * @param b the second array to be tested fro equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3048
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3049
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3050
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3051
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3052
     * @return {@code true} if the two arrays, over the specified ranges, are
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3053
     *         equal
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3054
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3055
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3056
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3057
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3058
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3059
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3060
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3061
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3062
     * @see Double#equals(Object)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3063
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3064
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3065
    public static boolean equals(double[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3066
                                 double[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3067
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3068
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3069
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3070
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3071
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3072
        if (aLength != bLength)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3073
            return false;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3074
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  3075
        return ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  3076
                                      b, bFromIndex, aLength) < 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3077
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3078
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3079
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3080
     * Returns {@code true} if the two specified arrays of floats are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3081
     * <i>equal</i> to one another.  Two arrays are considered equal if both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3082
     * arrays contain the same number of elements, and all corresponding pairs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3083
     * of elements in the two arrays are equal.  In other words, two arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3084
     * are equal if they contain the same elements in the same order.  Also,
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3085
     * two array references are considered equal if both are {@code null}.
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3086
     *
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3087
     * Two floats {@code f1} and {@code f2} are considered equal if:
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3088
     * <pre>    {@code new Float(f1).equals(new Float(f2))}</pre>
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3089
     * (Unlike the {@code ==} operator, this method considers
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3090
     * {@code NaN} equals to itself, and 0.0f unequal to -0.0f.)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3091
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3092
     * @param a one array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3093
     * @param a2 the other array to be tested for equality
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3094
     * @return {@code true} if the two arrays are equal
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3095
     * @see Float#equals(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3096
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3097
    public static boolean equals(float[] a, float[] a2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3098
        if (a==a2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3099
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3100
        if (a==null || a2==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3101
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3102
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3103
        int length = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3104
        if (a2.length != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3105
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3106
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  3107
        return ArraysSupport.mismatch(a, a2, length) < 0;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3108
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3109
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3110
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3111
     * Returns true if the two specified arrays of floats, over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3112
     * ranges, are <i>equal</i> to one another.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3113
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3114
     * <p>Two arrays are considered equal if the number of elements covered by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3115
     * each range is the same, and all corresponding pairs of elements over the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3116
     * specified ranges in the two arrays are equal.  In other words, two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3117
     * are equal if they contain, over the specified ranges, the same elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3118
     * in the same order.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3119
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3120
     * <p>Two floats {@code f1} and {@code f2} are considered equal if:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3121
     * <pre>    {@code new Float(f1).equals(new Float(f2))}</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3122
     * (Unlike the {@code ==} operator, this method considers
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3123
     * {@code NaN} equals to itself, and 0.0f unequal to -0.0f.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3124
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3125
     * @param a the first array to be tested for equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3126
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3127
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3128
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3129
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3130
     * @param b the second array to be tested fro equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3131
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3132
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3133
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3134
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3135
     * @return {@code true} if the two arrays, over the specified ranges, are
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3136
     *         equal
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3137
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3138
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3139
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3140
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3141
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3142
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3143
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3144
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3145
     * @see Float#equals(Object)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3146
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3147
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3148
    public static boolean equals(float[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3149
                                 float[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3150
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3151
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3152
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3153
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3154
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3155
        if (aLength != bLength)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3156
            return false;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3157
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  3158
        return ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  3159
                                      b, bFromIndex, aLength) < 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3161
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3162
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3163
     * Returns {@code true} if the two specified arrays of Objects are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3164
     * <i>equal</i> to one another.  The two arrays are considered equal if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3165
     * both arrays contain the same number of elements, and all corresponding
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3166
     * pairs of elements in the two arrays are equal.  Two objects {@code e1}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3167
     * and {@code e2} are considered <i>equal</i> if
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3168
     * {@code Objects.equals(e1, e2)}.
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3169
     * In other words, the two arrays are equal if
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3170
     * they contain the same elements in the same order.  Also, two array
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3171
     * references are considered equal if both are {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3172
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3173
     * @param a one array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3174
     * @param a2 the other array to be tested for equality
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3175
     * @return {@code true} if the two arrays are equal
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3177
    public static boolean equals(Object[] a, Object[] a2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3178
        if (a==a2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3179
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3180
        if (a==null || a2==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3181
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3182
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3183
        int length = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3184
        if (a2.length != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3185
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3186
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3187
        for (int i=0; i<length; i++) {
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3188
            if (!Objects.equals(a[i], a2[i]))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3189
                return false;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3190
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3191
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3192
        return true;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3193
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3194
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3195
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3196
     * Returns true if the two specified arrays of Objects, over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3197
     * ranges, are <i>equal</i> to one another.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3198
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3199
     * <p>Two arrays are considered equal if the number of elements covered by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3200
     * each range is the same, and all corresponding pairs of elements over the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3201
     * specified ranges in the two arrays are equal.  In other words, two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3202
     * are equal if they contain, over the specified ranges, the same elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3203
     * in the same order.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3204
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3205
     * <p>Two objects {@code e1} and {@code e2} are considered <i>equal</i> if
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3206
     * {@code Objects.equals(e1, e2)}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3207
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3208
     * @param a the first array to be tested for equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3209
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3210
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3211
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3212
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3213
     * @param b the second array to be tested fro equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3214
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3215
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3216
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3217
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3218
     * @return {@code true} if the two arrays, over the specified ranges, are
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3219
     *         equal
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3220
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3221
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3222
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3223
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3224
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3225
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3226
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3227
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3228
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3229
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3230
    public static boolean equals(Object[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3231
                                 Object[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3232
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3233
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3234
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3235
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3236
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3237
        if (aLength != bLength)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3238
            return false;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3239
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3240
        for (int i = 0; i < aLength; i++) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3241
            if (!Objects.equals(a[aFromIndex++], b[bFromIndex++]))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3242
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3244
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3245
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3246
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3247
33843
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3248
    /**
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3249
     * Returns {@code true} if the two specified arrays of Objects are
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3250
     * <i>equal</i> to one another.
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3251
     *
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3252
     * <p>Two arrays are considered equal if both arrays contain the same number
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3253
     * of elements, and all corresponding pairs of elements in the two arrays
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3254
     * are equal.  In other words, the two arrays are equal if they contain the
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3255
     * same elements in the same order.  Also, two array references are
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3256
     * considered equal if both are {@code null}.
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3257
     *
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3258
     * <p>Two objects {@code e1} and {@code e2} are considered <i>equal</i> if,
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3259
     * given the specified comparator, {@code cmp.compare(e1, e2) == 0}.
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3260
     *
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3261
     * @param a one array to be tested for equality
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3262
     * @param a2 the other array to be tested for equality
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3263
     * @param cmp the comparator to compare array elements
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3264
     * @param <T> the type of array elements
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3265
     * @return {@code true} if the two arrays are equal
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3266
     * @throws NullPointerException if the comparator is {@code null}
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3267
     * @since 9
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3268
     */
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3269
    public static <T> boolean equals(T[] a, T[] a2, Comparator<? super T> cmp) {
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3270
        Objects.requireNonNull(cmp);
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3271
        if (a==a2)
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3272
            return true;
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3273
        if (a==null || a2==null)
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3274
            return false;
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3275
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3276
        int length = a.length;
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3277
        if (a2.length != length)
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3278
            return false;
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3279
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3280
        for (int i=0; i<length; i++) {
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3281
            if (cmp.compare(a[i], a2[i]) != 0)
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3282
                return false;
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3283
        }
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3284
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3285
        return true;
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3286
    }
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3287
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3288
    /**
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3289
     * Returns true if the two specified arrays of Objects, over the specified
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3290
     * ranges, are <i>equal</i> to one another.
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3291
     *
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3292
     * <p>Two arrays are considered equal if the number of elements covered by
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3293
     * each range is the same, and all corresponding pairs of elements over the
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3294
     * specified ranges in the two arrays are equal.  In other words, two arrays
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3295
     * are equal if they contain, over the specified ranges, the same elements
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3296
     * in the same order.
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3297
     *
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3298
     * <p>Two objects {@code e1} and {@code e2} are considered <i>equal</i> if,
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3299
     * given the specified comparator, {@code cmp.compare(e1, e2) == 0}.
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3300
     *
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3301
     * @param a the first array to be tested for equality
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3302
     * @param aFromIndex the index (inclusive) of the first element in the
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3303
     *                   first array to be tested
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3304
     * @param aToIndex the index (exclusive) of the last element in the
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3305
     *                 first array to be tested
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3306
     * @param b the second array to be tested fro equality
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3307
     * @param bFromIndex the index (inclusive) of the first element in the
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3308
     *                   second array to be tested
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3309
     * @param bToIndex the index (exclusive) of the last element in the
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3310
     *                 second array to be tested
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3311
     * @param cmp the comparator to compare array elements
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3312
     * @param <T> the type of array elements
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3313
     * @return {@code true} if the two arrays, over the specified ranges, are
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3314
     *         equal
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3315
     * @throws IllegalArgumentException
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3316
     *         if {@code aFromIndex > aToIndex} or
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3317
     *         if {@code bFromIndex > bToIndex}
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3318
     * @throws ArrayIndexOutOfBoundsException
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3319
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3320
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3321
     * @throws NullPointerException
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3322
     *         if either array or the comparator is {@code null}
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3323
     * @since 9
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3324
     */
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3325
    public static <T> boolean equals(T[] a, int aFromIndex, int aToIndex,
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3326
                                     T[] b, int bFromIndex, int bToIndex,
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3327
                                     Comparator<? super T> cmp) {
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3328
        Objects.requireNonNull(cmp);
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3329
        rangeCheck(a.length, aFromIndex, aToIndex);
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3330
        rangeCheck(b.length, bFromIndex, bToIndex);
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3331
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3332
        int aLength = aToIndex - aFromIndex;
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3333
        int bLength = bToIndex - bFromIndex;
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3334
        if (aLength != bLength)
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3335
            return false;
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3336
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3337
        for (int i = 0; i < aLength; i++) {
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3338
            if (cmp.compare(a[aFromIndex++], b[bFromIndex++]) != 0)
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3339
                return false;
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3340
        }
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3341
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3342
        return true;
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3343
    }
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3344
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3345
    // Filling
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3346
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3347
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3348
     * Assigns the specified long value to each element of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3349
     * of longs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3350
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3351
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3352
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3353
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3354
    public static void fill(long[] a, long val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3355
        for (int i = 0, len = a.length; i < len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3356
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3357
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3358
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3359
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3360
     * Assigns the specified long value to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3361
     * range of the specified array of longs.  The range to be filled
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3362
     * extends from index {@code fromIndex}, inclusive, to index
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3363
     * {@code toIndex}, exclusive.  (If {@code fromIndex==toIndex}, the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3364
     * range to be filled is empty.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3365
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3366
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3367
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3368
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3369
     * @param toIndex the index of the last element (exclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3370
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3371
     * @param val the value to be stored in all elements of the array
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3372
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3373
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3374
     *         {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3375
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3376
    public static void fill(long[] a, int fromIndex, int toIndex, long val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3377
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3378
        for (int i = fromIndex; i < toIndex; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3379
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3380
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3381
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3382
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3383
     * Assigns the specified int value to each element of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3384
     * of ints.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3385
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3386
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3387
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3388
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3389
    public static void fill(int[] a, int val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3390
        for (int i = 0, len = a.length; i < len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3391
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3392
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3393
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3394
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3395
     * Assigns the specified int value to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3396
     * range of the specified array of ints.  The range to be filled
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3397
     * extends from index {@code fromIndex}, inclusive, to index
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3398
     * {@code toIndex}, exclusive.  (If {@code fromIndex==toIndex}, the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3399
     * range to be filled is empty.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3400
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3401
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3402
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3403
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3404
     * @param toIndex the index of the last element (exclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3405
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3406
     * @param val the value to be stored in all elements of the array
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3407
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3408
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3409
     *         {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3410
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3411
    public static void fill(int[] a, int fromIndex, int toIndex, int val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3412
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3413
        for (int i = fromIndex; i < toIndex; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3414
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3415
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3416
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3417
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3418
     * Assigns the specified short value to each element of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3419
     * of shorts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3420
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3421
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3422
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3423
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3424
    public static void fill(short[] a, short val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3425
        for (int i = 0, len = a.length; i < len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3426
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3427
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3428
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3429
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3430
     * Assigns the specified short value to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3431
     * range of the specified array of shorts.  The range to be filled
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3432
     * extends from index {@code fromIndex}, inclusive, to index
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3433
     * {@code toIndex}, exclusive.  (If {@code fromIndex==toIndex}, the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3434
     * range to be filled is empty.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3435
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3436
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3437
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3438
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3439
     * @param toIndex the index of the last element (exclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3440
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3441
     * @param val the value to be stored in all elements of the array
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3442
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3443
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3444
     *         {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3445
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3446
    public static void fill(short[] a, int fromIndex, int toIndex, short val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3447
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3448
        for (int i = fromIndex; i < toIndex; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3449
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3450
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3451
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3452
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3453
     * Assigns the specified char value to each element of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3454
     * of chars.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3455
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3456
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3457
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3458
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3459
    public static void fill(char[] a, char val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3460
        for (int i = 0, len = a.length; i < len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3461
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3462
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3463
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3464
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3465
     * Assigns the specified char value to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3466
     * range of the specified array of chars.  The range to be filled
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3467
     * extends from index {@code fromIndex}, inclusive, to index
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3468
     * {@code toIndex}, exclusive.  (If {@code fromIndex==toIndex}, the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3469
     * range to be filled is empty.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3470
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3471
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3472
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3473
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3474
     * @param toIndex the index of the last element (exclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3475
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3476
     * @param val the value to be stored in all elements of the array
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3477
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3478
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3479
     *         {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3480
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3481
    public static void fill(char[] a, int fromIndex, int toIndex, char val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3482
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3483
        for (int i = fromIndex; i < toIndex; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3484
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3485
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3486
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3487
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3488
     * Assigns the specified byte value to each element of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3489
     * of bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3490
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3491
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3492
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3493
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3494
    public static void fill(byte[] a, byte val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3495
        for (int i = 0, len = a.length; i < len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3496
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3497
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3498
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3499
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3500
     * Assigns the specified byte value to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3501
     * range of the specified array of bytes.  The range to be filled
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3502
     * extends from index {@code fromIndex}, inclusive, to index
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3503
     * {@code toIndex}, exclusive.  (If {@code fromIndex==toIndex}, the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3504
     * range to be filled is empty.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3505
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3506
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3507
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3508
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3509
     * @param toIndex the index of the last element (exclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3510
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3511
     * @param val the value to be stored in all elements of the array
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3512
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3513
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3514
     *         {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3515
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3516
    public static void fill(byte[] a, int fromIndex, int toIndex, byte val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3517
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3518
        for (int i = fromIndex; i < toIndex; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3519
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3520
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3521
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3522
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3523
     * Assigns the specified boolean value to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3524
     * array of booleans.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3525
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3526
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3527
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3528
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3529
    public static void fill(boolean[] a, boolean val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3530
        for (int i = 0, len = a.length; i < len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3531
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3532
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3533
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3534
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3535
     * Assigns the specified boolean value to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3536
     * range of the specified array of booleans.  The range to be filled
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3537
     * extends from index {@code fromIndex}, inclusive, to index
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3538
     * {@code toIndex}, exclusive.  (If {@code fromIndex==toIndex}, the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3539
     * range to be filled is empty.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3540
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3541
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3542
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3543
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3544
     * @param toIndex the index of the last element (exclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3545
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3546
     * @param val the value to be stored in all elements of the array
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3547
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3548
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3549
     *         {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3550
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3551
    public static void fill(boolean[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3552
                            boolean val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3553
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3554
        for (int i = fromIndex; i < toIndex; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3555
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3556
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3557
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3558
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3559
     * Assigns the specified double value to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3560
     * array of doubles.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3561
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3562
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3563
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3564
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3565
    public static void fill(double[] a, double val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3566
        for (int i = 0, len = a.length; i < len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3567
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3568
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3569
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3570
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3571
     * Assigns the specified double value to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3572
     * range of the specified array of doubles.  The range to be filled
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3573
     * extends from index {@code fromIndex}, inclusive, to index
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3574
     * {@code toIndex}, exclusive.  (If {@code fromIndex==toIndex}, the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3575
     * range to be filled is empty.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3576
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3577
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3578
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3579
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3580
     * @param toIndex the index of the last element (exclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3581
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3582
     * @param val the value to be stored in all elements of the array
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3583
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3584
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3585
     *         {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3586
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3587
    public static void fill(double[] a, int fromIndex, int toIndex,double val){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3588
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3589
        for (int i = fromIndex; i < toIndex; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3590
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3591
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3592
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3593
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3594
     * Assigns the specified float value to each element of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3595
     * of floats.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3596
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3597
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3598
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3599
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3600
    public static void fill(float[] a, float val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3601
        for (int i = 0, len = a.length; i < len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3602
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3603
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3604
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3605
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3606
     * Assigns the specified float value to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3607
     * range of the specified array of floats.  The range to be filled
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3608
     * extends from index {@code fromIndex}, inclusive, to index
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3609
     * {@code toIndex}, exclusive.  (If {@code fromIndex==toIndex}, the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3610
     * range to be filled is empty.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3611
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3612
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3613
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3614
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3615
     * @param toIndex the index of the last element (exclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3616
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3617
     * @param val the value to be stored in all elements of the array
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3618
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3619
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3620
     *         {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3621
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3622
    public static void fill(float[] a, int fromIndex, int toIndex, float val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3623
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3624
        for (int i = fromIndex; i < toIndex; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3625
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3626
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3627
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3628
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3629
     * Assigns the specified Object reference to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3630
     * array of Objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3631
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3632
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3633
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3634
     * @throws ArrayStoreException if the specified value is not of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3635
     *         runtime type that can be stored in the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3636
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3637
    public static void fill(Object[] a, Object val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3638
        for (int i = 0, len = a.length; i < len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3639
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3640
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3641
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3642
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3643
     * Assigns the specified Object reference to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3644
     * range of the specified array of Objects.  The range to be filled
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3645
     * extends from index {@code fromIndex}, inclusive, to index
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3646
     * {@code toIndex}, exclusive.  (If {@code fromIndex==toIndex}, the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3647
     * range to be filled is empty.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3648
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3649
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3650
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3651
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3652
     * @param toIndex the index of the last element (exclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3653
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3654
     * @param val the value to be stored in all elements of the array
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3655
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3656
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3657
     *         {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3658
     * @throws ArrayStoreException if the specified value is not of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3659
     *         runtime type that can be stored in the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3660
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3661
    public static void fill(Object[] a, int fromIndex, int toIndex, Object val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3662
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3663
        for (int i = fromIndex; i < toIndex; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3664
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3665
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3666
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
  3667
    // Cloning
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3668
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3669
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3670
     * Copies the specified array, truncating or padding with nulls (if necessary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3671
     * so the copy has the specified length.  For all indices that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3672
     * valid in both the original array and the copy, the two arrays will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3673
     * contain identical values.  For any indices that are valid in the
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3674
     * copy but not the original, the copy will contain {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3675
     * Such indices will exist if and only if the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3676
     * is greater than that of the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3677
     * The resulting array is of exactly the same class as the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3678
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  3679
     * @param <T> the class of the objects in the array
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3680
     * @param original the array to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3681
     * @param newLength the length of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3682
     * @return a copy of the original array, truncated or padded with nulls
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3683
     *     to obtain the specified length
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3684
     * @throws NegativeArraySizeException if {@code newLength} is negative
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3685
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3686
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3687
     */
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  3688
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3689
    public static <T> T[] copyOf(T[] original, int newLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3690
        return (T[]) copyOf(original, newLength, original.getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3691
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3692
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3693
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3694
     * Copies the specified array, truncating or padding with nulls (if necessary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3695
     * so the copy has the specified length.  For all indices that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3696
     * valid in both the original array and the copy, the two arrays will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3697
     * contain identical values.  For any indices that are valid in the
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3698
     * copy but not the original, the copy will contain {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3699
     * Such indices will exist if and only if the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3700
     * is greater than that of the original array.
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3701
     * The resulting array is of the class {@code newType}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3702
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  3703
     * @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
  3704
     * @param <T> the class of the objects in the returned array
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3705
     * @param original the array to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3706
     * @param newLength the length of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3707
     * @param newType the class of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3708
     * @return a copy of the original array, truncated or padded with nulls
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3709
     *     to obtain the specified length
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3710
     * @throws NegativeArraySizeException if {@code newLength} is negative
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3711
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3712
     * @throws ArrayStoreException if an element copied from
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3713
     *     {@code original} is not of a runtime type that can be stored in
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3714
     *     an array of class {@code newType}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3715
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3716
     */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 29223
diff changeset
  3717
    @HotSpotIntrinsicCandidate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3718
    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
  3719
        @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3720
        T[] copy = ((Object)newType == (Object)Object[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3721
            ? (T[]) new Object[newLength]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3722
            : (T[]) Array.newInstance(newType.getComponentType(), newLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3723
        System.arraycopy(original, 0, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3724
                         Math.min(original.length, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3725
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3726
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3727
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3728
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3729
     * Copies the specified array, truncating or padding with zeros (if necessary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3730
     * so the copy has the specified length.  For all indices that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3731
     * valid in both the original array and the copy, the two arrays will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3732
     * contain identical values.  For any indices that are valid in the
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3733
     * copy but not the original, the copy will contain {@code (byte)0}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3734
     * Such indices will exist if and only if the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3735
     * is greater than that of the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3736
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3737
     * @param original the array to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3738
     * @param newLength the length of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3739
     * @return a copy of the original array, truncated or padded with zeros
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3740
     *     to obtain the specified length
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3741
     * @throws NegativeArraySizeException if {@code newLength} is negative
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3742
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3743
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3744
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3745
    public static byte[] copyOf(byte[] original, int newLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3746
        byte[] copy = new byte[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3747
        System.arraycopy(original, 0, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3748
                         Math.min(original.length, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3749
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3750
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3751
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3752
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3753
     * Copies the specified array, truncating or padding with zeros (if necessary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3754
     * so the copy has the specified length.  For all indices that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3755
     * valid in both the original array and the copy, the two arrays will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3756
     * contain identical values.  For any indices that are valid in the
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3757
     * copy but not the original, the copy will contain {@code (short)0}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3758
     * Such indices will exist if and only if the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3759
     * is greater than that of the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3760
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3761
     * @param original the array to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3762
     * @param newLength the length of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3763
     * @return a copy of the original array, truncated or padded with zeros
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3764
     *     to obtain the specified length
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3765
     * @throws NegativeArraySizeException if {@code newLength} is negative
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3766
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3767
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3768
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3769
    public static short[] copyOf(short[] original, int newLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3770
        short[] copy = new short[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3771
        System.arraycopy(original, 0, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3772
                         Math.min(original.length, 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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3777
     * Copies the specified array, truncating or padding with zeros (if necessary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3778
     * so the copy has the specified length.  For all indices that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3779
     * valid in both the original array and the copy, the two arrays will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3780
     * contain identical values.  For any indices that are valid in the
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3781
     * copy but not the original, the copy will contain {@code 0}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3782
     * Such indices will exist if and only if the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3783
     * is greater than that of the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3784
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3785
     * @param original the array to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3786
     * @param newLength the length of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3787
     * @return a copy of the original array, truncated or padded with zeros
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3788
     *     to obtain the specified length
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3789
     * @throws NegativeArraySizeException if {@code newLength} is negative
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3790
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3791
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3792
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3793
    public static int[] copyOf(int[] original, int newLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3794
        int[] copy = new int[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3795
        System.arraycopy(original, 0, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3796
                         Math.min(original.length, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3797
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3798
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3799
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3800
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3801
     * Copies the specified array, truncating or padding with zeros (if necessary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3802
     * so the copy has the specified length.  For all indices that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3803
     * valid in both the original array and the copy, the two arrays will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3804
     * contain identical values.  For any indices that are valid in the
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3805
     * copy but not the original, the copy will contain {@code 0L}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3806
     * Such indices will exist if and only if the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3807
     * is greater than that of the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3808
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3809
     * @param original the array to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3810
     * @param newLength the length of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3811
     * @return a copy of the original array, truncated or padded with zeros
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3812
     *     to obtain the specified length
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3813
     * @throws NegativeArraySizeException if {@code newLength} is negative
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3814
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3815
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3816
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3817
    public static long[] copyOf(long[] original, int newLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3818
        long[] copy = new long[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3819
        System.arraycopy(original, 0, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3820
                         Math.min(original.length, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3821
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3822
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3823
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3824
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3825
     * Copies the specified array, truncating or padding with null characters (if necessary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3826
     * so the copy has the specified length.  For all indices that are valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3827
     * in both the original array and the copy, the two arrays will contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3828
     * identical values.  For any indices that are valid in the copy but not
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3829
     * the original, the copy will contain {@code '\\u000'}.  Such indices
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3830
     * will exist if and only if the specified length is greater than that of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3831
     * the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3832
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3833
     * @param original the array to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3834
     * @param newLength the length of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3835
     * @return a copy of the original array, truncated or padded with null characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3836
     *     to obtain the specified length
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3837
     * @throws NegativeArraySizeException if {@code newLength} is negative
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3838
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3839
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3840
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3841
    public static char[] copyOf(char[] original, int newLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3842
        char[] copy = new char[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3843
        System.arraycopy(original, 0, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3844
                         Math.min(original.length, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3845
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3846
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3847
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3848
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3849
     * Copies the specified array, truncating or padding with zeros (if necessary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3850
     * so the copy has the specified length.  For all indices that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3851
     * valid in both the original array and the copy, the two arrays will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3852
     * contain identical values.  For any indices that are valid in the
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3853
     * copy but not the original, the copy will contain {@code 0f}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3854
     * Such indices will exist if and only if the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3855
     * is greater than that of the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3856
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3857
     * @param original the array to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3858
     * @param newLength the length of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3859
     * @return a copy of the original array, truncated or padded with zeros
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3860
     *     to obtain the specified length
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3861
     * @throws NegativeArraySizeException if {@code newLength} is negative
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3862
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3863
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3864
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3865
    public static float[] copyOf(float[] original, int newLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3866
        float[] copy = new float[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3867
        System.arraycopy(original, 0, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3868
                         Math.min(original.length, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3869
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3870
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3871
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3872
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3873
     * Copies the specified array, truncating or padding with zeros (if necessary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3874
     * so the copy has the specified length.  For all indices that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3875
     * valid in both the original array and the copy, the two arrays will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3876
     * contain identical values.  For any indices that are valid in the
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3877
     * copy but not the original, the copy will contain {@code 0d}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3878
     * Such indices will exist if and only if the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3879
     * is greater than that of the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3880
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3881
     * @param original the array to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3882
     * @param newLength the length of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3883
     * @return a copy of the original array, truncated or padded with zeros
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3884
     *     to obtain the specified length
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3885
     * @throws NegativeArraySizeException if {@code newLength} is negative
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3886
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3887
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3888
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3889
    public static double[] copyOf(double[] original, int newLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3890
        double[] copy = new double[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3891
        System.arraycopy(original, 0, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3892
                         Math.min(original.length, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3893
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3894
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3895
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3896
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3897
     * Copies the specified array, truncating or padding with {@code false} (if necessary)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3898
     * so the copy has the specified length.  For all indices that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3899
     * valid in both the original array and the copy, the two arrays will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3900
     * contain identical values.  For any indices that are valid in the
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3901
     * copy but not the original, the copy will contain {@code false}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3902
     * Such indices will exist if and only if the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3903
     * is greater than that of the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3904
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3905
     * @param original the array to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3906
     * @param newLength the length of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3907
     * @return a copy of the original array, truncated or padded with false elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3908
     *     to obtain the specified length
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3909
     * @throws NegativeArraySizeException if {@code newLength} is negative
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3910
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3911
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3912
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3913
    public static boolean[] copyOf(boolean[] original, int newLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3914
        boolean[] copy = new boolean[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3915
        System.arraycopy(original, 0, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3916
                         Math.min(original.length, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3917
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3918
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3919
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3920
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3921
     * Copies the specified range of the specified array into a new array.
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3922
     * The initial index of the range ({@code from}) must lie between zero
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3923
     * and {@code original.length}, inclusive.  The value at
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3924
     * {@code original[from]} is placed into the initial element of the copy
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3925
     * (unless {@code from == original.length} or {@code from == to}).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3926
     * Values from subsequent elements in the original array are placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3927
     * subsequent elements in the copy.  The final index of the range
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3928
     * ({@code to}), which must be greater than or equal to {@code from},
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3929
     * may be greater than {@code original.length}, in which case
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3930
     * {@code null} is placed in all elements of the copy whose index is
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3931
     * greater than or equal to {@code original.length - from}.  The length
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3932
     * of the returned array will be {@code to - from}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3933
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3934
     * The resulting array is of exactly the same class as the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3935
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  3936
     * @param <T> the class of the objects in the array
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3937
     * @param original the array from which a range is to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3938
     * @param from the initial index of the range to be copied, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3939
     * @param to the final index of the range to be copied, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3940
     *     (This index may lie outside the array.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3941
     * @return a new array containing the specified range from the original array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3942
     *     truncated or padded with nulls to obtain the required length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3943
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3944
     *     or {@code from > original.length}
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3945
     * @throws IllegalArgumentException if {@code from > to}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3946
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3947
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3948
     */
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  3949
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3950
    public static <T> T[] copyOfRange(T[] original, int from, int to) {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  3951
        return copyOfRange(original, from, to, (Class<? extends T[]>) original.getClass());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3952
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3953
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3954
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3955
     * Copies the specified range of the specified array into a new array.
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3956
     * The initial index of the range ({@code from}) must lie between zero
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3957
     * and {@code original.length}, inclusive.  The value at
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3958
     * {@code original[from]} is placed into the initial element of the copy
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3959
     * (unless {@code from == original.length} or {@code from == to}).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3960
     * Values from subsequent elements in the original array are placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3961
     * subsequent elements in the copy.  The final index of the range
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3962
     * ({@code to}), which must be greater than or equal to {@code from},
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3963
     * may be greater than {@code original.length}, in which case
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3964
     * {@code null} is placed in all elements of the copy whose index is
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3965
     * greater than or equal to {@code original.length - from}.  The length
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3966
     * of the returned array will be {@code to - from}.
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3967
     * The resulting array is of the class {@code newType}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3968
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  3969
     * @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
  3970
     * @param <T> the class of the objects in the returned array
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3971
     * @param original the array from which a range is to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3972
     * @param from the initial index of the range to be copied, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3973
     * @param to the final index of the range to be copied, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3974
     *     (This index may lie outside the array.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3975
     * @param newType the class of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3976
     * @return a new array containing the specified range from the original array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3977
     *     truncated or padded with nulls to obtain the required length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3978
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3979
     *     or {@code from > original.length}
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3980
     * @throws IllegalArgumentException if {@code from > to}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3981
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3982
     * @throws ArrayStoreException if an element copied from
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3983
     *     {@code original} is not of a runtime type that can be stored in
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3984
     *     an array of class {@code newType}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3985
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3986
     */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 29223
diff changeset
  3987
    @HotSpotIntrinsicCandidate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3988
    public static <T,U> T[] copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3989
        int newLength = to - from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3990
        if (newLength < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3991
            throw new IllegalArgumentException(from + " > " + to);
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  3992
        @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3993
        T[] copy = ((Object)newType == (Object)Object[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3994
            ? (T[]) new Object[newLength]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3995
            : (T[]) Array.newInstance(newType.getComponentType(), newLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3996
        System.arraycopy(original, from, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3997
                         Math.min(original.length - from, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3998
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3999
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4000
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4001
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4002
     * Copies the specified range of the specified array into a new array.
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4003
     * The initial index of the range ({@code from}) must lie between zero
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4004
     * and {@code original.length}, inclusive.  The value at
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4005
     * {@code original[from]} is placed into the initial element of the copy
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4006
     * (unless {@code from == original.length} or {@code from == to}).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4007
     * Values from subsequent elements in the original array are placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4008
     * subsequent elements in the copy.  The final index of the range
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4009
     * ({@code to}), which must be greater than or equal to {@code from},
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4010
     * may be greater than {@code original.length}, in which case
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4011
     * {@code (byte)0} is placed in all elements of the copy whose index is
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4012
     * greater than or equal to {@code original.length - from}.  The length
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4013
     * of the returned array will be {@code to - from}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4014
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4015
     * @param original the array from which a range is to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4016
     * @param from the initial index of the range to be copied, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4017
     * @param to the final index of the range to be copied, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4018
     *     (This index may lie outside the array.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4019
     * @return a new array containing the specified range from the original array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4020
     *     truncated or padded with zeros to obtain the required length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4021
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4022
     *     or {@code from > original.length}
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4023
     * @throws IllegalArgumentException if {@code from > to}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4024
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4025
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4026
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4027
    public static byte[] copyOfRange(byte[] original, int from, int to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4028
        int newLength = to - from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4029
        if (newLength < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4030
            throw new IllegalArgumentException(from + " > " + to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4031
        byte[] copy = new byte[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4032
        System.arraycopy(original, from, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4033
                         Math.min(original.length - from, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4034
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4035
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4036
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4037
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4038
     * Copies the specified range of the specified array into a new array.
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4039
     * The initial index of the range ({@code from}) must lie between zero
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4040
     * and {@code original.length}, inclusive.  The value at
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4041
     * {@code original[from]} is placed into the initial element of the copy
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4042
     * (unless {@code from == original.length} or {@code from == to}).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4043
     * Values from subsequent elements in the original array are placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4044
     * subsequent elements in the copy.  The final index of the range
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4045
     * ({@code to}), which must be greater than or equal to {@code from},
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4046
     * may be greater than {@code original.length}, in which case
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4047
     * {@code (short)0} is placed in all elements of the copy whose index is
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4048
     * greater than or equal to {@code original.length - from}.  The length
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4049
     * of the returned array will be {@code to - from}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4050
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4051
     * @param original the array from which a range is to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4052
     * @param from the initial index of the range to be copied, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4053
     * @param to the final index of the range to be copied, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4054
     *     (This index may lie outside the array.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4055
     * @return a new array containing the specified range from the original array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4056
     *     truncated or padded with zeros to obtain the required length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4057
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4058
     *     or {@code from > original.length}
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4059
     * @throws IllegalArgumentException if {@code from > to}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4060
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4061
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4062
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4063
    public static short[] copyOfRange(short[] original, int from, int to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4064
        int newLength = to - from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4065
        if (newLength < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4066
            throw new IllegalArgumentException(from + " > " + to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4067
        short[] copy = new short[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4068
        System.arraycopy(original, from, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4069
                         Math.min(original.length - from, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4070
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4071
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4072
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4073
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4074
     * Copies the specified range of the specified array into a new array.
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4075
     * The initial index of the range ({@code from}) must lie between zero
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4076
     * and {@code original.length}, inclusive.  The value at
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4077
     * {@code original[from]} is placed into the initial element of the copy
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4078
     * (unless {@code from == original.length} or {@code from == to}).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4079
     * Values from subsequent elements in the original array are placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4080
     * subsequent elements in the copy.  The final index of the range
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4081
     * ({@code to}), which must be greater than or equal to {@code from},
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4082
     * may be greater than {@code original.length}, in which case
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4083
     * {@code 0} is placed in all elements of the copy whose index is
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4084
     * greater than or equal to {@code original.length - from}.  The length
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4085
     * of the returned array will be {@code to - from}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4086
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4087
     * @param original the array from which a range is to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4088
     * @param from the initial index of the range to be copied, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4089
     * @param to the final index of the range to be copied, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4090
     *     (This index may lie outside the array.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4091
     * @return a new array containing the specified range from the original array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4092
     *     truncated or padded with zeros to obtain the required length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4093
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4094
     *     or {@code from > original.length}
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4095
     * @throws IllegalArgumentException if {@code from > to}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4096
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4097
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4098
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4099
    public static int[] copyOfRange(int[] original, int from, int to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4100
        int newLength = to - from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4101
        if (newLength < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4102
            throw new IllegalArgumentException(from + " > " + to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4103
        int[] copy = new int[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4104
        System.arraycopy(original, from, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4105
                         Math.min(original.length - from, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4106
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4108
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4109
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4110
     * Copies the specified range of the specified array into a new array.
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4111
     * The initial index of the range ({@code from}) must lie between zero
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4112
     * and {@code original.length}, inclusive.  The value at
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4113
     * {@code original[from]} is placed into the initial element of the copy
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4114
     * (unless {@code from == original.length} or {@code from == to}).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4115
     * Values from subsequent elements in the original array are placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4116
     * subsequent elements in the copy.  The final index of the range
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4117
     * ({@code to}), which must be greater than or equal to {@code from},
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4118
     * may be greater than {@code original.length}, in which case
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4119
     * {@code 0L} is placed in all elements of the copy whose index is
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4120
     * greater than or equal to {@code original.length - from}.  The length
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4121
     * of the returned array will be {@code to - from}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4122
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4123
     * @param original the array from which a range is to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4124
     * @param from the initial index of the range to be copied, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4125
     * @param to the final index of the range to be copied, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4126
     *     (This index may lie outside the array.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4127
     * @return a new array containing the specified range from the original array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4128
     *     truncated or padded with zeros to obtain the required length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4129
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4130
     *     or {@code from > original.length}
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4131
     * @throws IllegalArgumentException if {@code from > to}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4132
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4133
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4134
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4135
    public static long[] copyOfRange(long[] original, int from, int to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4136
        int newLength = to - from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4137
        if (newLength < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4138
            throw new IllegalArgumentException(from + " > " + to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4139
        long[] copy = new long[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4140
        System.arraycopy(original, from, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4141
                         Math.min(original.length - from, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4142
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4144
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4146
     * Copies the specified range of the specified array into a new array.
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4147
     * The initial index of the range ({@code from}) must lie between zero
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4148
     * and {@code original.length}, inclusive.  The value at
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4149
     * {@code original[from]} is placed into the initial element of the copy
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4150
     * (unless {@code from == original.length} or {@code from == to}).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4151
     * Values from subsequent elements in the original array are placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4152
     * subsequent elements in the copy.  The final index of the range
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4153
     * ({@code to}), which must be greater than or equal to {@code from},
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4154
     * may be greater than {@code original.length}, in which case
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4155
     * {@code '\\u000'} is placed in all elements of the copy whose index is
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4156
     * greater than or equal to {@code original.length - from}.  The length
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4157
     * of the returned array will be {@code to - from}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4158
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4159
     * @param original the array from which a range is to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4160
     * @param from the initial index of the range to be copied, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4161
     * @param to the final index of the range to be copied, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4162
     *     (This index may lie outside the array.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4163
     * @return a new array containing the specified range from the original array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4164
     *     truncated or padded with null characters to obtain the required length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4165
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4166
     *     or {@code from > original.length}
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4167
     * @throws IllegalArgumentException if {@code from > to}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4168
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4169
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4171
    public static char[] copyOfRange(char[] original, int from, int to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4172
        int newLength = to - from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4173
        if (newLength < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4174
            throw new IllegalArgumentException(from + " > " + to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4175
        char[] copy = new char[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4176
        System.arraycopy(original, from, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4177
                         Math.min(original.length - from, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4178
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4180
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4181
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4182
     * Copies the specified range of the specified array into a new array.
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4183
     * The initial index of the range ({@code from}) must lie between zero
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4184
     * and {@code original.length}, inclusive.  The value at
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4185
     * {@code original[from]} is placed into the initial element of the copy
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4186
     * (unless {@code from == original.length} or {@code from == to}).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4187
     * Values from subsequent elements in the original array are placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4188
     * subsequent elements in the copy.  The final index of the range
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4189
     * ({@code to}), which must be greater than or equal to {@code from},
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4190
     * may be greater than {@code original.length}, in which case
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4191
     * {@code 0f} is placed in all elements of the copy whose index is
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4192
     * greater than or equal to {@code original.length - from}.  The length
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4193
     * of the returned array will be {@code to - from}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4194
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4195
     * @param original the array from which a range is to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4196
     * @param from the initial index of the range to be copied, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4197
     * @param to the final index of the range to be copied, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4198
     *     (This index may lie outside the array.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4199
     * @return a new array containing the specified range from the original array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4200
     *     truncated or padded with zeros to obtain the required length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4201
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4202
     *     or {@code from > original.length}
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4203
     * @throws IllegalArgumentException if {@code from > to}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4204
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4205
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4206
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4207
    public static float[] copyOfRange(float[] original, int from, int to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4208
        int newLength = to - from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4209
        if (newLength < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4210
            throw new IllegalArgumentException(from + " > " + to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4211
        float[] copy = new float[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4212
        System.arraycopy(original, from, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4213
                         Math.min(original.length - from, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4214
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4216
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4217
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4218
     * Copies the specified range of the specified array into a new array.
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4219
     * The initial index of the range ({@code from}) must lie between zero
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4220
     * and {@code original.length}, inclusive.  The value at
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4221
     * {@code original[from]} is placed into the initial element of the copy
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4222
     * (unless {@code from == original.length} or {@code from == to}).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4223
     * Values from subsequent elements in the original array are placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4224
     * subsequent elements in the copy.  The final index of the range
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4225
     * ({@code to}), which must be greater than or equal to {@code from},
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4226
     * may be greater than {@code original.length}, in which case
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4227
     * {@code 0d} is placed in all elements of the copy whose index is
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4228
     * greater than or equal to {@code original.length - from}.  The length
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4229
     * of the returned array will be {@code to - from}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4230
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4231
     * @param original the array from which a range is to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4232
     * @param from the initial index of the range to be copied, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4233
     * @param to the final index of the range to be copied, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4234
     *     (This index may lie outside the array.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4235
     * @return a new array containing the specified range from the original array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4236
     *     truncated or padded with zeros to obtain the required length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4237
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4238
     *     or {@code from > original.length}
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4239
     * @throws IllegalArgumentException if {@code from > to}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4240
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4241
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4242
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4243
    public static double[] copyOfRange(double[] original, int from, int to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4244
        int newLength = to - from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4245
        if (newLength < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4246
            throw new IllegalArgumentException(from + " > " + to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4247
        double[] copy = new double[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4248
        System.arraycopy(original, from, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4249
                         Math.min(original.length - from, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4250
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4251
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4252
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4253
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4254
     * Copies the specified range of the specified array into a new array.
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4255
     * The initial index of the range ({@code from}) must lie between zero
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4256
     * and {@code original.length}, inclusive.  The value at
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4257
     * {@code original[from]} is placed into the initial element of the copy
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4258
     * (unless {@code from == original.length} or {@code from == to}).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4259
     * Values from subsequent elements in the original array are placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4260
     * subsequent elements in the copy.  The final index of the range
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4261
     * ({@code to}), which must be greater than or equal to {@code from},
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4262
     * may be greater than {@code original.length}, in which case
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4263
     * {@code false} is placed in all elements of the copy whose index is
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4264
     * greater than or equal to {@code original.length - from}.  The length
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4265
     * of the returned array will be {@code to - from}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4266
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4267
     * @param original the array from which a range is to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4268
     * @param from the initial index of the range to be copied, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4269
     * @param to the final index of the range to be copied, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4270
     *     (This index may lie outside the array.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4271
     * @return a new array containing the specified range from the original array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4272
     *     truncated or padded with false elements to obtain the required length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4273
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4274
     *     or {@code from > original.length}
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4275
     * @throws IllegalArgumentException if {@code from > to}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4276
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4277
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4278
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4279
    public static boolean[] copyOfRange(boolean[] original, int from, int to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4280
        int newLength = to - from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4281
        if (newLength < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4282
            throw new IllegalArgumentException(from + " > " + to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4283
        boolean[] copy = new boolean[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4284
        System.arraycopy(original, from, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4285
                         Math.min(original.length - from, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4286
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4288
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4289
    // Misc
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4290
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4291
    /**
51884
2ee7e1b7ba66 7033681: Arrays.asList methods needs better documentation
smarks
parents: 49955
diff changeset
  4292
     * Returns a fixed-size list backed by the specified array. Changes made to
2ee7e1b7ba66 7033681: Arrays.asList methods needs better documentation
smarks
parents: 49955
diff changeset
  4293
     * the array will be visible in the returned list, and changes made to the
2ee7e1b7ba66 7033681: Arrays.asList methods needs better documentation
smarks
parents: 49955
diff changeset
  4294
     * list will be visible in the array. The returned list is
2ee7e1b7ba66 7033681: Arrays.asList methods needs better documentation
smarks
parents: 49955
diff changeset
  4295
     * {@link Serializable} and implements {@link RandomAccess}.
2ee7e1b7ba66 7033681: Arrays.asList methods needs better documentation
smarks
parents: 49955
diff changeset
  4296
     *
2ee7e1b7ba66 7033681: Arrays.asList methods needs better documentation
smarks
parents: 49955
diff changeset
  4297
     * <p>The returned list implements the optional {@code Collection} methods, except
2ee7e1b7ba66 7033681: Arrays.asList methods needs better documentation
smarks
parents: 49955
diff changeset
  4298
     * those that would change the size of the returned list. Those methods leave
2ee7e1b7ba66 7033681: Arrays.asList methods needs better documentation
smarks
parents: 49955
diff changeset
  4299
     * the list unchanged and throw {@link UnsupportedOperationException}.
2ee7e1b7ba66 7033681: Arrays.asList methods needs better documentation
smarks
parents: 49955
diff changeset
  4300
     *
2ee7e1b7ba66 7033681: Arrays.asList methods needs better documentation
smarks
parents: 49955
diff changeset
  4301
     * @apiNote
2ee7e1b7ba66 7033681: Arrays.asList methods needs better documentation
smarks
parents: 49955
diff changeset
  4302
     * This method acts as bridge between array-based and collection-based
2ee7e1b7ba66 7033681: Arrays.asList methods needs better documentation
smarks
parents: 49955
diff changeset
  4303
     * APIs, in combination with {@link Collection#toArray}.
2ee7e1b7ba66 7033681: Arrays.asList methods needs better documentation
smarks
parents: 49955
diff changeset
  4304
     *
2ee7e1b7ba66 7033681: Arrays.asList methods needs better documentation
smarks
parents: 49955
diff changeset
  4305
     * <p>This method provides a way to wrap an existing array:
2ee7e1b7ba66 7033681: Arrays.asList methods needs better documentation
smarks
parents: 49955
diff changeset
  4306
     * <pre>{@code
2ee7e1b7ba66 7033681: Arrays.asList methods needs better documentation
smarks
parents: 49955
diff changeset
  4307
     *     Integer[] numbers = ...
2ee7e1b7ba66 7033681: Arrays.asList methods needs better documentation
smarks
parents: 49955
diff changeset
  4308
     *     ...
2ee7e1b7ba66 7033681: Arrays.asList methods needs better documentation
smarks
parents: 49955
diff changeset
  4309
     *     List<Integer> values = Arrays.asList(numbers);
2ee7e1b7ba66 7033681: Arrays.asList methods needs better documentation
smarks
parents: 49955
diff changeset
  4310
     * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4311
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4312
     * <p>This method also provides a convenient way to create a fixed-size
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4313
     * list initialized to contain several elements:
51884
2ee7e1b7ba66 7033681: Arrays.asList methods needs better documentation
smarks
parents: 49955
diff changeset
  4314
     * <pre>{@code
2ee7e1b7ba66 7033681: Arrays.asList methods needs better documentation
smarks
parents: 49955
diff changeset
  4315
     *     List<String> stooges = Arrays.asList("Larry", "Moe", "Curly");
2ee7e1b7ba66 7033681: Arrays.asList methods needs better documentation
smarks
parents: 49955
diff changeset
  4316
     * }</pre>
2ee7e1b7ba66 7033681: Arrays.asList methods needs better documentation
smarks
parents: 49955
diff changeset
  4317
     *
2ee7e1b7ba66 7033681: Arrays.asList methods needs better documentation
smarks
parents: 49955
diff changeset
  4318
     * <p><em>The list returned by this method is modifiable.</em>
2ee7e1b7ba66 7033681: Arrays.asList methods needs better documentation
smarks
parents: 49955
diff changeset
  4319
     * To create an unmodifiable list, use
2ee7e1b7ba66 7033681: Arrays.asList methods needs better documentation
smarks
parents: 49955
diff changeset
  4320
     * {@link Collections#unmodifiableList Collections.unmodifiableList}
2ee7e1b7ba66 7033681: Arrays.asList methods needs better documentation
smarks
parents: 49955
diff changeset
  4321
     * or <a href="List.html#unmodifiable">Unmodifiable Lists</a>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4322
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  4323
     * @param <T> the class of the objects in the array
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4324
     * @param a the array by which the list will be backed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4325
     * @return a list view of the specified array
51884
2ee7e1b7ba66 7033681: Arrays.asList methods needs better documentation
smarks
parents: 49955
diff changeset
  4326
     * @throws NullPointerException if the specified array is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4327
     */
8151
88b01a6d5f51 7006578: Project Coin: Retrofit JDK libraries with @SafeVarargs
darcy
parents: 7816
diff changeset
  4328
    @SafeVarargs
13795
73850c397272 7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents: 12448
diff changeset
  4329
    @SuppressWarnings("varargs")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4330
    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
  4331
        return new ArrayList<>(a);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4333
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4334
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4335
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4336
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4337
    private static class ArrayList<E> extends AbstractList<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4338
        implements RandomAccess, java.io.Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4339
    {
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 51884
diff changeset
  4340
        @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4341
        private static final long serialVersionUID = -2764017481108945198L;
58520
e036ee8bae56 8231202: Suppress warnings on non-serializable non-transient instance fields in serializable classes
darcy
parents: 57956
diff changeset
  4342
        @SuppressWarnings("serial") // Conditionally serializable
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4343
        private final E[] a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4344
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4345
        ArrayList(E[] array) {
14925
72729557c226 8003981: Support Parallel Array Sorting - JEP 103
chegar
parents: 13795
diff changeset
  4346
            a = Objects.requireNonNull(array);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4347
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4348
14925
72729557c226 8003981: Support Parallel Array Sorting - JEP 103
chegar
parents: 13795
diff changeset
  4349
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4350
        public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4351
            return a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4352
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4353
14925
72729557c226 8003981: Support Parallel Array Sorting - JEP 103
chegar
parents: 13795
diff changeset
  4354
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4355
        public Object[] toArray() {
31540
6efd719b3330 6260652: (coll) Arrays.asList(x).toArray().getClass() should be Object[].class
martin
parents: 29223
diff changeset
  4356
            return Arrays.copyOf(a, a.length, Object[].class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4357
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4358
14925
72729557c226 8003981: Support Parallel Array Sorting - JEP 103
chegar
parents: 13795
diff changeset
  4359
        @Override
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  4360
        @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4361
        public <T> T[] toArray(T[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4362
            int size = size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4363
            if (a.length < size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4364
                return Arrays.copyOf(this.a, size,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4365
                                     (Class<? extends T[]>) a.getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4366
            System.arraycopy(this.a, 0, a, 0, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4367
            if (a.length > size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4368
                a[size] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4369
            return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4370
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4371
14925
72729557c226 8003981: Support Parallel Array Sorting - JEP 103
chegar
parents: 13795
diff changeset
  4372
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4373
        public E get(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4374
            return a[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4375
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4376
14925
72729557c226 8003981: Support Parallel Array Sorting - JEP 103
chegar
parents: 13795
diff changeset
  4377
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4378
        public E set(int index, E element) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4379
            E oldValue = a[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4380
            a[index] = element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4381
            return oldValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4383
14925
72729557c226 8003981: Support Parallel Array Sorting - JEP 103
chegar
parents: 13795
diff changeset
  4384
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4385
        public int indexOf(Object o) {
23354
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4386
            E[] a = this.a;
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4387
            if (o == null) {
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4388
                for (int i = 0; i < a.length; i++)
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4389
                    if (a[i] == null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4390
                        return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4391
            } else {
23354
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4392
                for (int i = 0; i < a.length; i++)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4393
                    if (o.equals(a[i]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4394
                        return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4395
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4396
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4397
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4398
14925
72729557c226 8003981: Support Parallel Array Sorting - JEP 103
chegar
parents: 13795
diff changeset
  4399
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4400
        public boolean contains(Object o) {
26451
f86e59f18322 8056951: pico-optimize contains(Object) methods
martin
parents: 25859
diff changeset
  4401
            return indexOf(o) >= 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4402
        }
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4403
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4404
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4405
        public Spliterator<E> spliterator() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4406
            return Spliterators.spliterator(a, Spliterator.ORDERED);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4407
        }
23354
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4408
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4409
        @Override
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4410
        public void forEach(Consumer<? super E> action) {
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4411
            Objects.requireNonNull(action);
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4412
            for (E e : a) {
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4413
                action.accept(e);
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4414
            }
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4415
        }
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4416
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4417
        @Override
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4418
        public void replaceAll(UnaryOperator<E> operator) {
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4419
            Objects.requireNonNull(operator);
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4420
            E[] a = this.a;
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4421
            for (int i = 0; i < a.length; i++) {
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4422
                a[i] = operator.apply(a[i]);
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4423
            }
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4424
        }
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4425
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4426
        @Override
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4427
        public void sort(Comparator<? super E> c) {
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4428
            Arrays.sort(a, c);
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4429
        }
37685
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4430
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4431
        @Override
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4432
        public Iterator<E> iterator() {
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4433
            return new ArrayItr<>(a);
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4434
        }
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4435
    }
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4436
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4437
    private static class ArrayItr<E> implements Iterator<E> {
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4438
        private int cursor;
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4439
        private final E[] a;
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4440
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4441
        ArrayItr(E[] a) {
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4442
            this.a = a;
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4443
        }
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4444
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4445
        @Override
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4446
        public boolean hasNext() {
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4447
            return cursor < a.length;
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4448
        }
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4449
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4450
        @Override
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4451
        public E next() {
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4452
            int i = cursor;
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4453
            if (i >= a.length) {
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4454
                throw new NoSuchElementException();
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4455
            }
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4456
            cursor = i + 1;
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4457
            return a[i];
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4458
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4459
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4460
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4461
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4462
     * Returns a hash code based on the contents of the specified array.
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4463
     * For any two {@code long} arrays {@code a} and {@code b}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4464
     * such that {@code Arrays.equals(a, b)}, it is also the case that
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4465
     * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4466
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4467
     * <p>The value returned by this method is the same value that would be
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4468
     * obtained by invoking the {@link List#hashCode() hashCode}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4469
     * method on a {@link List} containing a sequence of {@link Long}
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4470
     * instances representing the elements of {@code a} in the same order.
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4471
     * If {@code a} is {@code null}, this method returns 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4472
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4473
     * @param a the array whose hash value to compute
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4474
     * @return a content-based hash code for {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4475
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4476
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4477
    public static int hashCode(long a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4478
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4479
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4480
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4481
        int result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4482
        for (long element : a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4483
            int elementHash = (int)(element ^ (element >>> 32));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4484
            result = 31 * result + elementHash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4485
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4486
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4487
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4488
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4489
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4490
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4491
     * Returns a hash code based on the contents of the specified array.
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4492
     * For any two non-null {@code int} arrays {@code a} and {@code b}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4493
     * such that {@code Arrays.equals(a, b)}, it is also the case that
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4494
     * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4495
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4496
     * <p>The value returned by this method is the same value that would be
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4497
     * obtained by invoking the {@link List#hashCode() hashCode}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4498
     * method on a {@link List} containing a sequence of {@link Integer}
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4499
     * instances representing the elements of {@code a} in the same order.
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4500
     * If {@code a} is {@code null}, this method returns 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4501
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4502
     * @param a the array whose hash value to compute
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4503
     * @return a content-based hash code for {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4504
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4505
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4506
    public static int hashCode(int a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4507
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4508
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4509
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4510
        int result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4511
        for (int element : a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4512
            result = 31 * result + element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4513
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4514
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4515
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4516
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4517
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4518
     * Returns a hash code based on the contents of the specified array.
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4519
     * For any two {@code short} arrays {@code a} and {@code b}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4520
     * such that {@code Arrays.equals(a, b)}, it is also the case that
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4521
     * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4522
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4523
     * <p>The value returned by this method is the same value that would be
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4524
     * obtained by invoking the {@link List#hashCode() hashCode}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4525
     * method on a {@link List} containing a sequence of {@link Short}
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4526
     * instances representing the elements of {@code a} in the same order.
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4527
     * If {@code a} is {@code null}, this method returns 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4528
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4529
     * @param a the array whose hash value to compute
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4530
     * @return a content-based hash code for {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4531
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4532
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4533
    public static int hashCode(short a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4534
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4535
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4536
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4537
        int result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4538
        for (short element : a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4539
            result = 31 * result + element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4540
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4541
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4542
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4543
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4544
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4545
     * Returns a hash code based on the contents of the specified array.
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4546
     * For any two {@code char} arrays {@code a} and {@code b}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4547
     * such that {@code Arrays.equals(a, b)}, it is also the case that
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4548
     * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4549
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4550
     * <p>The value returned by this method is the same value that would be
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4551
     * obtained by invoking the {@link List#hashCode() hashCode}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4552
     * method on a {@link List} containing a sequence of {@link Character}
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4553
     * instances representing the elements of {@code a} in the same order.
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4554
     * If {@code a} is {@code null}, this method returns 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4555
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4556
     * @param a the array whose hash value to compute
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4557
     * @return a content-based hash code for {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4558
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4559
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4560
    public static int hashCode(char a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4561
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4562
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4563
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4564
        int result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4565
        for (char element : a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4566
            result = 31 * result + element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4567
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4568
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4569
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4570
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4571
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4572
     * Returns a hash code based on the contents of the specified array.
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4573
     * For any two {@code byte} arrays {@code a} and {@code b}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4574
     * such that {@code Arrays.equals(a, b)}, it is also the case that
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4575
     * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4576
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4577
     * <p>The value returned by this method is the same value that would be
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4578
     * obtained by invoking the {@link List#hashCode() hashCode}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4579
     * method on a {@link List} containing a sequence of {@link Byte}
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4580
     * instances representing the elements of {@code a} in the same order.
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4581
     * If {@code a} is {@code null}, this method returns 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4582
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4583
     * @param a the array whose hash value to compute
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4584
     * @return a content-based hash code for {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4585
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4586
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4587
    public static int hashCode(byte a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4588
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4589
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4590
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4591
        int result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4592
        for (byte element : a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4593
            result = 31 * result + element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4594
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4595
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4596
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4597
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4598
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4599
     * Returns a hash code based on the contents of the specified array.
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4600
     * For any two {@code boolean} arrays {@code a} and {@code b}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4601
     * such that {@code Arrays.equals(a, b)}, it is also the case that
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4602
     * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4603
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4604
     * <p>The value returned by this method is the same value that would be
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4605
     * obtained by invoking the {@link List#hashCode() hashCode}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4606
     * method on a {@link List} containing a sequence of {@link Boolean}
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4607
     * instances representing the elements of {@code a} in the same order.
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4608
     * If {@code a} is {@code null}, this method returns 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4609
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4610
     * @param a the array whose hash value to compute
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4611
     * @return a content-based hash code for {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4612
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4613
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4614
    public static int hashCode(boolean a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4615
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4616
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4617
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4618
        int result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4619
        for (boolean element : a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4620
            result = 31 * result + (element ? 1231 : 1237);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4621
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4622
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4623
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4624
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4625
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4626
     * Returns a hash code based on the contents of the specified array.
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4627
     * For any two {@code float} arrays {@code a} and {@code b}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4628
     * such that {@code Arrays.equals(a, b)}, it is also the case that
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4629
     * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4630
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4631
     * <p>The value returned by this method is the same value that would be
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4632
     * obtained by invoking the {@link List#hashCode() hashCode}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4633
     * method on a {@link List} containing a sequence of {@link Float}
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4634
     * instances representing the elements of {@code a} in the same order.
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4635
     * If {@code a} is {@code null}, this method returns 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4636
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4637
     * @param a the array whose hash value to compute
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4638
     * @return a content-based hash code for {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4639
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4640
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4641
    public static int hashCode(float a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4642
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4643
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4644
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4645
        int result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4646
        for (float element : a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4647
            result = 31 * result + Float.floatToIntBits(element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4648
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4649
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4650
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4651
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4652
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4653
     * Returns a hash code based on the contents of the specified array.
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4654
     * For any two {@code double} arrays {@code a} and {@code b}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4655
     * such that {@code Arrays.equals(a, b)}, it is also the case that
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4656
     * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4657
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4658
     * <p>The value returned by this method is the same value that would be
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4659
     * obtained by invoking the {@link List#hashCode() hashCode}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4660
     * method on a {@link List} containing a sequence of {@link Double}
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4661
     * instances representing the elements of {@code a} in the same order.
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4662
     * If {@code a} is {@code null}, this method returns 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4663
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4664
     * @param a the array whose hash value to compute
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4665
     * @return a content-based hash code for {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4666
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4667
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4668
    public static int hashCode(double a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4669
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4670
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4671
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4672
        int result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4673
        for (double element : a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4674
            long bits = Double.doubleToLongBits(element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4675
            result = 31 * result + (int)(bits ^ (bits >>> 32));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4676
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4677
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4678
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4679
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4680
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4681
     * Returns a hash code based on the contents of the specified array.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4682
     * the array contains other arrays as elements, the hash code is based on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4683
     * their identities rather than their contents.  It is therefore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4684
     * acceptable to invoke this method on an array that contains itself as an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4685
     * element,  either directly or indirectly through one or more levels of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4686
     * arrays.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4687
     *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4688
     * <p>For any two arrays {@code a} and {@code b} such that
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4689
     * {@code Arrays.equals(a, b)}, it is also the case that
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4690
     * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4691
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4692
     * <p>The value returned by this method is equal to the value that would
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4693
     * be returned by {@code Arrays.asList(a).hashCode()}, unless {@code a}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4694
     * is {@code null}, in which case {@code 0} is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4695
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4696
     * @param a the array whose content-based hash code to compute
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4697
     * @return a content-based hash code for {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4698
     * @see #deepHashCode(Object[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4699
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4700
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4701
    public static int hashCode(Object a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4702
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4703
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4704
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4705
        int result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4706
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4707
        for (Object element : a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4708
            result = 31 * result + (element == null ? 0 : element.hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4709
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4710
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4711
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4712
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4713
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4714
     * Returns a hash code based on the "deep contents" of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4715
     * array.  If the array contains other arrays as elements, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4716
     * hash code is based on their contents and so on, ad infinitum.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4717
     * It is therefore unacceptable to invoke this method on an array that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4718
     * contains itself as an element, either directly or indirectly through
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4719
     * one or more levels of arrays.  The behavior of such an invocation is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4720
     * undefined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4721
     *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4722
     * <p>For any two arrays {@code a} and {@code b} such that
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4723
     * {@code Arrays.deepEquals(a, b)}, it is also the case that
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4724
     * {@code Arrays.deepHashCode(a) == Arrays.deepHashCode(b)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4725
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4726
     * <p>The computation of the value returned by this method is similar to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4727
     * that of the value returned by {@link List#hashCode()} on a list
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4728
     * containing the same elements as {@code a} in the same order, with one
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4729
     * difference: If an element {@code e} of {@code a} is itself an array,
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4730
     * its hash code is computed not by calling {@code e.hashCode()}, but as
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4731
     * by calling the appropriate overloading of {@code Arrays.hashCode(e)}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4732
     * if {@code e} is an array of a primitive type, or as by calling
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4733
     * {@code Arrays.deepHashCode(e)} recursively if {@code e} is an array
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4734
     * of a reference type.  If {@code a} is {@code null}, this method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4735
     * returns 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4736
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4737
     * @param a the array whose deep-content-based hash code to compute
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4738
     * @return a deep-content-based hash code for {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4739
     * @see #hashCode(Object[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4740
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4741
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4742
    public static int deepHashCode(Object a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4743
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4744
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4745
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4746
        int result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4747
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4748
        for (Object element : a) {
49955
ea246151be08 8202398: Optimize Arrays.deepHashCode
martin
parents: 49433
diff changeset
  4749
            final int elementHash;
ea246151be08 8202398: Optimize Arrays.deepHashCode
martin
parents: 49433
diff changeset
  4750
            final Class<?> cl;
ea246151be08 8202398: Optimize Arrays.deepHashCode
martin
parents: 49433
diff changeset
  4751
            if (element == null)
ea246151be08 8202398: Optimize Arrays.deepHashCode
martin
parents: 49433
diff changeset
  4752
                elementHash = 0;
ea246151be08 8202398: Optimize Arrays.deepHashCode
martin
parents: 49433
diff changeset
  4753
            else if ((cl = element.getClass().getComponentType()) == null)
ea246151be08 8202398: Optimize Arrays.deepHashCode
martin
parents: 49433
diff changeset
  4754
                elementHash = element.hashCode();
ea246151be08 8202398: Optimize Arrays.deepHashCode
martin
parents: 49433
diff changeset
  4755
            else if (element instanceof Object[])
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4756
                elementHash = deepHashCode((Object[]) element);
49955
ea246151be08 8202398: Optimize Arrays.deepHashCode
martin
parents: 49433
diff changeset
  4757
            else
ea246151be08 8202398: Optimize Arrays.deepHashCode
martin
parents: 49433
diff changeset
  4758
                elementHash = primitiveArrayHashCode(element, cl);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4759
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4760
            result = 31 * result + elementHash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4761
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4762
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4763
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4764
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4765
49955
ea246151be08 8202398: Optimize Arrays.deepHashCode
martin
parents: 49433
diff changeset
  4766
    private static int primitiveArrayHashCode(Object a, Class<?> cl) {
ea246151be08 8202398: Optimize Arrays.deepHashCode
martin
parents: 49433
diff changeset
  4767
        return
ea246151be08 8202398: Optimize Arrays.deepHashCode
martin
parents: 49433
diff changeset
  4768
            (cl == byte.class)    ? hashCode((byte[]) a)    :
ea246151be08 8202398: Optimize Arrays.deepHashCode
martin
parents: 49433
diff changeset
  4769
            (cl == int.class)     ? hashCode((int[]) a)     :
ea246151be08 8202398: Optimize Arrays.deepHashCode
martin
parents: 49433
diff changeset
  4770
            (cl == long.class)    ? hashCode((long[]) a)    :
ea246151be08 8202398: Optimize Arrays.deepHashCode
martin
parents: 49433
diff changeset
  4771
            (cl == char.class)    ? hashCode((char[]) a)    :
ea246151be08 8202398: Optimize Arrays.deepHashCode
martin
parents: 49433
diff changeset
  4772
            (cl == short.class)   ? hashCode((short[]) a)   :
ea246151be08 8202398: Optimize Arrays.deepHashCode
martin
parents: 49433
diff changeset
  4773
            (cl == boolean.class) ? hashCode((boolean[]) a) :
ea246151be08 8202398: Optimize Arrays.deepHashCode
martin
parents: 49433
diff changeset
  4774
            (cl == double.class)  ? hashCode((double[]) a)  :
ea246151be08 8202398: Optimize Arrays.deepHashCode
martin
parents: 49433
diff changeset
  4775
            // If new primitive types are ever added, this method must be
ea246151be08 8202398: Optimize Arrays.deepHashCode
martin
parents: 49433
diff changeset
  4776
            // expanded or we will fail here with ClassCastException.
ea246151be08 8202398: Optimize Arrays.deepHashCode
martin
parents: 49433
diff changeset
  4777
            hashCode((float[]) a);
ea246151be08 8202398: Optimize Arrays.deepHashCode
martin
parents: 49433
diff changeset
  4778
    }
ea246151be08 8202398: Optimize Arrays.deepHashCode
martin
parents: 49433
diff changeset
  4779
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4780
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4781
     * Returns {@code true} if the two specified arrays are <i>deeply
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4782
     * equal</i> to one another.  Unlike the {@link #equals(Object[],Object[])}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4783
     * method, this method is appropriate for use with nested arrays of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4784
     * arbitrary depth.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4785
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4786
     * <p>Two array references are considered deeply equal if both
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4787
     * are {@code null}, or if they refer to arrays that contain the same
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4788
     * number of elements and all corresponding pairs of elements in the two
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4789
     * arrays are deeply equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4790
     *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4791
     * <p>Two possibly {@code null} elements {@code e1} and {@code e2} are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4792
     * deeply equal if any of the following conditions hold:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4793
     * <ul>
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4794
     *    <li> {@code e1} and {@code e2} are both arrays of object reference
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4795
     *         types, and {@code Arrays.deepEquals(e1, e2) would return true}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4796
     *    <li> {@code e1} and {@code e2} are arrays of the same primitive
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4797
     *         type, and the appropriate overloading of
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4798
     *         {@code Arrays.equals(e1, e2)} would return true.
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4799
     *    <li> {@code e1 == e2}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4800
     *    <li> {@code e1.equals(e2)} would return true.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4801
     * </ul>
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4802
     * Note that this definition permits {@code null} elements at any depth.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4803
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4804
     * <p>If either of the specified arrays contain themselves as elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4805
     * either directly or indirectly through one or more levels of arrays,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4806
     * the behavior of this method is undefined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4807
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4808
     * @param a1 one array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4809
     * @param a2 the other array to be tested for equality
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4810
     * @return {@code true} if the two arrays are equal
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4811
     * @see #equals(Object[],Object[])
4165
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4812
     * @see Objects#deepEquals(Object, Object)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4813
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4814
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4815
    public static boolean deepEquals(Object[] a1, Object[] a2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4816
        if (a1 == a2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4817
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4818
        if (a1 == null || a2==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4819
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4820
        int length = a1.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4821
        if (a2.length != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4822
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4823
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4824
        for (int i = 0; i < length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4825
            Object e1 = a1[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4826
            Object e2 = a2[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4827
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4828
            if (e1 == e2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4829
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4830
            if (e1 == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4831
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4832
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4833
            // 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
  4834
            boolean eq = deepEquals0(e1, e2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4835
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4836
            if (!eq)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4837
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4838
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4839
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4840
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4841
4165
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4842
    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
  4843
        assert e1 != null;
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4844
        boolean eq;
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4845
        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
  4846
            eq = deepEquals ((Object[]) e1, (Object[]) e2);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4847
        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
  4848
            eq = equals((byte[]) e1, (byte[]) e2);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4849
        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
  4850
            eq = equals((short[]) e1, (short[]) e2);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4851
        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
  4852
            eq = equals((int[]) e1, (int[]) e2);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4853
        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
  4854
            eq = equals((long[]) e1, (long[]) e2);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4855
        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
  4856
            eq = equals((char[]) e1, (char[]) e2);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4857
        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
  4858
            eq = equals((float[]) e1, (float[]) e2);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4859
        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
  4860
            eq = equals((double[]) e1, (double[]) e2);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4861
        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
  4862
            eq = equals((boolean[]) e1, (boolean[]) e2);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4863
        else
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4864
            eq = e1.equals(e2);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4865
        return eq;
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4866
    }
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4867
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4868
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4869
     * Returns a string representation of the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4870
     * The string representation consists of a list of the array's elements,
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4871
     * enclosed in square brackets ({@code "[]"}).  Adjacent elements are
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4872
     * separated by the characters {@code ", "} (a comma followed by a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4873
     * space).  Elements are converted to strings as by
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4874
     * {@code String.valueOf(long)}.  Returns {@code "null"} if {@code a}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4875
     * is {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4876
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4877
     * @param a the array whose string representation to return
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4878
     * @return a string representation of {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4879
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4880
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4881
    public static String toString(long[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4882
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4883
            return "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4884
        int iMax = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4885
        if (iMax == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4886
            return "[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4887
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4888
        StringBuilder b = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4889
        b.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4890
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4891
            b.append(a[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4892
            if (i == iMax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4893
                return b.append(']').toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4894
            b.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4895
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4896
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4897
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4898
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4899
     * Returns a string representation of the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4900
     * The string representation consists of a list of the array's elements,
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4901
     * enclosed in square brackets ({@code "[]"}).  Adjacent elements are
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4902
     * separated by the characters {@code ", "} (a comma followed by a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4903
     * space).  Elements are converted to strings as by
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4904
     * {@code String.valueOf(int)}.  Returns {@code "null"} if {@code a} is
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4905
     * {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4906
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4907
     * @param a the array whose string representation to return
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4908
     * @return a string representation of {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4909
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4910
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4911
    public static String toString(int[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4912
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4913
            return "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4914
        int iMax = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4915
        if (iMax == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4916
            return "[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4917
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4918
        StringBuilder b = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4919
        b.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4920
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4921
            b.append(a[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4922
            if (i == iMax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4923
                return b.append(']').toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4924
            b.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4925
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4926
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4927
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4928
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4929
     * Returns a string representation of the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4930
     * The string representation consists of a list of the array's elements,
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4931
     * enclosed in square brackets ({@code "[]"}).  Adjacent elements are
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4932
     * separated by the characters {@code ", "} (a comma followed by a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4933
     * space).  Elements are converted to strings as by
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4934
     * {@code String.valueOf(short)}.  Returns {@code "null"} if {@code a}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4935
     * is {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4936
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4937
     * @param a the array whose string representation to return
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4938
     * @return a string representation of {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4939
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4940
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4941
    public static String toString(short[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4942
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4943
            return "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4944
        int iMax = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4945
        if (iMax == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4946
            return "[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4947
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4948
        StringBuilder b = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4949
        b.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4950
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4951
            b.append(a[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4952
            if (i == iMax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4953
                return b.append(']').toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4954
            b.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4955
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4956
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4957
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4958
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4959
     * Returns a string representation of the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4960
     * The string representation consists of a list of the array's elements,
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4961
     * enclosed in square brackets ({@code "[]"}).  Adjacent elements are
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4962
     * separated by the characters {@code ", "} (a comma followed by a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4963
     * space).  Elements are converted to strings as by
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4964
     * {@code String.valueOf(char)}.  Returns {@code "null"} if {@code a}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4965
     * is {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4966
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4967
     * @param a the array whose string representation to return
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4968
     * @return a string representation of {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4969
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4970
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4971
    public static String toString(char[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4972
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4973
            return "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4974
        int iMax = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4975
        if (iMax == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4976
            return "[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4977
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4978
        StringBuilder b = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4979
        b.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4980
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4981
            b.append(a[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4982
            if (i == iMax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4983
                return b.append(']').toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4984
            b.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4985
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4986
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4987
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4988
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4989
     * Returns a string representation of the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4990
     * The string representation consists of a list of the array's elements,
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4991
     * enclosed in square brackets ({@code "[]"}).  Adjacent elements
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4992
     * are separated by the characters {@code ", "} (a comma followed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4993
     * by a space).  Elements are converted to strings as by
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4994
     * {@code String.valueOf(byte)}.  Returns {@code "null"} if
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4995
     * {@code a} is {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4996
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4997
     * @param a the array whose string representation to return
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4998
     * @return a string representation of {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4999
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5000
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5001
    public static String toString(byte[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5002
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5003
            return "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5004
        int iMax = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5005
        if (iMax == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5006
            return "[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5007
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5008
        StringBuilder b = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5009
        b.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5010
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5011
            b.append(a[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5012
            if (i == iMax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5013
                return b.append(']').toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5014
            b.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5015
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5016
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5017
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5018
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5019
     * Returns a string representation of the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5020
     * The string representation consists of a list of the array's elements,
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  5021
     * enclosed in square brackets ({@code "[]"}).  Adjacent elements are
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  5022
     * separated by the characters {@code ", "} (a comma followed by a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5023
     * space).  Elements are converted to strings as by
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  5024
     * {@code String.valueOf(boolean)}.  Returns {@code "null"} if
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  5025
     * {@code a} is {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5026
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5027
     * @param a the array whose string representation to return
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  5028
     * @return a string representation of {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5029
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5030
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5031
    public static String toString(boolean[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5032
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5033
            return "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5034
        int iMax = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5035
        if (iMax == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5036
            return "[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5037
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5038
        StringBuilder b = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5039
        b.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5040
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5041
            b.append(a[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5042
            if (i == iMax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5043
                return b.append(']').toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5044
            b.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5045
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5046
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5047
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5048
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5049
     * Returns a string representation of the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5050
     * The string representation consists of a list of the array's elements,
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  5051
     * enclosed in square brackets ({@code "[]"}).  Adjacent elements are
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  5052
     * separated by the characters {@code ", "} (a comma followed by a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5053
     * space).  Elements are converted to strings as by
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  5054
     * {@code String.valueOf(float)}.  Returns {@code "null"} if {@code a}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  5055
     * is {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5056
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5057
     * @param a the array whose string representation to return
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  5058
     * @return a string representation of {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5059
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5060
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5061
    public static String toString(float[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5062
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5063
            return "null";
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
  5064
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5065
        int iMax = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5066
        if (iMax == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5067
            return "[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5068
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5069
        StringBuilder b = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5070
        b.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5071
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5072
            b.append(a[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5073
            if (i == iMax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5074
                return b.append(']').toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5075
            b.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5076
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5077
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5078
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5079
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5080
     * Returns a string representation of the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5081
     * The string representation consists of a list of the array's elements,
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  5082
     * enclosed in square brackets ({@code "[]"}).  Adjacent elements are
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  5083
     * separated by the characters {@code ", "} (a comma followed by a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5084
     * space).  Elements are converted to strings as by
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  5085
     * {@code String.valueOf(double)}.  Returns {@code "null"} if {@code a}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  5086
     * is {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5087
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5088
     * @param a the array whose string representation to return
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  5089
     * @return a string representation of {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5090
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5091
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5092
    public static String toString(double[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5093
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5094
            return "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5095
        int iMax = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5096
        if (iMax == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5097
            return "[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5098
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5099
        StringBuilder b = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5100
        b.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5101
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5102
            b.append(a[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5103
            if (i == iMax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5104
                return b.append(']').toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5105
            b.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5108
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5109
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5110
     * Returns a string representation of the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5111
     * If the array contains other arrays as elements, they are converted to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5112
     * strings by the {@link Object#toString} method inherited from
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  5113
     * {@code Object}, which describes their <i>identities</i> rather than
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5114
     * their contents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5115
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5116
     * <p>The value returned by this method is equal to the value that would
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  5117
     * be returned by {@code Arrays.asList(a).toString()}, unless {@code a}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  5118
     * is {@code null}, in which case {@code "null"} is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5119
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5120
     * @param a the array whose string representation to return
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  5121
     * @return a string representation of {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5122
     * @see #deepToString(Object[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5123
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5125
    public static String toString(Object[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5126
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5127
            return "null";
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
  5128
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5129
        int iMax = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5130
        if (iMax == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5131
            return "[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5132
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5133
        StringBuilder b = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5134
        b.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5135
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5136
            b.append(String.valueOf(a[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5137
            if (i == iMax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5138
                return b.append(']').toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5139
            b.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5142
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5144
     * Returns a string representation of the "deep contents" of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5145
     * array.  If the array contains other arrays as elements, the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5146
     * representation contains their contents and so on.  This method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5147
     * designed for converting multidimensional arrays to strings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5148
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5149
     * <p>The string representation consists of a list of the array's
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  5150
     * elements, enclosed in square brackets ({@code "[]"}).  Adjacent
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  5151
     * elements are separated by the characters {@code ", "} (a comma
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5152
     * followed by a space).  Elements are converted to strings as by
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  5153
     * {@code String.valueOf(Object)}, unless they are themselves
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5154
     * arrays.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5155
     *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  5156
     * <p>If an element {@code e} is an array of a primitive type, it is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5157
     * converted to a string as by invoking the appropriate overloading of
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  5158
     * {@code Arrays.toString(e)}.  If an element {@code e} is an array of a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5159
     * reference type, it is converted to a string as by invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5160
     * this method recursively.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5161
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5162
     * <p>To avoid infinite recursion, if the specified array contains itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5163
     * as an element, or contains an indirect reference to itself through one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5164
     * or more levels of arrays, the self-reference is converted to the string
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  5165
     * {@code "[...]"}.  For example, an array containing only a reference
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  5166
     * to itself would be rendered as {@code "[[...]]"}.
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  5167
     *
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  5168
     * <p>This method returns {@code "null"} if the specified array
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  5169
     * is {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5170
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5171
     * @param a the array whose string representation to return
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  5172
     * @return a string representation of {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5173
     * @see #toString(Object[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5174
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5175
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5176
    public static String deepToString(Object[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5177
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5178
            return "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5179
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5180
        int bufLen = 20 * a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5181
        if (a.length != 0 && bufLen <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5182
            bufLen = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5183
        StringBuilder buf = new StringBuilder(bufLen);
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 21278
diff changeset
  5184
        deepToString(a, buf, new HashSet<>());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5185
        return buf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5187
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5188
    private static void deepToString(Object[] a, StringBuilder buf,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5189
                                     Set<Object[]> dejaVu) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5190
        if (a == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5191
            buf.append("null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5192
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5194
        int iMax = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5195
        if (iMax == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5196
            buf.append("[]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5197
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5199
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5200
        dejaVu.add(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5201
        buf.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5202
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5203
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5204
            Object element = a[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5205
            if (element == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5206
                buf.append("null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5207
            } else {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  5208
                Class<?> eClass = element.getClass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5209
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5210
                if (eClass.isArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5211
                    if (eClass == byte[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5212
                        buf.append(toString((byte[]) element));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5213
                    else if (eClass == short[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5214
                        buf.append(toString((short[]) element));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5215
                    else if (eClass == int[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5216
                        buf.append(toString((int[]) element));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5217
                    else if (eClass == long[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5218
                        buf.append(toString((long[]) element));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5219
                    else if (eClass == char[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5220
                        buf.append(toString((char[]) element));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5221
                    else if (eClass == float[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5222
                        buf.append(toString((float[]) element));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5223
                    else if (eClass == double[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5224
                        buf.append(toString((double[]) element));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5225
                    else if (eClass == boolean[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5226
                        buf.append(toString((boolean[]) element));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5227
                    else { // element is an array of object references
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5228
                        if (dejaVu.contains(element))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5229
                            buf.append("[...]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5230
                        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5231
                            deepToString((Object[])element, buf, dejaVu);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5232
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5233
                } else {  // element is non-null and not an array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5234
                    buf.append(element.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5235
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5236
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5237
            if (i == iMax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5238
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5239
            buf.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5241
        buf.append(']');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5242
        dejaVu.remove(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5243
    }
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5244
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5245
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5246
    /**
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5247
     * Set all elements of the specified array, using the provided
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5248
     * generator function to compute each element.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5249
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5250
     * <p>If the generator function throws an exception, it is relayed to
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5251
     * the caller and the array is left in an indeterminate state.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5252
     *
29223
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5253
     * @apiNote
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5254
     * Setting a subrange of an array, using a generator function to compute
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5255
     * each element, can be written as follows:
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5256
     * <pre>{@code
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5257
     * IntStream.range(startInclusive, endExclusive)
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5258
     *          .forEach(i -> array[i] = generator.apply(i));
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5259
     * }</pre>
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5260
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5261
     * @param <T> type of elements of the array
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5262
     * @param array array to be initialized
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5263
     * @param generator a function accepting an index and producing the desired
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5264
     *        value for that position
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5265
     * @throws NullPointerException if the generator is null
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5266
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5267
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5268
    public static <T> void setAll(T[] array, IntFunction<? extends T> generator) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5269
        Objects.requireNonNull(generator);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5270
        for (int i = 0; i < array.length; i++)
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5271
            array[i] = generator.apply(i);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5272
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5273
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5274
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5275
     * Set all elements of the specified array, in parallel, using the
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5276
     * provided generator function to compute each element.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5277
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5278
     * <p>If the generator function throws an exception, an unchecked exception
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5279
     * is thrown from {@code parallelSetAll} and the array is left in an
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5280
     * indeterminate state.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5281
     *
29223
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5282
     * @apiNote
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5283
     * Setting a subrange of an array, in parallel, using a generator function
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5284
     * to compute each element, can be written as follows:
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5285
     * <pre>{@code
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5286
     * IntStream.range(startInclusive, endExclusive)
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5287
     *          .parallel()
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5288
     *          .forEach(i -> array[i] = generator.apply(i));
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5289
     * }</pre>
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5290
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5291
     * @param <T> type of elements of the array
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5292
     * @param array array to be initialized
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5293
     * @param generator a function accepting an index and producing the desired
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5294
     *        value for that position
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5295
     * @throws NullPointerException if the generator is null
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5296
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5297
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5298
    public static <T> void parallelSetAll(T[] array, IntFunction<? extends T> generator) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5299
        Objects.requireNonNull(generator);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5300
        IntStream.range(0, array.length).parallel().forEach(i -> { array[i] = generator.apply(i); });
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5301
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5302
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5303
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5304
     * Set all elements of the specified array, using the provided
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5305
     * generator function to compute each element.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5306
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5307
     * <p>If the generator function throws an exception, it is relayed to
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5308
     * the caller and the array is left in an indeterminate state.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5309
     *
29223
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5310
     * @apiNote
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5311
     * Setting a subrange of an array, using a generator function to compute
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5312
     * each element, can be written as follows:
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5313
     * <pre>{@code
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5314
     * IntStream.range(startInclusive, endExclusive)
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5315
     *          .forEach(i -> array[i] = generator.applyAsInt(i));
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5316
     * }</pre>
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5317
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5318
     * @param array array to be initialized
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5319
     * @param generator a function accepting an index and producing the desired
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5320
     *        value for that position
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5321
     * @throws NullPointerException if the generator is null
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5322
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5323
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5324
    public static void setAll(int[] array, IntUnaryOperator generator) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5325
        Objects.requireNonNull(generator);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5326
        for (int i = 0; i < array.length; i++)
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5327
            array[i] = generator.applyAsInt(i);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5328
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5329
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5330
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5331
     * Set all elements of the specified array, in parallel, using the
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5332
     * provided generator function to compute each element.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5333
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5334
     * <p>If the generator function throws an exception, an unchecked exception
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5335
     * is thrown from {@code parallelSetAll} and the array is left in an
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5336
     * indeterminate state.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5337
     *
29223
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5338
     * @apiNote
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5339
     * Setting a subrange of an array, in parallel, using a generator function
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5340
     * to compute each element, can be written as follows:
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5341
     * <pre>{@code
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5342
     * IntStream.range(startInclusive, endExclusive)
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5343
     *          .parallel()
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5344
     *          .forEach(i -> array[i] = generator.applyAsInt(i));
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5345
     * }</pre>
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5346
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5347
     * @param array array to be initialized
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5348
     * @param generator a function accepting an index and producing the desired
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5349
     * value for that position
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5350
     * @throws NullPointerException if the generator is null
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5351
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5352
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5353
    public static void parallelSetAll(int[] array, IntUnaryOperator generator) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5354
        Objects.requireNonNull(generator);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5355
        IntStream.range(0, array.length).parallel().forEach(i -> { array[i] = generator.applyAsInt(i); });
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5356
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5357
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5358
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5359
     * Set all elements of the specified array, using the provided
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5360
     * generator function to compute each element.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5361
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5362
     * <p>If the generator function throws an exception, it is relayed to
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5363
     * the caller and the array is left in an indeterminate state.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5364
     *
29223
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5365
     * @apiNote
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5366
     * Setting a subrange of an array, using a generator function to compute
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5367
     * each element, can be written as follows:
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5368
     * <pre>{@code
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5369
     * IntStream.range(startInclusive, endExclusive)
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5370
     *          .forEach(i -> array[i] = generator.applyAsLong(i));
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5371
     * }</pre>
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5372
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5373
     * @param array array to be initialized
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5374
     * @param generator a function accepting an index and producing the desired
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5375
     *        value for that position
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5376
     * @throws NullPointerException if the generator is null
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5377
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5378
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5379
    public static void setAll(long[] array, IntToLongFunction generator) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5380
        Objects.requireNonNull(generator);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5381
        for (int i = 0; i < array.length; i++)
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5382
            array[i] = generator.applyAsLong(i);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5383
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5384
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5385
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5386
     * Set all elements of the specified array, in parallel, using the
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5387
     * provided generator function to compute each element.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5388
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5389
     * <p>If the generator function throws an exception, an unchecked exception
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5390
     * is thrown from {@code parallelSetAll} and the array is left in an
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5391
     * indeterminate state.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5392
     *
29223
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5393
     * @apiNote
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5394
     * Setting a subrange of an array, in parallel, using a generator function
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5395
     * to compute each element, can be written as follows:
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5396
     * <pre>{@code
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5397
     * IntStream.range(startInclusive, endExclusive)
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5398
     *          .parallel()
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5399
     *          .forEach(i -> array[i] = generator.applyAsLong(i));
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5400
     * }</pre>
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5401
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5402
     * @param array array to be initialized
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5403
     * @param generator a function accepting an index and producing the desired
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5404
     *        value for that position
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5405
     * @throws NullPointerException if the generator is null
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5406
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5407
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5408
    public static void parallelSetAll(long[] array, IntToLongFunction generator) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5409
        Objects.requireNonNull(generator);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5410
        IntStream.range(0, array.length).parallel().forEach(i -> { array[i] = generator.applyAsLong(i); });
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5411
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5412
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5413
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5414
     * Set all elements of the specified array, using the provided
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5415
     * generator function to compute each element.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5416
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5417
     * <p>If the generator function throws an exception, it is relayed to
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5418
     * the caller and the array is left in an indeterminate state.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5419
     *
29223
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5420
     * @apiNote
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5421
     * Setting a subrange of an array, using a generator function to compute
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5422
     * each element, can be written as follows:
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5423
     * <pre>{@code
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5424
     * IntStream.range(startInclusive, endExclusive)
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5425
     *          .forEach(i -> array[i] = generator.applyAsDouble(i));
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5426
     * }</pre>
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5427
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5428
     * @param array array to be initialized
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5429
     * @param generator a function accepting an index and producing the desired
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5430
     *        value for that position
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5431
     * @throws NullPointerException if the generator is null
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5432
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5433
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5434
    public static void setAll(double[] array, IntToDoubleFunction generator) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5435
        Objects.requireNonNull(generator);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5436
        for (int i = 0; i < array.length; i++)
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5437
            array[i] = generator.applyAsDouble(i);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5438
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5439
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5440
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5441
     * Set all elements of the specified array, in parallel, using the
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5442
     * provided generator function to compute each element.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5443
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5444
     * <p>If the generator function throws an exception, an unchecked exception
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5445
     * is thrown from {@code parallelSetAll} and the array is left in an
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5446
     * indeterminate state.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5447
     *
29223
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5448
     * @apiNote
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5449
     * Setting a subrange of an array, in parallel, using a generator function
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5450
     * to compute each element, can be written as follows:
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5451
     * <pre>{@code
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5452
     * IntStream.range(startInclusive, endExclusive)
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5453
     *          .parallel()
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5454
     *          .forEach(i -> array[i] = generator.applyAsDouble(i));
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5455
     * }</pre>
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5456
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5457
     * @param array array to be initialized
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5458
     * @param generator a function accepting an index and producing the desired
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5459
     *        value for that position
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5460
     * @throws NullPointerException if the generator is null
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5461
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5462
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5463
    public static void parallelSetAll(double[] array, IntToDoubleFunction generator) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5464
        Objects.requireNonNull(generator);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5465
        IntStream.range(0, array.length).parallel().forEach(i -> { array[i] = generator.applyAsDouble(i); });
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5466
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5467
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5468
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5469
     * Returns a {@link Spliterator} covering all of the specified array.
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5470
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5471
     * <p>The spliterator reports {@link Spliterator#SIZED},
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5472
     * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5473
     * {@link Spliterator#IMMUTABLE}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5474
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5475
     * @param <T> type of elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5476
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5477
     * @return a spliterator for the array elements
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5478
     * @since 1.8
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5479
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5480
    public static <T> Spliterator<T> spliterator(T[] array) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5481
        return Spliterators.spliterator(array,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5482
                                        Spliterator.ORDERED | Spliterator.IMMUTABLE);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5483
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5484
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5485
    /**
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5486
     * Returns a {@link Spliterator} covering the specified range of the
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5487
     * specified array.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5488
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5489
     * <p>The spliterator reports {@link Spliterator#SIZED},
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5490
     * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5491
     * {@link Spliterator#IMMUTABLE}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5492
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5493
     * @param <T> type of elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5494
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5495
     * @param startInclusive the first index to cover, inclusive
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5496
     * @param endExclusive index immediately past the last index to cover
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5497
     * @return a spliterator for the array elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5498
     * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5499
     *         negative, {@code endExclusive} is less than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5500
     *         {@code startInclusive}, or {@code endExclusive} is greater than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5501
     *         the array size
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5502
     * @since 1.8
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5503
     */
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5504
    public static <T> Spliterator<T> spliterator(T[] array, int startInclusive, int endExclusive) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5505
        return Spliterators.spliterator(array, startInclusive, endExclusive,
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5506
                                        Spliterator.ORDERED | Spliterator.IMMUTABLE);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5507
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5508
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5509
    /**
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5510
     * Returns a {@link Spliterator.OfInt} covering all of the specified array.
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5511
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5512
     * <p>The spliterator reports {@link Spliterator#SIZED},
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5513
     * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5514
     * {@link Spliterator#IMMUTABLE}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5515
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5516
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5517
     * @return a spliterator for the array elements
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5518
     * @since 1.8
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5519
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5520
    public static Spliterator.OfInt spliterator(int[] array) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5521
        return Spliterators.spliterator(array,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5522
                                        Spliterator.ORDERED | Spliterator.IMMUTABLE);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5523
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5524
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5525
    /**
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5526
     * Returns a {@link Spliterator.OfInt} covering the specified range of the
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5527
     * specified array.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5528
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5529
     * <p>The spliterator reports {@link Spliterator#SIZED},
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5530
     * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5531
     * {@link Spliterator#IMMUTABLE}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5532
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5533
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5534
     * @param startInclusive the first index to cover, inclusive
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5535
     * @param endExclusive index immediately past the last index to cover
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5536
     * @return a spliterator for the array elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5537
     * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5538
     *         negative, {@code endExclusive} is less than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5539
     *         {@code startInclusive}, or {@code endExclusive} is greater than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5540
     *         the array size
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5541
     * @since 1.8
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5542
     */
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5543
    public static Spliterator.OfInt spliterator(int[] array, int startInclusive, int endExclusive) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5544
        return Spliterators.spliterator(array, startInclusive, endExclusive,
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5545
                                        Spliterator.ORDERED | Spliterator.IMMUTABLE);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5546
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5547
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5548
    /**
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5549
     * Returns a {@link Spliterator.OfLong} covering all of the specified array.
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5550
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5551
     * <p>The spliterator reports {@link Spliterator#SIZED},
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5552
     * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5553
     * {@link Spliterator#IMMUTABLE}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5554
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5555
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5556
     * @return the spliterator for the array elements
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5557
     * @since 1.8
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5558
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5559
    public static Spliterator.OfLong spliterator(long[] array) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5560
        return Spliterators.spliterator(array,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5561
                                        Spliterator.ORDERED | Spliterator.IMMUTABLE);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5562
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5563
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5564
    /**
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5565
     * Returns a {@link Spliterator.OfLong} covering the specified range of the
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5566
     * specified array.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5567
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5568
     * <p>The spliterator reports {@link Spliterator#SIZED},
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5569
     * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5570
     * {@link Spliterator#IMMUTABLE}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5571
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5572
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5573
     * @param startInclusive the first index to cover, inclusive
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5574
     * @param endExclusive index immediately past the last index to cover
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5575
     * @return a spliterator for the array elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5576
     * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5577
     *         negative, {@code endExclusive} is less than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5578
     *         {@code startInclusive}, or {@code endExclusive} is greater than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5579
     *         the array size
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5580
     * @since 1.8
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5581
     */
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5582
    public static Spliterator.OfLong spliterator(long[] array, int startInclusive, int endExclusive) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5583
        return Spliterators.spliterator(array, startInclusive, endExclusive,
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5584
                                        Spliterator.ORDERED | Spliterator.IMMUTABLE);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5585
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5586
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5587
    /**
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5588
     * Returns a {@link Spliterator.OfDouble} covering all of the specified
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5589
     * array.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5590
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5591
     * <p>The spliterator reports {@link Spliterator#SIZED},
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5592
     * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5593
     * {@link Spliterator#IMMUTABLE}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5594
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5595
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5596
     * @return a spliterator for the array elements
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5597
     * @since 1.8
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5598
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5599
    public static Spliterator.OfDouble spliterator(double[] array) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5600
        return Spliterators.spliterator(array,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5601
                                        Spliterator.ORDERED | Spliterator.IMMUTABLE);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5602
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5603
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5604
    /**
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5605
     * Returns a {@link Spliterator.OfDouble} covering the specified range of
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5606
     * the specified array.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5607
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5608
     * <p>The spliterator reports {@link Spliterator#SIZED},
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5609
     * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5610
     * {@link Spliterator#IMMUTABLE}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5611
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5612
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5613
     * @param startInclusive the first index to cover, inclusive
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5614
     * @param endExclusive index immediately past the last index to cover
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5615
     * @return a spliterator for the array elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5616
     * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5617
     *         negative, {@code endExclusive} is less than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5618
     *         {@code startInclusive}, or {@code endExclusive} is greater than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5619
     *         the array size
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5620
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5621
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5622
    public static Spliterator.OfDouble spliterator(double[] array, int startInclusive, int endExclusive) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5623
        return Spliterators.spliterator(array, startInclusive, endExclusive,
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5624
                                        Spliterator.ORDERED | Spliterator.IMMUTABLE);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5625
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5626
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5627
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5628
     * Returns a sequential {@link Stream} with the specified array as its
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5629
     * source.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5630
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5631
     * @param <T> The type of the array elements
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5632
     * @param array The array, assumed to be unmodified during use
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5633
     * @return a {@code Stream} for the array
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5634
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5635
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5636
    public static <T> Stream<T> stream(T[] array) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5637
        return stream(array, 0, array.length);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5638
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5639
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5640
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5641
     * Returns a sequential {@link Stream} with the specified range of the
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5642
     * specified array as its source.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5643
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5644
     * @param <T> the type of the array elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5645
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5646
     * @param startInclusive the first index to cover, inclusive
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5647
     * @param endExclusive index immediately past the last index to cover
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5648
     * @return a {@code Stream} for the array range
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5649
     * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5650
     *         negative, {@code endExclusive} is less than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5651
     *         {@code startInclusive}, or {@code endExclusive} is greater than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5652
     *         the array size
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5653
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5654
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5655
    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
  5656
        return StreamSupport.stream(spliterator(array, startInclusive, endExclusive), false);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5657
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5658
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5659
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5660
     * Returns a sequential {@link IntStream} with the specified array as its
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5661
     * source.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5662
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5663
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5664
     * @return an {@code IntStream} for the array
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5665
     * @since 1.8
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5666
     */
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5667
    public static IntStream stream(int[] array) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5668
        return stream(array, 0, array.length);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5669
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5670
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5671
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5672
     * Returns a sequential {@link IntStream} with the specified range of the
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5673
     * specified array as its source.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5674
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5675
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5676
     * @param startInclusive the first index to cover, inclusive
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5677
     * @param endExclusive index immediately past the last index to cover
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5678
     * @return an {@code IntStream} for the array range
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5679
     * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5680
     *         negative, {@code endExclusive} is less than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5681
     *         {@code startInclusive}, or {@code endExclusive} is greater than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5682
     *         the array size
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5683
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5684
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5685
    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
  5686
        return StreamSupport.intStream(spliterator(array, startInclusive, endExclusive), false);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5687
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5688
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5689
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5690
     * Returns a sequential {@link LongStream} with the specified array as its
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5691
     * source.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5692
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5693
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5694
     * @return a {@code LongStream} for the array
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5695
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5696
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5697
    public static LongStream stream(long[] array) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5698
        return stream(array, 0, array.length);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5699
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5700
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5701
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5702
     * Returns a sequential {@link LongStream} with the specified range of the
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5703
     * specified array as its source.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5704
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5705
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5706
     * @param startInclusive the first index to cover, inclusive
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5707
     * @param endExclusive index immediately past the last index to cover
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5708
     * @return a {@code LongStream} for the array range
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5709
     * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5710
     *         negative, {@code endExclusive} is less than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5711
     *         {@code startInclusive}, or {@code endExclusive} is greater than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5712
     *         the array size
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5713
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5714
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5715
    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
  5716
        return StreamSupport.longStream(spliterator(array, startInclusive, endExclusive), false);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5717
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5718
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5719
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5720
     * Returns a sequential {@link DoubleStream} with the specified array as its
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5721
     * source.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5722
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5723
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5724
     * @return a {@code DoubleStream} for the array
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5725
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5726
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5727
    public static DoubleStream stream(double[] array) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5728
        return stream(array, 0, array.length);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5729
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5730
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5731
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5732
     * Returns a sequential {@link DoubleStream} with the specified range of the
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5733
     * specified array as its source.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5734
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5735
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5736
     * @param startInclusive the first index to cover, inclusive
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5737
     * @param endExclusive index immediately past the last index to cover
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5738
     * @return a {@code DoubleStream} for the array range
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5739
     * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5740
     *         negative, {@code endExclusive} is less than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5741
     *         {@code startInclusive}, or {@code endExclusive} is greater than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5742
     *         the array size
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5743
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5744
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5745
    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
  5746
        return StreamSupport.doubleStream(spliterator(array, startInclusive, endExclusive), false);
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5747
    }
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5748
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5749
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5750
    // Comparison methods
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5751
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5752
    // Compare boolean
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5753
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5754
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5755
     * Compares two {@code boolean} arrays lexicographically.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5756
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5757
     * <p>If the two arrays share a common prefix then the lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5758
     * comparison is the result of comparing two elements, as if by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5759
     * {@link Boolean#compare(boolean, boolean)}, at an index within the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5760
     * respective arrays that is the prefix length.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5761
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5762
     * comparison is the result of comparing the two array lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5763
     * (See {@link #mismatch(boolean[], boolean[])} for the definition of a
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5764
     * common and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5765
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5766
     * <p>A {@code null} array reference is considered lexicographically less
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5767
     * than a non-{@code null} array reference.  Two {@code null} array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5768
     * references are considered equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5769
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5770
     * <p>The comparison is consistent with {@link #equals(boolean[], boolean[]) equals},
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5771
     * more specifically the following holds for arrays {@code a} and {@code b}:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5772
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5773
     *     Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5774
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5775
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5776
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5777
     * <p>This method behaves as if (for non-{@code null} array references):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5778
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5779
     *     int i = Arrays.mismatch(a, b);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5780
     *     if (i >= 0 && i < Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5781
     *         return Boolean.compare(a[i], b[i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5782
     *     return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5783
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5784
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5785
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5786
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5787
     * @return the value {@code 0} if the first and second array are equal and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5788
     *         contain the same elements in the same order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5789
     *         a value less than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5790
     *         lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5791
     *         a value greater than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5792
     *         lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5793
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5794
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5795
    public static int compare(boolean[] a, boolean[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5796
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5797
            return 0;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5798
        if (a == null || b == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5799
            return a == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5800
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5801
        int i = ArraysSupport.mismatch(a, b,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5802
                                       Math.min(a.length, b.length));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5803
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5804
            return Boolean.compare(a[i], b[i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5805
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5806
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5807
        return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5808
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5809
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5810
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5811
     * Compares two {@code boolean} arrays lexicographically over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5812
     * ranges.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5813
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5814
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5815
     * then the lexicographic comparison is the result of comparing two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5816
     * elements, as if by {@link Boolean#compare(boolean, boolean)}, at a
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5817
     * relative index within the respective arrays that is the length of the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5818
     * prefix.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5819
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5820
     * comparison is the result of comparing the two range lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5821
     * (See {@link #mismatch(boolean[], int, int, boolean[], int, int)} for the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5822
     * definition of a common and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5823
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5824
     * <p>The comparison is consistent with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5825
     * {@link #equals(boolean[], int, int, boolean[], int, int) equals}, more
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5826
     * specifically the following holds for arrays {@code a} and {@code b} with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5827
     * specified ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5828
     * [{@code bFromIndex}, {@code btoIndex}) respectively:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5829
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5830
     *     Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5831
     *         (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5832
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5833
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5834
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5835
     * <p>This method behaves as if:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5836
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5837
     *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5838
     *                             b, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5839
     *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5840
     *         return Boolean.compare(a[aFromIndex + i], b[bFromIndex + i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5841
     *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5842
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5843
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5844
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5845
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5846
     *                   first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5847
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5848
     *                 first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5849
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5850
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5851
     *                   second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5852
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5853
     *                 second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5854
     * @return the value {@code 0} if, over the specified ranges, the first and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5855
     *         second array are equal and contain the same elements in the same
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5856
     *         order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5857
     *         a value less than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5858
     *         first array is lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5859
     *         a value greater than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5860
     *         first array is lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5861
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5862
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5863
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5864
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5865
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5866
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5867
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5868
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5869
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5870
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5871
    public static int compare(boolean[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5872
                              boolean[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5873
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5874
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5875
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5876
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5877
        int bLength = bToIndex - bFromIndex;
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5878
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5879
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5880
                                       Math.min(aLength, bLength));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5881
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5882
            return Boolean.compare(a[aFromIndex + i], b[bFromIndex + i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5883
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5884
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5885
        return aLength - bLength;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5886
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5887
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5888
    // Compare byte
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5889
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5890
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5891
     * Compares two {@code byte} arrays lexicographically.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5892
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5893
     * <p>If the two arrays share a common prefix then the lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5894
     * comparison is the result of comparing two elements, as if by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5895
     * {@link Byte#compare(byte, byte)}, at an index within the respective
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5896
     * arrays that is the prefix length.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5897
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5898
     * comparison is the result of comparing the two array lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5899
     * (See {@link #mismatch(byte[], byte[])} for the definition of a common and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5900
     * proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5901
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5902
     * <p>A {@code null} array reference is considered lexicographically less
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5903
     * than a non-{@code null} array reference.  Two {@code null} array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5904
     * references are considered equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5905
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5906
     * <p>The comparison is consistent with {@link #equals(byte[], byte[]) equals},
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5907
     * more specifically the following holds for arrays {@code a} and {@code b}:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5908
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5909
     *     Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5910
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5911
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5912
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5913
     * <p>This method behaves as if (for non-{@code null} array references):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5914
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5915
     *     int i = Arrays.mismatch(a, b);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5916
     *     if (i >= 0 && i < Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5917
     *         return Byte.compare(a[i], b[i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5918
     *     return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5919
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5920
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5921
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5922
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5923
     * @return the value {@code 0} if the first and second array are equal and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5924
     *         contain the same elements in the same order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5925
     *         a value less than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5926
     *         lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5927
     *         a value greater than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5928
     *         lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5929
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5930
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5931
    public static int compare(byte[] a, byte[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5932
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5933
            return 0;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5934
        if (a == null || b == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5935
            return a == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5936
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5937
        int i = ArraysSupport.mismatch(a, b,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5938
                                       Math.min(a.length, b.length));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5939
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5940
            return Byte.compare(a[i], b[i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5941
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5942
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5943
        return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5944
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5945
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5946
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5947
     * Compares two {@code byte} arrays lexicographically over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5948
     * ranges.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5949
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5950
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5951
     * then the lexicographic comparison is the result of comparing two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5952
     * elements, as if by {@link Byte#compare(byte, byte)}, at a relative index
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5953
     * within the respective arrays that is the length of the prefix.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5954
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5955
     * comparison is the result of comparing the two range lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5956
     * (See {@link #mismatch(byte[], int, int, byte[], int, int)} for the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5957
     * definition of a common and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5958
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5959
     * <p>The comparison is consistent with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5960
     * {@link #equals(byte[], int, int, byte[], int, int) equals}, more
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5961
     * specifically the following holds for arrays {@code a} and {@code b} with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5962
     * specified ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5963
     * [{@code bFromIndex}, {@code btoIndex}) respectively:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5964
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5965
     *     Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5966
     *         (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5967
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5968
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5969
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5970
     * <p>This method behaves as if:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5971
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5972
     *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5973
     *                             b, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5974
     *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5975
     *         return Byte.compare(a[aFromIndex + i], b[bFromIndex + i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5976
     *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5977
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5978
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5979
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5980
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5981
     *                   first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5982
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5983
     *                 first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5984
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5985
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5986
     *                   second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5987
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5988
     *                 second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5989
     * @return the value {@code 0} if, over the specified ranges, the first and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5990
     *         second array are equal and contain the same elements in the same
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5991
     *         order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5992
     *         a value less than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5993
     *         first array is lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5994
     *         a value greater than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5995
     *         first array is lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5996
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5997
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5998
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5999
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6000
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6001
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6002
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6003
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6004
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6005
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6006
    public static int compare(byte[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6007
                              byte[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6008
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6009
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6010
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6011
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6012
        int bLength = bToIndex - bFromIndex;
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6013
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6014
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6015
                                       Math.min(aLength, bLength));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6016
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6017
            return Byte.compare(a[aFromIndex + i], b[bFromIndex + i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6018
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6019
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6020
        return aLength - bLength;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6021
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6022
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6023
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6024
     * Compares two {@code byte} arrays lexicographically, numerically treating
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6025
     * elements as unsigned.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6026
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6027
     * <p>If the two arrays share a common prefix then the lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6028
     * comparison is the result of comparing two elements, as if by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6029
     * {@link Byte#compareUnsigned(byte, byte)}, at an index within the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6030
     * respective arrays that is the prefix length.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6031
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6032
     * comparison is the result of comparing the two array lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6033
     * (See {@link #mismatch(byte[], byte[])} for the definition of a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6034
     * and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6035
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6036
     * <p>A {@code null} array reference is considered lexicographically less
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6037
     * than a non-{@code null} array reference.  Two {@code null} array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6038
     * references are considered equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6039
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6040
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6041
     * <p>This method behaves as if (for non-{@code null} array references):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6042
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6043
     *     int i = Arrays.mismatch(a, b);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6044
     *     if (i >= 0 && i < Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6045
     *         return Byte.compareUnsigned(a[i], b[i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6046
     *     return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6047
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6048
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6049
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6050
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6051
     * @return the value {@code 0} if the first and second array are
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6052
     *         equal and contain the same elements in the same order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6053
     *         a value less than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6054
     *         lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6055
     *         a value greater than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6056
     *         lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6057
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6058
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6059
    public static int compareUnsigned(byte[] a, byte[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6060
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6061
            return 0;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6062
        if (a == null || b == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6063
            return a == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6064
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6065
        int i = ArraysSupport.mismatch(a, b,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6066
                                       Math.min(a.length, b.length));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6067
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6068
            return Byte.compareUnsigned(a[i], b[i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6069
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6070
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6071
        return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6072
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6073
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6074
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6075
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6076
     * Compares two {@code byte} arrays lexicographically over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6077
     * ranges, numerically treating elements as unsigned.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6078
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6079
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6080
     * then the lexicographic comparison is the result of comparing two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6081
     * elements, as if by {@link Byte#compareUnsigned(byte, byte)}, at a
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6082
     * relative index within the respective arrays that is the length of the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6083
     * prefix.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6084
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6085
     * comparison is the result of comparing the two range lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6086
     * (See {@link #mismatch(byte[], int, int, byte[], int, int)} for the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6087
     * definition of a common and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6088
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6089
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6090
     * <p>This method behaves as if:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6091
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6092
     *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6093
     *                             b, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6094
     *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6095
     *         return Byte.compareUnsigned(a[aFromIndex + i], b[bFromIndex + i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6096
     *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6097
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6098
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6099
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6100
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6101
     *                   first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6102
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6103
     *                 first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6104
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6105
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6106
     *                   second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6107
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6108
     *                 second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6109
     * @return the value {@code 0} if, over the specified ranges, the first and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6110
     *         second array are equal and contain the same elements in the same
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6111
     *         order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6112
     *         a value less than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6113
     *         first array is lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6114
     *         a value greater than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6115
     *         first array is lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6116
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6117
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6118
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6119
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6120
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6121
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6122
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6123
     *         if either array is null
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6124
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6125
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6126
    public static int compareUnsigned(byte[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6127
                                      byte[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6128
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6129
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6130
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6131
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6132
        int bLength = bToIndex - bFromIndex;
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6133
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6134
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6135
                                       Math.min(aLength, bLength));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6136
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6137
            return Byte.compareUnsigned(a[aFromIndex + i], b[bFromIndex + i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6138
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6139
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6140
        return aLength - bLength;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6141
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6142
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6143
    // Compare short
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6144
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6145
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6146
     * Compares two {@code short} arrays lexicographically.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6147
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6148
     * <p>If the two arrays share a common prefix then the lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6149
     * comparison is the result of comparing two elements, as if by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6150
     * {@link Short#compare(short, short)}, at an index within the respective
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6151
     * arrays that is the prefix length.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6152
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6153
     * comparison is the result of comparing the two array lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6154
     * (See {@link #mismatch(short[], short[])} for the definition of a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6155
     * and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6156
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6157
     * <p>A {@code null} array reference is considered lexicographically less
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6158
     * than a non-{@code null} array reference.  Two {@code null} array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6159
     * references are considered equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6160
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6161
     * <p>The comparison is consistent with {@link #equals(short[], short[]) equals},
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6162
     * more specifically the following holds for arrays {@code a} and {@code b}:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6163
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6164
     *     Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6165
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6166
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6167
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6168
     * <p>This method behaves as if (for non-{@code null} array references):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6169
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6170
     *     int i = Arrays.mismatch(a, b);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6171
     *     if (i >= 0 && i < Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6172
     *         return Short.compare(a[i], b[i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6173
     *     return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6174
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6175
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6176
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6177
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6178
     * @return the value {@code 0} if the first and second array are equal and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6179
     *         contain the same elements in the same order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6180
     *         a value less than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6181
     *         lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6182
     *         a value greater than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6183
     *         lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6184
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6185
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6186
    public static int compare(short[] a, short[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6187
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6188
            return 0;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6189
        if (a == null || b == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6190
            return a == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6191
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6192
        int i = ArraysSupport.mismatch(a, b,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6193
                                       Math.min(a.length, b.length));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6194
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6195
            return Short.compare(a[i], b[i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6196
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6197
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6198
        return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6199
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6200
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6201
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6202
     * Compares two {@code short} arrays lexicographically over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6203
     * ranges.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6204
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6205
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6206
     * then the lexicographic comparison is the result of comparing two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6207
     * elements, as if by {@link Short#compare(short, short)}, at a relative
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6208
     * index within the respective arrays that is the length of the prefix.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6209
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6210
     * comparison is the result of comparing the two range lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6211
     * (See {@link #mismatch(short[], int, int, short[], int, int)} for the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6212
     * definition of a common and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6213
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6214
     * <p>The comparison is consistent with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6215
     * {@link #equals(short[], int, int, short[], int, int) equals}, more
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6216
     * specifically the following holds for arrays {@code a} and {@code b} with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6217
     * specified ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6218
     * [{@code bFromIndex}, {@code btoIndex}) respectively:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6219
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6220
     *     Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6221
     *         (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6222
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6223
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6224
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6225
     * <p>This method behaves as if:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6226
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6227
     *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6228
     *                             b, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6229
     *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6230
     *         return Short.compare(a[aFromIndex + i], b[bFromIndex + i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6231
     *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6232
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6233
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6234
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6235
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6236
     *                   first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6237
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6238
     *                 first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6239
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6240
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6241
     *                   second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6242
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6243
     *                 second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6244
     * @return the value {@code 0} if, over the specified ranges, the first and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6245
     *         second array are equal and contain the same elements in the same
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6246
     *         order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6247
     *         a value less than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6248
     *         first array is lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6249
     *         a value greater than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6250
     *         first array is lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6251
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6252
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6253
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6254
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6255
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6256
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6257
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6258
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6259
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6260
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6261
    public static int compare(short[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6262
                              short[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6263
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6264
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6265
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6266
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6267
        int bLength = bToIndex - bFromIndex;
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6268
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6269
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6270
                                       Math.min(aLength, bLength));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6271
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6272
            return Short.compare(a[aFromIndex + i], b[bFromIndex + i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6273
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6274
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6275
        return aLength - bLength;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6276
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6277
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6278
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6279
     * Compares two {@code short} arrays lexicographically, numerically treating
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6280
     * elements as unsigned.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6281
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6282
     * <p>If the two arrays share a common prefix then the lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6283
     * comparison is the result of comparing two elements, as if by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6284
     * {@link Short#compareUnsigned(short, short)}, at an index within the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6285
     * respective arrays that is the prefix length.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6286
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6287
     * comparison is the result of comparing the two array lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6288
     * (See {@link #mismatch(short[], short[])} for the definition of a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6289
     * and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6290
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6291
     * <p>A {@code null} array reference is considered lexicographically less
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6292
     * than a non-{@code null} array reference.  Two {@code null} array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6293
     * references are considered equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6294
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6295
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6296
     * <p>This method behaves as if (for non-{@code null} array references):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6297
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6298
     *     int i = Arrays.mismatch(a, b);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6299
     *     if (i >= 0 && i < Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6300
     *         return Short.compareUnsigned(a[i], b[i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6301
     *     return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6302
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6303
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6304
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6305
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6306
     * @return the value {@code 0} if the first and second array are
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6307
     *         equal and contain the same elements in the same order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6308
     *         a value less than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6309
     *         lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6310
     *         a value greater than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6311
     *         lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6312
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6313
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6314
    public static int compareUnsigned(short[] a, short[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6315
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6316
            return 0;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6317
        if (a == null || b == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6318
            return a == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6319
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6320
        int i = ArraysSupport.mismatch(a, b,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6321
                                       Math.min(a.length, b.length));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6322
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6323
            return Short.compareUnsigned(a[i], b[i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6324
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6325
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6326
        return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6327
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6328
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6329
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6330
     * Compares two {@code short} arrays lexicographically over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6331
     * ranges, numerically treating elements as unsigned.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6332
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6333
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6334
     * then the lexicographic comparison is the result of comparing two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6335
     * elements, as if by {@link Short#compareUnsigned(short, short)}, at a
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6336
     * relative index within the respective arrays that is the length of the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6337
     * prefix.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6338
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6339
     * comparison is the result of comparing the two range lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6340
     * (See {@link #mismatch(short[], int, int, short[], int, int)} for the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6341
     * definition of a common and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6342
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6343
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6344
     * <p>This method behaves as if:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6345
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6346
     *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6347
     *                             b, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6348
     *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6349
     *         return Short.compareUnsigned(a[aFromIndex + i], b[bFromIndex + i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6350
     *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6351
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6352
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6353
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6354
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6355
     *                   first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6356
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6357
     *                 first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6358
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6359
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6360
     *                   second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6361
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6362
     *                 second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6363
     * @return the value {@code 0} if, over the specified ranges, the first and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6364
     *         second array are equal and contain the same elements in the same
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6365
     *         order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6366
     *         a value less than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6367
     *         first array is lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6368
     *         a value greater than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6369
     *         first array is lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6370
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6371
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6372
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6373
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6374
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6375
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6376
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6377
     *         if either array is null
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6378
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6379
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6380
    public static int compareUnsigned(short[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6381
                                      short[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6382
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6383
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6384
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6385
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6386
        int bLength = bToIndex - bFromIndex;
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6387
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6388
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6389
                                       Math.min(aLength, bLength));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6390
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6391
            return Short.compareUnsigned(a[aFromIndex + i], b[bFromIndex + i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6392
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6393
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6394
        return aLength - bLength;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6395
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6396
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6397
    // Compare char
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6398
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6399
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6400
     * Compares two {@code char} arrays lexicographically.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6401
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6402
     * <p>If the two arrays share a common prefix then the lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6403
     * comparison is the result of comparing two elements, as if by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6404
     * {@link Character#compare(char, char)}, at an index within the respective
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6405
     * arrays that is the prefix length.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6406
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6407
     * comparison is the result of comparing the two array lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6408
     * (See {@link #mismatch(char[], char[])} for the definition of a common and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6409
     * proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6410
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6411
     * <p>A {@code null} array reference is considered lexicographically less
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6412
     * than a non-{@code null} array reference.  Two {@code null} array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6413
     * references are considered equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6414
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6415
     * <p>The comparison is consistent with {@link #equals(char[], char[]) equals},
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6416
     * more specifically the following holds for arrays {@code a} and {@code b}:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6417
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6418
     *     Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6419
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6420
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6421
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6422
     * <p>This method behaves as if (for non-{@code null} array references):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6423
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6424
     *     int i = Arrays.mismatch(a, b);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6425
     *     if (i >= 0 && i < Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6426
     *         return Character.compare(a[i], b[i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6427
     *     return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6428
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6429
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6430
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6431
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6432
     * @return the value {@code 0} if the first and second array are equal and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6433
     *         contain the same elements in the same order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6434
     *         a value less than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6435
     *         lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6436
     *         a value greater than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6437
     *         lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6438
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6439
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6440
    public static int compare(char[] a, char[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6441
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6442
            return 0;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6443
        if (a == null || b == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6444
            return a == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6445
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6446
        int i = ArraysSupport.mismatch(a, b,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6447
                                       Math.min(a.length, b.length));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6448
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6449
            return Character.compare(a[i], b[i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6450
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6451
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6452
        return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6453
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6454
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6455
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6456
     * Compares two {@code char} arrays lexicographically over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6457
     * ranges.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6458
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6459
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6460
     * then the lexicographic comparison is the result of comparing two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6461
     * elements, as if by {@link Character#compare(char, char)}, at a relative
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6462
     * index within the respective arrays that is the length of the prefix.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6463
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6464
     * comparison is the result of comparing the two range lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6465
     * (See {@link #mismatch(char[], int, int, char[], int, int)} for the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6466
     * definition of a common and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6467
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6468
     * <p>The comparison is consistent with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6469
     * {@link #equals(char[], int, int, char[], int, int) equals}, more
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6470
     * specifically the following holds for arrays {@code a} and {@code b} with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6471
     * specified ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6472
     * [{@code bFromIndex}, {@code btoIndex}) respectively:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6473
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6474
     *     Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6475
     *         (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6476
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6477
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6478
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6479
     * <p>This method behaves as if:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6480
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6481
     *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6482
     *                             b, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6483
     *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6484
     *         return Character.compare(a[aFromIndex + i], b[bFromIndex + i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6485
     *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6486
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6487
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6488
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6489
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6490
     *                   first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6491
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6492
     *                 first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6493
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6494
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6495
     *                   second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6496
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6497
     *                 second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6498
     * @return the value {@code 0} if, over the specified ranges, the first and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6499
     *         second array are equal and contain the same elements in the same
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6500
     *         order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6501
     *         a value less than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6502
     *         first array is lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6503
     *         a value greater than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6504
     *         first array is lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6505
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6506
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6507
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6508
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6509
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6510
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6511
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6512
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6513
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6514
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6515
    public static int compare(char[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6516
                              char[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6517
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6518
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6520
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6521
        int bLength = bToIndex - bFromIndex;
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6522
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6523
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6524
                                       Math.min(aLength, bLength));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6525
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6526
            return Character.compare(a[aFromIndex + i], b[bFromIndex + i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6527
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6528
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6529
        return aLength - bLength;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6530
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6531
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6532
    // Compare int
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6533
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6534
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6535
     * Compares two {@code int} arrays lexicographically.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6536
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6537
     * <p>If the two arrays share a common prefix then the lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6538
     * comparison is the result of comparing two elements, as if by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6539
     * {@link Integer#compare(int, int)}, at an index within the respective
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6540
     * arrays that is the prefix length.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6541
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6542
     * comparison is the result of comparing the two array lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6543
     * (See {@link #mismatch(int[], int[])} for the definition of a common and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6544
     * proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6545
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6546
     * <p>A {@code null} array reference is considered lexicographically less
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6547
     * than a non-{@code null} array reference.  Two {@code null} array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6548
     * references are considered equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6549
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6550
     * <p>The comparison is consistent with {@link #equals(int[], int[]) equals},
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6551
     * more specifically the following holds for arrays {@code a} and {@code b}:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6552
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6553
     *     Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6554
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6555
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6556
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6557
     * <p>This method behaves as if (for non-{@code null} array references):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6558
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6559
     *     int i = Arrays.mismatch(a, b);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6560
     *     if (i >= 0 && i < Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6561
     *         return Integer.compare(a[i], b[i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6562
     *     return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6563
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6564
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6565
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6566
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6567
     * @return the value {@code 0} if the first and second array are equal and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6568
     *         contain the same elements in the same order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6569
     *         a value less than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6570
     *         lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6571
     *         a value greater than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6572
     *         lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6573
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6574
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6575
    public static int compare(int[] a, int[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6576
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6577
            return 0;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6578
        if (a == null || b == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6579
            return a == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6580
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6581
        int i = ArraysSupport.mismatch(a, b,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6582
                                       Math.min(a.length, b.length));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6583
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6584
            return Integer.compare(a[i], b[i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6585
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6586
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6587
        return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6588
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6589
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6590
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6591
     * Compares two {@code int} arrays lexicographically over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6592
     * ranges.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6593
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6594
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6595
     * then the lexicographic comparison is the result of comparing two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6596
     * elements, as if by {@link Integer#compare(int, int)}, at a relative index
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6597
     * within the respective arrays that is the length of the prefix.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6598
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6599
     * comparison is the result of comparing the two range lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6600
     * (See {@link #mismatch(int[], int, int, int[], int, int)} for the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6601
     * definition of a common and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6602
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6603
     * <p>The comparison is consistent with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6604
     * {@link #equals(int[], int, int, int[], int, int) equals}, more
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6605
     * specifically the following holds for arrays {@code a} and {@code b} with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6606
     * specified ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6607
     * [{@code bFromIndex}, {@code btoIndex}) respectively:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6608
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6609
     *     Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6610
     *         (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6611
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6612
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6613
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6614
     * <p>This method behaves as if:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6615
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6616
     *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6617
     *                             b, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6618
     *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6619
     *         return Integer.compare(a[aFromIndex + i], b[bFromIndex + i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6620
     *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6621
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6622
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6623
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6624
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6625
     *                   first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6626
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6627
     *                 first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6628
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6629
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6630
     *                   second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6631
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6632
     *                 second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6633
     * @return the value {@code 0} if, over the specified ranges, the first and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6634
     *         second array are equal and contain the same elements in the same
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6635
     *         order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6636
     *         a value less than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6637
     *         first array is lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6638
     *         a value greater than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6639
     *         first array is lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6640
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6641
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6642
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6643
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6644
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6645
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6646
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6647
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6648
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6649
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6650
    public static int compare(int[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6651
                              int[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6652
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6653
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6654
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6655
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6656
        int bLength = bToIndex - bFromIndex;
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6657
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6658
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6659
                                       Math.min(aLength, bLength));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6660
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6661
            return Integer.compare(a[aFromIndex + i], b[bFromIndex + i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6662
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6663
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6664
        return aLength - bLength;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6665
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6666
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6667
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6668
     * Compares two {@code int} arrays lexicographically, numerically treating
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6669
     * elements as unsigned.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6670
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6671
     * <p>If the two arrays share a common prefix then the lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6672
     * comparison is the result of comparing two elements, as if by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6673
     * {@link Integer#compareUnsigned(int, int)}, at an index within the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6674
     * respective arrays that is the prefix length.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6675
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6676
     * comparison is the result of comparing the two array lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6677
     * (See {@link #mismatch(int[], int[])} for the definition of a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6678
     * and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6679
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6680
     * <p>A {@code null} array reference is considered lexicographically less
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6681
     * than a non-{@code null} array reference.  Two {@code null} array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6682
     * references are considered equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6683
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6684
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6685
     * <p>This method behaves as if (for non-{@code null} array references):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6686
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6687
     *     int i = Arrays.mismatch(a, b);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6688
     *     if (i >= 0 && i < Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6689
     *         return Integer.compareUnsigned(a[i], b[i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6690
     *     return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6691
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6692
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6693
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6694
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6695
     * @return the value {@code 0} if the first and second array are
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6696
     *         equal and contain the same elements in the same order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6697
     *         a value less than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6698
     *         lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6699
     *         a value greater than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6700
     *         lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6701
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6702
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6703
    public static int compareUnsigned(int[] a, int[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6704
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6705
            return 0;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6706
        if (a == null || b == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6707
            return a == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6708
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6709
        int i = ArraysSupport.mismatch(a, b,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6710
                                       Math.min(a.length, b.length));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6711
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6712
            return Integer.compareUnsigned(a[i], b[i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6713
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6714
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6715
        return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6716
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6717
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6718
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6719
     * Compares two {@code int} arrays lexicographically over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6720
     * ranges, numerically treating elements as unsigned.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6721
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6722
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6723
     * then the lexicographic comparison is the result of comparing two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6724
     * elements, as if by {@link Integer#compareUnsigned(int, int)}, at a
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6725
     * relative index within the respective arrays that is the length of the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6726
     * prefix.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6727
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6728
     * comparison is the result of comparing the two range lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6729
     * (See {@link #mismatch(int[], int, int, int[], int, int)} for the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6730
     * definition of a common and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6731
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6732
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6733
     * <p>This method behaves as if:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6734
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6735
     *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6736
     *                             b, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6737
     *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6738
     *         return Integer.compareUnsigned(a[aFromIndex + i], b[bFromIndex + i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6739
     *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6740
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6741
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6742
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6743
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6744
     *                   first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6745
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6746
     *                 first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6747
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6748
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6749
     *                   second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6750
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6751
     *                 second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6752
     * @return the value {@code 0} if, over the specified ranges, the first and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6753
     *         second array are equal and contain the same elements in the same
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6754
     *         order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6755
     *         a value less than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6756
     *         first array is lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6757
     *         a value greater than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6758
     *         first array is lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6759
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6760
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6761
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6762
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6763
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6764
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6765
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6766
     *         if either array is null
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6767
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6768
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6769
    public static int compareUnsigned(int[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6770
                                      int[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6771
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6772
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6773
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6774
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6775
        int bLength = bToIndex - bFromIndex;
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6776
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6777
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6778
                                       Math.min(aLength, bLength));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6779
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6780
            return Integer.compareUnsigned(a[aFromIndex + i], b[bFromIndex + i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6781
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6782
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6783
        return aLength - bLength;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6784
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6785
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6786
    // Compare long
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6787
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6788
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6789
     * Compares two {@code long} arrays lexicographically.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6790
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6791
     * <p>If the two arrays share a common prefix then the lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6792
     * comparison is the result of comparing two elements, as if by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6793
     * {@link Long#compare(long, long)}, at an index within the respective
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6794
     * arrays that is the prefix length.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6795
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6796
     * comparison is the result of comparing the two array lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6797
     * (See {@link #mismatch(long[], long[])} for the definition of a common and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6798
     * proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6799
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6800
     * <p>A {@code null} array reference is considered lexicographically less
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6801
     * than a non-{@code null} array reference.  Two {@code null} array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6802
     * references are considered equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6803
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6804
     * <p>The comparison is consistent with {@link #equals(long[], long[]) equals},
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6805
     * more specifically the following holds for arrays {@code a} and {@code b}:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6806
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6807
     *     Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6808
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6809
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6810
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6811
     * <p>This method behaves as if (for non-{@code null} array references):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6812
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6813
     *     int i = Arrays.mismatch(a, b);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6814
     *     if (i >= 0 && i < Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6815
     *         return Long.compare(a[i], b[i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6816
     *     return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6817
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6818
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6819
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6820
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6821
     * @return the value {@code 0} if the first and second array are equal and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6822
     *         contain the same elements in the same order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6823
     *         a value less than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6824
     *         lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6825
     *         a value greater than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6826
     *         lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6827
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6828
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6829
    public static int compare(long[] a, long[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6830
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6831
            return 0;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6832
        if (a == null || b == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6833
            return a == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6834
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6835
        int i = ArraysSupport.mismatch(a, b,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6836
                                       Math.min(a.length, b.length));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6837
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6838
            return Long.compare(a[i], b[i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6839
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6840
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6841
        return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6842
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6843
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6844
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6845
     * Compares two {@code long} arrays lexicographically over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6846
     * ranges.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6847
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6848
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6849
     * then the lexicographic comparison is the result of comparing two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6850
     * elements, as if by {@link Long#compare(long, long)}, at a relative index
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6851
     * within the respective arrays that is the length of the prefix.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6852
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6853
     * comparison is the result of comparing the two range lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6854
     * (See {@link #mismatch(long[], int, int, long[], int, int)} for the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6855
     * definition of a common and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6856
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6857
     * <p>The comparison is consistent with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6858
     * {@link #equals(long[], int, int, long[], int, int) equals}, more
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6859
     * specifically the following holds for arrays {@code a} and {@code b} with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6860
     * specified ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6861
     * [{@code bFromIndex}, {@code btoIndex}) respectively:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6862
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6863
     *     Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6864
     *         (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6865
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6866
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6867
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6868
     * <p>This method behaves as if:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6869
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6870
     *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6871
     *                             b, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6872
     *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6873
     *         return Long.compare(a[aFromIndex + i], b[bFromIndex + i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6874
     *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6875
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6876
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6877
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6878
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6879
     *                   first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6880
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6881
     *                 first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6882
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6883
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6884
     *                   second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6885
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6886
     *                 second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6887
     * @return the value {@code 0} if, over the specified ranges, the first and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6888
     *         second array are equal and contain the same elements in the same
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6889
     *         order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6890
     *         a value less than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6891
     *         first array is lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6892
     *         a value greater than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6893
     *         first array is lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6894
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6895
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6896
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6897
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6898
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6899
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6900
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6901
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6902
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6903
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6904
    public static int compare(long[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6905
                              long[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6906
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6907
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6908
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6909
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6910
        int bLength = bToIndex - bFromIndex;
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6911
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6912
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6913
                                       Math.min(aLength, bLength));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6914
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6915
            return Long.compare(a[aFromIndex + i], b[bFromIndex + i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6916
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6917
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6918
        return aLength - bLength;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6919
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6920
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6921
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6922
     * Compares two {@code long} arrays lexicographically, numerically treating
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6923
     * elements as unsigned.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6924
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6925
     * <p>If the two arrays share a common prefix then the lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6926
     * comparison is the result of comparing two elements, as if by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6927
     * {@link Long#compareUnsigned(long, long)}, at an index within the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6928
     * respective arrays that is the prefix length.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6929
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6930
     * comparison is the result of comparing the two array lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6931
     * (See {@link #mismatch(long[], long[])} for the definition of a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6932
     * and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6933
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6934
     * <p>A {@code null} array reference is considered lexicographically less
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6935
     * than a non-{@code null} array reference.  Two {@code null} array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6936
     * references are considered equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6937
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6938
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6939
     * <p>This method behaves as if (for non-{@code null} array references):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6940
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6941
     *     int i = Arrays.mismatch(a, b);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6942
     *     if (i >= 0 && i < Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6943
     *         return Long.compareUnsigned(a[i], b[i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6944
     *     return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6945
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6946
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6947
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6948
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6949
     * @return the value {@code 0} if the first and second array are
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6950
     *         equal and contain the same elements in the same order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6951
     *         a value less than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6952
     *         lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6953
     *         a value greater than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6954
     *         lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6955
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6956
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6957
    public static int compareUnsigned(long[] a, long[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6958
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6959
            return 0;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6960
        if (a == null || b == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6961
            return a == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6962
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6963
        int i = ArraysSupport.mismatch(a, b,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6964
                                       Math.min(a.length, b.length));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6965
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6966
            return Long.compareUnsigned(a[i], b[i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6967
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6968
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6969
        return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6970
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6971
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6972
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6973
     * Compares two {@code long} arrays lexicographically over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6974
     * ranges, numerically treating elements as unsigned.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6975
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6976
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6977
     * then the lexicographic comparison is the result of comparing two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6978
     * elements, as if by {@link Long#compareUnsigned(long, long)}, at a
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6979
     * relative index within the respective arrays that is the length of the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6980
     * prefix.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6981
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6982
     * comparison is the result of comparing the two range lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6983
     * (See {@link #mismatch(long[], int, int, long[], int, int)} for the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6984
     * definition of a common and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6985
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6986
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6987
     * <p>This method behaves as if:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6988
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6989
     *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6990
     *                             b, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6991
     *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6992
     *         return Long.compareUnsigned(a[aFromIndex + i], b[bFromIndex + i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6993
     *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6994
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6995
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6996
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6997
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6998
     *                   first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6999
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7000
     *                 first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7001
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7002
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7003
     *                   second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7004
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7005
     *                 second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7006
     * @return the value {@code 0} if, over the specified ranges, the first and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7007
     *         second array are equal and contain the same elements in the same
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7008
     *         order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7009
     *         a value less than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7010
     *         first array is lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7011
     *         a value greater than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7012
     *         first array is lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7013
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7014
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7015
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7016
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7017
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7018
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7019
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7020
     *         if either array is null
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7021
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7022
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7023
    public static int compareUnsigned(long[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7024
                                      long[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7025
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7026
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7027
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7028
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7029
        int bLength = bToIndex - bFromIndex;
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7030
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7031
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7032
                                       Math.min(aLength, bLength));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7033
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7034
            return Long.compareUnsigned(a[aFromIndex + i], b[bFromIndex + i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7035
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7036
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7037
        return aLength - bLength;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7038
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7039
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7040
    // Compare float
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7041
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7042
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7043
     * Compares two {@code float} arrays lexicographically.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7044
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7045
     * <p>If the two arrays share a common prefix then the lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7046
     * comparison is the result of comparing two elements, as if by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7047
     * {@link Float#compare(float, float)}, at an index within the respective
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7048
     * arrays that is the prefix length.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7049
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7050
     * comparison is the result of comparing the two array lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7051
     * (See {@link #mismatch(float[], float[])} for the definition of a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7052
     * and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7053
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7054
     * <p>A {@code null} array reference is considered lexicographically less
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7055
     * than a non-{@code null} array reference.  Two {@code null} array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7056
     * references are considered equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7057
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7058
     * <p>The comparison is consistent with {@link #equals(float[], float[]) equals},
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7059
     * more specifically the following holds for arrays {@code a} and {@code b}:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7060
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7061
     *     Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7062
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7063
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7064
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7065
     * <p>This method behaves as if (for non-{@code null} array references):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7066
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7067
     *     int i = Arrays.mismatch(a, b);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7068
     *     if (i >= 0 && i < Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7069
     *         return Float.compare(a[i], b[i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7070
     *     return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7071
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7072
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7073
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7074
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7075
     * @return the value {@code 0} if the first and second array are equal and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7076
     *         contain the same elements in the same order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7077
     *         a value less than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7078
     *         lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7079
     *         a value greater than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7080
     *         lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7081
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7082
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7083
    public static int compare(float[] a, float[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7084
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7085
            return 0;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7086
        if (a == null || b == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7087
            return a == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7088
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7089
        int i = ArraysSupport.mismatch(a, b,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7090
                                       Math.min(a.length, b.length));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7091
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7092
            return Float.compare(a[i], b[i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7093
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7094
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7095
        return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7096
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7097
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7098
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7099
     * Compares two {@code float} arrays lexicographically over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7100
     * ranges.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7101
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7102
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7103
     * then the lexicographic comparison is the result of comparing two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7104
     * elements, as if by {@link Float#compare(float, float)}, at a relative
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7105
     * index within the respective arrays that is the length of the prefix.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7106
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7107
     * comparison is the result of comparing the two range lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7108
     * (See {@link #mismatch(float[], int, int, float[], int, int)} for the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7109
     * definition of a common and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7110
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7111
     * <p>The comparison is consistent with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7112
     * {@link #equals(float[], int, int, float[], int, int) equals}, more
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7113
     * specifically the following holds for arrays {@code a} and {@code b} with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7114
     * specified ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7115
     * [{@code bFromIndex}, {@code btoIndex}) respectively:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7116
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7117
     *     Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7118
     *         (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7119
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7120
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7121
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7122
     * <p>This method behaves as if:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7123
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7124
     *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7125
     *                             b, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7126
     *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7127
     *         return Float.compare(a[aFromIndex + i], b[bFromIndex + i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7128
     *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7129
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7130
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7131
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7132
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7133
     *                   first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7134
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7135
     *                 first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7136
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7137
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7138
     *                   second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7139
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7140
     *                 second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7141
     * @return the value {@code 0} if, over the specified ranges, the first and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7142
     *         second array are equal and contain the same elements in the same
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7143
     *         order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7144
     *         a value less than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7145
     *         first array is lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7146
     *         a value greater than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7147
     *         first array is lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7148
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7149
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7150
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7151
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7152
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7153
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7154
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7155
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7156
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7157
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7158
    public static int compare(float[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7159
                              float[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7160
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7161
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7162
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7163
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7164
        int bLength = bToIndex - bFromIndex;
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7165
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7166
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7167
                                       Math.min(aLength, bLength));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7168
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7169
            return Float.compare(a[aFromIndex + i], b[bFromIndex + i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7170
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7171
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7172
        return aLength - bLength;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7173
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7174
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7175
    // Compare double
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7176
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7177
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7178
     * Compares two {@code double} arrays lexicographically.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7179
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7180
     * <p>If the two arrays share a common prefix then the lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7181
     * comparison is the result of comparing two elements, as if by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7182
     * {@link Double#compare(double, double)}, at an index within the respective
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7183
     * arrays that is the prefix length.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7184
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7185
     * comparison is the result of comparing the two array lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7186
     * (See {@link #mismatch(double[], double[])} for the definition of a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7187
     * and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7188
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7189
     * <p>A {@code null} array reference is considered lexicographically less
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7190
     * than a non-{@code null} array reference.  Two {@code null} array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7191
     * references are considered equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7192
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7193
     * <p>The comparison is consistent with {@link #equals(double[], double[]) equals},
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7194
     * more specifically the following holds for arrays {@code a} and {@code b}:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7195
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7196
     *     Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7197
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7198
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7199
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7200
     * <p>This method behaves as if (for non-{@code null} array references):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7201
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7202
     *     int i = Arrays.mismatch(a, b);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7203
     *     if (i >= 0 && i < Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7204
     *         return Double.compare(a[i], b[i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7205
     *     return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7206
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7207
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7208
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7209
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7210
     * @return the value {@code 0} if the first and second array are equal and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7211
     *         contain the same elements in the same order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7212
     *         a value less than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7213
     *         lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7214
     *         a value greater than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7215
     *         lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7216
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7217
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7218
    public static int compare(double[] a, double[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7219
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7220
            return 0;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7221
        if (a == null || b == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7222
            return a == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7223
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7224
        int i = ArraysSupport.mismatch(a, b,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7225
                                       Math.min(a.length, b.length));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7226
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7227
            return Double.compare(a[i], b[i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7228
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7229
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7230
        return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7231
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7232
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7233
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7234
     * Compares two {@code double} arrays lexicographically over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7235
     * ranges.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7236
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7237
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7238
     * then the lexicographic comparison is the result of comparing two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7239
     * elements, as if by {@link Double#compare(double, double)}, at a relative
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7240
     * index within the respective arrays that is the length of the prefix.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7241
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7242
     * comparison is the result of comparing the two range lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7243
     * (See {@link #mismatch(double[], int, int, double[], int, int)} for the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7244
     * definition of a common and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7245
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7246
     * <p>The comparison is consistent with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7247
     * {@link #equals(double[], int, int, double[], int, int) equals}, more
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7248
     * specifically the following holds for arrays {@code a} and {@code b} with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7249
     * specified ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7250
     * [{@code bFromIndex}, {@code btoIndex}) respectively:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7251
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7252
     *     Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7253
     *         (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7254
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7255
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7256
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7257
     * <p>This method behaves as if:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7258
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7259
     *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7260
     *                             b, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7261
     *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7262
     *         return Double.compare(a[aFromIndex + i], b[bFromIndex + i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7263
     *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7264
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7265
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7266
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7267
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7268
     *                   first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7269
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7270
     *                 first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7271
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7272
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7273
     *                   second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7274
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7275
     *                 second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7276
     * @return the value {@code 0} if, over the specified ranges, the first and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7277
     *         second array are equal and contain the same elements in the same
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7278
     *         order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7279
     *         a value less than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7280
     *         first array is lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7281
     *         a value greater than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7282
     *         first array is lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7283
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7284
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7285
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7286
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7287
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7288
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7289
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7290
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7291
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7292
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7293
    public static int compare(double[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7294
                              double[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7295
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7296
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7297
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7298
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7299
        int bLength = bToIndex - bFromIndex;
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7300
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7301
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7302
                                       Math.min(aLength, bLength));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7303
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7304
            return Double.compare(a[aFromIndex + i], b[bFromIndex + i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7305
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7306
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7307
        return aLength - bLength;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7308
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7309
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7310
    // Compare objects
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7311
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7312
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7313
     * Compares two {@code Object} arrays, within comparable elements,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7314
     * lexicographically.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7315
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7316
     * <p>If the two arrays share a common prefix then the lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7317
     * comparison is the result of comparing two elements of type {@code T} at
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7318
     * an index {@code i} within the respective arrays that is the prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7319
     * length, as if by:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7320
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7321
     *     Comparator.nullsFirst(Comparator.<T>naturalOrder()).
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7322
     *         compare(a[i], b[i])
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7323
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7324
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7325
     * comparison is the result of comparing the two array lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7326
     * (See {@link #mismatch(Object[], Object[])} for the definition of a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7327
     * and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7328
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7329
     * <p>A {@code null} array reference is considered lexicographically less
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7330
     * than a non-{@code null} array reference.  Two {@code null} array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7331
     * references are considered equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7332
     * A {@code null} array element is considered lexicographically than a
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7333
     * non-{@code null} array element.  Two {@code null} array elements are
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7334
     * considered equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7335
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7336
     * <p>The comparison is consistent with {@link #equals(Object[], Object[]) equals},
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7337
     * more specifically the following holds for arrays {@code a} and {@code b}:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7338
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7339
     *     Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7340
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7341
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7342
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7343
     * <p>This method behaves as if (for non-{@code null} array references
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7344
     * and elements):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7345
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7346
     *     int i = Arrays.mismatch(a, b);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7347
     *     if (i >= 0 && i < Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7348
     *         return a[i].compareTo(b[i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7349
     *     return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7350
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7351
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7352
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7353
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7354
     * @param <T> the type of comparable array elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7355
     * @return the value {@code 0} if the first and second array are equal and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7356
     *         contain the same elements in the same order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7357
     *         a value less than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7358
     *         lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7359
     *         a value greater than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7360
     *         lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7361
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7362
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7363
    public static <T extends Comparable<? super T>> int compare(T[] a, T[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7364
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7365
            return 0;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7366
        // A null array is less than a non-null array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7367
        if (a == null || b == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7368
            return a == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7369
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7370
        int length = Math.min(a.length, b.length);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7371
        for (int i = 0; i < length; i++) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7372
            T oa = a[i];
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7373
            T ob = b[i];
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7374
            if (oa != ob) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7375
                // A null element is less than a non-null element
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7376
                if (oa == null || ob == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7377
                    return oa == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7378
                int v = oa.compareTo(ob);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7379
                if (v != 0) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7380
                    return v;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7381
                }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7382
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7383
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7384
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7385
        return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7386
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7387
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7388
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7389
     * Compares two {@code Object} arrays lexicographically over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7390
     * ranges.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7391
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7392
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7393
     * then the lexicographic comparison is the result of comparing two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7394
     * elements of type {@code T} at a relative index {@code i} within the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7395
     * respective arrays that is the prefix length, as if by:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7396
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7397
     *     Comparator.nullsFirst(Comparator.<T>naturalOrder()).
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7398
     *         compare(a[aFromIndex + i, b[bFromIndex + i])
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7399
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7400
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7401
     * comparison is the result of comparing the two range lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7402
     * (See {@link #mismatch(Object[], int, int, Object[], int, int)} for the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7403
     * definition of a common and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7404
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7405
     * <p>The comparison is consistent with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7406
     * {@link #equals(Object[], int, int, Object[], int, int) equals}, more
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7407
     * specifically the following holds for arrays {@code a} and {@code b} with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7408
     * specified ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7409
     * [{@code bFromIndex}, {@code btoIndex}) respectively:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7410
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7411
     *     Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7412
     *         (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7413
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7414
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7415
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7416
     * <p>This method behaves as if (for non-{@code null} array elements):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7417
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7418
     *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7419
     *                             b, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7420
     *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7421
     *         return a[aFromIndex + i].compareTo(b[bFromIndex + i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7422
     *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7423
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7424
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7425
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7426
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7427
     *                   first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7428
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7429
     *                 first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7430
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7431
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7432
     *                   second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7433
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7434
     *                 second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7435
     * @param <T> the type of comparable array elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7436
     * @return the value {@code 0} if, over the specified ranges, the first and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7437
     *         second array are equal and contain the same elements in the same
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7438
     *         order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7439
     *         a value less than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7440
     *         first array is lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7441
     *         a value greater than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7442
     *         first array is lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7443
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7444
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7445
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7446
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7447
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7448
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7449
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7450
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7451
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7452
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7453
    public static <T extends Comparable<? super T>> int compare(
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7454
            T[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7455
            T[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7456
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7457
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7458
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7459
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7460
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7461
        int length = Math.min(aLength, bLength);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7462
        for (int i = 0; i < length; i++) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7463
            T oa = a[aFromIndex++];
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7464
            T ob = b[bFromIndex++];
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7465
            if (oa != ob) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7466
                if (oa == null || ob == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7467
                    return oa == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7468
                int v = oa.compareTo(ob);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7469
                if (v != 0) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7470
                    return v;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7471
                }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7472
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7473
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7474
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7475
        return aLength - bLength;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7476
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7477
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7478
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7479
     * Compares two {@code Object} arrays lexicographically using a specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7480
     * comparator.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7481
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7482
     * <p>If the two arrays share a common prefix then the lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7483
     * comparison is the result of comparing with the specified comparator two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7484
     * elements at an index within the respective arrays that is the prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7485
     * length.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7486
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7487
     * comparison is the result of comparing the two array lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7488
     * (See {@link #mismatch(Object[], Object[])} for the definition of a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7489
     * and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7490
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7491
     * <p>A {@code null} array reference is considered lexicographically less
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7492
     * than a non-{@code null} array reference.  Two {@code null} array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7493
     * references are considered equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7494
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7495
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7496
     * <p>This method behaves as if (for non-{@code null} array references):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7497
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7498
     *     int i = Arrays.mismatch(a, b, cmp);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7499
     *     if (i >= 0 && i < Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7500
     *         return cmp.compare(a[i], b[i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7501
     *     return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7502
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7503
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7504
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7505
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7506
     * @param cmp the comparator to compare array elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7507
     * @param <T> the type of array elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7508
     * @return the value {@code 0} if the first and second array are equal and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7509
     *         contain the same elements in the same order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7510
     *         a value less than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7511
     *         lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7512
     *         a value greater than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7513
     *         lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7514
     * @throws NullPointerException if the comparator is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7515
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7516
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7517
    public static <T> int compare(T[] a, T[] b,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7518
                                  Comparator<? super T> cmp) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7519
        Objects.requireNonNull(cmp);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7520
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7521
            return 0;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7522
        if (a == null || b == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7523
            return a == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7524
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7525
        int length = Math.min(a.length, b.length);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7526
        for (int i = 0; i < length; i++) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7527
            T oa = a[i];
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7528
            T ob = b[i];
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7529
            if (oa != ob) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7530
                // Null-value comparison is deferred to the comparator
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7531
                int v = cmp.compare(oa, ob);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7532
                if (v != 0) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7533
                    return v;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7534
                }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7535
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7536
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7537
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7538
        return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7539
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7540
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7541
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7542
     * Compares two {@code Object} arrays lexicographically over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7543
     * ranges.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7544
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7545
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7546
     * then the lexicographic comparison is the result of comparing with the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7547
     * specified comparator two elements at a relative index within the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7548
     * respective arrays that is the prefix length.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7549
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7550
     * comparison is the result of comparing the two range lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7551
     * (See {@link #mismatch(Object[], int, int, Object[], int, int)} for the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7552
     * definition of a common and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7553
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7554
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7555
     * <p>This method behaves as if (for non-{@code null} array elements):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7556
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7557
     *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7558
     *                             b, bFromIndex, bToIndex, cmp);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7559
     *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7560
     *         return cmp.compare(a[aFromIndex + i], b[bFromIndex + i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7561
     *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7562
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7563
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7564
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7565
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7566
     *                   first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7567
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7568
     *                 first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7569
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7570
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7571
     *                   second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7572
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7573
     *                 second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7574
     * @param cmp the comparator to compare array elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7575
     * @param <T> the type of array elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7576
     * @return the value {@code 0} if, over the specified ranges, the first and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7577
     *         second array are equal and contain the same elements in the same
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7578
     *         order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7579
     *         a value less than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7580
     *         first array is lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7581
     *         a value greater than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7582
     *         first array is lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7583
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7584
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7585
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7586
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7587
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7588
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7589
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7590
     *         if either array or the comparator is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7591
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7592
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7593
    public static <T> int compare(
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7594
            T[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7595
            T[] b, int bFromIndex, int bToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7596
            Comparator<? super T> cmp) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7597
        Objects.requireNonNull(cmp);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7598
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7599
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7600
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7601
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7602
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7603
        int length = Math.min(aLength, bLength);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7604
        for (int i = 0; i < length; i++) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7605
            T oa = a[aFromIndex++];
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7606
            T ob = b[bFromIndex++];
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7607
            if (oa != ob) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7608
                // Null-value comparison is deferred to the comparator
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7609
                int v = cmp.compare(oa, ob);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7610
                if (v != 0) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7611
                    return v;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7612
                }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7613
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7614
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7615
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7616
        return aLength - bLength;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7617
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7618
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7619
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7620
    // Mismatch methods
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7621
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7622
    // Mismatch boolean
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7623
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7624
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7625
     * Finds and returns the index of the first mismatch between two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7626
     * {@code boolean} arrays, otherwise return -1 if no mismatch is found.  The
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7627
     * index will be in the range of 0 (inclusive) up to the length (inclusive)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7628
     * of the smaller array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7629
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7630
     * <p>If the two arrays share a common prefix then the returned index is the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7631
     * length of the common prefix and it follows that there is a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7632
     * between the two elements at that index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7633
     * If one array is a proper prefix of the other then the returned index is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7634
     * the length of the smaller array and it follows that the index is only
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7635
     * valid for the larger array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7636
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7637
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7638
     * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7639
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7640
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7641
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7642
     *     pl < Math.min(a.length, b.length) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7643
     *     Arrays.equals(a, 0, pl, b, 0, pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7644
     *     a[pl] != b[pl]
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7645
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7646
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7647
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7648
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7649
     * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a proper
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7650
     * prefix if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7651
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7652
     *     a.length != b.length &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7653
     *     Arrays.equals(a, 0, Math.min(a.length, b.length),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7654
     *                   b, 0, Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7655
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7656
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7657
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7658
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7659
     * @return the index of the first mismatch between the two arrays,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7660
     *         otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7661
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7662
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7663
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7664
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7665
    public static int mismatch(boolean[] a, boolean[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7666
        int length = Math.min(a.length, b.length); // Check null array refs
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7667
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7668
            return -1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7669
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7670
        int i = ArraysSupport.mismatch(a, b, length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7671
        return (i < 0 && a.length != b.length) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7672
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7673
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7674
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7675
     * Finds and returns the relative index of the first mismatch between two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7676
     * {@code boolean} arrays over the specified ranges, otherwise return -1 if
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7677
     * no mismatch is found.  The index will be in the range of 0 (inclusive) up
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7678
     * to the length (inclusive) of the smaller range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7679
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7680
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7681
     * then the returned relative index is the length of the common prefix and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7682
     * it follows that there is a mismatch between the two elements at that
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7683
     * relative index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7684
     * If one array is a proper prefix of the other, over the specified ranges,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7685
     * then the returned relative index is the length of the smaller range and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7686
     * it follows that the relative index is only valid for the array with the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7687
     * larger range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7688
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7689
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7690
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7691
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7692
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7693
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7694
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7695
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7696
     *     pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7697
     *     Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7698
     *     a[aFromIndex + pl] != b[bFromIndex + pl]
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7699
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7700
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7701
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7702
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7703
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7704
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7705
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7706
     * if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7707
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7708
     *     (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7709
     *     Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7710
     *                   b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7711
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7712
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7713
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7714
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7715
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7716
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7717
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7718
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7719
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7720
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7721
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7722
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7723
     * @return the relative index of the first mismatch between the two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7724
     *         over the specified ranges, otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7725
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7726
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7727
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7728
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7729
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7730
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7731
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7732
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7733
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7734
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7735
    public static int mismatch(boolean[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7736
                               boolean[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7737
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7738
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7739
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7740
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7741
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7742
        int length = Math.min(aLength, bLength);
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7743
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7744
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7745
                                       length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7746
        return (i < 0 && aLength != bLength) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7747
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7748
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7749
    // Mismatch byte
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7750
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7751
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7752
     * Finds and returns the index of the first mismatch between two {@code byte}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7753
     * arrays, otherwise return -1 if no mismatch is found.  The index will be
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7754
     * in the range of 0 (inclusive) up to the length (inclusive) of the smaller
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7755
     * array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7756
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7757
     * <p>If the two arrays share a common prefix then the returned index is the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7758
     * length of the common prefix and it follows that there is a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7759
     * between the two elements at that index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7760
     * If one array is a proper prefix of the other then the returned index is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7761
     * the length of the smaller array and it follows that the index is only
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7762
     * valid for the larger array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7763
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7764
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7765
     * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7766
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7767
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7768
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7769
     *     pl < Math.min(a.length, b.length) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7770
     *     Arrays.equals(a, 0, pl, b, 0, pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7771
     *     a[pl] != b[pl]
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7772
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7773
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7774
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7775
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7776
     * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a proper
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7777
     * prefix if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7778
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7779
     *     a.length != b.length &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7780
     *     Arrays.equals(a, 0, Math.min(a.length, b.length),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7781
     *                   b, 0, Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7782
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7783
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7784
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7785
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7786
     * @return the index of the first mismatch between the two arrays,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7787
     *         otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7788
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7789
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7790
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7791
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7792
    public static int mismatch(byte[] a, byte[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7793
        int length = Math.min(a.length, b.length); // Check null array refs
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7794
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7795
            return -1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7796
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7797
        int i = ArraysSupport.mismatch(a, b, length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7798
        return (i < 0 && a.length != b.length) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7799
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7800
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7801
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7802
     * Finds and returns the relative index of the first mismatch between two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7803
     * {@code byte} arrays over the specified ranges, otherwise return -1 if no
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7804
     * mismatch is found.  The index will be in the range of 0 (inclusive) up to
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7805
     * the length (inclusive) of the smaller range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7806
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7807
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7808
     * then the returned relative index is the length of the common prefix and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7809
     * it follows that there is a mismatch between the two elements at that
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7810
     * relative index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7811
     * If one array is a proper prefix of the other, over the specified ranges,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7812
     * then the returned relative index is the length of the smaller range and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7813
     * it follows that the relative index is only valid for the array with the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7814
     * larger range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7815
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7816
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7817
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7818
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7819
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7820
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7821
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7822
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7823
     *     pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7824
     *     Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7825
     *     a[aFromIndex + pl] != b[bFromIndex + pl]
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7826
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7827
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7828
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7829
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7830
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7831
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7832
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7833
     * if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7834
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7835
     *     (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7836
     *     Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7837
     *                   b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7838
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7839
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7840
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7841
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7842
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7843
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7844
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7845
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7846
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7847
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7848
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7849
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7850
     * @return the relative index of the first mismatch between the two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7851
     *         over the specified ranges, otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7852
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7853
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7854
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7855
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7856
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7857
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7858
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7859
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7860
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7861
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7862
    public static int mismatch(byte[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7863
                               byte[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7864
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7865
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7866
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7867
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7868
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7869
        int length = Math.min(aLength, bLength);
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7870
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7871
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7872
                                       length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7873
        return (i < 0 && aLength != bLength) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7874
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7875
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7876
    // Mismatch char
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7877
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7878
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7879
     * Finds and returns the index of the first mismatch between two {@code char}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7880
     * arrays, otherwise return -1 if no mismatch is found.  The index will be
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7881
     * in the range of 0 (inclusive) up to the length (inclusive) of the smaller
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7882
     * array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7883
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7884
     * <p>If the two arrays share a common prefix then the returned index is the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7885
     * length of the common prefix and it follows that there is a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7886
     * between the two elements at that index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7887
     * If one array is a proper prefix of the other then the returned index is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7888
     * the length of the smaller array and it follows that the index is only
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7889
     * valid for the larger array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7890
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7891
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7892
     * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7893
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7894
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7895
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7896
     *     pl < Math.min(a.length, b.length) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7897
     *     Arrays.equals(a, 0, pl, b, 0, pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7898
     *     a[pl] != b[pl]
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7899
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7900
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7901
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7902
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7903
     * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a proper
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7904
     * prefix if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7905
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7906
     *     a.length != b.length &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7907
     *     Arrays.equals(a, 0, Math.min(a.length, b.length),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7908
     *                   b, 0, Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7909
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7910
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7911
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7912
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7913
     * @return the index of the first mismatch between the two arrays,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7914
     *         otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7915
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7916
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7917
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7918
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7919
    public static int mismatch(char[] a, char[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7920
        int length = Math.min(a.length, b.length); // Check null array refs
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7921
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7922
            return -1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7923
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7924
        int i = ArraysSupport.mismatch(a, b, length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7925
        return (i < 0 && a.length != b.length) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7926
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7927
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7928
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7929
     * Finds and returns the relative index of the first mismatch between two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7930
     * {@code char} arrays over the specified ranges, otherwise return -1 if no
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7931
     * mismatch is found.  The index will be in the range of 0 (inclusive) up to
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7932
     * the length (inclusive) of the smaller range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7933
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7934
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7935
     * then the returned relative index is the length of the common prefix and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7936
     * it follows that there is a mismatch between the two elements at that
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7937
     * relative index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7938
     * If one array is a proper prefix of the other, over the specified ranges,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7939
     * then the returned relative index is the length of the smaller range and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7940
     * it follows that the relative index is only valid for the array with the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7941
     * larger range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7942
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7943
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7944
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7945
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7946
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7947
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7948
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7949
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7950
     *     pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7951
     *     Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7952
     *     a[aFromIndex + pl] != b[bFromIndex + pl]
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7953
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7954
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7955
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7956
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7957
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7958
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7959
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7960
     * if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7961
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7962
     *     (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7963
     *     Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7964
     *                   b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7965
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7966
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7967
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7968
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7969
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7970
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7971
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7972
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7973
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7974
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7975
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7976
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7977
     * @return the relative index of the first mismatch between the two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7978
     *         over the specified ranges, otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7979
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7980
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7981
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7982
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7983
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7984
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7985
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7986
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7987
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7988
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7989
    public static int mismatch(char[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7990
                               char[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7991
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7992
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7993
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7994
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7995
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7996
        int length = Math.min(aLength, bLength);
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7997
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7998
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7999
                                       length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8000
        return (i < 0 && aLength != bLength) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8001
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8002
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8003
    // Mismatch short
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8004
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8005
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8006
     * Finds and returns the index of the first mismatch between two {@code short}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8007
     * arrays, otherwise return -1 if no mismatch is found.  The index will be
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8008
     * in the range of 0 (inclusive) up to the length (inclusive) of the smaller
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8009
     * array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8010
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8011
     * <p>If the two arrays share a common prefix then the returned index is the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8012
     * length of the common prefix and it follows that there is a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8013
     * between the two elements at that index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8014
     * If one array is a proper prefix of the other then the returned index is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8015
     * the length of the smaller array and it follows that the index is only
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8016
     * valid for the larger array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8017
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8018
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8019
     * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8020
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8021
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8022
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8023
     *     pl < Math.min(a.length, b.length) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8024
     *     Arrays.equals(a, 0, pl, b, 0, pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8025
     *     a[pl] != b[pl]
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8026
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8027
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8028
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8029
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8030
     * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a proper
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8031
     * prefix if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8032
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8033
     *     a.length != b.length &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8034
     *     Arrays.equals(a, 0, Math.min(a.length, b.length),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8035
     *                   b, 0, Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8036
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8037
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8038
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8039
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8040
     * @return the index of the first mismatch between the two arrays,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8041
     *         otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8042
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8043
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8044
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8045
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8046
    public static int mismatch(short[] a, short[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8047
        int length = Math.min(a.length, b.length); // Check null array refs
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8048
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8049
            return -1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8050
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8051
        int i = ArraysSupport.mismatch(a, b, length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8052
        return (i < 0 && a.length != b.length) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8053
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8054
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8055
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8056
     * Finds and returns the relative index of the first mismatch between two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8057
     * {@code short} arrays over the specified ranges, otherwise return -1 if no
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8058
     * mismatch is found.  The index will be in the range of 0 (inclusive) up to
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8059
     * the length (inclusive) of the smaller range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8060
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8061
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8062
     * then the returned relative index is the length of the common prefix and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8063
     * it follows that there is a mismatch between the two elements at that
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8064
     * relative index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8065
     * If one array is a proper prefix of the other, over the specified ranges,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8066
     * then the returned relative index is the length of the smaller range and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8067
     * it follows that the relative index is only valid for the array with the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8068
     * larger range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8069
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8070
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8071
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8072
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8073
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8074
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8075
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8076
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8077
     *     pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8078
     *     Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8079
     *     a[aFromIndex + pl] != b[bFromIndex + pl]
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8080
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8081
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8082
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8083
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8084
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8085
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8086
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8087
     * if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8088
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8089
     *     (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8090
     *     Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8091
     *                   b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8092
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8093
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8094
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8095
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8096
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8097
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8098
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8099
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8100
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8101
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8102
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8103
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8104
     * @return the relative index of the first mismatch between the two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8105
     *         over the specified ranges, otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8106
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8107
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8108
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8109
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8110
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8111
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8112
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8113
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8114
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8115
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8116
    public static int mismatch(short[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8117
                               short[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8118
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8119
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8120
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8121
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8122
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8123
        int length = Math.min(aLength, bLength);
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8124
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8125
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8126
                                       length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8127
        return (i < 0 && aLength != bLength) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8128
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8129
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8130
    // Mismatch int
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8131
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8132
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8133
     * Finds and returns the index of the first mismatch between two {@code int}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8134
     * arrays, otherwise return -1 if no mismatch is found.  The index will be
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8135
     * in the range of 0 (inclusive) up to the length (inclusive) of the smaller
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8136
     * array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8137
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8138
     * <p>If the two arrays share a common prefix then the returned index is the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8139
     * length of the common prefix and it follows that there is a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8140
     * between the two elements at that index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8141
     * If one array is a proper prefix of the other then the returned index is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8142
     * the length of the smaller array and it follows that the index is only
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8143
     * valid for the larger array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8144
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8145
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8146
     * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8147
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8148
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8149
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8150
     *     pl < Math.min(a.length, b.length) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8151
     *     Arrays.equals(a, 0, pl, b, 0, pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8152
     *     a[pl] != b[pl]
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8153
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8154
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8155
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8156
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8157
     * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a proper
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8158
     * prefix if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8159
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8160
     *     a.length != b.length &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8161
     *     Arrays.equals(a, 0, Math.min(a.length, b.length),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8162
     *                   b, 0, Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8163
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8164
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8165
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8166
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8167
     * @return the index of the first mismatch between the two arrays,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8168
     *         otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8169
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8170
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8171
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8172
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8173
    public static int mismatch(int[] a, int[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8174
        int length = Math.min(a.length, b.length); // Check null array refs
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8175
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8176
            return -1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8177
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8178
        int i = ArraysSupport.mismatch(a, b, length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8179
        return (i < 0 && a.length != b.length) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8180
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8181
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8182
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8183
     * Finds and returns the relative index of the first mismatch between two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8184
     * {@code int} arrays over the specified ranges, otherwise return -1 if no
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8185
     * mismatch is found.  The index will be in the range of 0 (inclusive) up to
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8186
     * the length (inclusive) of the smaller range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8187
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8188
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8189
     * then the returned relative index is the length of the common prefix and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8190
     * it follows that there is a mismatch between the two elements at that
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8191
     * relative index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8192
     * If one array is a proper prefix of the other, over the specified ranges,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8193
     * then the returned relative index is the length of the smaller range and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8194
     * it follows that the relative index is only valid for the array with the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8195
     * larger range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8196
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8197
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8198
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8199
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8200
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8201
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8202
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8203
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8204
     *     pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8205
     *     Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8206
     *     a[aFromIndex + pl] != b[bFromIndex + pl]
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8207
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8208
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8209
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8210
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8211
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8212
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8213
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8214
     * if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8215
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8216
     *     (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8217
     *     Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8218
     *                   b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8219
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8220
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8221
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8222
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8223
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8224
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8225
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8226
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8227
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8228
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8229
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8230
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8231
     * @return the relative index of the first mismatch between the two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8232
     *         over the specified ranges, otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8233
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8234
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8235
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8236
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8237
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8238
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8239
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8240
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8241
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8242
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8243
    public static int mismatch(int[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8244
                               int[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8245
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8246
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8247
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8248
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8249
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8250
        int length = Math.min(aLength, bLength);
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8251
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8252
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8253
                                       length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8254
        return (i < 0 && aLength != bLength) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8255
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8256
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8257
    // Mismatch long
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8258
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8259
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8260
     * Finds and returns the index of the first mismatch between two {@code long}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8261
     * arrays, otherwise return -1 if no mismatch is found.  The index will be
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8262
     * in the range of 0 (inclusive) up to the length (inclusive) of the smaller
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8263
     * array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8264
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8265
     * <p>If the two arrays share a common prefix then the returned index is the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8266
     * length of the common prefix and it follows that there is a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8267
     * between the two elements at that index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8268
     * If one array is a proper prefix of the other then the returned index is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8269
     * the length of the smaller array and it follows that the index is only
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8270
     * valid for the larger array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8271
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8272
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8273
     * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8274
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8275
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8276
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8277
     *     pl < Math.min(a.length, b.length) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8278
     *     Arrays.equals(a, 0, pl, b, 0, pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8279
     *     a[pl] != b[pl]
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8280
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8281
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8282
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8283
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8284
     * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a proper
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8285
     * prefix if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8286
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8287
     *     a.length != b.length &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8288
     *     Arrays.equals(a, 0, Math.min(a.length, b.length),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8289
     *                   b, 0, Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8290
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8291
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8292
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8293
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8294
     * @return the index of the first mismatch between the two arrays,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8295
     *         otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8296
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8297
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8298
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8299
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8300
    public static int mismatch(long[] a, long[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8301
        int length = Math.min(a.length, b.length); // Check null array refs
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8302
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8303
            return -1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8304
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8305
        int i = ArraysSupport.mismatch(a, b, length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8306
        return (i < 0 && a.length != b.length) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8307
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8308
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8309
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8310
     * Finds and returns the relative index of the first mismatch between two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8311
     * {@code long} arrays over the specified ranges, otherwise return -1 if no
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8312
     * mismatch is found.  The index will be in the range of 0 (inclusive) up to
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8313
     * the length (inclusive) of the smaller range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8314
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8315
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8316
     * then the returned relative index is the length of the common prefix and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8317
     * it follows that there is a mismatch between the two elements at that
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8318
     * relative index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8319
     * If one array is a proper prefix of the other, over the specified ranges,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8320
     * then the returned relative index is the length of the smaller range and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8321
     * it follows that the relative index is only valid for the array with the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8322
     * larger range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8323
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8324
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8325
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8326
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8327
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8328
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8329
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8330
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8331
     *     pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8332
     *     Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8333
     *     a[aFromIndex + pl] != b[bFromIndex + pl]
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8334
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8335
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8336
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8337
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8338
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8339
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8340
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8341
     * if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8342
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8343
     *     (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8344
     *     Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8345
     *                   b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8346
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8347
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8348
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8349
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8350
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8351
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8352
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8353
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8354
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8355
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8356
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8357
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8358
     * @return the relative index of the first mismatch between the two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8359
     *         over the specified ranges, otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8360
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8361
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8362
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8363
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8364
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8365
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8366
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8367
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8368
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8369
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8370
    public static int mismatch(long[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8371
                               long[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8372
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8373
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8374
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8375
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8376
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8377
        int length = Math.min(aLength, bLength);
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8378
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8379
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8380
                                       length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8381
        return (i < 0 && aLength != bLength) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8382
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8383
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8384
    // Mismatch float
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8385
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8386
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8387
     * Finds and returns the index of the first mismatch between two {@code float}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8388
     * arrays, otherwise return -1 if no mismatch is found.  The index will be
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8389
     * in the range of 0 (inclusive) up to the length (inclusive) of the smaller
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8390
     * array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8391
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8392
     * <p>If the two arrays share a common prefix then the returned index is the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8393
     * length of the common prefix and it follows that there is a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8394
     * between the two elements at that index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8395
     * If one array is a proper prefix of the other then the returned index is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8396
     * the length of the smaller array and it follows that the index is only
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8397
     * valid for the larger array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8398
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8399
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8400
     * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8401
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8402
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8403
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8404
     *     pl < Math.min(a.length, b.length) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8405
     *     Arrays.equals(a, 0, pl, b, 0, pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8406
     *     Float.compare(a[pl], b[pl]) != 0
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8407
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8408
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8409
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8410
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8411
     * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a proper
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8412
     * prefix if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8413
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8414
     *     a.length != b.length &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8415
     *     Arrays.equals(a, 0, Math.min(a.length, b.length),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8416
     *                   b, 0, Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8417
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8418
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8419
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8420
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8421
     * @return the index of the first mismatch between the two arrays,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8422
     *         otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8423
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8424
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8425
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8426
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8427
    public static int mismatch(float[] a, float[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8428
        int length = Math.min(a.length, b.length); // Check null array refs
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8429
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8430
            return -1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8431
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8432
        int i = ArraysSupport.mismatch(a, b, length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8433
        return (i < 0 && a.length != b.length) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8434
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8435
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8436
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8437
     * Finds and returns the relative index of the first mismatch between two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8438
     * {@code float} arrays over the specified ranges, otherwise return -1 if no
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8439
     * mismatch is found.  The index will be in the range of 0 (inclusive) up to
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8440
     * the length (inclusive) of the smaller range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8441
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8442
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8443
     * then the returned relative index is the length of the common prefix and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8444
     * it follows that there is a mismatch between the two elements at that
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8445
     * relative index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8446
     * If one array is a proper prefix of the other, over the specified ranges,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8447
     * then the returned relative index is the length of the smaller range and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8448
     * it follows that the relative index is only valid for the array with the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8449
     * larger range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8450
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8451
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8452
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8453
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8454
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8455
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8456
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8457
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8458
     *     pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8459
     *     Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8460
     *     Float.compare(a[aFromIndex + pl], b[bFromIndex + pl]) != 0
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8461
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8462
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8463
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8464
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8465
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8466
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8467
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8468
     * if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8469
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8470
     *     (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8471
     *     Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8472
     *                   b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8473
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8474
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8475
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8476
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8477
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8478
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8479
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8480
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8481
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8482
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8483
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8484
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8485
     * @return the relative index of the first mismatch between the two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8486
     *         over the specified ranges, otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8487
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8488
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8489
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8490
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8491
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8492
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8493
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8494
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8495
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8496
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8497
    public static int mismatch(float[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8498
                               float[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8499
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8500
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8501
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8502
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8503
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8504
        int length = Math.min(aLength, bLength);
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8505
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8506
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8507
                                       length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8508
        return (i < 0 && aLength != bLength) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8509
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8510
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8511
    // Mismatch double
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8512
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8513
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8514
     * Finds and returns the index of the first mismatch between two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8515
     * {@code double} arrays, otherwise return -1 if no mismatch is found.  The
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8516
     * index will be in the range of 0 (inclusive) up to the length (inclusive)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8517
     * of the smaller array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8518
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8519
     * <p>If the two arrays share a common prefix then the returned index is the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8520
     * length of the common prefix and it follows that there is a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8521
     * between the two elements at that index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8522
     * If one array is a proper prefix of the other then the returned index is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8523
     * the length of the smaller array and it follows that the index is only
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8524
     * valid for the larger array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8525
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8526
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8527
     * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8528
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8529
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8530
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8531
     *     pl < Math.min(a.length, b.length) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8532
     *     Arrays.equals(a, 0, pl, b, 0, pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8533
     *     Double.compare(a[pl], b[pl]) != 0
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8534
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8535
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8536
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8537
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8538
     * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a proper
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8539
     * prefix if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8540
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8541
     *     a.length != b.length &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8542
     *     Arrays.equals(a, 0, Math.min(a.length, b.length),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8543
     *                   b, 0, Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8544
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8545
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8546
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8547
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8548
     * @return the index of the first mismatch between the two arrays,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8549
     *         otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8550
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8551
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8552
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8553
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8554
    public static int mismatch(double[] a, double[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8555
        int length = Math.min(a.length, b.length); // Check null array refs
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8556
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8557
            return -1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8558
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8559
        int i = ArraysSupport.mismatch(a, b, length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8560
        return (i < 0 && a.length != b.length) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8561
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8562
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8563
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8564
     * Finds and returns the relative index of the first mismatch between two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8565
     * {@code double} arrays over the specified ranges, otherwise return -1 if
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8566
     * no mismatch is found.  The index will be in the range of 0 (inclusive) up
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8567
     * to the length (inclusive) of the smaller range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8568
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8569
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8570
     * then the returned relative index is the length of the common prefix and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8571
     * it follows that there is a mismatch between the two elements at that
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8572
     * relative index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8573
     * If one array is a proper prefix of the other, over the specified ranges,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8574
     * then the returned relative index is the length of the smaller range and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8575
     * it follows that the relative index is only valid for the array with the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8576
     * larger range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8577
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8578
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8579
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8580
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8581
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8582
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8583
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8584
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8585
     *     pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8586
     *     Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8587
     *     Double.compare(a[aFromIndex + pl], b[bFromIndex + pl]) != 0
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8588
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8589
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8590
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8591
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8592
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8593
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8594
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8595
     * if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8596
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8597
     *     (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8598
     *     Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8599
     *                   b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8600
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8601
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8602
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8603
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8604
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8605
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8606
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8607
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8608
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8609
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8610
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8611
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8612
     * @return the relative index of the first mismatch between the two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8613
     *         over the specified ranges, otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8614
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8615
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8616
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8617
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8618
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8619
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8620
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8621
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8622
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8623
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8624
    public static int mismatch(double[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8625
                               double[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8626
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8627
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8628
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8629
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8630
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8631
        int length = Math.min(aLength, bLength);
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8632
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8633
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8634
                                       length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8635
        return (i < 0 && aLength != bLength) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8636
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8637
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8638
    // Mismatch objects
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8639
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8640
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8641
     * Finds and returns the index of the first mismatch between two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8642
     * {@code Object} arrays, otherwise return -1 if no mismatch is found.  The
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8643
     * index will be in the range of 0 (inclusive) up to the length (inclusive)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8644
     * of the smaller array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8645
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8646
     * <p>If the two arrays share a common prefix then the returned index is the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8647
     * length of the common prefix and it follows that there is a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8648
     * between the two elements at that index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8649
     * If one array is a proper prefix of the other then the returned index is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8650
     * the length of the smaller array and it follows that the index is only
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8651
     * valid for the larger array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8652
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8653
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8654
     * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8655
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8656
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8657
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8658
     *     pl < Math.min(a.length, b.length) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8659
     *     Arrays.equals(a, 0, pl, b, 0, pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8660
     *     !Objects.equals(a[pl], b[pl])
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8661
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8662
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8663
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8664
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8665
     * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a proper
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8666
     * prefix if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8667
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8668
     *     a.length != b.length &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8669
     *     Arrays.equals(a, 0, Math.min(a.length, b.length),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8670
     *                   b, 0, Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8671
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8672
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8673
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8674
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8675
     * @return the index of the first mismatch between the two arrays,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8676
     *         otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8677
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8678
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8679
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8680
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8681
    public static int mismatch(Object[] a, Object[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8682
        int length = Math.min(a.length, b.length); // Check null array refs
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8683
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8684
            return -1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8685
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8686
        for (int i = 0; i < length; i++) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8687
            if (!Objects.equals(a[i], b[i]))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8688
                return i;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8689
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8690
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8691
        return a.length != b.length ? length : -1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8692
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8693
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8694
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8695
     * Finds and returns the relative index of the first mismatch between two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8696
     * {@code Object} arrays over the specified ranges, otherwise return -1 if
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8697
     * no mismatch is found.  The index will be in the range of 0 (inclusive) up
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8698
     * to the length (inclusive) of the smaller range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8699
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8700
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8701
     * then the returned relative index is the length of the common prefix and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8702
     * it follows that there is a mismatch between the two elements at that
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8703
     * relative index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8704
     * If one array is a proper prefix of the other, over the specified ranges,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8705
     * then the returned relative index is the length of the smaller range and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8706
     * it follows that the relative index is only valid for the array with the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8707
     * larger range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8708
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8709
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8710
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8711
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8712
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8713
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8714
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8715
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8716
     *     pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8717
     *     Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8718
     *     !Objects.equals(a[aFromIndex + pl], b[bFromIndex + pl])
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8719
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8720
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8721
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8722
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8723
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8724
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8725
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8726
     * if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8727
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8728
     *     (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8729
     *     Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8730
     *                   b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8731
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8732
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8733
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8734
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8735
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8736
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8737
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8738
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8739
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8740
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8741
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8742
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8743
     * @return the relative index of the first mismatch between the two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8744
     *         over the specified ranges, otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8745
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8746
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8747
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8748
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8749
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8750
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8751
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8752
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8753
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8754
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8755
    public static int mismatch(
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8756
            Object[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8757
            Object[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8758
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8759
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8760
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8761
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8762
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8763
        int length = Math.min(aLength, bLength);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8764
        for (int i = 0; i < length; i++) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8765
            if (!Objects.equals(a[aFromIndex++], b[bFromIndex++]))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8766
                return i;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8767
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8768
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8769
        return aLength != bLength ? length : -1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8770
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8771
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8772
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8773
     * Finds and returns the index of the first mismatch between two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8774
     * {@code Object} arrays, otherwise return -1 if no mismatch is found.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8775
     * The index will be in the range of 0 (inclusive) up to the length
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8776
     * (inclusive) of the smaller array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8777
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8778
     * <p>The specified comparator is used to determine if two array elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8779
     * from the each array are not equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8780
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8781
     * <p>If the two arrays share a common prefix then the returned index is the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8782
     * length of the common prefix and it follows that there is a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8783
     * between the two elements at that index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8784
     * If one array is a proper prefix of the other then the returned index is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8785
     * the length of the smaller array and it follows that the index is only
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8786
     * valid for the larger array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8787
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8788
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8789
     * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8790
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8791
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8792
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8793
     *     pl < Math.min(a.length, b.length) &&
33843
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  8794
     *     Arrays.equals(a, 0, pl, b, 0, pl, cmp)
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8795
     *     cmp.compare(a[pl], b[pl]) != 0
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8796
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8797
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8798
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8799
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8800
     * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a proper
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8801
     * prefix if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8802
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8803
     *     a.length != b.length &&
33843
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  8804
     *     Arrays.equals(a, 0, Math.min(a.length, b.length),
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  8805
     *                   b, 0, Math.min(a.length, b.length),
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  8806
     *                   cmp)
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8807
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8808
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8809
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8810
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8811
     * @param cmp the comparator to compare array elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8812
     * @param <T> the type of array elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8813
     * @return the index of the first mismatch between the two arrays,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8814
     *         otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8815
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8816
     *         if either array or the comparator is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8817
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8818
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8819
    public static <T> int mismatch(T[] a, T[] b, Comparator<? super T> cmp) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8820
        Objects.requireNonNull(cmp);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8821
        int length = Math.min(a.length, b.length); // Check null array refs
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8822
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8823
            return -1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8824
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8825
        for (int i = 0; i < length; i++) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8826
            T oa = a[i];
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8827
            T ob = b[i];
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8828
            if (oa != ob) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8829
                // Null-value comparison is deferred to the comparator
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8830
                int v = cmp.compare(oa, ob);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8831
                if (v != 0) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8832
                    return i;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8833
                }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8834
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8835
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8836
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8837
        return a.length != b.length ? length : -1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8838
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8839
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8840
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8841
     * Finds and returns the relative index of the first mismatch between two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8842
     * {@code Object} arrays over the specified ranges, otherwise return -1 if
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8843
     * no mismatch is found.  The index will be in the range of 0 (inclusive) up
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8844
     * to the length (inclusive) of the smaller range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8845
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8846
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8847
     * then the returned relative index is the length of the common prefix and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8848
     * it follows that there is a mismatch between the two elements at that
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8849
     * relative index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8850
     * If one array is a proper prefix of the other, over the specified ranges,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8851
     * then the returned relative index is the length of the smaller range and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8852
     * it follows that the relative index is only valid for the array with the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8853
     * larger range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8854
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8855
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8856
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8857
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8858
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8859
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8860
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8861
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8862
     *     pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&
33843
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  8863
     *     Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl, cmp) &&
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8864
     *     cmp.compare(a[aFromIndex + pl], b[bFromIndex + pl]) != 0
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8865
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8866
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8867
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8868
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8869
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8870
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8871
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8872
     * if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8873
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8874
     *     (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&
33843
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  8875
     *     Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  8876
     *                   b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  8877
     *                   cmp)
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8878
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8879
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8880
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8881
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8882
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8883
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8884
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8885
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8886
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8887
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8888
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8889
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8890
     * @param cmp the comparator to compare array elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8891
     * @param <T> the type of array elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8892
     * @return the relative index of the first mismatch between the two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8893
     *         over the specified ranges, otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8894
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8895
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8896
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8897
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8898
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8899
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8900
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8901
     *         if either array or the comparator is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8902
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8903
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8904
    public static <T> int mismatch(
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8905
            T[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8906
            T[] b, int bFromIndex, int bToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8907
            Comparator<? super T> cmp) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8908
        Objects.requireNonNull(cmp);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8909
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8910
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8911
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8912
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8913
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8914
        int length = Math.min(aLength, bLength);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8915
        for (int i = 0; i < length; i++) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8916
            T oa = a[aFromIndex++];
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8917
            T ob = b[bFromIndex++];
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8918
            if (oa != ob) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8919
                // Null-value comparison is deferred to the comparator
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8920
                int v = cmp.compare(oa, ob);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8921
                if (v != 0) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8922
                    return i;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8923
                }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8924
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8925
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8926
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8927
        return aLength != bLength ? length : -1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8928
    }
44743
f0bbd698c486 8177789: fix collections framework links to point to java.util package doc
smarks
parents: 37685
diff changeset
  8929
}