src/java.base/share/classes/java/util/Arrays.java
author psandoz
Wed, 20 Dec 2017 09:14:52 -0800
changeset 48356 29e165bdf669
parent 47216 71c04702a3d5
child 49433 b6671a111395
permissions -rw-r--r--
8193085: Vectorize the nio Buffer equals and compareTo implementations Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
37685
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
     2
 * Copyright (c) 1997, 2016, 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
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    31
import java.lang.reflect.Array;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    32
import java.util.concurrent.ForkJoinPool;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    33
import java.util.function.BinaryOperator;
23354
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
    34
import java.util.function.Consumer;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    35
import java.util.function.DoubleBinaryOperator;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    36
import java.util.function.IntBinaryOperator;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    37
import java.util.function.IntFunction;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    38
import java.util.function.IntToDoubleFunction;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    39
import java.util.function.IntToLongFunction;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    40
import java.util.function.IntUnaryOperator;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    41
import java.util.function.LongBinaryOperator;
23354
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
    42
import java.util.function.UnaryOperator;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    43
import java.util.stream.DoubleStream;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    44
import java.util.stream.IntStream;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    45
import java.util.stream.LongStream;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    46
import java.util.stream.Stream;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    47
import java.util.stream.StreamSupport;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * 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
    51
 * sorting and searching). This class also contains a static factory
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * that allows arrays to be viewed as lists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    54
 * <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
    55
 * if the specified array reference is null, except where noted.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <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
    58
 * brief descriptions of the <i>implementations</i>. Such descriptions should
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * 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
    60
 * <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
    61
 * 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
    62
 * 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
    63
 * a MergeSort, but it does have to be <i>stable</i>.)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <p>This class is a member of the
44743
f0bbd698c486 8177789: fix collections framework links to point to java.util package doc
smarks
parents: 37685
diff changeset
    66
 * <a href="{@docRoot}/java/util/package-summary.html#CollectionsFramework">
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    69
 * @author Josh Bloch
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    70
 * @author Neal Gafter
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    71
 * @author John Rose
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    72
 * @since  1.2
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 */
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    74
public class Arrays {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
14925
72729557c226 8003981: Support Parallel Array Sorting - JEP 103
chegar
parents: 13795
diff changeset
    76
    /**
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    77
     * The minimum array length below which a parallel sorting
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    78
     * algorithm will not further partition the sorting task. Using
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    79
     * smaller sizes typically results in memory contention across
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    80
     * tasks that makes parallel speedups unlikely.
14925
72729557c226 8003981: Support Parallel Array Sorting - JEP 103
chegar
parents: 13795
diff changeset
    81
     */
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    82
    private static final int MIN_ARRAY_SORT_GRAN = 1 << 13;
14925
72729557c226 8003981: Support Parallel Array Sorting - JEP 103
chegar
parents: 13795
diff changeset
    83
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    // Suppresses default constructor, ensuring non-instantiability.
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    85
    private Arrays() {}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    87
    /**
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    88
     * A comparator that implements the natural ordering of a group of
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    89
     * mutually comparable elements. May be used when a supplied
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    90
     * comparator is null. To simplify code-sharing within underlying
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    91
     * implementations, the compare method only declares type Object
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    92
     * for its second argument.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    93
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    94
     * Arrays class implementor's note: It is an empirical matter
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    95
     * whether ComparableTimSort offers any performance benefit over
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    96
     * TimSort used with this comparator.  If not, you are better off
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    97
     * deleting or bypassing ComparableTimSort.  There is currently no
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    98
     * empirical case for separating them for parallel sorting, so all
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    99
     * public Object parallelSort methods use the same comparator
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   100
     * based implementation.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   101
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   102
    static final class NaturalOrder implements Comparator<Object> {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   103
        @SuppressWarnings("unchecked")
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   104
        public int compare(Object first, Object second) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   105
            return ((Comparable<Object>)first).compareTo(second);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   106
        }
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   107
        static final NaturalOrder INSTANCE = new NaturalOrder();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   108
    }
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   109
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
     * Checks that {@code fromIndex} and {@code toIndex} are in
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   112
     * the range and throws an exception if they aren't.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   113
     */
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
   114
    static void rangeCheck(int arrayLength, int fromIndex, int toIndex) {
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   115
        if (fromIndex > toIndex) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   116
            throw new IllegalArgumentException(
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   117
                    "fromIndex(" + fromIndex + ") > toIndex(" + toIndex + ")");
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   118
        }
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   119
        if (fromIndex < 0) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   120
            throw new ArrayIndexOutOfBoundsException(fromIndex);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   121
        }
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   122
        if (toIndex > arrayLength) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   123
            throw new ArrayIndexOutOfBoundsException(toIndex);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   124
        }
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
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   127
    /*
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   128
     * Sorting methods. Note that all public "sort" methods take the
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   129
     * same form: Performing argument checks if necessary, and then
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   130
     * expanding arguments into those required for the internal
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   131
     * implementation methods residing in other package-private
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   132
     * classes (except for legacyMergeSort, included in this class).
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   133
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   136
     * 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
   137
     *
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   138
     * <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
   139
     * 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
   140
     * 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
   141
     * 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
   142
     * faster than traditional (one-pivot) Quicksort implementations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @param a the array to be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    public static void sort(int[] a) {
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   147
        DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    /**
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   151
     * Sorts the specified range of the array into ascending order. The range
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   152
     * to be sorted extends from the index {@code fromIndex}, inclusive, to
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   153
     * 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
   154
     * the range to be sorted is empty.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   156
     * <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
   157
     * 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
   158
     * 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
   159
     * 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
   160
     * faster than traditional (one-pivot) Quicksort implementations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @param a the array to be sorted
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   163
     * @param fromIndex the index of the first element, inclusive, to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   164
     * @param toIndex the index of the last element, exclusive, to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   165
     *
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   166
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   167
     * @throws ArrayIndexOutOfBoundsException
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   168
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    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
   171
        rangeCheck(a.length, fromIndex, toIndex);
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   172
        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    /**
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   176
     * 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
   177
     *
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   178
     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   179
     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   180
     * offers O(n log(n)) performance on many data sets that cause other
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   181
     * quicksorts to degrade to quadratic performance, and is typically
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   182
     * faster than traditional (one-pivot) Quicksort implementations.
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   183
     *
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   184
     * @param a the array to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   185
     */
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   186
    public static void sort(long[] a) {
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   187
        DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   188
    }
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
     * Sorts the specified range of the array into ascending order. The range
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   192
     * to be sorted extends from the index {@code fromIndex}, inclusive, to
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   193
     * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   194
     * the range to be sorted is empty.
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   195
     *
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   196
     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   197
     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   198
     * offers O(n log(n)) performance on many data sets that cause other
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   199
     * quicksorts to degrade to quadratic performance, and is typically
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   200
     * faster than traditional (one-pivot) Quicksort implementations.
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   201
     *
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   202
     * @param a the array to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   203
     * @param fromIndex the index of the first element, inclusive, to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   204
     * @param toIndex the index of the last element, exclusive, to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   205
     *
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   206
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   207
     * @throws ArrayIndexOutOfBoundsException
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   208
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   209
     */
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   210
    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
   211
        rangeCheck(a.length, fromIndex, toIndex);
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   212
        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   213
    }
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
     * Sorts the specified array into ascending numerical order.
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   217
     *
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   218
     * <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
   219
     * 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
   220
     * 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
   221
     * 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
   222
     * faster than traditional (one-pivot) Quicksort implementations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * @param a the array to be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    public static void sort(short[] a) {
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   227
        DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    /**
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   231
     * Sorts the specified range of the array into ascending order. The range
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   232
     * to be sorted extends from the index {@code fromIndex}, inclusive, to
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   233
     * 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
   234
     * the range to be sorted is empty.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     *
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   236
     * <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
   237
     * 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
   238
     * 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
   239
     * 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
   240
     * faster than traditional (one-pivot) Quicksort implementations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * @param a the array to be sorted
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   243
     * @param fromIndex the index of the first element, inclusive, to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   244
     * @param toIndex the index of the last element, exclusive, to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   245
     *
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   246
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   247
     * @throws ArrayIndexOutOfBoundsException
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   248
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    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
   251
        rangeCheck(a.length, fromIndex, toIndex);
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   252
        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    /**
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   256
     * 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
   257
     *
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   258
     * <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
   259
     * 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
   260
     * 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
   261
     * 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
   262
     * faster than traditional (one-pivot) Quicksort implementations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @param a the array to be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    public static void sort(char[] a) {
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   267
        DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    /**
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   271
     * Sorts the specified range of the array into ascending order. The range
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   272
     * to be sorted extends from the index {@code fromIndex}, inclusive, to
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   273
     * 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
   274
     * the range to be sorted is empty.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     *
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   276
     * <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
   277
     * 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
   278
     * 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
   279
     * 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
   280
     * faster than traditional (one-pivot) Quicksort implementations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * @param a the array to be sorted
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   283
     * @param fromIndex the index of the first element, inclusive, to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   284
     * @param toIndex the index of the last element, exclusive, to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   285
     *
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   286
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   287
     * @throws ArrayIndexOutOfBoundsException
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   288
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    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
   291
        rangeCheck(a.length, fromIndex, toIndex);
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   292
        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    /**
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   296
     * 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
   297
     *
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   298
     * <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
   299
     * 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
   300
     * 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
   301
     * 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
   302
     * faster than traditional (one-pivot) Quicksort implementations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * @param a the array to be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    public static void sort(byte[] a) {
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   307
        DualPivotQuicksort.sort(a, 0, a.length - 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    /**
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   311
     * Sorts the specified range of the array into ascending order. The range
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   312
     * to be sorted extends from the index {@code fromIndex}, inclusive, to
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   313
     * 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
   314
     * the range to be sorted is empty.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     *
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   316
     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   317
     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   318
     * offers O(n log(n)) performance on many data sets that cause other
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   319
     * quicksorts to degrade to quadratic performance, and is typically
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   320
     * faster than traditional (one-pivot) Quicksort implementations.
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   321
     *
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   322
     * @param a the array to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   323
     * @param fromIndex the index of the first element, inclusive, to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   324
     * @param toIndex the index of the last element, exclusive, to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   325
     *
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   326
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   327
     * @throws ArrayIndexOutOfBoundsException
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   328
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   329
     */
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   330
    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
   331
        rangeCheck(a.length, fromIndex, toIndex);
d229d56fd918 6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents: 5506
diff changeset
   332
        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1);
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   333
    }
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
     * Sorts the specified array into ascending numerical order.
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   337
     *
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   338
     * <p>The {@code <} relation does not provide a total order on all float
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   339
     * 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
   340
     * value compares neither less than, greater than, nor equal to any value,
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   341
     * even itself. This method uses the total order imposed by the method
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   342
     * {@link Float#compareTo}: {@code -0.0f} is treated as less than value
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   343
     * {@code 0.0f} and {@code Float.NaN} is considered greater than any
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   344
     * other value and all {@code Float.NaN} values are considered equal.
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   345
     *
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   346
     * <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
   347
     * 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
   348
     * 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
   349
     * 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
   350
     * faster than traditional (one-pivot) Quicksort implementations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * @param a the array to be sorted
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   353
     */
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   354
    public static void sort(float[] a) {
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   355
        DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   356
    }
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
     * Sorts the specified range of the array into ascending order. The range
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   360
     * to be sorted extends from the index {@code fromIndex}, inclusive, to
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   361
     * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   362
     * the range to be sorted is empty.
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   363
     *
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   364
     * <p>The {@code <} relation does not provide a total order on all float
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   365
     * 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
   366
     * value compares neither less than, greater than, nor equal to any value,
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   367
     * even itself. This method uses the total order imposed by the method
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   368
     * {@link Float#compareTo}: {@code -0.0f} is treated as less than value
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   369
     * {@code 0.0f} and {@code Float.NaN} is considered greater than any
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   370
     * other value and all {@code Float.NaN} values are considered equal.
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   371
     *
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   372
     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   373
     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   374
     * offers O(n log(n)) performance on many data sets that cause other
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   375
     * quicksorts to degrade to quadratic performance, and is typically
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   376
     * faster than traditional (one-pivot) Quicksort implementations.
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   377
     *
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   378
     * @param a the array to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   379
     * @param fromIndex the index of the first element, inclusive, to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   380
     * @param toIndex the index of the last element, exclusive, to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   381
     *
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   382
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   383
     * @throws ArrayIndexOutOfBoundsException
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   384
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     */
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   386
    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
   387
        rangeCheck(a.length, fromIndex, toIndex);
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   388
        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    /**
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   392
     * 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
   393
     *
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   394
     * <p>The {@code <} relation does not provide a total order on all double
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   395
     * 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
   396
     * value compares neither less than, greater than, nor equal to any value,
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   397
     * even itself. This method uses the total order imposed by the method
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   398
     * {@link Double#compareTo}: {@code -0.0d} is treated as less than value
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   399
     * {@code 0.0d} and {@code Double.NaN} is considered greater than any
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   400
     * 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
   401
     *
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   402
     * <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
   403
     * 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
   404
     * 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
   405
     * 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
   406
     * faster than traditional (one-pivot) Quicksort implementations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * @param a the array to be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    public static void sort(double[] a) {
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   411
        DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    /**
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   415
     * Sorts the specified range of the array into ascending order. The range
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   416
     * to be sorted extends from the index {@code fromIndex}, inclusive, to
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   417
     * 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
   418
     * the range to be sorted is empty.
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   419
     *
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   420
     * <p>The {@code <} relation does not provide a total order on all double
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   421
     * 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
   422
     * value compares neither less than, greater than, nor equal to any value,
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   423
     * even itself. This method uses the total order imposed by the method
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   424
     * {@link Double#compareTo}: {@code -0.0d} is treated as less than value
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   425
     * {@code 0.0d} and {@code Double.NaN} is considered greater than any
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   426
     * 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
   427
     *
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   428
     * <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
   429
     * 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
   430
     * 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
   431
     * 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
   432
     * faster than traditional (one-pivot) Quicksort implementations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * @param a the array to be sorted
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   435
     * @param fromIndex the index of the first element, inclusive, to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   436
     * @param toIndex the index of the last element, exclusive, to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   437
     *
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   438
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   439
     * @throws ArrayIndexOutOfBoundsException
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   440
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     */
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   442
    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
   443
        rangeCheck(a.length, fromIndex, toIndex);
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   444
        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   445
    }
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
     * Sorts the specified array into ascending numerical order.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   449
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   450
     * @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
   451
     * 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
   452
     * 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
   453
     * sorted using the appropriate {@link Arrays#sort(byte[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   454
     * 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
   455
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   456
     * Arrays#sort(byte[]) Arrays.sort} method. The algorithm requires a
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   457
     * 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
   458
     * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   459
     * execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   460
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   461
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   462
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   463
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   464
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   465
    public static void parallelSort(byte[] a) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   466
        int n = a.length, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   467
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   468
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   469
            DualPivotQuicksort.sort(a, 0, n - 1);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   470
        else
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   471
            new ArraysParallelSortHelpers.FJByte.Sorter
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   472
                (null, a, new byte[n], 0, n, 0,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   473
                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   474
                 MIN_ARRAY_SORT_GRAN : g).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   475
    }
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
     * Sorts the specified range of the array into ascending numerical order.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   479
     * The range to be sorted extends from the index {@code fromIndex},
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   480
     * inclusive, to the index {@code toIndex}, exclusive. If
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   481
     * {@code fromIndex == toIndex}, the range to be sorted is empty.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   482
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   483
     * @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
   484
     * 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
   485
     * 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
   486
     * sorted using the appropriate {@link Arrays#sort(byte[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   487
     * 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
   488
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   489
     * Arrays#sort(byte[]) Arrays.sort} method. The algorithm requires a working
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   490
     * 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
   491
     * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   492
     * used to execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   493
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   494
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   495
     * @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
   496
     * @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
   497
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   498
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   499
     * @throws ArrayIndexOutOfBoundsException
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   500
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   501
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   502
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   503
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   504
    public static void parallelSort(byte[] a, int fromIndex, int toIndex) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   505
        rangeCheck(a.length, fromIndex, toIndex);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   506
        int n = toIndex - fromIndex, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   507
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   508
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   509
            DualPivotQuicksort.sort(a, fromIndex, toIndex - 1);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   510
        else
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   511
            new ArraysParallelSortHelpers.FJByte.Sorter
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   512
                (null, a, new byte[n], fromIndex, n, 0,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   513
                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   514
                 MIN_ARRAY_SORT_GRAN : g).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   515
    }
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
     * Sorts the specified array into ascending numerical order.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   519
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   520
     * @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
   521
     * 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
   522
     * 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
   523
     * sorted using the appropriate {@link Arrays#sort(char[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   524
     * 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
   525
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   526
     * Arrays#sort(char[]) Arrays.sort} method. The algorithm requires a
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   527
     * 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
   528
     * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   529
     * execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   530
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   531
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   532
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   533
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   534
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   535
    public static void parallelSort(char[] a) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   536
        int n = a.length, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   537
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   538
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   539
            DualPivotQuicksort.sort(a, 0, n - 1, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   540
        else
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   541
            new ArraysParallelSortHelpers.FJChar.Sorter
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   542
                (null, a, new char[n], 0, n, 0,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   543
                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   544
                 MIN_ARRAY_SORT_GRAN : g).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   545
    }
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
     * Sorts the specified range of the array into ascending numerical order.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   549
     * The range to be sorted extends from the index {@code fromIndex},
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   550
     * inclusive, to the index {@code toIndex}, exclusive. If
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   551
     * {@code fromIndex == toIndex}, the range to be sorted is empty.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   552
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   553
      @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
   554
     * 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
   555
     * 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
   556
     * sorted using the appropriate {@link Arrays#sort(char[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   557
     * 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
   558
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   559
     * Arrays#sort(char[]) Arrays.sort} method. The algorithm requires a working
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   560
     * 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
   561
     * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   562
     * used to execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   563
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   564
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   565
     * @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
   566
     * @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
   567
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   568
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   569
     * @throws ArrayIndexOutOfBoundsException
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   570
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   571
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   572
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   573
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   574
    public static void parallelSort(char[] a, int fromIndex, int toIndex) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   575
        rangeCheck(a.length, fromIndex, toIndex);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   576
        int n = toIndex - fromIndex, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   577
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   578
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   579
            DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   580
        else
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   581
            new ArraysParallelSortHelpers.FJChar.Sorter
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   582
                (null, a, new char[n], fromIndex, n, 0,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   583
                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   584
                 MIN_ARRAY_SORT_GRAN : g).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   585
    }
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
     * Sorts the specified array into ascending numerical order.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   589
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   590
     * @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
   591
     * 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
   592
     * 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
   593
     * sorted using the appropriate {@link Arrays#sort(short[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   594
     * 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
   595
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   596
     * Arrays#sort(short[]) Arrays.sort} method. The algorithm requires a
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   597
     * 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
   598
     * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   599
     * execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   600
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   601
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   602
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   603
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   604
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   605
    public static void parallelSort(short[] a) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   606
        int n = a.length, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   607
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   608
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   609
            DualPivotQuicksort.sort(a, 0, n - 1, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   610
        else
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   611
            new ArraysParallelSortHelpers.FJShort.Sorter
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   612
                (null, a, new short[n], 0, n, 0,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   613
                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   614
                 MIN_ARRAY_SORT_GRAN : g).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   615
    }
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
     * Sorts the specified range of the array into ascending numerical order.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   619
     * The range to be sorted extends from the index {@code fromIndex},
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   620
     * inclusive, to the index {@code toIndex}, exclusive. If
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   621
     * {@code fromIndex == toIndex}, the range to be sorted is empty.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   622
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   623
     * @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
   624
     * 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
   625
     * 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
   626
     * sorted using the appropriate {@link Arrays#sort(short[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   627
     * 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
   628
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   629
     * Arrays#sort(short[]) Arrays.sort} method. The algorithm requires a working
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   630
     * 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
   631
     * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   632
     * used to execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   633
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   634
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   635
     * @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
   636
     * @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
   637
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   638
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   639
     * @throws ArrayIndexOutOfBoundsException
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   640
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   641
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   642
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   643
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   644
    public static void parallelSort(short[] a, int fromIndex, int toIndex) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   645
        rangeCheck(a.length, fromIndex, toIndex);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   646
        int n = toIndex - fromIndex, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   647
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   648
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   649
            DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   650
        else
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   651
            new ArraysParallelSortHelpers.FJShort.Sorter
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   652
                (null, a, new short[n], fromIndex, n, 0,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   653
                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   654
                 MIN_ARRAY_SORT_GRAN : g).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   655
    }
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
     * Sorts the specified array into ascending numerical order.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   659
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   660
     * @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
   661
     * 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
   662
     * 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
   663
     * sorted using the appropriate {@link Arrays#sort(int[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   664
     * 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
   665
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   666
     * Arrays#sort(int[]) Arrays.sort} method. The algorithm requires a
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   667
     * 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
   668
     * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   669
     * execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   670
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   671
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   672
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   673
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   674
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   675
    public static void parallelSort(int[] a) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   676
        int n = a.length, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   677
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   678
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   679
            DualPivotQuicksort.sort(a, 0, n - 1, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   680
        else
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   681
            new ArraysParallelSortHelpers.FJInt.Sorter
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   682
                (null, a, new int[n], 0, n, 0,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   683
                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   684
                 MIN_ARRAY_SORT_GRAN : g).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   685
    }
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
     * Sorts the specified range of the array into ascending numerical order.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   689
     * The range to be sorted extends from the index {@code fromIndex},
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   690
     * inclusive, to the index {@code toIndex}, exclusive. If
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   691
     * {@code fromIndex == toIndex}, the range to be sorted is empty.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   692
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   693
     * @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
   694
     * 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
   695
     * 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
   696
     * sorted using the appropriate {@link Arrays#sort(int[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   697
     * 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
   698
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   699
     * Arrays#sort(int[]) Arrays.sort} method. The algorithm requires a working
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   700
     * 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
   701
     * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   702
     * used to execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   703
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   704
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   705
     * @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
   706
     * @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
   707
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   708
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   709
     * @throws ArrayIndexOutOfBoundsException
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   710
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   711
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   712
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   713
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   714
    public static void parallelSort(int[] a, int fromIndex, int toIndex) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   715
        rangeCheck(a.length, fromIndex, toIndex);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   716
        int n = toIndex - fromIndex, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   717
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   718
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   719
            DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   720
        else
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   721
            new ArraysParallelSortHelpers.FJInt.Sorter
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   722
                (null, a, new int[n], fromIndex, n, 0,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   723
                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   724
                 MIN_ARRAY_SORT_GRAN : g).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   725
    }
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
     * Sorts the specified array into ascending numerical order.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   729
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   730
     * @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
   731
     * 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
   732
     * 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
   733
     * sorted using the appropriate {@link Arrays#sort(long[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   734
     * 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
   735
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   736
     * Arrays#sort(long[]) Arrays.sort} method. The algorithm requires a
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   737
     * 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
   738
     * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   739
     * execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   740
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   741
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   742
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   743
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   744
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   745
    public static void parallelSort(long[] a) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   746
        int n = a.length, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   747
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   748
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   749
            DualPivotQuicksort.sort(a, 0, n - 1, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   750
        else
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   751
            new ArraysParallelSortHelpers.FJLong.Sorter
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   752
                (null, a, new long[n], 0, n, 0,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   753
                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   754
                 MIN_ARRAY_SORT_GRAN : g).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   755
    }
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
     * Sorts the specified range of the array into ascending numerical order.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   759
     * The range to be sorted extends from the index {@code fromIndex},
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   760
     * inclusive, to the index {@code toIndex}, exclusive. If
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   761
     * {@code fromIndex == toIndex}, the range to be sorted is empty.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   762
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   763
     * @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
   764
     * 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
   765
     * 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
   766
     * sorted using the appropriate {@link Arrays#sort(long[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   767
     * 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
   768
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   769
     * Arrays#sort(long[]) Arrays.sort} method. The algorithm requires a working
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   770
     * 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
   771
     * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   772
     * used to execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   773
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   774
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   775
     * @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
   776
     * @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
   777
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   778
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   779
     * @throws ArrayIndexOutOfBoundsException
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   780
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   781
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   782
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   783
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   784
    public static void parallelSort(long[] a, int fromIndex, int toIndex) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   785
        rangeCheck(a.length, fromIndex, toIndex);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   786
        int n = toIndex - fromIndex, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   787
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   788
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   789
            DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   790
        else
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   791
            new ArraysParallelSortHelpers.FJLong.Sorter
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   792
                (null, a, new long[n], fromIndex, n, 0,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   793
                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   794
                 MIN_ARRAY_SORT_GRAN : g).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   795
    }
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
     * Sorts the specified array into ascending numerical order.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   799
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   800
     * <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
   801
     * 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
   802
     * 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
   803
     * 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
   804
     * {@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
   805
     * {@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
   806
     * other value and all {@code Float.NaN} values are considered equal.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   807
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   808
     * @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
   809
     * 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
   810
     * 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
   811
     * sorted using the appropriate {@link Arrays#sort(float[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   812
     * 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
   813
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   814
     * Arrays#sort(float[]) Arrays.sort} method. The algorithm requires a
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   815
     * 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
   816
     * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   817
     * execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   818
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   819
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   820
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   821
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   822
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   823
    public static void parallelSort(float[] a) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   824
        int n = a.length, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   825
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   826
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   827
            DualPivotQuicksort.sort(a, 0, n - 1, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   828
        else
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   829
            new ArraysParallelSortHelpers.FJFloat.Sorter
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   830
                (null, a, new float[n], 0, n, 0,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   831
                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   832
                 MIN_ARRAY_SORT_GRAN : g).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   833
    }
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
     * Sorts the specified range of the array into ascending numerical order.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   837
     * The range to be sorted extends from the index {@code fromIndex},
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   838
     * inclusive, to the index {@code toIndex}, exclusive. If
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   839
     * {@code fromIndex == toIndex}, the range to be sorted is empty.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   840
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   841
     * <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
   842
     * 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
   843
     * 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
   844
     * 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
   845
     * {@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
   846
     * {@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
   847
     * other value and all {@code Float.NaN} values are considered equal.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   848
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   849
     * @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
   850
     * 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
   851
     * 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
   852
     * sorted using the appropriate {@link Arrays#sort(float[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   853
     * 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
   854
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   855
     * Arrays#sort(float[]) Arrays.sort} method. The algorithm requires a working
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   856
     * 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
   857
     * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   858
     * used to execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   859
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   860
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   861
     * @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
   862
     * @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
   863
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   864
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   865
     * @throws ArrayIndexOutOfBoundsException
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   866
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   867
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   868
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   869
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   870
    public static void parallelSort(float[] a, int fromIndex, int toIndex) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   871
        rangeCheck(a.length, fromIndex, toIndex);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   872
        int n = toIndex - fromIndex, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   873
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   874
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   875
            DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   876
        else
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   877
            new ArraysParallelSortHelpers.FJFloat.Sorter
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   878
                (null, a, new float[n], fromIndex, n, 0,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   879
                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   880
                 MIN_ARRAY_SORT_GRAN : g).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   881
    }
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
     * Sorts the specified array into ascending numerical order.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   885
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   886
     * <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
   887
     * 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
   888
     * 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
   889
     * 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
   890
     * {@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
   891
     * {@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
   892
     * other value and all {@code Double.NaN} values are considered equal.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   893
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   894
     * @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
   895
     * 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
   896
     * 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
   897
     * sorted using the appropriate {@link Arrays#sort(double[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   898
     * 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
   899
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   900
     * Arrays#sort(double[]) Arrays.sort} method. The algorithm requires a
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   901
     * 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
   902
     * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   903
     * execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   904
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   905
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   906
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   907
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   908
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   909
    public static void parallelSort(double[] a) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   910
        int n = a.length, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   911
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   912
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   913
            DualPivotQuicksort.sort(a, 0, n - 1, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   914
        else
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   915
            new ArraysParallelSortHelpers.FJDouble.Sorter
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   916
                (null, a, new double[n], 0, n, 0,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   917
                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   918
                 MIN_ARRAY_SORT_GRAN : g).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   919
    }
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
     * Sorts the specified range of the array into ascending numerical order.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   923
     * The range to be sorted extends from the index {@code fromIndex},
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   924
     * inclusive, to the index {@code toIndex}, exclusive. If
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   925
     * {@code fromIndex == toIndex}, the range to be sorted is empty.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   926
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   927
     * <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
   928
     * 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
   929
     * 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
   930
     * 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
   931
     * {@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
   932
     * {@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
   933
     * other value and all {@code Double.NaN} values are considered equal.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   934
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   935
     * @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
   936
     * 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
   937
     * 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
   938
     * sorted using the appropriate {@link Arrays#sort(double[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   939
     * 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
   940
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   941
     * Arrays#sort(double[]) Arrays.sort} method. The algorithm requires a working
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   942
     * 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
   943
     * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   944
     * used to execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   945
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   946
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   947
     * @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
   948
     * @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
   949
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   950
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   951
     * @throws ArrayIndexOutOfBoundsException
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   952
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   953
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   954
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   955
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   956
    public static void parallelSort(double[] a, int fromIndex, int toIndex) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   957
        rangeCheck(a.length, fromIndex, toIndex);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   958
        int n = toIndex - fromIndex, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   959
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   960
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   961
            DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   962
        else
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   963
            new ArraysParallelSortHelpers.FJDouble.Sorter
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   964
                (null, a, new double[n], fromIndex, n, 0,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   965
                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   966
                 MIN_ARRAY_SORT_GRAN : g).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   967
    }
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
     * Sorts the specified array of objects into ascending order, according
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   971
     * to the {@linkplain Comparable natural ordering} of its elements.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   972
     * All elements in the array must implement the {@link Comparable}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   973
     * interface.  Furthermore, all elements in the array must be
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   974
     * <i>mutually comparable</i> (that is, {@code e1.compareTo(e2)} must
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   975
     * not throw a {@code ClassCastException} for any elements {@code e1}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   976
     * and {@code e2} in the array).
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   977
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   978
     * <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
   979
     * not be reordered as a result of the sort.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   980
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   981
     * @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
   982
     * 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
   983
     * 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
   984
     * sorted using the appropriate {@link Arrays#sort(Object[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   985
     * 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
   986
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   987
     * Arrays#sort(Object[]) Arrays.sort} method. The algorithm requires a
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   988
     * 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
   989
     * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   990
     * execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   991
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
   992
     * @param <T> the class of the objects to be sorted
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   993
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   994
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   995
     * @throws ClassCastException if the array contains elements that are not
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   996
     *         <i>mutually comparable</i> (for example, strings and integers)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   997
     * @throws IllegalArgumentException (optional) if the natural
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   998
     *         ordering of the array elements is found to violate the
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   999
     *         {@link Comparable} contract
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1000
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1001
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1002
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1003
    @SuppressWarnings("unchecked")
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1004
    public static <T extends Comparable<? super T>> void parallelSort(T[] a) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1005
        int n = a.length, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1006
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1007
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1008
            TimSort.sort(a, 0, n, NaturalOrder.INSTANCE, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1009
        else
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 21278
diff changeset
  1010
            new ArraysParallelSortHelpers.FJObject.Sorter<>
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1011
                (null, a,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1012
                 (T[])Array.newInstance(a.getClass().getComponentType(), n),
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1013
                 0, n, 0, ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1014
                 MIN_ARRAY_SORT_GRAN : g, NaturalOrder.INSTANCE).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1015
    }
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
     * Sorts the specified range of the specified array of objects into
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1019
     * ascending order, according to the
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1020
     * {@linkplain Comparable natural ordering} of its
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1021
     * elements.  The range to be sorted extends from index
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1022
     * {@code fromIndex}, inclusive, to index {@code toIndex}, exclusive.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1023
     * (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
  1024
     * elements in this range must implement the {@link Comparable}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1025
     * interface.  Furthermore, all elements in this range must be <i>mutually
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1026
     * 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
  1027
     * {@code ClassCastException} for any elements {@code e1} and
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1028
     * {@code e2} in the array).
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1029
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1030
     * <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
  1031
     * not be reordered as a result of the sort.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1032
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1033
     * @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
  1034
     * 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
  1035
     * 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
  1036
     * sorted using the appropriate {@link Arrays#sort(Object[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1037
     * 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
  1038
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1039
     * Arrays#sort(Object[]) Arrays.sort} method. The algorithm requires a working
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1040
     * 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
  1041
     * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1042
     * used to execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1043
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  1044
     * @param <T> the class of the objects to be sorted
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1045
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1046
     * @param fromIndex the index of the first element (inclusive) to be
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1047
     *        sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1048
     * @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
  1049
     * @throws IllegalArgumentException if {@code fromIndex > toIndex} or
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1050
     *         (optional) if the natural ordering of the array elements is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1051
     *         found to violate the {@link Comparable} contract
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1052
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1053
     *         {@code toIndex > a.length}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1054
     * @throws ClassCastException if the array contains elements that are
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1055
     *         not <i>mutually comparable</i> (for example, strings and
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1056
     *         integers).
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1057
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1058
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1059
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1060
    @SuppressWarnings("unchecked")
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1061
    public static <T extends Comparable<? super T>>
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1062
    void parallelSort(T[] a, int fromIndex, int toIndex) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1063
        rangeCheck(a.length, fromIndex, toIndex);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1064
        int n = toIndex - fromIndex, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1065
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1066
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1067
            TimSort.sort(a, fromIndex, toIndex, NaturalOrder.INSTANCE, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1068
        else
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 21278
diff changeset
  1069
            new ArraysParallelSortHelpers.FJObject.Sorter<>
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1070
                (null, a,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1071
                 (T[])Array.newInstance(a.getClass().getComponentType(), n),
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1072
                 fromIndex, n, 0, ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1073
                 MIN_ARRAY_SORT_GRAN : g, NaturalOrder.INSTANCE).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1074
    }
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
     * 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
  1078
     * the specified comparator.  All elements in the array must be
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1079
     * <i>mutually comparable</i> by the specified comparator (that is,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1080
     * {@code c.compare(e1, e2)} must not throw a {@code ClassCastException}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1081
     * for any elements {@code e1} and {@code e2} in the array).
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1082
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1083
     * <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
  1084
     * not be reordered as a result of the sort.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1085
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1086
     * @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
  1087
     * 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
  1088
     * 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
  1089
     * sorted using the appropriate {@link Arrays#sort(Object[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1090
     * 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
  1091
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1092
     * Arrays#sort(Object[]) Arrays.sort} method. The algorithm requires a
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1093
     * 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
  1094
     * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1095
     * execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1096
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  1097
     * @param <T> the class of the objects to be sorted
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1098
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1099
     * @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
  1100
     *        {@code null} value indicates that the elements'
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1101
     *        {@linkplain Comparable natural ordering} should be used.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1102
     * @throws ClassCastException if the array contains elements that are
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1103
     *         not <i>mutually comparable</i> using the specified comparator
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1104
     * @throws IllegalArgumentException (optional) if the comparator is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1105
     *         found to violate the {@link java.util.Comparator} contract
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1106
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1107
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1108
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1109
    @SuppressWarnings("unchecked")
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1110
    public static <T> void parallelSort(T[] a, Comparator<? super T> cmp) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1111
        if (cmp == null)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1112
            cmp = NaturalOrder.INSTANCE;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1113
        int n = a.length, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1114
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1115
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1116
            TimSort.sort(a, 0, n, cmp, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1117
        else
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 21278
diff changeset
  1118
            new ArraysParallelSortHelpers.FJObject.Sorter<>
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1119
                (null, a,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1120
                 (T[])Array.newInstance(a.getClass().getComponentType(), n),
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1121
                 0, n, 0, ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1122
                 MIN_ARRAY_SORT_GRAN : g, cmp).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1123
    }
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
     * Sorts the specified range of the specified array of objects according
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1127
     * 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
  1128
     * sorted extends from index {@code fromIndex}, inclusive, to index
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1129
     * {@code toIndex}, exclusive.  (If {@code fromIndex==toIndex}, the
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1130
     * 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
  1131
     * <i>mutually comparable</i> by the specified comparator (that is,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1132
     * {@code c.compare(e1, e2)} must not throw a {@code ClassCastException}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1133
     * for any elements {@code e1} and {@code e2} in the range).
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1134
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1135
     * <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
  1136
     * not be reordered as a result of the sort.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1137
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1138
     * @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
  1139
     * 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
  1140
     * 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
  1141
     * sorted using the appropriate {@link Arrays#sort(Object[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1142
     * 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
  1143
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1144
     * Arrays#sort(Object[]) Arrays.sort} method. The algorithm requires a working
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1145
     * 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
  1146
     * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1147
     * used to execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1148
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  1149
     * @param <T> the class of the objects to be sorted
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1150
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1151
     * @param fromIndex the index of the first element (inclusive) to be
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1152
     *        sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1153
     * @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
  1154
     * @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
  1155
     *        {@code null} value indicates that the elements'
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1156
     *        {@linkplain Comparable natural ordering} should be used.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1157
     * @throws IllegalArgumentException if {@code fromIndex > toIndex} or
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1158
     *         (optional) if the natural ordering of the array elements is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1159
     *         found to violate the {@link Comparable} contract
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1160
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1161
     *         {@code toIndex > a.length}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1162
     * @throws ClassCastException if the array contains elements that are
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1163
     *         not <i>mutually comparable</i> (for example, strings and
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1164
     *         integers).
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1165
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1166
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1167
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1168
    @SuppressWarnings("unchecked")
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1169
    public static <T> void parallelSort(T[] a, int fromIndex, int toIndex,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1170
                                        Comparator<? super T> cmp) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1171
        rangeCheck(a.length, fromIndex, toIndex);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1172
        if (cmp == null)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1173
            cmp = NaturalOrder.INSTANCE;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1174
        int n = toIndex - fromIndex, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1175
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1176
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1177
            TimSort.sort(a, fromIndex, toIndex, cmp, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1178
        else
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 21278
diff changeset
  1179
            new ArraysParallelSortHelpers.FJObject.Sorter<>
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1180
                (null, a,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1181
                 (T[])Array.newInstance(a.getClass().getComponentType(), n),
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1182
                 fromIndex, n, 0, ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1183
                 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
  1184
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
  1186
    /*
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
  1187
     * Sorting of complex type arrays.
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
  1188
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1190
    /**
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1191
     * 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
  1192
     * 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
  1193
     * 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
  1194
     * 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
  1195
     */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1196
    static final class LegacyMergeSort {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1197
        private static final boolean userRequested =
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1198
            java.security.AccessController.doPrivileged(
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1199
                new sun.security.action.GetBooleanAction(
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1200
                    "java.util.Arrays.useLegacyMergeSort")).booleanValue();
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1201
    }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1202
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
    /**
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1204
     * 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
  1205
     * 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
  1206
     * 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
  1207
     * 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
  1208
     * <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
  1209
     * 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
  1210
     * and {@code e2} in the array).
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1211
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1212
     * <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
  1213
     * not be reordered as a result of the sort.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
     *
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1215
     * <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
  1216
     * 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
  1217
     * 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
  1218
     * 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
  1219
     * 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
  1220
     * implementation requires approximately n comparisons.  Temporary
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1221
     * 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
  1222
     * 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
  1223
     * arrays.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
     *
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1225
     * <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
  1226
     * 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
  1227
     * 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
  1228
     * 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
  1229
     * 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
  1230
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1231
     * <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
  1232
     * (<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
  1233
     * 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
  1234
     * 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
  1235
     * 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
  1236
     * January 1993.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
     * @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
  1239
     * @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
  1240
     *         <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
  1241
     * @throws IllegalArgumentException (optional) if the natural
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1242
     *         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
  1243
     *         {@link Comparable} contract
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
    public static void sort(Object[] a) {
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1246
        if (LegacyMergeSort.userRequested)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1247
            legacyMergeSort(a);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1248
        else
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1249
            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
  1250
    }
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
    /** To be removed in a future release. */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1253
    private static void legacyMergeSort(Object[] a) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1254
        Object[] aux = a.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
        mergeSort(aux, a, 0, a.length, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
     * Sorts the specified range of the specified array of objects into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
     * ascending order, according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
     * {@linkplain Comparable natural ordering} of its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
     * 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
  1263
     * {@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
  1264
     * (If {@code fromIndex==toIndex}, the range to be sorted is empty.)  All
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
     * elements in this range must implement the {@link Comparable}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
     * 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
  1267
     * 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
  1268
     * {@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
  1269
     * {@code e2} in the array).
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1270
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1271
     * <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
  1272
     * not be reordered as a result of the sort.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
     *
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1274
     * <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
  1275
     * 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
  1276
     * 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
  1277
     * 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
  1278
     * 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
  1279
     * implementation requires approximately n comparisons.  Temporary
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1280
     * 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
  1281
     * 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
  1282
     * arrays.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
     *
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1284
     * <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
  1285
     * 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
  1286
     * 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
  1287
     * 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
  1288
     * 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
  1289
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1290
     * <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
  1291
     * (<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
  1292
     * 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
  1293
     * 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
  1294
     * 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
  1295
     * January 1993.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
     * @param a the array to be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
     *        sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
     * @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
  1301
     * @throws IllegalArgumentException if {@code fromIndex > toIndex} or
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1302
     *         (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
  1303
     *         found to violate the {@link Comparable} contract
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1304
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1305
     *         {@code toIndex > a.length}
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1306
     * @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
  1307
     *         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
  1308
     *         integers).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
    public static void sort(Object[] a, int fromIndex, int toIndex) {
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1311
        rangeCheck(a.length, fromIndex, toIndex);
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1312
        if (LegacyMergeSort.userRequested)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1313
            legacyMergeSort(a, fromIndex, toIndex);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1314
        else
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1315
            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
  1316
    }
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
    /** To be removed in a future release. */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1319
    private static void legacyMergeSort(Object[] a,
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1320
                                        int fromIndex, int toIndex) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
        Object[] aux = copyOfRange(a, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
        mergeSort(aux, a, fromIndex, toIndex, -fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
     * 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
  1327
     * used in preference to mergesort.
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1328
     * To be removed in a future release.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
    private static final int INSERTIONSORT_THRESHOLD = 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
     * Src is the source array that starts at index 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
     * Dest is the (possibly larger) array destination with a possible offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
     * low is the index in dest to start sorting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
     * high is the end index in dest to end sorting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
     * 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
  1338
     * To be removed in a future release.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
     */
13795
73850c397272 7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents: 12448
diff changeset
  1340
    @SuppressWarnings({"unchecked", "rawtypes"})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
    private static void mergeSort(Object[] src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
                                  Object[] dest,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
                                  int low,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
                                  int high,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
                                  int off) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
        int length = high - low;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
        // Insertion sort on smallest arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
        if (length < INSERTIONSORT_THRESHOLD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
            for (int i=low; i<high; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
                for (int j=i; j>low &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
                         ((Comparable) dest[j-1]).compareTo(dest[j])>0; j--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
                    swap(dest, j, j-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
        // Recursively sort halves of dest into src
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
        int destLow  = low;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
        int destHigh = high;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
        low  += off;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
        high += off;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
        int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
        mergeSort(dest, src, low, mid, -off);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
        mergeSort(dest, src, mid, high, -off);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
        // If list is already sorted, just copy from src to dest.  This is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
        // optimization that results in faster sorts for nearly ordered lists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
        if (((Comparable)src[mid-1]).compareTo(src[mid]) <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
            System.arraycopy(src, low, dest, destLow, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
        // Merge sorted halves (now in src) into dest
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
        for(int i = destLow, p = low, q = mid; i < destHigh; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
            if (q >= high || p < mid && ((Comparable)src[p]).compareTo(src[q])<=0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
                dest[i] = src[p++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
                dest[i] = src[q++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
        }
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
     * Swaps x[a] with x[b].
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
    private static void swap(Object[] x, int a, int b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
        Object t = x[a];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
        x[a] = x[b];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
        x[b] = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
     * Sorts the specified array of objects according to the order induced by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
     * the specified comparator.  All elements in the array must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
     * <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
  1395
     * {@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
  1396
     * 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
  1397
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1398
     * <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
  1399
     * not be reordered as a result of the sort.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
     *
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1401
     * <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
  1402
     * 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
  1403
     * 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
  1404
     * 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
  1405
     * 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
  1406
     * implementation requires approximately n comparisons.  Temporary
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1407
     * 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
  1408
     * 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
  1409
     * arrays.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
     *
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1411
     * <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
  1412
     * 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
  1413
     * 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
  1414
     * 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
  1415
     * 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
  1416
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1417
     * <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
  1418
     * (<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
  1419
     * 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
  1420
     * 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
  1421
     * 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
  1422
     * January 1993.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  1424
     * @param <T> the class of the objects to be sorted
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
     * @param a the array to be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
     * @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
  1427
     *        {@code null} value indicates that the elements'
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
     *        {@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
  1429
     * @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
  1430
     *         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
  1431
     * @throws IllegalArgumentException (optional) if the comparator is
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1432
     *         found to violate the {@link Comparator} contract
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
    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
  1435
        if (c == null) {
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1436
            sort(a);
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1437
        } else {
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1438
            if (LegacyMergeSort.userRequested)
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1439
                legacyMergeSort(a, c);
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1440
            else
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1441
                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
  1442
        }
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1443
    }
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
    /** To be removed in a future release. */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1446
    private static <T> void legacyMergeSort(T[] a, Comparator<? super T> c) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1447
        T[] aux = a.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
        if (c==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
            mergeSort(aux, a, 0, a.length, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
            mergeSort(aux, a, 0, a.length, 0, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
     * Sorts the specified range of the specified array of objects according
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
     * 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
  1457
     * 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
  1458
     * {@code toIndex}, exclusive.  (If {@code fromIndex==toIndex}, the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
     * range to be sorted is empty.)  All elements in the range must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
     * <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
  1461
     * {@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
  1462
     * 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
  1463
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1464
     * <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
  1465
     * not be reordered as a result of the sort.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
     *
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1467
     * <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
  1468
     * 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
  1469
     * 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
  1470
     * 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
  1471
     * 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
  1472
     * implementation requires approximately n comparisons.  Temporary
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1473
     * 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
  1474
     * 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
  1475
     * arrays.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
     *
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1477
     * <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
  1478
     * 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
  1479
     * 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
  1480
     * 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
  1481
     * 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
  1482
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1483
     * <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
  1484
     * (<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
  1485
     * 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
  1486
     * 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
  1487
     * 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
  1488
     * January 1993.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  1490
     * @param <T> the class of the objects to be sorted
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
     * @param a the array to be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
     *        sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
     * @param toIndex the index of the last element (exclusive) to be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
     * @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
  1496
     *        {@code null} value indicates that the elements'
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
     *        {@linkplain Comparable natural ordering} should be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
     * @throws ClassCastException if the array contains elements that are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
     *         <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
  1500
     * @throws IllegalArgumentException if {@code fromIndex > toIndex} or
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1501
     *         (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
  1502
     *         {@link Comparator} contract
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1503
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1504
     *         {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
    public static <T> void sort(T[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
                                Comparator<? super T> c) {
22285
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1508
        if (c == null) {
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1509
            sort(a, fromIndex, toIndex);
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1510
        } else {
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1511
            rangeCheck(a.length, fromIndex, toIndex);
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1512
            if (LegacyMergeSort.userRequested)
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1513
                legacyMergeSort(a, fromIndex, toIndex, c);
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1514
            else
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1515
                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
  1516
        }
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1517
    }
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
    /** To be removed in a future release. */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1520
    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
  1521
                                            Comparator<? super T> c) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1522
        T[] aux = copyOfRange(a, fromIndex, toIndex);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
        if (c==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
            mergeSort(aux, a, fromIndex, toIndex, -fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
            mergeSort(aux, a, fromIndex, toIndex, -fromIndex, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
     * Src is the source array that starts at index 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
     * Dest is the (possibly larger) array destination with a possible offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
     * low is the index in dest to start sorting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
     * high is the end index in dest to end sorting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
     * 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
  1535
     * To be removed in a future release.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
     */
13795
73850c397272 7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents: 12448
diff changeset
  1537
    @SuppressWarnings({"rawtypes", "unchecked"})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
    private static void mergeSort(Object[] src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
                                  Object[] dest,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
                                  int low, int high, int off,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
                                  Comparator c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
        int length = high - low;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
        // Insertion sort on smallest arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
        if (length < INSERTIONSORT_THRESHOLD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
            for (int i=low; i<high; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
                for (int j=i; j>low && c.compare(dest[j-1], dest[j])>0; j--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
                    swap(dest, j, j-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
        // Recursively sort halves of dest into src
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
        int destLow  = low;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
        int destHigh = high;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
        low  += off;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
        high += off;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
        int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
        mergeSort(dest, src, low, mid, -off, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
        mergeSort(dest, src, mid, high, -off, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
        // If list is already sorted, just copy from src to dest.  This is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
        // optimization that results in faster sorts for nearly ordered lists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
        if (c.compare(src[mid-1], src[mid]) <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
           System.arraycopy(src, low, dest, destLow, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
           return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
        // Merge sorted halves (now in src) into dest
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
        for(int i = destLow, p = low, q = mid; i < destHigh; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
            if (q >= high || p < mid && c.compare(src[p], src[q]) <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
                dest[i] = src[p++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
                dest[i] = src[q++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
18555
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1577
    // Parallel prefix
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1578
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
     * 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
  1581
     * 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
  1582
     * 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
  1583
     * 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
  1584
     * Parallel prefix computation is usually more efficient than
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1585
     * sequential loops for large arrays.
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1586
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  1587
     * @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
  1588
     * @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
  1589
     * @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
  1590
     * cumulation
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1591
     * @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
  1592
     * @since 1.8
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1593
     */
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1594
    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
  1595
        Objects.requireNonNull(op);
18555
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1596
        if (array.length > 0)
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1597
            new ArrayPrefixHelpers.CumulateTask<>
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1598
                    (null, op, array, 0, array.length).invoke();
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1599
    }
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
     * Performs {@link #parallelPrefix(Object[], BinaryOperator)}
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1603
     * for the given subrange of the array.
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1604
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  1605
     * @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
  1606
     * @param array the array
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1607
     * @param fromIndex the index of the first element, inclusive
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1608
     * @param toIndex the index of the last element, exclusive
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1609
     * @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
  1610
     * cumulation
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1611
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1612
     * @throws ArrayIndexOutOfBoundsException
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1613
     *     if {@code fromIndex < 0} or {@code toIndex > array.length}
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1614
     * @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
  1615
     * @since 1.8
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1616
     */
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1617
    public static <T> void parallelPrefix(T[] array, int fromIndex,
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1618
                                          int toIndex, BinaryOperator<T> op) {
20492
11418b1f61ff 8025067: Unconditionally throw NPE if null op provided to Arrays.parallelPrefix
mduigou
parents: 18829
diff changeset
  1619
        Objects.requireNonNull(op);
18555
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1620
        rangeCheck(array.length, fromIndex, toIndex);
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1621
        if (fromIndex < toIndex)
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1622
            new ArrayPrefixHelpers.CumulateTask<>
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1623
                    (null, op, array, fromIndex, toIndex).invoke();
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1624
    }
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
     * 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
  1628
     * 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
  1629
     * 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
  1630
     * 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
  1631
     * Parallel prefix computation is usually more efficient than
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1632
     * sequential loops for large arrays.
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1633
     *
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1634
     * @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
  1635
     * @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
  1636
     * cumulation
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1637
     * @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
  1638
     * @since 1.8
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1639
     */
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1640
    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
  1641
        Objects.requireNonNull(op);
18555
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1642
        if (array.length > 0)
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1643
            new ArrayPrefixHelpers.LongCumulateTask
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1644
                    (null, op, array, 0, array.length).invoke();
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1645
    }
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1646
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
     * Performs {@link #parallelPrefix(long[], LongBinaryOperator)}
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1649
     * for the given subrange of the array.
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1650
     *
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1651
     * @param array the array
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1652
     * @param fromIndex the index of the first element, inclusive
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1653
     * @param toIndex the index of the last element, exclusive
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1654
     * @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
  1655
     * cumulation
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1656
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1657
     * @throws ArrayIndexOutOfBoundsException
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1658
     *     if {@code fromIndex < 0} or {@code toIndex > array.length}
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1659
     * @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
  1660
     * @since 1.8
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1661
     */
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1662
    public static void parallelPrefix(long[] array, int fromIndex,
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1663
                                      int toIndex, LongBinaryOperator op) {
20492
11418b1f61ff 8025067: Unconditionally throw NPE if null op provided to Arrays.parallelPrefix
mduigou
parents: 18829
diff changeset
  1664
        Objects.requireNonNull(op);
18555
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1665
        rangeCheck(array.length, fromIndex, toIndex);
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1666
        if (fromIndex < toIndex)
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1667
            new ArrayPrefixHelpers.LongCumulateTask
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1668
                    (null, op, array, fromIndex, toIndex).invoke();
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1669
    }
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
     * 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
  1673
     * 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
  1674
     * 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
  1675
     * 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
  1676
     * Parallel prefix computation is usually more efficient than
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1677
     * sequential loops for large arrays.
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1678
     *
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1679
     * <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
  1680
     * 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
  1681
     * obtained if the operation was performed sequentially.
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1682
     *
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1683
     * @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
  1684
     * @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
  1685
     * @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
  1686
     * @since 1.8
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1687
     */
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1688
    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
  1689
        Objects.requireNonNull(op);
18555
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1690
        if (array.length > 0)
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1691
            new ArrayPrefixHelpers.DoubleCumulateTask
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1692
                    (null, op, array, 0, array.length).invoke();
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1693
    }
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1694
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
     * Performs {@link #parallelPrefix(double[], DoubleBinaryOperator)}
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1697
     * for the given subrange of the array.
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1698
     *
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1699
     * @param array the array
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1700
     * @param fromIndex the index of the first element, inclusive
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1701
     * @param toIndex the index of the last element, exclusive
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1702
     * @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
  1703
     * cumulation
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1704
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1705
     * @throws ArrayIndexOutOfBoundsException
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1706
     *     if {@code fromIndex < 0} or {@code toIndex > array.length}
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1707
     * @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
  1708
     * @since 1.8
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1709
     */
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1710
    public static void parallelPrefix(double[] array, int fromIndex,
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1711
                                      int toIndex, DoubleBinaryOperator op) {
20492
11418b1f61ff 8025067: Unconditionally throw NPE if null op provided to Arrays.parallelPrefix
mduigou
parents: 18829
diff changeset
  1712
        Objects.requireNonNull(op);
18555
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1713
        rangeCheck(array.length, fromIndex, toIndex);
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1714
        if (fromIndex < toIndex)
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1715
            new ArrayPrefixHelpers.DoubleCumulateTask
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1716
                    (null, op, array, fromIndex, toIndex).invoke();
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1717
    }
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
     * 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
  1721
     * 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
  1722
     * 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
  1723
     * 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
  1724
     * Parallel prefix computation is usually more efficient than
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1725
     * sequential loops for large arrays.
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1726
     *
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1727
     * @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
  1728
     * @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
  1729
     * cumulation
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1730
     * @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
  1731
     * @since 1.8
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1732
     */
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1733
    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
  1734
        Objects.requireNonNull(op);
18555
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1735
        if (array.length > 0)
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1736
            new ArrayPrefixHelpers.IntCumulateTask
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1737
                    (null, op, array, 0, array.length).invoke();
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1738
    }
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1739
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
     * Performs {@link #parallelPrefix(int[], IntBinaryOperator)}
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1742
     * for the given subrange of the array.
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1743
     *
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1744
     * @param array the array
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1745
     * @param fromIndex the index of the first element, inclusive
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1746
     * @param toIndex the index of the last element, exclusive
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1747
     * @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
  1748
     * cumulation
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1749
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1750
     * @throws ArrayIndexOutOfBoundsException
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1751
     *     if {@code fromIndex < 0} or {@code toIndex > array.length}
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1752
     * @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
  1753
     * @since 1.8
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1754
     */
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1755
    public static void parallelPrefix(int[] array, int fromIndex,
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1756
                                      int toIndex, IntBinaryOperator op) {
20492
11418b1f61ff 8025067: Unconditionally throw NPE if null op provided to Arrays.parallelPrefix
mduigou
parents: 18829
diff changeset
  1757
        Objects.requireNonNull(op);
18555
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1758
        rangeCheck(array.length, fromIndex, toIndex);
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1759
        if (fromIndex < toIndex)
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1760
            new ArrayPrefixHelpers.IntCumulateTask
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1761
                    (null, op, array, fromIndex, toIndex).invoke();
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1762
    }
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1763
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
    // Searching
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
     * Searches the specified array of longs for the specified value using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
     * binary search algorithm.  The array must be sorted (as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
     * by the {@link #sort(long[])} method) prior to making this call.  If it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
     * is not sorted, the results are undefined.  If the array contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
     * @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
  1777
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
     *         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
  1780
     *         element greater than the key, or {@code a.length} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
     *         elements in the array are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
    public static int binarySearch(long[] a, long key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
        return binarySearch0(a, 0, a.length, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
     * Searches a range of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
     * the specified array of longs for the specified value using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
     * binary search algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
     * The range must be sorted (as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
     * by the {@link #sort(long[], int, int)} method)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
     * prior to making this call.  If it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
     * is not sorted, the results are undefined.  If the range contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
     *          searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
     * @param toIndex the index of the last element (exclusive) to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
     * @return index of the search key, if it is contained in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
     *         within the specified range;
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  1807
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
     *         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
  1811
     *         or {@code toIndex} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
     *         elements in the range are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
     *         if {@code fromIndex > toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
     * @throws ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
     *         if {@code fromIndex < 0 or toIndex > a.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
    public static int binarySearch(long[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
                                   long key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
        return binarySearch0(a, fromIndex, toIndex, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
    // Like public version, but without range checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
    private static int binarySearch0(long[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
                                     long key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
        int low = fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
        int high = toIndex - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
            int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
            long midVal = a[mid];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
            if (midVal < key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
                low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
            else if (midVal > key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
                high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
                return mid; // key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
        return -(low + 1);  // key not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
     * Searches the specified array of ints for the specified value using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
     * binary search algorithm.  The array must be sorted (as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
     * by the {@link #sort(int[])} method) prior to making this call.  If it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
     * is not sorted, the results are undefined.  If the array contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
     * @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
  1858
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
     *         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
  1861
     *         element greater than the key, or {@code a.length} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
     *         elements in the array are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
    public static int binarySearch(int[] a, int key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
        return binarySearch0(a, 0, a.length, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
     * Searches a range of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
     * the specified array of ints for the specified value using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
     * binary search algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
     * The range must be sorted (as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
     * by the {@link #sort(int[], int, int)} method)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
     * prior to making this call.  If it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
     * is not sorted, the results are undefined.  If the range contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
     *          searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
     * @param toIndex the index of the last element (exclusive) to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
     * @return index of the search key, if it is contained in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
     *         within the specified range;
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  1888
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
     *         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
  1892
     *         or {@code toIndex} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
     *         elements in the range are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
     *         if {@code fromIndex > toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
     * @throws ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
     *         if {@code fromIndex < 0 or toIndex > a.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
    public static int binarySearch(int[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
                                   int key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
        return binarySearch0(a, fromIndex, toIndex, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
    // Like public version, but without range checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
    private static int binarySearch0(int[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
                                     int key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
        int low = fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
        int high = toIndex - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
            int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
            int midVal = a[mid];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
            if (midVal < key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
                low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
            else if (midVal > key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
                high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
                return mid; // key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
        return -(low + 1);  // key not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
     * Searches the specified array of shorts for the specified value using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
     * the binary search algorithm.  The array must be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
     * (as by the {@link #sort(short[])} method) prior to making this call.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
     * it is not sorted, the results are undefined.  If the array contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
     * @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
  1939
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
     *         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
  1942
     *         element greater than the key, or {@code a.length} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
     *         elements in the array are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
    public static int binarySearch(short[] a, short key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
        return binarySearch0(a, 0, a.length, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
     * Searches a range of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
     * the specified array of shorts for the specified value using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
     * the binary search algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
     * The range must be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
     * (as by the {@link #sort(short[], int, int)} method)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
     * prior to making this call.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
     * it is not sorted, the results are undefined.  If the range contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
     *          searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
     * @param toIndex the index of the last element (exclusive) to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
     * @return index of the search key, if it is contained in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
     *         within the specified range;
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  1969
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
     *         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
  1973
     *         or {@code toIndex} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
     *         elements in the range are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
     *         if {@code fromIndex > toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
     * @throws ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
     *         if {@code fromIndex < 0 or toIndex > a.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
    public static int binarySearch(short[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
                                   short key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
        return binarySearch0(a, fromIndex, toIndex, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
    // Like public version, but without range checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
    private static int binarySearch0(short[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
                                     short key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
        int low = fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
        int high = toIndex - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
            int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
            short midVal = a[mid];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
            if (midVal < key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
                low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
            else if (midVal > key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
                high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
                return mid; // key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
        return -(low + 1);  // key not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
     * Searches the specified array of chars for the specified value using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
     * binary search algorithm.  The array must be sorted (as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
     * by the {@link #sort(char[])} method) prior to making this call.  If it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
     * is not sorted, the results are undefined.  If the array contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
     * @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
  2020
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
     *         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
  2023
     *         element greater than the key, or {@code a.length} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
     *         elements in the array are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
    public static int binarySearch(char[] a, char key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
        return binarySearch0(a, 0, a.length, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
     * Searches a range of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
     * the specified array of chars for the specified value using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
     * binary search algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
     * The range must be sorted (as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
     * by the {@link #sort(char[], int, int)} method)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
     * prior to making this call.  If it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
     * is not sorted, the results are undefined.  If the range contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
     *          searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
     * @param toIndex the index of the last element (exclusive) to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
     * @return index of the search key, if it is contained in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
     *         within the specified range;
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2050
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
     *         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
  2054
     *         or {@code toIndex} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
     *         elements in the range are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
     *         if {@code fromIndex > toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
     * @throws ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
     *         if {@code fromIndex < 0 or toIndex > a.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
    public static int binarySearch(char[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
                                   char key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
        return binarySearch0(a, fromIndex, toIndex, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
    // Like public version, but without range checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
    private static int binarySearch0(char[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
                                     char key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
        int low = fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
        int high = toIndex - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
            int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
            char midVal = a[mid];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
            if (midVal < key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
                low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
            else if (midVal > key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
                high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
                return mid; // key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
        return -(low + 1);  // key not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
     * Searches the specified array of bytes for the specified value using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
     * binary search algorithm.  The array must be sorted (as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
     * by the {@link #sort(byte[])} method) prior to making this call.  If it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
     * is not sorted, the results are undefined.  If the array contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
     * @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
  2101
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
     *         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
  2104
     *         element greater than the key, or {@code a.length} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
     *         elements in the array are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
    public static int binarySearch(byte[] a, byte key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
        return binarySearch0(a, 0, a.length, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
     * Searches a range of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
     * the specified array of bytes for the specified value using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
     * binary search algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
     * The range must be sorted (as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
     * by the {@link #sort(byte[], int, int)} method)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
     * prior to making this call.  If it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
     * is not sorted, the results are undefined.  If the range contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
     *          searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
     * @param toIndex the index of the last element (exclusive) to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
     * @return index of the search key, if it is contained in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
     *         within the specified range;
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2131
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
     *         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
  2135
     *         or {@code toIndex} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
     *         elements in the range are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
     *         if {@code fromIndex > toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
     * @throws ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
     *         if {@code fromIndex < 0 or toIndex > a.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
    public static int binarySearch(byte[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
                                   byte key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
        return binarySearch0(a, fromIndex, toIndex, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
    // Like public version, but without range checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
    private static int binarySearch0(byte[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
                                     byte key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
        int low = fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
        int high = toIndex - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
            int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
            byte midVal = a[mid];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
            if (midVal < key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
                low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
            else if (midVal > key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
                high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
                return mid; // key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
        return -(low + 1);  // key not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
     * Searches the specified array of doubles for the specified value using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
     * the binary search algorithm.  The array must be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
     * (as by the {@link #sort(double[])} method) prior to making this call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
     * If it is not sorted, the results are undefined.  If the array contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
     * one will be found.  This method considers all NaN values to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
     * equivalent and equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
     * @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
  2183
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
     *         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
  2186
     *         element greater than the key, or {@code a.length} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
     *         elements in the array are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
    public static int binarySearch(double[] a, double key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
        return binarySearch0(a, 0, a.length, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
     * Searches a range of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
     * the specified array of doubles for the specified value using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
     * the binary search algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
     * The range must be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
     * (as by the {@link #sort(double[], int, int)} method)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
     * prior to making this call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
     * If it is not sorted, the results are undefined.  If the range contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
     * one will be found.  This method considers all NaN values to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
     * equivalent and equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
     *          searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
     * @param toIndex the index of the last element (exclusive) to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
     * @return index of the search key, if it is contained in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
     *         within the specified range;
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2214
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
     *         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
  2218
     *         or {@code toIndex} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
     *         elements in the range are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
     *         if {@code fromIndex > toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
     * @throws ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
     *         if {@code fromIndex < 0 or toIndex > a.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
    public static int binarySearch(double[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
                                   double key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
        return binarySearch0(a, fromIndex, toIndex, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
    // Like public version, but without range checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
    private static int binarySearch0(double[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
                                     double key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
        int low = fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
        int high = toIndex - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
            int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
            double midVal = a[mid];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
            if (midVal < key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
                low = mid + 1;  // Neither val is NaN, thisVal is smaller
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
            else if (midVal > key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
                high = mid - 1; // Neither val is NaN, thisVal is larger
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
                long midBits = Double.doubleToLongBits(midVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
                long keyBits = Double.doubleToLongBits(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
                if (midBits == keyBits)     // Values are equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
                    return mid;             // Key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
                else if (midBits < keyBits) // (-0.0, 0.0) or (!NaN, NaN)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
                    low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
                else                        // (0.0, -0.0) or (NaN, !NaN)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
                    high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
        return -(low + 1);  // key not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
     * 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
  2264
     * 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
  2265
     * (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
  2266
     * it is not sorted, the results are undefined. If the array contains
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
     * 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
  2268
     * one will be found. This method considers all NaN values to be
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
     * equivalent and equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
     * @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
  2274
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
     *         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
  2277
     *         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
  2278
     *         elements in the array are less than the specified key. Note
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
    public static int binarySearch(float[] a, float key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
        return binarySearch0(a, 0, a.length, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
     * Searches a range of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
     * the specified array of floats for the specified value using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
     * the binary search algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
     * The range must be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
     * (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
  2292
     * prior to making this call. If
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
  2293
     * it is not sorted, the results are undefined. If the range contains
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
     * 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
  2295
     * one will be found. This method considers all NaN values to be
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
     * equivalent and equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
     *          searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
     * @param toIndex the index of the last element (exclusive) to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
     * @return index of the search key, if it is contained in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
     *         within the specified range;
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2305
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
     *         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
  2309
     *         or {@code toIndex} if all
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
  2310
     *         elements in the range are less than the specified key. Note
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
     *         if {@code fromIndex > toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
     * @throws ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
     *         if {@code fromIndex < 0 or toIndex > a.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
    public static int binarySearch(float[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
                                   float key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
        return binarySearch0(a, fromIndex, toIndex, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
    // Like public version, but without range checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
    private static int binarySearch0(float[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
                                     float key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
        int low = fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
        int high = toIndex - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
            int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
            float midVal = a[mid];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
            if (midVal < key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
                low = mid + 1;  // Neither val is NaN, thisVal is smaller
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
            else if (midVal > key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
                high = mid - 1; // Neither val is NaN, thisVal is larger
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
                int midBits = Float.floatToIntBits(midVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
                int keyBits = Float.floatToIntBits(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
                if (midBits == keyBits)     // Values are equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
                    return mid;             // Key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
                else if (midBits < keyBits) // (-0.0, 0.0) or (!NaN, NaN)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
                    low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
                else                        // (0.0, -0.0) or (NaN, !NaN)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
                    high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
        return -(low + 1);  // key not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
     * 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
  2355
     * search algorithm. The array must be sorted into ascending order
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
     * according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
     * {@linkplain Comparable natural ordering}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
     * of its elements (as by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
     * {@link #sort(Object[])} method) prior to making this call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
     * If it is not sorted, the results are undefined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
     * (If the array contains elements that are not mutually comparable (for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
     * example, strings and integers), it <i>cannot</i> be sorted according
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
     * to the natural ordering of its elements, hence results are undefined.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
     * If the array contains multiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
     * elements equal to the specified object, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
     * @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
  2371
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
     *         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
  2374
     *         element greater than the key, or {@code a.length} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
     *         elements in the array are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
     * @throws ClassCastException if the search key is not comparable to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
     *         elements of the array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
    public static int binarySearch(Object[] a, Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
        return binarySearch0(a, 0, a.length, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
     * Searches a range of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
     * the specified array for the specified object using the binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
     * search algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
     * The range must be sorted into ascending order
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
     * according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
     * {@linkplain Comparable natural ordering}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
     * of its elements (as by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
     * {@link #sort(Object[], int, int)} method) prior to making this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
     * call.  If it is not sorted, the results are undefined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
     * (If the range contains elements that are not mutually comparable (for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
     * example, strings and integers), it <i>cannot</i> be sorted according
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
     * to the natural ordering of its elements, hence results are undefined.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
     * If the range contains multiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
     * elements equal to the specified object, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
     *          searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
     * @param toIndex the index of the last element (exclusive) to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
     * @return index of the search key, if it is contained in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
     *         within the specified range;
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2409
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
     *         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
  2413
     *         or {@code toIndex} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
     *         elements in the range are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
     * @throws ClassCastException if the search key is not comparable to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2418
     *         elements of the array within the specified range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2420
     *         if {@code fromIndex > toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
     * @throws ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
     *         if {@code fromIndex < 0 or toIndex > a.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
    public static int binarySearch(Object[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
                                   Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
        return binarySearch0(a, fromIndex, toIndex, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2430
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
    // Like public version, but without range checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
    private static int binarySearch0(Object[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2433
                                     Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
        int low = fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
        int high = toIndex - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
            int mid = (low + high) >>> 1;
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  2439
            @SuppressWarnings("rawtypes")
14925
72729557c226 8003981: Support Parallel Array Sorting - JEP 103
chegar
parents: 13795
diff changeset
  2440
            Comparable midVal = (Comparable)a[mid];
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  2441
            @SuppressWarnings("unchecked")
14925
72729557c226 8003981: Support Parallel Array Sorting - JEP 103
chegar
parents: 13795
diff changeset
  2442
            int cmp = midVal.compareTo(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2443
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2444
            if (cmp < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2445
                low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2446
            else if (cmp > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2447
                high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2448
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2449
                return mid; // key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2450
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2451
        return -(low + 1);  // key not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2452
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2453
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2454
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2455
     * Searches the specified array for the specified object using the binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2456
     * search algorithm.  The array must be sorted into ascending order
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2457
     * according to the specified comparator (as by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2458
     * {@link #sort(Object[], Comparator) sort(T[], Comparator)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2459
     * method) prior to making this call.  If it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2460
     * not sorted, the results are undefined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2461
     * If the array contains multiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2462
     * elements equal to the specified object, there is no guarantee which one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2463
     * will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2464
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  2465
     * @param <T> the class of the objects in the array
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2466
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2467
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2468
     * @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
  2469
     *        {@code null} value indicates that the elements'
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2470
     *        {@linkplain Comparable natural ordering} should be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2471
     * @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
  2472
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2473
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2474
     *         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
  2475
     *         element greater than the key, or {@code a.length} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2476
     *         elements in the array are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2477
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2478
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2479
     * @throws ClassCastException if the array contains elements that are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2480
     *         <i>mutually comparable</i> using the specified comparator,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2481
     *         or the search key is not comparable to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2482
     *         elements of the array using this comparator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2483
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2484
    public static <T> int binarySearch(T[] a, T key, Comparator<? super T> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2485
        return binarySearch0(a, 0, a.length, key, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2486
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2487
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2488
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2489
     * Searches a range of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2490
     * the specified array for the specified object using the binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2491
     * search algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2492
     * The range must be sorted into ascending order
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2493
     * according to the specified comparator (as by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2494
     * {@link #sort(Object[], int, int, Comparator)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2495
     * sort(T[], int, int, Comparator)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2496
     * method) prior to making this call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2497
     * If it is not sorted, the results are undefined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2498
     * If the range contains multiple elements equal to the specified object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2499
     * there is no guarantee which one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2500
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  2501
     * @param <T> the class of the objects in the array
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2502
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2503
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2504
     *          searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2505
     * @param toIndex the index of the last element (exclusive) to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2506
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2507
     * @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
  2508
     *        {@code null} value indicates that the elements'
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2509
     *        {@linkplain Comparable natural ordering} should be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2510
     * @return index of the search key, if it is contained in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2511
     *         within the specified range;
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2512
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2513
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2514
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2515
     *         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
  2516
     *         or {@code toIndex} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2517
     *         elements in the range are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2518
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2519
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2520
     * @throws ClassCastException if the range contains elements that are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2521
     *         <i>mutually comparable</i> using the specified comparator,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2522
     *         or the search key is not comparable to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2523
     *         elements in the range using this comparator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2524
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2525
     *         if {@code fromIndex > toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2526
     * @throws ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2527
     *         if {@code fromIndex < 0 or toIndex > a.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2528
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2529
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2530
    public static <T> int binarySearch(T[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2531
                                       T key, Comparator<? super T> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2532
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2533
        return binarySearch0(a, fromIndex, toIndex, key, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2534
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2535
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2536
    // Like public version, but without range checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2537
    private static <T> int binarySearch0(T[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2538
                                         T key, Comparator<? super T> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2539
        if (c == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2540
            return binarySearch0(a, fromIndex, toIndex, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2541
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2542
        int low = fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2543
        int high = toIndex - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2544
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2545
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2546
            int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2547
            T midVal = a[mid];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2548
            int cmp = c.compare(midVal, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2549
            if (cmp < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2550
                low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2551
            else if (cmp > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2552
                high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2553
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2554
                return mid; // key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2555
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2556
        return -(low + 1);  // key not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2557
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2558
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2559
    // Equality Testing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2560
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2561
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2562
     * Returns {@code true} if the two specified arrays of longs are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2563
     * <i>equal</i> to one another.  Two arrays are considered equal if both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2564
     * arrays contain the same number of elements, and all corresponding pairs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2565
     * of elements in the two arrays are equal.  In other words, two arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2566
     * 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
  2567
     * two array references are considered equal if both are {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2568
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2569
     * @param a one array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2570
     * @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
  2571
     * @return {@code true} if the two arrays are equal
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2572
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2573
    public static boolean equals(long[] a, long[] a2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2574
        if (a==a2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2575
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2576
        if (a==null || a2==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2577
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2578
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2579
        int length = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2580
        if (a2.length != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2581
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2582
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2583
        return ArraysSupport.mismatch(a, a2, length) < 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2584
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2585
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2586
    /**
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2587
     * Returns true if the two specified arrays of longs, over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2588
     * ranges, are <i>equal</i> to one another.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2589
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2590
     * <p>Two arrays are considered equal if the number of elements covered by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2591
     * each range is the same, and all corresponding pairs of elements over the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2592
     * specified ranges in the two arrays are equal.  In other words, two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2593
     * are equal if they contain, over the specified ranges, the same elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2594
     * in the same order.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2595
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2596
     * @param a the first array to be tested for equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2597
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2598
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2599
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2600
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2601
     * @param b the second array to be tested fro equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2602
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2603
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2604
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2605
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2606
     * @return {@code true} if the two arrays, over the specified ranges, are
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2607
     *         equal
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2608
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2609
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2610
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2611
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2612
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2613
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2614
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2615
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2616
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2617
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2618
    public static boolean equals(long[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2619
                                 long[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2620
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2621
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2622
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2623
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2624
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2625
        if (aLength != bLength)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2626
            return false;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2627
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2628
        return ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2629
                                      b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2630
                                      aLength) < 0;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2631
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2632
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2633
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2634
     * Returns {@code true} if the two specified arrays of ints are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2635
     * <i>equal</i> to one another.  Two arrays are considered equal if both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2636
     * arrays contain the same number of elements, and all corresponding pairs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2637
     * of elements in the two arrays are equal.  In other words, two arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2638
     * 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
  2639
     * two array references are considered equal if both are {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2640
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2641
     * @param a one array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2642
     * @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
  2643
     * @return {@code true} if the two arrays are equal
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2644
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2645
    public static boolean equals(int[] a, int[] a2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2646
        if (a==a2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2647
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2648
        if (a==null || a2==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2649
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2650
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2651
        int length = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2652
        if (a2.length != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2653
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2654
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2655
        return ArraysSupport.mismatch(a, a2, length) < 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2656
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2657
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2658
    /**
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2659
     * Returns true if the two specified arrays of ints, over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2660
     * ranges, are <i>equal</i> to one another.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2661
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2662
     * <p>Two arrays are considered equal if the number of elements covered by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2663
     * each range is the same, and all corresponding pairs of elements over the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2664
     * specified ranges in the two arrays are equal.  In other words, two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2665
     * are equal if they contain, over the specified ranges, the same elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2666
     * in the same order.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2667
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2668
     * @param a the first array to be tested for equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2669
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2670
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2671
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2672
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2673
     * @param b the second array to be tested fro equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2674
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2675
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2676
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2677
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2678
     * @return {@code true} if the two arrays, over the specified ranges, are
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2679
     *         equal
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2680
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2681
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2682
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2683
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2684
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2685
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2686
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2687
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2688
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2689
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2690
    public static boolean equals(int[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2691
                                 int[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2692
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2693
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2694
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2695
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2696
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2697
        if (aLength != bLength)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2698
            return false;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2699
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2700
        return ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2701
                                      b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2702
                                      aLength) < 0;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2703
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2704
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2705
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2706
     * Returns {@code true} if the two specified arrays of shorts are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2707
     * <i>equal</i> to one another.  Two arrays are considered equal if both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2708
     * arrays contain the same number of elements, and all corresponding pairs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2709
     * of elements in the two arrays are equal.  In other words, two arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2710
     * 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
  2711
     * two array references are considered equal if both are {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2712
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2713
     * @param a one array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2714
     * @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
  2715
     * @return {@code true} if the two arrays are equal
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2716
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2717
    public static boolean equals(short[] a, short a2[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2718
        if (a==a2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2719
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2720
        if (a==null || a2==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2721
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2722
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2723
        int length = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2724
        if (a2.length != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2725
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2726
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2727
        return ArraysSupport.mismatch(a, a2, length) < 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2728
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2729
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2730
    /**
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2731
     * Returns true if the two specified arrays of shorts, over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2732
     * ranges, are <i>equal</i> to one another.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2733
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2734
     * <p>Two arrays are considered equal if the number of elements covered by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2735
     * each range is the same, and all corresponding pairs of elements over the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2736
     * specified ranges in the two arrays are equal.  In other words, two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2737
     * are equal if they contain, over the specified ranges, the same elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2738
     * in the same order.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2739
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2740
     * @param a the first array to be tested for equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2741
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2742
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2743
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2744
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2745
     * @param b the second array to be tested fro equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2746
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2747
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2748
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2749
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2750
     * @return {@code true} if the two arrays, over the specified ranges, are
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2751
     *         equal
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2752
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2753
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2754
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2755
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2756
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2757
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2758
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2759
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2760
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2761
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2762
    public static boolean equals(short[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2763
                                 short[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2764
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2765
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2766
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2767
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2768
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2769
        if (aLength != bLength)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2770
            return false;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2771
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2772
        return ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2773
                                      b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2774
                                      aLength) < 0;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2775
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2776
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2777
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2778
     * Returns {@code true} if the two specified arrays of chars are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2779
     * <i>equal</i> to one another.  Two arrays are considered equal if both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2780
     * arrays contain the same number of elements, and all corresponding pairs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2781
     * of elements in the two arrays are equal.  In other words, two arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2782
     * 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
  2783
     * two array references are considered equal if both are {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2784
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2785
     * @param a one array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2786
     * @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
  2787
     * @return {@code true} if the two arrays are equal
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2788
     */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 29223
diff changeset
  2789
    @HotSpotIntrinsicCandidate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2790
    public static boolean equals(char[] a, char[] a2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2791
        if (a==a2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2792
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2793
        if (a==null || a2==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2794
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2795
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2796
        int length = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2797
        if (a2.length != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2798
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2799
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2800
        return ArraysSupport.mismatch(a, a2, length) < 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2801
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2802
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2803
    /**
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2804
     * Returns true if the two specified arrays of chars, over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2805
     * ranges, are <i>equal</i> to one another.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2806
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2807
     * <p>Two arrays are considered equal if the number of elements covered by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2808
     * each range is the same, and all corresponding pairs of elements over the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2809
     * specified ranges in the two arrays are equal.  In other words, two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2810
     * are equal if they contain, over the specified ranges, the same elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2811
     * in the same order.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2812
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2813
     * @param a the first array to be tested for equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2814
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2815
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2816
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2817
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2818
     * @param b the second array to be tested fro equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2819
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2820
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2821
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2822
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2823
     * @return {@code true} if the two arrays, over the specified ranges, are
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2824
     *         equal
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2825
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2826
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2827
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2828
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2829
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2830
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2831
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2832
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2833
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2834
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2835
    public static boolean equals(char[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2836
                                 char[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2837
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2838
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2839
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2840
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2841
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2842
        if (aLength != bLength)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2843
            return false;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2844
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2845
        return ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2846
                                      b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2847
                                      aLength) < 0;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2848
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2849
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2850
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2851
     * Returns {@code true} if the two specified arrays of bytes are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2852
     * <i>equal</i> to one another.  Two arrays are considered equal if both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2853
     * arrays contain the same number of elements, and all corresponding pairs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2854
     * of elements in the two arrays are equal.  In other words, two arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2855
     * 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
  2856
     * two array references are considered equal if both are {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2857
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2858
     * @param a one array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2859
     * @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
  2860
     * @return {@code true} if the two arrays are equal
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2861
     */
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32108
diff changeset
  2862
    @HotSpotIntrinsicCandidate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2863
    public static boolean equals(byte[] a, byte[] a2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2864
        if (a==a2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2865
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2866
        if (a==null || a2==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2867
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2868
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2869
        int length = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2870
        if (a2.length != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2871
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2872
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2873
        return ArraysSupport.mismatch(a, a2, length) < 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2874
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2875
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2876
    /**
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2877
     * Returns true if the two specified arrays of bytes, over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2878
     * ranges, are <i>equal</i> to one another.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2879
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2880
     * <p>Two arrays are considered equal if the number of elements covered by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2881
     * each range is the same, and all corresponding pairs of elements over the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2882
     * specified ranges in the two arrays are equal.  In other words, two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2883
     * are equal if they contain, over the specified ranges, the same elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2884
     * in the same order.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2885
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2886
     * @param a the first array to be tested for equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2887
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2888
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2889
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2890
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2891
     * @param b the second array to be tested fro equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2892
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2893
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2894
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2895
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2896
     * @return {@code true} if the two arrays, over the specified ranges, are
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2897
     *         equal
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2898
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2899
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2900
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2901
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2902
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2903
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2904
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2905
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2906
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2907
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2908
    public static boolean equals(byte[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2909
                                 byte[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2910
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2911
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2912
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2913
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2914
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2915
        if (aLength != bLength)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2916
            return false;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2917
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2918
        return ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2919
                                      b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2920
                                      aLength) < 0;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2921
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2922
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2923
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2924
     * Returns {@code true} if the two specified arrays of booleans are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2925
     * <i>equal</i> to one another.  Two arrays are considered equal if both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2926
     * arrays contain the same number of elements, and all corresponding pairs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2927
     * of elements in the two arrays are equal.  In other words, two arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2928
     * 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
  2929
     * two array references are considered equal if both are {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2930
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2931
     * @param a one array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2932
     * @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
  2933
     * @return {@code true} if the two arrays are equal
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2934
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2935
    public static boolean equals(boolean[] a, boolean[] a2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2936
        if (a==a2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2937
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2938
        if (a==null || a2==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2939
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2940
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2941
        int length = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2942
        if (a2.length != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2943
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2944
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2945
        return ArraysSupport.mismatch(a, a2, length) < 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2946
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2947
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2948
    /**
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2949
     * Returns true if the two specified arrays of booleans, over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2950
     * ranges, are <i>equal</i> to one another.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2951
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2952
     * <p>Two arrays are considered equal if the number of elements covered by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2953
     * each range is the same, and all corresponding pairs of elements over the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2954
     * specified ranges in the two arrays are equal.  In other words, two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2955
     * are equal if they contain, over the specified ranges, the same elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2956
     * in the same order.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2957
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2958
     * @param a the first array to be tested for equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2959
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2960
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2961
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2962
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2963
     * @param b the second array to be tested fro equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2964
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2965
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2966
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2967
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2968
     * @return {@code true} if the two arrays, over the specified ranges, are
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2969
     *         equal
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2970
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2971
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2972
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2973
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2974
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2975
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2976
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2977
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2978
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2979
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2980
    public static boolean equals(boolean[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2981
                                 boolean[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2982
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2983
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2984
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2985
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2986
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2987
        if (aLength != bLength)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2988
            return false;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2989
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2990
        return ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2991
                                      b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2992
                                      aLength) < 0;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2993
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2994
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2995
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2996
     * Returns {@code true} if the two specified arrays of doubles are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2997
     * <i>equal</i> to one another.  Two arrays are considered equal if both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2998
     * arrays contain the same number of elements, and all corresponding pairs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2999
     * of elements in the two arrays are equal.  In other words, two arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3000
     * 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
  3001
     * 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
  3002
     *
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3003
     * 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
  3004
     * <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
  3005
     * (Unlike the {@code ==} operator, this method considers
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3006
     * {@code NaN} equals to itself, and 0.0d unequal to -0.0d.)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3007
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3008
     * @param a one array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3009
     * @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
  3010
     * @return {@code true} if the two arrays are equal
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3011
     * @see Double#equals(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3012
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3013
    public static boolean equals(double[] a, double[] a2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3014
        if (a==a2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3015
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3016
        if (a==null || a2==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3017
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3018
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3019
        int length = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3020
        if (a2.length != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3021
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3022
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  3023
        return ArraysSupport.mismatch(a, a2, length) < 0;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3024
    }
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
     * Returns true if the two specified arrays of doubles, over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3028
     * ranges, are <i>equal</i> to one another.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3029
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3030
     * <p>Two arrays are considered equal if the number of elements covered by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3031
     * each range is the same, and all corresponding pairs of elements over the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3032
     * specified ranges in the two arrays are equal.  In other words, two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3033
     * are equal if they contain, over the specified ranges, the same elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3034
     * in the same order.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3035
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3036
     * <p>Two doubles {@code d1} and {@code d2} are considered equal if:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3037
     * <pre>    {@code new Double(d1).equals(new Double(d2))}</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3038
     * (Unlike the {@code ==} operator, this method considers
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3039
     * {@code NaN} equals to itself, and 0.0d unequal to -0.0d.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3040
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3041
     * @param a the first array to be tested for equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3042
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3043
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3044
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3045
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3046
     * @param b the second array to be tested fro equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3047
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3048
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3049
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3050
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3051
     * @return {@code true} if the two arrays, over the specified ranges, are
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3052
     *         equal
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3053
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3054
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3055
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3056
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3057
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3058
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3059
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3060
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3061
     * @see Double#equals(Object)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3062
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3063
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3064
    public static boolean equals(double[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3065
                                 double[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3066
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3067
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3068
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3069
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3070
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3071
        if (aLength != bLength)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3072
            return false;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3073
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  3074
        return ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  3075
                                      b, bFromIndex, aLength) < 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3076
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3077
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3078
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3079
     * Returns {@code true} if the two specified arrays of floats are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3080
     * <i>equal</i> to one another.  Two arrays are considered equal if both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3081
     * arrays contain the same number of elements, and all corresponding pairs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3082
     * of elements in the two arrays are equal.  In other words, two arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3083
     * 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
  3084
     * 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
  3085
     *
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3086
     * 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
  3087
     * <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
  3088
     * (Unlike the {@code ==} operator, this method considers
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3089
     * {@code NaN} equals to itself, and 0.0f unequal to -0.0f.)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3090
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3091
     * @param a one array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3092
     * @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
  3093
     * @return {@code true} if the two arrays are equal
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3094
     * @see Float#equals(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3095
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3096
    public static boolean equals(float[] a, float[] a2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3097
        if (a==a2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3098
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3099
        if (a==null || a2==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3100
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3101
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3102
        int length = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3103
        if (a2.length != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3104
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3105
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  3106
        return ArraysSupport.mismatch(a, a2, length) < 0;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3107
    }
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
     * Returns true if the two specified arrays of floats, over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3111
     * ranges, are <i>equal</i> to one another.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3112
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3113
     * <p>Two arrays are considered equal if the number of elements covered by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3114
     * each range is the same, and all corresponding pairs of elements over the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3115
     * specified ranges in the two arrays are equal.  In other words, two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3116
     * are equal if they contain, over the specified ranges, the same elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3117
     * in the same order.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3118
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3119
     * <p>Two floats {@code f1} and {@code f2} are considered equal if:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3120
     * <pre>    {@code new Float(f1).equals(new Float(f2))}</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3121
     * (Unlike the {@code ==} operator, this method considers
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3122
     * {@code NaN} equals to itself, and 0.0f unequal to -0.0f.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3123
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3124
     * @param a the first array to be tested for equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3125
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3126
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3127
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3128
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3129
     * @param b the second array to be tested fro equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3130
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3131
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3132
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3133
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3134
     * @return {@code true} if the two arrays, over the specified ranges, are
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3135
     *         equal
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3136
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3137
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3138
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3139
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3140
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3141
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3142
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3143
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3144
     * @see Float#equals(Object)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3145
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3146
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3147
    public static boolean equals(float[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3148
                                 float[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3149
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3150
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3151
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3152
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3153
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3154
        if (aLength != bLength)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3155
            return false;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3156
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  3157
        return ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  3158
                                      b, bFromIndex, aLength) < 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3160
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3161
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3162
     * Returns {@code true} if the two specified arrays of Objects are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3163
     * <i>equal</i> to one another.  The two arrays are considered equal if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3164
     * 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
  3165
     * 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
  3166
     * 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
  3167
     * {@code Objects.equals(e1, e2)}.
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3168
     * In other words, the two arrays are equal if
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3169
     * 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
  3170
     * references are considered equal if both are {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3171
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3172
     * @param a one array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3173
     * @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
  3174
     * @return {@code true} if the two arrays are equal
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3175
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3176
    public static boolean equals(Object[] a, Object[] a2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3177
        if (a==a2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3178
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3179
        if (a==null || a2==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3180
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3181
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3182
        int length = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3183
        if (a2.length != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3184
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3185
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3186
        for (int i=0; i<length; i++) {
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3187
            if (!Objects.equals(a[i], a2[i]))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3188
                return false;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3189
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3190
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3191
        return true;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3192
    }
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
     * Returns true if the two specified arrays of Objects, over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3196
     * ranges, are <i>equal</i> to one another.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3197
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3198
     * <p>Two arrays are considered equal if the number of elements covered by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3199
     * each range is the same, and all corresponding pairs of elements over the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3200
     * specified ranges in the two arrays are equal.  In other words, two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3201
     * are equal if they contain, over the specified ranges, the same elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3202
     * in the same order.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3203
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3204
     * <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
  3205
     * {@code Objects.equals(e1, e2)}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3206
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3207
     * @param a the first array to be tested for equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3208
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3209
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3210
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3211
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3212
     * @param b the second array to be tested fro equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3213
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3214
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3215
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3216
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3217
     * @return {@code true} if the two arrays, over the specified ranges, are
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3218
     *         equal
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3219
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3220
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3221
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3222
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3223
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3224
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3225
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3226
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3227
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3228
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3229
    public static boolean equals(Object[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3230
                                 Object[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3231
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3232
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3233
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3234
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3235
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3236
        if (aLength != bLength)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3237
            return false;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3238
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3239
        for (int i = 0; i < aLength; i++) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3240
            if (!Objects.equals(a[aFromIndex++], b[bFromIndex++]))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3241
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3243
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3244
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3245
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3246
33843
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3247
    /**
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3248
     * Returns {@code true} if the two specified arrays of Objects are
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3249
     * <i>equal</i> to one another.
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3250
     *
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3251
     * <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
  3252
     * of elements, and all corresponding pairs of elements in the two arrays
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3253
     * 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
  3254
     * same elements in the same order.  Also, two array references are
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3255
     * considered equal if both are {@code null}.
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3256
     *
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3257
     * <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
  3258
     * given the specified comparator, {@code cmp.compare(e1, e2) == 0}.
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3259
     *
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3260
     * @param a one array to be tested for equality
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3261
     * @param a2 the other array to be tested for equality
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3262
     * @param cmp the comparator to compare array elements
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3263
     * @param <T> the type of array elements
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3264
     * @return {@code true} if the two arrays are equal
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3265
     * @throws NullPointerException if the comparator is {@code null}
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3266
     * @since 9
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3267
     */
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3268
    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
  3269
        Objects.requireNonNull(cmp);
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3270
        if (a==a2)
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3271
            return true;
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3272
        if (a==null || a2==null)
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3273
            return false;
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3274
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3275
        int length = a.length;
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3276
        if (a2.length != length)
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3277
            return false;
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3278
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3279
        for (int i=0; i<length; i++) {
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3280
            if (cmp.compare(a[i], a2[i]) != 0)
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3281
                return false;
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3282
        }
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
        return true;
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3285
    }
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
     * Returns true if the two specified arrays of Objects, over the specified
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3289
     * ranges, are <i>equal</i> to one another.
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3290
     *
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3291
     * <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
  3292
     * 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
  3293
     * 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
  3294
     * are equal if they contain, over the specified ranges, the same elements
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3295
     * in the same order.
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3296
     *
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3297
     * <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
  3298
     * given the specified comparator, {@code cmp.compare(e1, e2) == 0}.
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3299
     *
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3300
     * @param a the first array to be tested for equality
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3301
     * @param aFromIndex the index (inclusive) of the first element in the
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3302
     *                   first array to be tested
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3303
     * @param aToIndex the index (exclusive) of the last element in the
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3304
     *                 first array to be tested
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3305
     * @param b the second array to be tested fro equality
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3306
     * @param bFromIndex the index (inclusive) of the first element in the
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3307
     *                   second array to be tested
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3308
     * @param bToIndex the index (exclusive) of the last element in the
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3309
     *                 second array to be tested
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3310
     * @param cmp the comparator to compare array elements
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3311
     * @param <T> the type of array elements
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3312
     * @return {@code true} if the two arrays, over the specified ranges, are
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3313
     *         equal
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3314
     * @throws IllegalArgumentException
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3315
     *         if {@code aFromIndex > aToIndex} or
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3316
     *         if {@code bFromIndex > bToIndex}
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3317
     * @throws ArrayIndexOutOfBoundsException
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3318
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3319
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3320
     * @throws NullPointerException
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3321
     *         if either array or the comparator is {@code null}
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3322
     * @since 9
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3323
     */
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3324
    public static <T> boolean equals(T[] a, int aFromIndex, int aToIndex,
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3325
                                     T[] b, int bFromIndex, int bToIndex,
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3326
                                     Comparator<? super T> cmp) {
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3327
        Objects.requireNonNull(cmp);
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3328
        rangeCheck(a.length, aFromIndex, aToIndex);
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3329
        rangeCheck(b.length, bFromIndex, bToIndex);
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3330
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3331
        int aLength = aToIndex - aFromIndex;
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3332
        int bLength = bToIndex - bFromIndex;
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3333
        if (aLength != bLength)
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3334
            return false;
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3335
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3336
        for (int i = 0; i < aLength; i++) {
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3337
            if (cmp.compare(a[aFromIndex++], b[bFromIndex++]) != 0)
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3338
                return false;
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3339
        }
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
        return true;
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3342
    }
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3343
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3344
    // Filling
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3345
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3346
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3347
     * Assigns the specified long value to each element of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3348
     * of longs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3349
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3350
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3351
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3352
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3353
    public static void fill(long[] a, long val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3354
        for (int i = 0, len = a.length; i < len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3355
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3357
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3358
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3359
     * Assigns the specified long value to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3360
     * 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
  3361
     * 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
  3362
     * {@code toIndex}, exclusive.  (If {@code fromIndex==toIndex}, the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3363
     * range to be filled is empty.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3364
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3365
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3366
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3367
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3368
     * @param toIndex the index of the last element (exclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3369
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3370
     * @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
  3371
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3372
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3373
     *         {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3374
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3375
    public static void fill(long[] a, int fromIndex, int toIndex, long val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3376
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3377
        for (int i = fromIndex; i < toIndex; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3378
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3379
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3380
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3381
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3382
     * Assigns the specified int value to each element of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3383
     * of ints.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3384
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3385
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3386
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3387
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3388
    public static void fill(int[] a, int val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3389
        for (int i = 0, len = a.length; i < len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3390
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3391
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3392
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3393
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3394
     * Assigns the specified int value to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3395
     * 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
  3396
     * 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
  3397
     * {@code toIndex}, exclusive.  (If {@code fromIndex==toIndex}, the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3398
     * range to be filled is empty.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3399
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3400
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3401
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3402
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3403
     * @param toIndex the index of the last element (exclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3404
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3405
     * @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
  3406
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3407
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3408
     *         {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3409
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3410
    public static void fill(int[] a, int fromIndex, int toIndex, int val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3411
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3412
        for (int i = fromIndex; i < toIndex; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3413
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3414
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3415
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3416
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3417
     * Assigns the specified short value to each element of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3418
     * of shorts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3419
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3420
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3421
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3422
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3423
    public static void fill(short[] a, short val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3424
        for (int i = 0, len = a.length; i < len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3425
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3426
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3427
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3428
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3429
     * Assigns the specified short value to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3430
     * 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
  3431
     * 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
  3432
     * {@code toIndex}, exclusive.  (If {@code fromIndex==toIndex}, the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3433
     * range to be filled is empty.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3434
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3435
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3436
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3437
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3438
     * @param toIndex the index of the last element (exclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3439
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3440
     * @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
  3441
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3442
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3443
     *         {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3444
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3445
    public static void fill(short[] a, int fromIndex, int toIndex, short val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3446
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3447
        for (int i = fromIndex; i < toIndex; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3448
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3449
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3450
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3451
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3452
     * Assigns the specified char value to each element of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3453
     * of chars.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3454
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3455
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3456
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3457
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3458
    public static void fill(char[] a, char val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3459
        for (int i = 0, len = a.length; i < len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3460
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3461
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3462
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3463
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3464
     * Assigns the specified char value to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3465
     * 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
  3466
     * 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
  3467
     * {@code toIndex}, exclusive.  (If {@code fromIndex==toIndex}, the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3468
     * range to be filled is empty.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3469
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3470
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3471
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3472
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3473
     * @param toIndex the index of the last element (exclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3474
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3475
     * @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
  3476
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3477
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3478
     *         {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3479
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3480
    public static void fill(char[] a, int fromIndex, int toIndex, char val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3481
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3482
        for (int i = fromIndex; i < toIndex; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3483
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3484
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3485
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3486
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3487
     * Assigns the specified byte value to each element of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3488
     * of bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3489
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3490
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3491
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3492
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3493
    public static void fill(byte[] a, byte val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3494
        for (int i = 0, len = a.length; i < len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3495
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3496
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3497
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3498
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3499
     * Assigns the specified byte value to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3500
     * 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
  3501
     * 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
  3502
     * {@code toIndex}, exclusive.  (If {@code fromIndex==toIndex}, the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3503
     * range to be filled is empty.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3504
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3505
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3506
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3507
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3508
     * @param toIndex the index of the last element (exclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3509
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3510
     * @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
  3511
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3512
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3513
     *         {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3514
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3515
    public static void fill(byte[] a, int fromIndex, int toIndex, byte val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3516
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3517
        for (int i = fromIndex; i < toIndex; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3518
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3519
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3520
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3521
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3522
     * Assigns the specified boolean value to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3523
     * array of booleans.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3524
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3525
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3526
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3527
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3528
    public static void fill(boolean[] a, boolean val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3529
        for (int i = 0, len = a.length; i < len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3530
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3531
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3532
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3533
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3534
     * Assigns the specified boolean value to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3535
     * 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
  3536
     * 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
  3537
     * {@code toIndex}, exclusive.  (If {@code fromIndex==toIndex}, the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3538
     * range to be filled is empty.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3539
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3540
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3541
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3542
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3543
     * @param toIndex the index of the last element (exclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3544
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3545
     * @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
  3546
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3547
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3548
     *         {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3549
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3550
    public static void fill(boolean[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3551
                            boolean val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3552
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3553
        for (int i = fromIndex; i < toIndex; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3554
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3555
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3556
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3557
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3558
     * Assigns the specified double value to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3559
     * array of doubles.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3560
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3561
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3562
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3563
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3564
    public static void fill(double[] a, double val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3565
        for (int i = 0, len = a.length; i < len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3566
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3567
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3568
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3569
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3570
     * Assigns the specified double value to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3571
     * 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
  3572
     * 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
  3573
     * {@code toIndex}, exclusive.  (If {@code fromIndex==toIndex}, the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3574
     * range to be filled is empty.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3575
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3576
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3577
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3578
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3579
     * @param toIndex the index of the last element (exclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3580
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3581
     * @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
  3582
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3583
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3584
     *         {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3585
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3586
    public static void fill(double[] a, int fromIndex, int toIndex,double val){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3587
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3588
        for (int i = fromIndex; i < toIndex; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3589
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3590
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3591
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3592
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3593
     * Assigns the specified float value to each element of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3594
     * of floats.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3595
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3596
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3597
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3598
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3599
    public static void fill(float[] a, float val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3600
        for (int i = 0, len = a.length; i < len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3601
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3602
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3603
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3604
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3605
     * Assigns the specified float value to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3606
     * 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
  3607
     * 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
  3608
     * {@code toIndex}, exclusive.  (If {@code fromIndex==toIndex}, the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3609
     * range to be filled is empty.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3610
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3611
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3612
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3613
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3614
     * @param toIndex the index of the last element (exclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3615
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3616
     * @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
  3617
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3618
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3619
     *         {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3620
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3621
    public static void fill(float[] a, int fromIndex, int toIndex, float val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3622
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3623
        for (int i = fromIndex; i < toIndex; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3624
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3625
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3626
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3627
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3628
     * Assigns the specified Object reference to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3629
     * array of Objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3630
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3631
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3632
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3633
     * @throws ArrayStoreException if the specified value is not of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3634
     *         runtime type that can be stored in the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3635
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3636
    public static void fill(Object[] a, Object val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3637
        for (int i = 0, len = a.length; i < len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3638
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3639
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3640
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3641
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3642
     * Assigns the specified Object reference to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3643
     * 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
  3644
     * 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
  3645
     * {@code toIndex}, exclusive.  (If {@code fromIndex==toIndex}, the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3646
     * range to be filled is empty.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3647
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3648
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3649
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3650
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3651
     * @param toIndex the index of the last element (exclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3652
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3653
     * @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
  3654
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3655
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3656
     *         {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3657
     * @throws ArrayStoreException if the specified value is not of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3658
     *         runtime type that can be stored in the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3659
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3660
    public static void fill(Object[] a, int fromIndex, int toIndex, Object val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3661
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3662
        for (int i = fromIndex; i < toIndex; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3663
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3664
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3665
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
  3666
    // Cloning
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3667
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3668
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3669
     * Copies the specified array, truncating or padding with nulls (if necessary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3670
     * so the copy has the specified length.  For all indices that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3671
     * valid in both the original array and the copy, the two arrays will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3672
     * 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
  3673
     * copy but not the original, the copy will contain {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3674
     * Such indices will exist if and only if the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3675
     * is greater than that of the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3676
     * The resulting array is of exactly the same class as the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3677
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  3678
     * @param <T> the class of the objects in the array
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3679
     * @param original the array to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3680
     * @param newLength the length of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3681
     * @return a copy of the original array, truncated or padded with nulls
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3682
     *     to obtain the specified length
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3683
     * @throws NegativeArraySizeException if {@code newLength} is negative
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3684
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3685
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3686
     */
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  3687
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3688
    public static <T> T[] copyOf(T[] original, int newLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3689
        return (T[]) copyOf(original, newLength, original.getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3690
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3691
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3692
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3693
     * Copies the specified array, truncating or padding with nulls (if necessary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3694
     * so the copy has the specified length.  For all indices that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3695
     * valid in both the original array and the copy, the two arrays will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3696
     * 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
  3697
     * copy but not the original, the copy will contain {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3698
     * Such indices will exist if and only if the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3699
     * 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
  3700
     * The resulting array is of the class {@code newType}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3701
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  3702
     * @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
  3703
     * @param <T> the class of the objects in the returned array
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3704
     * @param original the array to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3705
     * @param newLength the length of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3706
     * @param newType the class of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3707
     * @return a copy of the original array, truncated or padded with nulls
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3708
     *     to obtain the specified length
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3709
     * @throws NegativeArraySizeException if {@code newLength} is negative
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3710
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3711
     * @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
  3712
     *     {@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
  3713
     *     an array of class {@code newType}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3714
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3715
     */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 29223
diff changeset
  3716
    @HotSpotIntrinsicCandidate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3717
    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
  3718
        @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3719
        T[] copy = ((Object)newType == (Object)Object[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3720
            ? (T[]) new Object[newLength]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3721
            : (T[]) Array.newInstance(newType.getComponentType(), newLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3722
        System.arraycopy(original, 0, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3723
                         Math.min(original.length, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3724
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3725
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3726
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3727
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3728
     * Copies the specified array, truncating or padding with zeros (if necessary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3729
     * so the copy has the specified length.  For all indices that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3730
     * valid in both the original array and the copy, the two arrays will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3731
     * 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
  3732
     * copy but not the original, the copy will contain {@code (byte)0}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3733
     * Such indices will exist if and only if the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3734
     * is greater than that of the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3735
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3736
     * @param original the array to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3737
     * @param newLength the length of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3738
     * @return a copy of the original array, truncated or padded with zeros
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3739
     *     to obtain the specified length
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3740
     * @throws NegativeArraySizeException if {@code newLength} is negative
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3741
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3742
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3743
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3744
    public static byte[] copyOf(byte[] original, int newLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3745
        byte[] copy = new byte[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3746
        System.arraycopy(original, 0, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3747
                         Math.min(original.length, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3748
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3749
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3750
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3751
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3752
     * Copies the specified array, truncating or padding with zeros (if necessary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3753
     * so the copy has the specified length.  For all indices that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3754
     * valid in both the original array and the copy, the two arrays will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3755
     * 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
  3756
     * copy but not the original, the copy will contain {@code (short)0}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3757
     * Such indices will exist if and only if the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3758
     * is greater than that of the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3759
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3760
     * @param original the array to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3761
     * @param newLength the length of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3762
     * @return a copy of the original array, truncated or padded with zeros
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3763
     *     to obtain the specified length
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3764
     * @throws NegativeArraySizeException if {@code newLength} is negative
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3765
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3766
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3767
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3768
    public static short[] copyOf(short[] original, int newLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3769
        short[] copy = new short[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3770
        System.arraycopy(original, 0, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3771
                         Math.min(original.length, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3772
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3773
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3774
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3775
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3776
     * Copies the specified array, truncating or padding with zeros (if necessary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3777
     * so the copy has the specified length.  For all indices that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3778
     * valid in both the original array and the copy, the two arrays will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3779
     * 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
  3780
     * copy but not the original, the copy will contain {@code 0}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3781
     * Such indices will exist if and only if the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3782
     * is greater than that of the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3783
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3784
     * @param original the array to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3785
     * @param newLength the length of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3786
     * @return a copy of the original array, truncated or padded with zeros
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3787
     *     to obtain the specified length
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3788
     * @throws NegativeArraySizeException if {@code newLength} is negative
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3789
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3790
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3791
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3792
    public static int[] copyOf(int[] original, int newLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3793
        int[] copy = new int[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3794
        System.arraycopy(original, 0, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3795
                         Math.min(original.length, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3796
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3797
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3798
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3799
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3800
     * Copies the specified array, truncating or padding with zeros (if necessary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3801
     * so the copy has the specified length.  For all indices that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3802
     * valid in both the original array and the copy, the two arrays will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3803
     * 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
  3804
     * copy but not the original, the copy will contain {@code 0L}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3805
     * Such indices will exist if and only if the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3806
     * is greater than that of the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3807
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3808
     * @param original the array to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3809
     * @param newLength the length of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3810
     * @return a copy of the original array, truncated or padded with zeros
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3811
     *     to obtain the specified length
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3812
     * @throws NegativeArraySizeException if {@code newLength} is negative
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3813
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3814
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3815
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3816
    public static long[] copyOf(long[] original, int newLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3817
        long[] copy = new long[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3818
        System.arraycopy(original, 0, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3819
                         Math.min(original.length, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3820
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3821
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3822
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3823
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3824
     * Copies the specified array, truncating or padding with null characters (if necessary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3825
     * so the copy has the specified length.  For all indices that are valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3826
     * in both the original array and the copy, the two arrays will contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3827
     * 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
  3828
     * the original, the copy will contain {@code '\\u000'}.  Such indices
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3829
     * will exist if and only if the specified length is greater than that of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3830
     * the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3831
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3832
     * @param original the array to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3833
     * @param newLength the length of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3834
     * @return a copy of the original array, truncated or padded with null characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3835
     *     to obtain the specified length
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3836
     * @throws NegativeArraySizeException if {@code newLength} is negative
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3837
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3838
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3839
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3840
    public static char[] copyOf(char[] original, int newLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3841
        char[] copy = new char[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3842
        System.arraycopy(original, 0, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3843
                         Math.min(original.length, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3844
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3845
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3846
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3847
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3848
     * Copies the specified array, truncating or padding with zeros (if necessary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3849
     * so the copy has the specified length.  For all indices that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3850
     * valid in both the original array and the copy, the two arrays will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3851
     * 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
  3852
     * copy but not the original, the copy will contain {@code 0f}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3853
     * Such indices will exist if and only if the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3854
     * is greater than that of the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3855
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3856
     * @param original the array to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3857
     * @param newLength the length of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3858
     * @return a copy of the original array, truncated or padded with zeros
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3859
     *     to obtain the specified length
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3860
     * @throws NegativeArraySizeException if {@code newLength} is negative
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3861
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3862
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3863
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3864
    public static float[] copyOf(float[] original, int newLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3865
        float[] copy = new float[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3866
        System.arraycopy(original, 0, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3867
                         Math.min(original.length, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3868
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3869
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3870
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3871
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3872
     * Copies the specified array, truncating or padding with zeros (if necessary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3873
     * so the copy has the specified length.  For all indices that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3874
     * valid in both the original array and the copy, the two arrays will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3875
     * 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
  3876
     * copy but not the original, the copy will contain {@code 0d}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3877
     * Such indices will exist if and only if the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3878
     * is greater than that of the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3879
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3880
     * @param original the array to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3881
     * @param newLength the length of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3882
     * @return a copy of the original array, truncated or padded with zeros
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3883
     *     to obtain the specified length
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3884
     * @throws NegativeArraySizeException if {@code newLength} is negative
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3885
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3886
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3887
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3888
    public static double[] copyOf(double[] original, int newLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3889
        double[] copy = new double[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3890
        System.arraycopy(original, 0, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3891
                         Math.min(original.length, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3892
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3893
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3894
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3895
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3896
     * Copies the specified array, truncating or padding with {@code false} (if necessary)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3897
     * so the copy has the specified length.  For all indices that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3898
     * valid in both the original array and the copy, the two arrays will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3899
     * 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
  3900
     * copy but not the original, the copy will contain {@code false}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3901
     * Such indices will exist if and only if the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3902
     * is greater than that of the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3903
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3904
     * @param original the array to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3905
     * @param newLength the length of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3906
     * @return a copy of the original array, truncated or padded with false elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3907
     *     to obtain the specified length
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3908
     * @throws NegativeArraySizeException if {@code newLength} is negative
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3909
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3910
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3911
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3912
    public static boolean[] copyOf(boolean[] original, int newLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3913
        boolean[] copy = new boolean[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3914
        System.arraycopy(original, 0, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3915
                         Math.min(original.length, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3916
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3917
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3918
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3919
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3920
     * 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
  3921
     * 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
  3922
     * 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
  3923
     * {@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
  3924
     * (unless {@code from == original.length} or {@code from == to}).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3925
     * Values from subsequent elements in the original array are placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3926
     * 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
  3927
     * ({@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
  3928
     * 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
  3929
     * {@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
  3930
     * 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
  3931
     * of the returned array will be {@code to - from}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3932
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3933
     * The resulting array is of exactly the same class as the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3934
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  3935
     * @param <T> the class of the objects in the array
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3936
     * @param original the array from which a range is to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3937
     * @param from the initial index of the range to be copied, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3938
     * @param to the final index of the range to be copied, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3939
     *     (This index may lie outside the array.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3940
     * @return a new array containing the specified range from the original array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3941
     *     truncated or padded with nulls to obtain the required length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3942
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3943
     *     or {@code from > original.length}
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3944
     * @throws IllegalArgumentException if {@code from > to}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3945
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3946
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3947
     */
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  3948
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3949
    public static <T> T[] copyOfRange(T[] original, int from, int to) {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  3950
        return copyOfRange(original, from, to, (Class<? extends T[]>) original.getClass());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3951
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3952
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3953
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3954
     * 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
  3955
     * 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
  3956
     * 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
  3957
     * {@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
  3958
     * (unless {@code from == original.length} or {@code from == to}).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3959
     * Values from subsequent elements in the original array are placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3960
     * 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
  3961
     * ({@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
  3962
     * 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
  3963
     * {@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
  3964
     * 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
  3965
     * 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
  3966
     * The resulting array is of the class {@code newType}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3967
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  3968
     * @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
  3969
     * @param <T> the class of the objects in the returned array
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3970
     * @param original the array from which a range is to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3971
     * @param from the initial index of the range to be copied, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3972
     * @param to the final index of the range to be copied, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3973
     *     (This index may lie outside the array.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3974
     * @param newType the class of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3975
     * @return a new array containing the specified range from the original array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3976
     *     truncated or padded with nulls to obtain the required length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3977
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3978
     *     or {@code from > original.length}
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3979
     * @throws IllegalArgumentException if {@code from > to}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3980
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3981
     * @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
  3982
     *     {@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
  3983
     *     an array of class {@code newType}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3984
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3985
     */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 29223
diff changeset
  3986
    @HotSpotIntrinsicCandidate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3987
    public static <T,U> T[] copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3988
        int newLength = to - from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3989
        if (newLength < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3990
            throw new IllegalArgumentException(from + " > " + to);
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  3991
        @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3992
        T[] copy = ((Object)newType == (Object)Object[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3993
            ? (T[]) new Object[newLength]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3994
            : (T[]) Array.newInstance(newType.getComponentType(), newLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3995
        System.arraycopy(original, from, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3996
                         Math.min(original.length - from, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3997
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3998
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3999
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4000
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4001
     * 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
  4002
     * 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
  4003
     * 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
  4004
     * {@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
  4005
     * (unless {@code from == original.length} or {@code from == to}).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4006
     * Values from subsequent elements in the original array are placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4007
     * 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
  4008
     * ({@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
  4009
     * 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
  4010
     * {@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
  4011
     * 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
  4012
     * of the returned array will be {@code to - from}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4013
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4014
     * @param original the array from which a range is to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4015
     * @param from the initial index of the range to be copied, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4016
     * @param to the final index of the range to be copied, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4017
     *     (This index may lie outside the array.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4018
     * @return a new array containing the specified range from the original array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4019
     *     truncated or padded with zeros to obtain the required length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4020
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4021
     *     or {@code from > original.length}
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4022
     * @throws IllegalArgumentException if {@code from > to}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4023
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4024
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4025
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4026
    public static byte[] copyOfRange(byte[] original, int from, int to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4027
        int newLength = to - from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4028
        if (newLength < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4029
            throw new IllegalArgumentException(from + " > " + to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4030
        byte[] copy = new byte[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4031
        System.arraycopy(original, from, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4032
                         Math.min(original.length - from, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4033
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4034
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4035
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4036
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4037
     * 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
  4038
     * 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
  4039
     * 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
  4040
     * {@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
  4041
     * (unless {@code from == original.length} or {@code from == to}).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4042
     * Values from subsequent elements in the original array are placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4043
     * 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
  4044
     * ({@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
  4045
     * 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
  4046
     * {@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
  4047
     * 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
  4048
     * of the returned array will be {@code to - from}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4049
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4050
     * @param original the array from which a range is to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4051
     * @param from the initial index of the range to be copied, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4052
     * @param to the final index of the range to be copied, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4053
     *     (This index may lie outside the array.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4054
     * @return a new array containing the specified range from the original array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4055
     *     truncated or padded with zeros to obtain the required length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4056
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4057
     *     or {@code from > original.length}
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4058
     * @throws IllegalArgumentException if {@code from > to}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4059
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4060
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4061
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4062
    public static short[] copyOfRange(short[] original, int from, int to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4063
        int newLength = to - from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4064
        if (newLength < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4065
            throw new IllegalArgumentException(from + " > " + to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4066
        short[] copy = new short[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4067
        System.arraycopy(original, from, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4068
                         Math.min(original.length - from, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4069
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4070
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4071
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4072
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4073
     * 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
  4074
     * 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
  4075
     * 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
  4076
     * {@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
  4077
     * (unless {@code from == original.length} or {@code from == to}).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4078
     * Values from subsequent elements in the original array are placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4079
     * 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
  4080
     * ({@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
  4081
     * 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
  4082
     * {@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
  4083
     * 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
  4084
     * of the returned array will be {@code to - from}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4085
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4086
     * @param original the array from which a range is to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4087
     * @param from the initial index of the range to be copied, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4088
     * @param to the final index of the range to be copied, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4089
     *     (This index may lie outside the array.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4090
     * @return a new array containing the specified range from the original array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4091
     *     truncated or padded with zeros to obtain the required length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4092
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4093
     *     or {@code from > original.length}
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4094
     * @throws IllegalArgumentException if {@code from > to}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4095
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4096
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4097
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4098
    public static int[] copyOfRange(int[] original, int from, int to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4099
        int newLength = to - from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4100
        if (newLength < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4101
            throw new IllegalArgumentException(from + " > " + to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4102
        int[] copy = new int[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4103
        System.arraycopy(original, from, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4104
                         Math.min(original.length - from, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4105
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4107
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4108
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4109
     * 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
  4110
     * 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
  4111
     * 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
  4112
     * {@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
  4113
     * (unless {@code from == original.length} or {@code from == to}).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4114
     * Values from subsequent elements in the original array are placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4115
     * 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
  4116
     * ({@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
  4117
     * 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
  4118
     * {@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
  4119
     * 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
  4120
     * of the returned array will be {@code to - from}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4121
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4122
     * @param original the array from which a range is to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4123
     * @param from the initial index of the range to be copied, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4124
     * @param to the final index of the range to be copied, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4125
     *     (This index may lie outside the array.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4126
     * @return a new array containing the specified range from the original array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4127
     *     truncated or padded with zeros to obtain the required length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4128
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4129
     *     or {@code from > original.length}
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4130
     * @throws IllegalArgumentException if {@code from > to}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4131
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4132
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4134
    public static long[] copyOfRange(long[] original, int from, int to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4135
        int newLength = to - from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4136
        if (newLength < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4137
            throw new IllegalArgumentException(from + " > " + to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4138
        long[] copy = new long[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4139
        System.arraycopy(original, from, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4140
                         Math.min(original.length - from, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4141
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4143
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4145
     * 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
  4146
     * 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
  4147
     * 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
  4148
     * {@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
  4149
     * (unless {@code from == original.length} or {@code from == to}).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4150
     * Values from subsequent elements in the original array are placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4151
     * 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
  4152
     * ({@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
  4153
     * 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
  4154
     * {@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
  4155
     * 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
  4156
     * of the returned array will be {@code to - from}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4157
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4158
     * @param original the array from which a range is to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4159
     * @param from the initial index of the range to be copied, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4160
     * @param to the final index of the range to be copied, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4161
     *     (This index may lie outside the array.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4162
     * @return a new array containing the specified range from the original array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4163
     *     truncated or padded with null characters to obtain the required length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4164
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4165
     *     or {@code from > original.length}
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4166
     * @throws IllegalArgumentException if {@code from > to}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4167
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4168
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4170
    public static char[] copyOfRange(char[] original, int from, int to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4171
        int newLength = to - from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4172
        if (newLength < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4173
            throw new IllegalArgumentException(from + " > " + to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4174
        char[] copy = new char[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4175
        System.arraycopy(original, from, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4176
                         Math.min(original.length - from, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4177
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4179
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4180
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4181
     * 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
  4182
     * 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
  4183
     * 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
  4184
     * {@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
  4185
     * (unless {@code from == original.length} or {@code from == to}).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4186
     * Values from subsequent elements in the original array are placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4187
     * 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
  4188
     * ({@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
  4189
     * 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
  4190
     * {@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
  4191
     * 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
  4192
     * of the returned array will be {@code to - from}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4193
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4194
     * @param original the array from which a range is to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4195
     * @param from the initial index of the range to be copied, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4196
     * @param to the final index of the range to be copied, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4197
     *     (This index may lie outside the array.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4198
     * @return a new array containing the specified range from the original array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4199
     *     truncated or padded with zeros to obtain the required length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4200
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4201
     *     or {@code from > original.length}
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4202
     * @throws IllegalArgumentException if {@code from > to}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4203
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4204
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4205
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4206
    public static float[] copyOfRange(float[] original, int from, int to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4207
        int newLength = to - from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4208
        if (newLength < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4209
            throw new IllegalArgumentException(from + " > " + to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4210
        float[] copy = new float[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4211
        System.arraycopy(original, from, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4212
                         Math.min(original.length - from, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4213
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4215
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4216
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4217
     * 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
  4218
     * 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
  4219
     * 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
  4220
     * {@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
  4221
     * (unless {@code from == original.length} or {@code from == to}).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4222
     * Values from subsequent elements in the original array are placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4223
     * 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
  4224
     * ({@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
  4225
     * 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
  4226
     * {@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
  4227
     * 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
  4228
     * of the returned array will be {@code to - from}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4229
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4230
     * @param original the array from which a range is to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4231
     * @param from the initial index of the range to be copied, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4232
     * @param to the final index of the range to be copied, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4233
     *     (This index may lie outside the array.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4234
     * @return a new array containing the specified range from the original array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4235
     *     truncated or padded with zeros to obtain the required length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4236
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4237
     *     or {@code from > original.length}
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4238
     * @throws IllegalArgumentException if {@code from > to}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4239
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4240
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4241
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4242
    public static double[] copyOfRange(double[] original, int from, int to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4243
        int newLength = to - from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4244
        if (newLength < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4245
            throw new IllegalArgumentException(from + " > " + to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4246
        double[] copy = new double[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4247
        System.arraycopy(original, from, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4248
                         Math.min(original.length - from, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4249
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4250
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4251
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4252
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4253
     * 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
  4254
     * 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
  4255
     * 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
  4256
     * {@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
  4257
     * (unless {@code from == original.length} or {@code from == to}).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4258
     * Values from subsequent elements in the original array are placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4259
     * 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
  4260
     * ({@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
  4261
     * 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
  4262
     * {@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
  4263
     * 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
  4264
     * of the returned array will be {@code to - from}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4265
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4266
     * @param original the array from which a range is to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4267
     * @param from the initial index of the range to be copied, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4268
     * @param to the final index of the range to be copied, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4269
     *     (This index may lie outside the array.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4270
     * @return a new array containing the specified range from the original array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4271
     *     truncated or padded with false elements to obtain the required length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4272
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4273
     *     or {@code from > original.length}
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4274
     * @throws IllegalArgumentException if {@code from > to}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4275
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4276
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4277
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4278
    public static boolean[] copyOfRange(boolean[] original, int from, int to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4279
        int newLength = to - from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4280
        if (newLength < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4281
            throw new IllegalArgumentException(from + " > " + to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4282
        boolean[] copy = new boolean[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4283
        System.arraycopy(original, from, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4284
                         Math.min(original.length - from, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4285
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4287
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4288
    // Misc
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4289
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4290
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4291
     * Returns a fixed-size list backed by the specified array.  (Changes to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4292
     * the returned list "write through" to the array.)  This method acts
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4293
     * as bridge between array-based and collection-based APIs, in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4294
     * combination with {@link Collection#toArray}.  The returned list is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4295
     * serializable and implements {@link RandomAccess}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4296
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4297
     * <p>This method also provides a convenient way to create a fixed-size
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4298
     * list initialized to contain several elements:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4299
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4300
     *     List&lt;String&gt; stooges = Arrays.asList("Larry", "Moe", "Curly");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4301
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4302
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  4303
     * @param <T> the class of the objects in the array
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4304
     * @param a the array by which the list will be backed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4305
     * @return a list view of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4306
     */
8151
88b01a6d5f51 7006578: Project Coin: Retrofit JDK libraries with @SafeVarargs
darcy
parents: 7816
diff changeset
  4307
    @SafeVarargs
13795
73850c397272 7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents: 12448
diff changeset
  4308
    @SuppressWarnings("varargs")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4309
    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
  4310
        return new ArrayList<>(a);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4312
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4313
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4314
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4315
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4316
    private static class ArrayList<E> extends AbstractList<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4317
        implements RandomAccess, java.io.Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4318
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4319
        private static final long serialVersionUID = -2764017481108945198L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4320
        private final E[] a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4321
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4322
        ArrayList(E[] array) {
14925
72729557c226 8003981: Support Parallel Array Sorting - JEP 103
chegar
parents: 13795
diff changeset
  4323
            a = Objects.requireNonNull(array);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4324
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4325
14925
72729557c226 8003981: Support Parallel Array Sorting - JEP 103
chegar
parents: 13795
diff changeset
  4326
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4327
        public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4328
            return a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4329
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4330
14925
72729557c226 8003981: Support Parallel Array Sorting - JEP 103
chegar
parents: 13795
diff changeset
  4331
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4332
        public Object[] toArray() {
31540
6efd719b3330 6260652: (coll) Arrays.asList(x).toArray().getClass() should be Object[].class
martin
parents: 29223
diff changeset
  4333
            return Arrays.copyOf(a, a.length, Object[].class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4334
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4335
14925
72729557c226 8003981: Support Parallel Array Sorting - JEP 103
chegar
parents: 13795
diff changeset
  4336
        @Override
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  4337
        @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4338
        public <T> T[] toArray(T[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4339
            int size = size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4340
            if (a.length < size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4341
                return Arrays.copyOf(this.a, size,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4342
                                     (Class<? extends T[]>) a.getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4343
            System.arraycopy(this.a, 0, a, 0, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4344
            if (a.length > size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4345
                a[size] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4346
            return a;
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 E get(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4351
            return a[index];
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 E set(int index, E element) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4356
            E oldValue = a[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4357
            a[index] = element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4358
            return oldValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4359
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4360
14925
72729557c226 8003981: Support Parallel Array Sorting - JEP 103
chegar
parents: 13795
diff changeset
  4361
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4362
        public int indexOf(Object o) {
23354
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4363
            E[] a = this.a;
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4364
            if (o == null) {
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4365
                for (int i = 0; i < a.length; i++)
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4366
                    if (a[i] == null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4367
                        return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4368
            } else {
23354
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4369
                for (int i = 0; i < a.length; i++)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4370
                    if (o.equals(a[i]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4371
                        return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4372
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4373
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4374
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4375
14925
72729557c226 8003981: Support Parallel Array Sorting - JEP 103
chegar
parents: 13795
diff changeset
  4376
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4377
        public boolean contains(Object o) {
26451
f86e59f18322 8056951: pico-optimize contains(Object) methods
martin
parents: 25859
diff changeset
  4378
            return indexOf(o) >= 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4379
        }
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4380
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4381
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4382
        public Spliterator<E> spliterator() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4383
            return Spliterators.spliterator(a, Spliterator.ORDERED);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4384
        }
23354
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4385
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4386
        @Override
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4387
        public void forEach(Consumer<? super E> action) {
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4388
            Objects.requireNonNull(action);
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4389
            for (E e : a) {
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4390
                action.accept(e);
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4391
            }
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4392
        }
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4393
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4394
        @Override
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4395
        public void replaceAll(UnaryOperator<E> operator) {
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4396
            Objects.requireNonNull(operator);
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4397
            E[] a = this.a;
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4398
            for (int i = 0; i < a.length; i++) {
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4399
                a[i] = operator.apply(a[i]);
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4400
            }
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4401
        }
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4402
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4403
        @Override
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4404
        public void sort(Comparator<? super E> c) {
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4405
            Arrays.sort(a, c);
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4406
        }
37685
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4407
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4408
        @Override
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4409
        public Iterator<E> iterator() {
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4410
            return new ArrayItr<>(a);
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4411
        }
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4412
    }
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4413
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4414
    private static class ArrayItr<E> implements Iterator<E> {
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4415
        private int cursor;
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4416
        private final E[] a;
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4417
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4418
        ArrayItr(E[] a) {
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4419
            this.a = a;
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4420
        }
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4421
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4422
        @Override
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4423
        public boolean hasNext() {
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4424
            return cursor < a.length;
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4425
        }
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4426
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4427
        @Override
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4428
        public E next() {
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4429
            int i = cursor;
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4430
            if (i >= a.length) {
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4431
                throw new NoSuchElementException();
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4432
            }
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4433
            cursor = i + 1;
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4434
            return a[i];
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4435
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4436
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4437
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4438
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4439
     * 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
  4440
     * 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
  4441
     * 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
  4442
     * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4443
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4444
     * <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
  4445
     * obtained by invoking the {@link List#hashCode() hashCode}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4446
     * 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
  4447
     * 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
  4448
     * If {@code a} is {@code null}, this method returns 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4449
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4450
     * @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
  4451
     * @return a content-based hash code for {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4452
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4453
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4454
    public static int hashCode(long a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4455
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4456
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4457
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4458
        int result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4459
        for (long element : a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4460
            int elementHash = (int)(element ^ (element >>> 32));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4461
            result = 31 * result + elementHash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4462
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4463
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4464
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4465
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4466
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4467
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4468
     * 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
  4469
     * 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
  4470
     * 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
  4471
     * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4472
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4473
     * <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
  4474
     * obtained by invoking the {@link List#hashCode() hashCode}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4475
     * 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
  4476
     * 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
  4477
     * If {@code a} is {@code null}, this method returns 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4478
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4479
     * @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
  4480
     * @return a content-based hash code for {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4481
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4482
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4483
    public static int hashCode(int a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4484
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4485
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4486
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4487
        int result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4488
        for (int element : a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4489
            result = 31 * result + element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4490
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4491
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4492
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4493
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4494
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4495
     * 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
  4496
     * 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
  4497
     * 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
  4498
     * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4499
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4500
     * <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
  4501
     * obtained by invoking the {@link List#hashCode() hashCode}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4502
     * 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
  4503
     * 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
  4504
     * If {@code a} is {@code null}, this method returns 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4505
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4506
     * @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
  4507
     * @return a content-based hash code for {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4508
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4509
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4510
    public static int hashCode(short a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4511
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4512
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4513
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4514
        int result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4515
        for (short element : a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4516
            result = 31 * result + element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4517
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4518
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4519
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4520
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4521
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4522
     * 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
  4523
     * 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
  4524
     * 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
  4525
     * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4526
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4527
     * <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
  4528
     * obtained by invoking the {@link List#hashCode() hashCode}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4529
     * 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
  4530
     * 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
  4531
     * If {@code a} is {@code null}, this method returns 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4532
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4533
     * @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
  4534
     * @return a content-based hash code for {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4535
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4536
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4537
    public static int hashCode(char a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4538
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4539
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4540
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4541
        int result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4542
        for (char element : a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4543
            result = 31 * result + element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4544
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4545
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4546
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4547
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4548
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4549
     * 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
  4550
     * 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
  4551
     * 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
  4552
     * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4553
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4554
     * <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
  4555
     * obtained by invoking the {@link List#hashCode() hashCode}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4556
     * 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
  4557
     * 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
  4558
     * If {@code a} is {@code null}, this method returns 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4559
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4560
     * @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
  4561
     * @return a content-based hash code for {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4562
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4563
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4564
    public static int hashCode(byte a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4565
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4566
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4567
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4568
        int result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4569
        for (byte element : a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4570
            result = 31 * result + element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4571
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4572
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4573
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4574
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4575
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4576
     * 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
  4577
     * 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
  4578
     * 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
  4579
     * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4580
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4581
     * <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
  4582
     * obtained by invoking the {@link List#hashCode() hashCode}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4583
     * 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
  4584
     * 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
  4585
     * If {@code a} is {@code null}, this method returns 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4586
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4587
     * @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
  4588
     * @return a content-based hash code for {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4589
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4590
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4591
    public static int hashCode(boolean a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4592
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4593
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4594
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4595
        int result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4596
        for (boolean element : a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4597
            result = 31 * result + (element ? 1231 : 1237);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4598
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4599
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4600
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4601
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4602
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4603
     * 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
  4604
     * 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
  4605
     * 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
  4606
     * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4607
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4608
     * <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
  4609
     * obtained by invoking the {@link List#hashCode() hashCode}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4610
     * 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
  4611
     * 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
  4612
     * If {@code a} is {@code null}, this method returns 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4613
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4614
     * @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
  4615
     * @return a content-based hash code for {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4616
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4617
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4618
    public static int hashCode(float a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4619
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4620
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4621
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4622
        int result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4623
        for (float element : a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4624
            result = 31 * result + Float.floatToIntBits(element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4625
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4626
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4627
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4628
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4629
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4630
     * 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
  4631
     * 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
  4632
     * 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
  4633
     * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4634
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4635
     * <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
  4636
     * obtained by invoking the {@link List#hashCode() hashCode}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4637
     * 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
  4638
     * 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
  4639
     * If {@code a} is {@code null}, this method returns 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4640
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4641
     * @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
  4642
     * @return a content-based hash code for {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4643
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4644
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4645
    public static int hashCode(double a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4646
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4647
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4648
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4649
        int result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4650
        for (double element : a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4651
            long bits = Double.doubleToLongBits(element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4652
            result = 31 * result + (int)(bits ^ (bits >>> 32));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4653
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4654
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4655
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4656
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4657
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4658
     * Returns a hash code based on the contents of the specified array.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4659
     * the array contains other arrays as elements, the hash code is based on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4660
     * their identities rather than their contents.  It is therefore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4661
     * acceptable to invoke this method on an array that contains itself as an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4662
     * element,  either directly or indirectly through one or more levels of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4663
     * arrays.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4664
     *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4665
     * <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
  4666
     * {@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
  4667
     * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4668
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4669
     * <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
  4670
     * 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
  4671
     * is {@code null}, in which case {@code 0} is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4672
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4673
     * @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
  4674
     * @return a content-based hash code for {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4675
     * @see #deepHashCode(Object[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4676
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4677
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4678
    public static int hashCode(Object a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4679
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4680
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4681
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4682
        int result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4683
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4684
        for (Object element : a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4685
            result = 31 * result + (element == null ? 0 : element.hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4686
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4687
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4688
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4689
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4690
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4691
     * Returns a hash code based on the "deep contents" of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4692
     * array.  If the array contains other arrays as elements, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4693
     * hash code is based on their contents and so on, ad infinitum.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4694
     * It is therefore unacceptable to invoke this method on an array that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4695
     * contains itself as an element, either directly or indirectly through
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4696
     * one or more levels of arrays.  The behavior of such an invocation is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4697
     * undefined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4698
     *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4699
     * <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
  4700
     * {@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
  4701
     * {@code Arrays.deepHashCode(a) == Arrays.deepHashCode(b)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4702
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4703
     * <p>The computation of the value returned by this method is similar to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4704
     * 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
  4705
     * 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
  4706
     * 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
  4707
     * 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
  4708
     * 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
  4709
     * 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
  4710
     * {@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
  4711
     * of a reference type.  If {@code a} is {@code null}, this method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4712
     * returns 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4713
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4714
     * @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
  4715
     * @return a deep-content-based hash code for {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4716
     * @see #hashCode(Object[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4717
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4718
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4719
    public static int deepHashCode(Object a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4720
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4721
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4722
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4723
        int result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4724
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4725
        for (Object element : a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4726
            int elementHash = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4727
            if (element instanceof Object[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4728
                elementHash = deepHashCode((Object[]) element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4729
            else if (element instanceof byte[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4730
                elementHash = hashCode((byte[]) element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4731
            else if (element instanceof short[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4732
                elementHash = hashCode((short[]) element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4733
            else if (element instanceof int[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4734
                elementHash = hashCode((int[]) element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4735
            else if (element instanceof long[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4736
                elementHash = hashCode((long[]) element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4737
            else if (element instanceof char[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4738
                elementHash = hashCode((char[]) element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4739
            else if (element instanceof float[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4740
                elementHash = hashCode((float[]) element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4741
            else if (element instanceof double[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4742
                elementHash = hashCode((double[]) element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4743
            else if (element instanceof boolean[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4744
                elementHash = hashCode((boolean[]) element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4745
            else if (element != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4746
                elementHash = element.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4747
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4748
            result = 31 * result + elementHash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4749
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4750
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4751
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4752
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4753
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4754
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4755
     * Returns {@code true} if the two specified arrays are <i>deeply
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4756
     * equal</i> to one another.  Unlike the {@link #equals(Object[],Object[])}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4757
     * method, this method is appropriate for use with nested arrays of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4758
     * arbitrary depth.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4759
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4760
     * <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
  4761
     * are {@code null}, or if they refer to arrays that contain the same
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4762
     * number of elements and all corresponding pairs of elements in the two
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4763
     * arrays are deeply equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4764
     *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4765
     * <p>Two possibly {@code null} elements {@code e1} and {@code e2} are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4766
     * deeply equal if any of the following conditions hold:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4767
     * <ul>
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4768
     *    <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
  4769
     *         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
  4770
     *    <li> {@code e1} and {@code e2} are arrays of the same primitive
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4771
     *         type, and the appropriate overloading of
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4772
     *         {@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
  4773
     *    <li> {@code e1 == e2}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4774
     *    <li> {@code e1.equals(e2)} would return true.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4775
     * </ul>
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4776
     * Note that this definition permits {@code null} elements at any depth.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4777
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4778
     * <p>If either of the specified arrays contain themselves as elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4779
     * either directly or indirectly through one or more levels of arrays,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4780
     * the behavior of this method is undefined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4781
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4782
     * @param a1 one array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4783
     * @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
  4784
     * @return {@code true} if the two arrays are equal
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4785
     * @see #equals(Object[],Object[])
4165
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4786
     * @see Objects#deepEquals(Object, Object)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4787
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4788
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4789
    public static boolean deepEquals(Object[] a1, Object[] a2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4790
        if (a1 == a2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4791
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4792
        if (a1 == null || a2==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4793
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4794
        int length = a1.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4795
        if (a2.length != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4796
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4797
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4798
        for (int i = 0; i < length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4799
            Object e1 = a1[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4800
            Object e2 = a2[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4801
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4802
            if (e1 == e2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4803
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4804
            if (e1 == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4805
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4806
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4807
            // 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
  4808
            boolean eq = deepEquals0(e1, e2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4809
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4810
            if (!eq)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4811
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4812
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4813
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4814
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4815
4165
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4816
    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
  4817
        assert e1 != null;
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4818
        boolean eq;
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4819
        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
  4820
            eq = deepEquals ((Object[]) e1, (Object[]) e2);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4821
        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
  4822
            eq = equals((byte[]) e1, (byte[]) e2);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4823
        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
  4824
            eq = equals((short[]) e1, (short[]) e2);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4825
        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
  4826
            eq = equals((int[]) e1, (int[]) e2);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4827
        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
  4828
            eq = equals((long[]) e1, (long[]) e2);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4829
        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
  4830
            eq = equals((char[]) e1, (char[]) e2);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4831
        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
  4832
            eq = equals((float[]) e1, (float[]) e2);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4833
        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
  4834
            eq = equals((double[]) e1, (double[]) e2);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4835
        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
  4836
            eq = equals((boolean[]) e1, (boolean[]) e2);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4837
        else
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4838
            eq = e1.equals(e2);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4839
        return eq;
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4840
    }
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4841
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4842
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4843
     * Returns a string representation of the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4844
     * 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
  4845
     * 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
  4846
     * separated by the characters {@code ", "} (a comma followed by a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4847
     * 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
  4848
     * {@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
  4849
     * is {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4850
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4851
     * @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
  4852
     * @return a string representation of {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4853
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4854
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4855
    public static String toString(long[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4856
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4857
            return "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4858
        int iMax = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4859
        if (iMax == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4860
            return "[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4861
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4862
        StringBuilder b = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4863
        b.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4864
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4865
            b.append(a[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4866
            if (i == iMax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4867
                return b.append(']').toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4868
            b.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4869
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4870
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4871
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4872
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4873
     * Returns a string representation of the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4874
     * 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
  4875
     * 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
  4876
     * separated by the characters {@code ", "} (a comma followed by a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4877
     * 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
  4878
     * {@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
  4879
     * {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4880
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4881
     * @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
  4882
     * @return a string representation of {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4883
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4884
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4885
    public static String toString(int[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4886
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4887
            return "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4888
        int iMax = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4889
        if (iMax == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4890
            return "[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4891
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4892
        StringBuilder b = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4893
        b.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4894
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4895
            b.append(a[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4896
            if (i == iMax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4897
                return b.append(']').toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4898
            b.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4899
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4900
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4901
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4902
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4903
     * Returns a string representation of the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4904
     * 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
  4905
     * 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
  4906
     * separated by the characters {@code ", "} (a comma followed by a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4907
     * 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
  4908
     * {@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
  4909
     * is {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4910
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4911
     * @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
  4912
     * @return a string representation of {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4913
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4914
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4915
    public static String toString(short[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4916
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4917
            return "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4918
        int iMax = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4919
        if (iMax == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4920
            return "[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4921
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4922
        StringBuilder b = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4923
        b.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4924
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4925
            b.append(a[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4926
            if (i == iMax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4927
                return b.append(']').toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4928
            b.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4929
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4930
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4931
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4932
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4933
     * Returns a string representation of the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4934
     * 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
  4935
     * 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
  4936
     * separated by the characters {@code ", "} (a comma followed by a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4937
     * 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
  4938
     * {@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
  4939
     * is {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4940
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4941
     * @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
  4942
     * @return a string representation of {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4943
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4944
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4945
    public static String toString(char[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4946
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4947
            return "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4948
        int iMax = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4949
        if (iMax == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4950
            return "[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4951
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4952
        StringBuilder b = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4953
        b.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4954
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4955
            b.append(a[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4956
            if (i == iMax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4957
                return b.append(']').toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4958
            b.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4959
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4960
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4961
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4962
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4963
     * Returns a string representation of the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4964
     * 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
  4965
     * enclosed in square brackets ({@code "[]"}).  Adjacent elements
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4966
     * are separated by the characters {@code ", "} (a comma followed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4967
     * 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
  4968
     * {@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
  4969
     * {@code a} is {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4970
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4971
     * @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
  4972
     * @return a string representation of {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4973
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4974
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4975
    public static String toString(byte[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4976
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4977
            return "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4978
        int iMax = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4979
        if (iMax == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4980
            return "[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4981
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4982
        StringBuilder b = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4983
        b.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4984
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4985
            b.append(a[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4986
            if (i == iMax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4987
                return b.append(']').toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4988
            b.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4989
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4990
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4991
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4992
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4993
     * Returns a string representation of the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4994
     * 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
  4995
     * 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
  4996
     * separated by the characters {@code ", "} (a comma followed by a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4997
     * 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
  4998
     * {@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
  4999
     * {@code a} is {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5000
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5001
     * @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
  5002
     * @return a string representation of {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5003
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5004
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5005
    public static String toString(boolean[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5006
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5007
            return "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5008
        int iMax = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5009
        if (iMax == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5010
            return "[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5011
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5012
        StringBuilder b = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5013
        b.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5014
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5015
            b.append(a[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5016
            if (i == iMax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5017
                return b.append(']').toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5018
            b.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5019
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5020
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5021
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5022
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5023
     * Returns a string representation of the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5024
     * 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
  5025
     * 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
  5026
     * separated by the characters {@code ", "} (a comma followed by a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5027
     * 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
  5028
     * {@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
  5029
     * is {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5030
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5031
     * @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
  5032
     * @return a string representation of {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5033
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5034
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5035
    public static String toString(float[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5036
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5037
            return "null";
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
  5038
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5039
        int iMax = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5040
        if (iMax == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5041
            return "[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5042
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5043
        StringBuilder b = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5044
        b.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5045
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5046
            b.append(a[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5047
            if (i == iMax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5048
                return b.append(']').toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5049
            b.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5050
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5051
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5052
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5053
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5054
     * Returns a string representation of the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5055
     * 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
  5056
     * 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
  5057
     * separated by the characters {@code ", "} (a comma followed by a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5058
     * 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
  5059
     * {@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
  5060
     * is {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5061
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5062
     * @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
  5063
     * @return a string representation of {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5064
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5065
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5066
    public static String toString(double[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5067
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5068
            return "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5069
        int iMax = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5070
        if (iMax == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5071
            return "[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5072
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5073
        StringBuilder b = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5074
        b.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5075
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5076
            b.append(a[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5077
            if (i == iMax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5078
                return b.append(']').toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5079
            b.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5080
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5081
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5082
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5083
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5084
     * Returns a string representation of the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5085
     * If the array contains other arrays as elements, they are converted to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5086
     * 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
  5087
     * {@code Object}, which describes their <i>identities</i> rather than
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5088
     * their contents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5089
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5090
     * <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
  5091
     * 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
  5092
     * is {@code null}, in which case {@code "null"} is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5093
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5094
     * @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
  5095
     * @return a string representation of {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5096
     * @see #deepToString(Object[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5097
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5098
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5099
    public static String toString(Object[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5100
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5101
            return "null";
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
  5102
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5103
        int iMax = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5104
        if (iMax == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5105
            return "[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5106
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5107
        StringBuilder b = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5108
        b.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5109
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5110
            b.append(String.valueOf(a[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5111
            if (i == iMax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5112
                return b.append(']').toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5113
            b.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5116
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5118
     * Returns a string representation of the "deep contents" of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5119
     * array.  If the array contains other arrays as elements, the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5120
     * representation contains their contents and so on.  This method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5121
     * designed for converting multidimensional arrays to strings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5122
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5123
     * <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
  5124
     * elements, enclosed in square brackets ({@code "[]"}).  Adjacent
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  5125
     * elements are separated by the characters {@code ", "} (a comma
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5126
     * 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
  5127
     * {@code String.valueOf(Object)}, unless they are themselves
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5128
     * arrays.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5129
     *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  5130
     * <p>If an element {@code e} is an array of a primitive type, it is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5131
     * 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
  5132
     * {@code Arrays.toString(e)}.  If an element {@code e} is an array of a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5133
     * reference type, it is converted to a string as by invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5134
     * this method recursively.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5136
     * <p>To avoid infinite recursion, if the specified array contains itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5137
     * as an element, or contains an indirect reference to itself through one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5138
     * 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
  5139
     * {@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
  5140
     * to itself would be rendered as {@code "[[...]]"}.
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  5141
     *
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  5142
     * <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
  5143
     * is {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5144
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5145
     * @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
  5146
     * @return a string representation of {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5147
     * @see #toString(Object[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5148
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5150
    public static String deepToString(Object[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5151
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5152
            return "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5153
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5154
        int bufLen = 20 * a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5155
        if (a.length != 0 && bufLen <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5156
            bufLen = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5157
        StringBuilder buf = new StringBuilder(bufLen);
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 21278
diff changeset
  5158
        deepToString(a, buf, new HashSet<>());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5159
        return buf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5161
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5162
    private static void deepToString(Object[] a, StringBuilder buf,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5163
                                     Set<Object[]> dejaVu) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5164
        if (a == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5165
            buf.append("null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5166
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5168
        int iMax = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5169
        if (iMax == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5170
            buf.append("[]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5171
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5173
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5174
        dejaVu.add(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5175
        buf.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5176
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5177
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5178
            Object element = a[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5179
            if (element == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5180
                buf.append("null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5181
            } else {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  5182
                Class<?> eClass = element.getClass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5183
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5184
                if (eClass.isArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5185
                    if (eClass == byte[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5186
                        buf.append(toString((byte[]) element));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5187
                    else if (eClass == short[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5188
                        buf.append(toString((short[]) element));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5189
                    else if (eClass == int[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5190
                        buf.append(toString((int[]) element));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5191
                    else if (eClass == long[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5192
                        buf.append(toString((long[]) element));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5193
                    else if (eClass == char[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5194
                        buf.append(toString((char[]) element));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5195
                    else if (eClass == float[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5196
                        buf.append(toString((float[]) element));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5197
                    else if (eClass == double[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5198
                        buf.append(toString((double[]) element));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5199
                    else if (eClass == boolean[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5200
                        buf.append(toString((boolean[]) element));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5201
                    else { // element is an array of object references
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5202
                        if (dejaVu.contains(element))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5203
                            buf.append("[...]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5204
                        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5205
                            deepToString((Object[])element, buf, dejaVu);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5206
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5207
                } else {  // element is non-null and not an array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5208
                    buf.append(element.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5209
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5210
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5211
            if (i == iMax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5212
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5213
            buf.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5214
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5215
        buf.append(']');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5216
        dejaVu.remove(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5217
    }
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5218
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5219
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5220
    /**
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5221
     * Set all elements of the specified array, using the provided
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5222
     * generator function to compute each element.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5223
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5224
     * <p>If the generator function throws an exception, it is relayed to
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5225
     * the caller and the array is left in an indeterminate state.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5226
     *
29223
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5227
     * @apiNote
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5228
     * 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
  5229
     * each element, can be written as follows:
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5230
     * <pre>{@code
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5231
     * IntStream.range(startInclusive, endExclusive)
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5232
     *          .forEach(i -> array[i] = generator.apply(i));
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5233
     * }</pre>
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5234
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5235
     * @param <T> type of elements of the array
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5236
     * @param array array to be initialized
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5237
     * @param generator a function accepting an index and producing the desired
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5238
     *        value for that position
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5239
     * @throws NullPointerException if the generator is null
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5240
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5241
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5242
    public static <T> void setAll(T[] array, IntFunction<? extends T> generator) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5243
        Objects.requireNonNull(generator);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5244
        for (int i = 0; i < array.length; i++)
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5245
            array[i] = generator.apply(i);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5246
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5247
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5248
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5249
     * Set all elements of the specified array, in parallel, using the
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5250
     * provided generator function to compute each element.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5251
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5252
     * <p>If the generator function throws an exception, an unchecked exception
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5253
     * is thrown from {@code parallelSetAll} and the array is left in an
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5254
     * indeterminate state.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5255
     *
29223
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5256
     * @apiNote
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5257
     * 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
  5258
     * to compute each element, can be written as follows:
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5259
     * <pre>{@code
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5260
     * IntStream.range(startInclusive, endExclusive)
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5261
     *          .parallel()
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5262
     *          .forEach(i -> array[i] = generator.apply(i));
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5263
     * }</pre>
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5264
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5265
     * @param <T> type of elements of the array
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5266
     * @param array array to be initialized
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5267
     * @param generator a function accepting an index and producing the desired
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5268
     *        value for that position
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5269
     * @throws NullPointerException if the generator is null
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5270
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5271
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5272
    public static <T> void parallelSetAll(T[] array, IntFunction<? extends T> generator) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5273
        Objects.requireNonNull(generator);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5274
        IntStream.range(0, array.length).parallel().forEach(i -> { array[i] = generator.apply(i); });
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5275
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5276
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5277
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5278
     * Set all elements of the specified array, using the provided
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5279
     * generator function to compute each element.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5280
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5281
     * <p>If the generator function throws an exception, it is relayed to
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5282
     * the caller and the array is left in an indeterminate state.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5283
     *
29223
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5284
     * @apiNote
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5285
     * 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
  5286
     * each element, can be written as follows:
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5287
     * <pre>{@code
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5288
     * IntStream.range(startInclusive, endExclusive)
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5289
     *          .forEach(i -> array[i] = generator.applyAsInt(i));
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5290
     * }</pre>
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5291
     *
17195
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 void setAll(int[] array, IntUnaryOperator 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
        for (int i = 0; i < array.length; i++)
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5301
            array[i] = generator.applyAsInt(i);
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
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5305
     * Set all elements of the specified array, in parallel, using the
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5306
     * provided generator function to compute each element.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5307
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5308
     * <p>If the generator function throws an exception, an unchecked exception
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5309
     * is thrown from {@code parallelSetAll} and the array is left in an
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5310
     * indeterminate state.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5311
     *
29223
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5312
     * @apiNote
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5313
     * 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
  5314
     * to compute each element, can be written as follows:
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5315
     * <pre>{@code
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5316
     * IntStream.range(startInclusive, endExclusive)
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5317
     *          .parallel()
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5318
     *          .forEach(i -> array[i] = generator.applyAsInt(i));
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5319
     * }</pre>
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5320
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5321
     * @param array array to be initialized
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5322
     * @param generator a function accepting an index and producing the desired
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5323
     * value for that position
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5324
     * @throws NullPointerException if the generator is null
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5325
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5326
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5327
    public static void parallelSetAll(int[] array, IntUnaryOperator generator) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5328
        Objects.requireNonNull(generator);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5329
        IntStream.range(0, array.length).parallel().forEach(i -> { array[i] = generator.applyAsInt(i); });
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5330
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5331
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5332
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5333
     * Set all elements of the specified array, using the provided
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5334
     * generator function to compute each element.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5335
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5336
     * <p>If the generator function throws an exception, it is relayed to
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5337
     * the caller and the array is left in an indeterminate state.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5338
     *
29223
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5339
     * @apiNote
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5340
     * 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
  5341
     * each element, can be written as follows:
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5342
     * <pre>{@code
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5343
     * IntStream.range(startInclusive, endExclusive)
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5344
     *          .forEach(i -> array[i] = generator.applyAsLong(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 setAll(long[] array, IntToLongFunction 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
        for (int i = 0; i < array.length; i++)
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5356
            array[i] = generator.applyAsLong(i);
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
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5360
     * Set all elements of the specified array, in parallel, using the
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5361
     * provided generator function to compute each element.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5362
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5363
     * <p>If the generator function throws an exception, an unchecked exception
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5364
     * is thrown from {@code parallelSetAll} and the array is left in an
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5365
     * indeterminate state.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5366
     *
29223
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5367
     * @apiNote
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5368
     * 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
  5369
     * to compute each element, can be written as follows:
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5370
     * <pre>{@code
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5371
     * IntStream.range(startInclusive, endExclusive)
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5372
     *          .parallel()
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5373
     *          .forEach(i -> array[i] = generator.applyAsLong(i));
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5374
     * }</pre>
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5375
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5376
     * @param array array to be initialized
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5377
     * @param generator a function accepting an index and producing the desired
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5378
     *        value for that position
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5379
     * @throws NullPointerException if the generator is null
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5380
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5381
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5382
    public static void parallelSetAll(long[] array, IntToLongFunction generator) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5383
        Objects.requireNonNull(generator);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5384
        IntStream.range(0, array.length).parallel().forEach(i -> { array[i] = generator.applyAsLong(i); });
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5385
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5386
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5387
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5388
     * Set all elements of the specified array, using the provided
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5389
     * generator function to compute each element.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5390
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5391
     * <p>If the generator function throws an exception, it is relayed to
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5392
     * the caller and the array is left in an indeterminate state.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5393
     *
29223
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5394
     * @apiNote
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5395
     * 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
  5396
     * each element, can be written as follows:
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5397
     * <pre>{@code
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5398
     * IntStream.range(startInclusive, endExclusive)
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5399
     *          .forEach(i -> array[i] = generator.applyAsDouble(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 setAll(double[] array, IntToDoubleFunction 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
        for (int i = 0; i < array.length; i++)
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5411
            array[i] = generator.applyAsDouble(i);
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
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5415
     * Set all elements of the specified array, in parallel, using the
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5416
     * provided generator function to compute each element.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5417
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5418
     * <p>If the generator function throws an exception, an unchecked exception
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5419
     * is thrown from {@code parallelSetAll} and the array is left in an
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5420
     * indeterminate state.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5421
     *
29223
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5422
     * @apiNote
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5423
     * 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
  5424
     * to compute each element, can be written as follows:
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5425
     * <pre>{@code
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5426
     * IntStream.range(startInclusive, endExclusive)
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5427
     *          .parallel()
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5428
     *          .forEach(i -> array[i] = generator.applyAsDouble(i));
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5429
     * }</pre>
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5430
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5431
     * @param array array to be initialized
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5432
     * @param generator a function accepting an index and producing the desired
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5433
     *        value for that position
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5434
     * @throws NullPointerException if the generator is null
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5435
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5436
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5437
    public static void parallelSetAll(double[] array, IntToDoubleFunction generator) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5438
        Objects.requireNonNull(generator);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5439
        IntStream.range(0, array.length).parallel().forEach(i -> { array[i] = generator.applyAsDouble(i); });
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5440
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5441
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5442
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5443
     * Returns a {@link Spliterator} covering all of the specified array.
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5444
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5445
     * <p>The spliterator reports {@link Spliterator#SIZED},
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5446
     * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5447
     * {@link Spliterator#IMMUTABLE}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5448
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5449
     * @param <T> type of elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5450
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5451
     * @return a spliterator for the array elements
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5452
     * @since 1.8
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5453
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5454
    public static <T> Spliterator<T> spliterator(T[] array) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5455
        return Spliterators.spliterator(array,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5456
                                        Spliterator.ORDERED | Spliterator.IMMUTABLE);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5457
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5458
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5459
    /**
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5460
     * Returns a {@link Spliterator} covering the specified range of the
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5461
     * specified array.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5462
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5463
     * <p>The spliterator reports {@link Spliterator#SIZED},
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5464
     * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5465
     * {@link Spliterator#IMMUTABLE}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5466
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5467
     * @param <T> type of elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5468
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5469
     * @param startInclusive the first index to cover, inclusive
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5470
     * @param endExclusive index immediately past the last index to cover
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5471
     * @return a spliterator for the array elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5472
     * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5473
     *         negative, {@code endExclusive} is less than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5474
     *         {@code startInclusive}, or {@code endExclusive} is greater than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5475
     *         the array size
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5476
     * @since 1.8
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5477
     */
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5478
    public static <T> Spliterator<T> spliterator(T[] array, int startInclusive, int endExclusive) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5479
        return Spliterators.spliterator(array, startInclusive, endExclusive,
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5480
                                        Spliterator.ORDERED | Spliterator.IMMUTABLE);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5481
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5482
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5483
    /**
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5484
     * Returns a {@link Spliterator.OfInt} covering all of the specified array.
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5485
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5486
     * <p>The spliterator reports {@link Spliterator#SIZED},
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5487
     * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5488
     * {@link Spliterator#IMMUTABLE}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5489
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5490
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5491
     * @return a spliterator for the array elements
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5492
     * @since 1.8
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5493
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5494
    public static Spliterator.OfInt spliterator(int[] array) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5495
        return Spliterators.spliterator(array,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5496
                                        Spliterator.ORDERED | Spliterator.IMMUTABLE);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5497
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5498
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5499
    /**
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5500
     * Returns a {@link Spliterator.OfInt} covering the specified range of the
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5501
     * specified array.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5502
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5503
     * <p>The spliterator reports {@link Spliterator#SIZED},
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5504
     * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5505
     * {@link Spliterator#IMMUTABLE}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5506
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5507
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5508
     * @param startInclusive the first index to cover, inclusive
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5509
     * @param endExclusive index immediately past the last index to cover
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5510
     * @return a spliterator for the array elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5511
     * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5512
     *         negative, {@code endExclusive} is less than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5513
     *         {@code startInclusive}, or {@code endExclusive} is greater than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5514
     *         the array size
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5515
     * @since 1.8
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5516
     */
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5517
    public static Spliterator.OfInt spliterator(int[] array, int startInclusive, int endExclusive) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5518
        return Spliterators.spliterator(array, startInclusive, endExclusive,
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5519
                                        Spliterator.ORDERED | Spliterator.IMMUTABLE);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5520
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5521
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5522
    /**
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5523
     * Returns a {@link Spliterator.OfLong} covering all of the specified array.
16929
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
     * <p>The spliterator reports {@link Spliterator#SIZED},
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5526
     * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5527
     * {@link Spliterator#IMMUTABLE}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5528
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5529
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5530
     * @return the spliterator for the array elements
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5531
     * @since 1.8
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5532
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5533
    public static Spliterator.OfLong spliterator(long[] array) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5534
        return Spliterators.spliterator(array,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5535
                                        Spliterator.ORDERED | Spliterator.IMMUTABLE);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5536
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5537
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5538
    /**
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5539
     * Returns a {@link Spliterator.OfLong} covering the specified range of the
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5540
     * specified array.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5541
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5542
     * <p>The spliterator reports {@link Spliterator#SIZED},
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5543
     * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5544
     * {@link Spliterator#IMMUTABLE}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5545
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5546
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5547
     * @param startInclusive the first index to cover, inclusive
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5548
     * @param endExclusive index immediately past the last index to cover
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5549
     * @return a spliterator for the array elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5550
     * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5551
     *         negative, {@code endExclusive} is less than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5552
     *         {@code startInclusive}, or {@code endExclusive} is greater than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5553
     *         the array size
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5554
     * @since 1.8
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5555
     */
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5556
    public static Spliterator.OfLong spliterator(long[] array, int startInclusive, int endExclusive) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5557
        return Spliterators.spliterator(array, startInclusive, endExclusive,
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5558
                                        Spliterator.ORDERED | Spliterator.IMMUTABLE);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5559
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5560
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5561
    /**
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5562
     * Returns a {@link Spliterator.OfDouble} covering all of the specified
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5563
     * array.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5564
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5565
     * <p>The spliterator reports {@link Spliterator#SIZED},
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5566
     * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5567
     * {@link Spliterator#IMMUTABLE}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5568
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5569
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5570
     * @return a spliterator for the array elements
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5571
     * @since 1.8
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5572
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5573
    public static Spliterator.OfDouble spliterator(double[] array) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5574
        return Spliterators.spliterator(array,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5575
                                        Spliterator.ORDERED | Spliterator.IMMUTABLE);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5576
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5577
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5578
    /**
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5579
     * Returns a {@link Spliterator.OfDouble} covering the specified range of
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5580
     * the specified array.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5581
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5582
     * <p>The spliterator reports {@link Spliterator#SIZED},
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5583
     * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5584
     * {@link Spliterator#IMMUTABLE}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5585
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5586
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5587
     * @param startInclusive the first index to cover, inclusive
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5588
     * @param endExclusive index immediately past the last index to cover
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5589
     * @return a spliterator for the array elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5590
     * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5591
     *         negative, {@code endExclusive} is less than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5592
     *         {@code startInclusive}, or {@code endExclusive} is greater than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5593
     *         the array size
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5594
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5595
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5596
    public static Spliterator.OfDouble spliterator(double[] array, int startInclusive, int endExclusive) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5597
        return Spliterators.spliterator(array, startInclusive, endExclusive,
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5598
                                        Spliterator.ORDERED | Spliterator.IMMUTABLE);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5599
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5600
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5601
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5602
     * Returns a sequential {@link Stream} with the specified array as its
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5603
     * source.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5604
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5605
     * @param <T> The type of the array elements
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5606
     * @param array The array, assumed to be unmodified during use
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5607
     * @return a {@code Stream} for the array
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5608
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5609
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5610
    public static <T> Stream<T> stream(T[] array) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5611
        return stream(array, 0, array.length);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5612
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5613
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5614
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5615
     * Returns a sequential {@link Stream} with the specified range of the
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5616
     * specified array as its source.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5617
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5618
     * @param <T> the type of the array elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5619
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5620
     * @param startInclusive the first index to cover, inclusive
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5621
     * @param endExclusive index immediately past the last index to cover
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5622
     * @return a {@code Stream} for the array range
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5623
     * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5624
     *         negative, {@code endExclusive} is less than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5625
     *         {@code startInclusive}, or {@code endExclusive} is greater than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5626
     *         the array size
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5627
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5628
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5629
    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
  5630
        return StreamSupport.stream(spliterator(array, startInclusive, endExclusive), false);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5631
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5632
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5633
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5634
     * Returns a sequential {@link IntStream} with the specified array as its
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5635
     * source.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5636
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5637
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5638
     * @return an {@code IntStream} for the array
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5639
     * @since 1.8
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5640
     */
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5641
    public static IntStream stream(int[] array) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5642
        return stream(array, 0, array.length);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5643
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5644
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5645
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5646
     * Returns a sequential {@link IntStream} with the specified range of the
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5647
     * specified array as its source.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5648
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5649
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5650
     * @param startInclusive the first index to cover, inclusive
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5651
     * @param endExclusive index immediately past the last index to cover
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5652
     * @return an {@code IntStream} for the array range
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5653
     * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5654
     *         negative, {@code endExclusive} is less than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5655
     *         {@code startInclusive}, or {@code endExclusive} is greater than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5656
     *         the array size
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5657
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5658
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5659
    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
  5660
        return StreamSupport.intStream(spliterator(array, startInclusive, endExclusive), false);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5661
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5662
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5663
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5664
     * Returns a sequential {@link LongStream} with the specified array as its
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5665
     * source.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5666
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5667
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5668
     * @return a {@code LongStream} for the array
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5669
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5670
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5671
    public static LongStream stream(long[] array) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5672
        return stream(array, 0, array.length);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5673
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5674
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5675
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5676
     * Returns a sequential {@link LongStream} with the specified range of the
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5677
     * specified array as its source.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5678
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5679
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5680
     * @param startInclusive the first index to cover, inclusive
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5681
     * @param endExclusive index immediately past the last index to cover
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5682
     * @return a {@code LongStream} for the array range
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5683
     * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5684
     *         negative, {@code endExclusive} is less than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5685
     *         {@code startInclusive}, or {@code endExclusive} is greater than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5686
     *         the array size
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5687
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5688
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5689
    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
  5690
        return StreamSupport.longStream(spliterator(array, startInclusive, endExclusive), false);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5691
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5692
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5693
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5694
     * Returns a sequential {@link DoubleStream} with the specified array as its
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5695
     * source.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5696
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5697
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5698
     * @return a {@code DoubleStream} for the array
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5699
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5700
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5701
    public static DoubleStream stream(double[] array) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5702
        return stream(array, 0, array.length);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5703
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5704
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5705
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5706
     * Returns a sequential {@link DoubleStream} with the specified range of the
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5707
     * specified array as its source.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5708
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5709
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5710
     * @param startInclusive the first index to cover, inclusive
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5711
     * @param endExclusive index immediately past the last index to cover
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5712
     * @return a {@code DoubleStream} for the array range
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5713
     * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5714
     *         negative, {@code endExclusive} is less than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5715
     *         {@code startInclusive}, or {@code endExclusive} is greater than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5716
     *         the array size
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5717
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5718
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5719
    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
  5720
        return StreamSupport.doubleStream(spliterator(array, startInclusive, endExclusive), false);
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5721
    }
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5722
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5723
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5724
    // Comparison methods
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5725
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5726
    // Compare boolean
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5727
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5728
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5729
     * Compares two {@code boolean} arrays lexicographically.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5730
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5731
     * <p>If the two arrays share a common prefix then the lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5732
     * comparison is the result of comparing two elements, as if by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5733
     * {@link Boolean#compare(boolean, boolean)}, at an index within the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5734
     * respective arrays that is the prefix length.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5735
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5736
     * comparison is the result of comparing the two array lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5737
     * (See {@link #mismatch(boolean[], boolean[])} for the definition of a
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5738
     * common and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5739
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5740
     * <p>A {@code null} array reference is considered lexicographically less
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5741
     * than a non-{@code null} array reference.  Two {@code null} array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5742
     * references are considered equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5743
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5744
     * <p>The comparison is consistent with {@link #equals(boolean[], boolean[]) equals},
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5745
     * more specifically the following holds for arrays {@code a} and {@code b}:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5746
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5747
     *     Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5748
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5749
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5750
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5751
     * <p>This method behaves as if (for non-{@code null} array references):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5752
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5753
     *     int i = Arrays.mismatch(a, b);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5754
     *     if (i >= 0 && i < Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5755
     *         return Boolean.compare(a[i], b[i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5756
     *     return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5757
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5758
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5759
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5760
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5761
     * @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
  5762
     *         contain the same elements in the same order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5763
     *         a value less than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5764
     *         lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5765
     *         a value greater than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5766
     *         lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5767
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5768
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5769
    public static int compare(boolean[] a, boolean[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5770
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5771
            return 0;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5772
        if (a == null || b == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5773
            return a == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5774
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5775
        int i = ArraysSupport.mismatch(a, b,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5776
                                       Math.min(a.length, b.length));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5777
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5778
            return Boolean.compare(a[i], b[i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5779
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5780
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5781
        return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5782
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5783
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5784
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5785
     * Compares two {@code boolean} arrays lexicographically over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5786
     * ranges.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5787
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5788
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5789
     * then the lexicographic comparison is the result of comparing two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5790
     * elements, as if by {@link Boolean#compare(boolean, boolean)}, at a
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5791
     * relative index within the respective arrays that is the length of the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5792
     * prefix.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5793
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5794
     * comparison is the result of comparing the two range lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5795
     * (See {@link #mismatch(boolean[], int, int, boolean[], int, int)} for the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5796
     * definition of a common and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5797
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5798
     * <p>The comparison is consistent with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5799
     * {@link #equals(boolean[], int, int, boolean[], int, int) equals}, more
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5800
     * specifically the following holds for arrays {@code a} and {@code b} with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5801
     * specified ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5802
     * [{@code bFromIndex}, {@code btoIndex}) respectively:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5803
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5804
     *     Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5805
     *         (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5806
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5807
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5808
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5809
     * <p>This method behaves as if:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5810
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5811
     *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5812
     *                             b, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5813
     *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5814
     *         return Boolean.compare(a[aFromIndex + i], b[bFromIndex + i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5815
     *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5816
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5817
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5818
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5819
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5820
     *                   first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5821
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5822
     *                 first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5823
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5824
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5825
     *                   second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5826
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5827
     *                 second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5828
     * @return the value {@code 0} if, over the specified ranges, the first and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5829
     *         second array are equal and contain the same elements in the same
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5830
     *         order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5831
     *         a value less than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5832
     *         first array is lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5833
     *         a value greater than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5834
     *         first array is lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5835
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5836
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5837
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5838
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5839
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5840
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5841
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5842
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5843
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5844
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5845
    public static int compare(boolean[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5846
                              boolean[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5847
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5848
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5849
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5850
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5851
        int bLength = bToIndex - bFromIndex;
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5852
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5853
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5854
                                       Math.min(aLength, bLength));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5855
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5856
            return Boolean.compare(a[aFromIndex + i], b[bFromIndex + i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5857
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5858
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5859
        return aLength - bLength;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5860
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5861
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5862
    // Compare byte
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5863
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5864
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5865
     * Compares two {@code byte} arrays lexicographically.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5866
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5867
     * <p>If the two arrays share a common prefix then the lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5868
     * comparison is the result of comparing two elements, as if by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5869
     * {@link Byte#compare(byte, byte)}, at an index within the respective
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5870
     * arrays that is the prefix length.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5871
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5872
     * comparison is the result of comparing the two array lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5873
     * (See {@link #mismatch(byte[], byte[])} for the definition of a common and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5874
     * proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5875
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5876
     * <p>A {@code null} array reference is considered lexicographically less
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5877
     * than a non-{@code null} array reference.  Two {@code null} array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5878
     * references are considered equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5879
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5880
     * <p>The comparison is consistent with {@link #equals(byte[], byte[]) equals},
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5881
     * more specifically the following holds for arrays {@code a} and {@code b}:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5882
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5883
     *     Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5884
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5885
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5886
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5887
     * <p>This method behaves as if (for non-{@code null} array references):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5888
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5889
     *     int i = Arrays.mismatch(a, b);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5890
     *     if (i >= 0 && i < Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5891
     *         return Byte.compare(a[i], b[i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5892
     *     return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5893
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5894
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5895
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5896
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5897
     * @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
  5898
     *         contain the same elements in the same order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5899
     *         a value less than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5900
     *         lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5901
     *         a value greater than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5902
     *         lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5903
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5904
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5905
    public static int compare(byte[] a, byte[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5906
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5907
            return 0;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5908
        if (a == null || b == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5909
            return a == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5910
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5911
        int i = ArraysSupport.mismatch(a, b,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5912
                                       Math.min(a.length, b.length));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5913
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5914
            return Byte.compare(a[i], b[i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5915
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5916
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5917
        return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5918
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5919
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5920
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5921
     * Compares two {@code byte} arrays lexicographically over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5922
     * ranges.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5923
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5924
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5925
     * then the lexicographic comparison is the result of comparing two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5926
     * elements, as if by {@link Byte#compare(byte, byte)}, at a relative index
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5927
     * within the respective arrays that is the length of the prefix.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5928
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5929
     * comparison is the result of comparing the two range lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5930
     * (See {@link #mismatch(byte[], int, int, byte[], int, int)} for the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5931
     * definition of a common and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5932
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5933
     * <p>The comparison is consistent with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5934
     * {@link #equals(byte[], int, int, byte[], int, int) equals}, more
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5935
     * specifically the following holds for arrays {@code a} and {@code b} with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5936
     * specified ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5937
     * [{@code bFromIndex}, {@code btoIndex}) respectively:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5938
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5939
     *     Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5940
     *         (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5941
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5942
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5943
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5944
     * <p>This method behaves as if:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5945
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5946
     *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5947
     *                             b, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5948
     *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5949
     *         return Byte.compare(a[aFromIndex + i], b[bFromIndex + i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5950
     *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5951
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5952
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5953
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5954
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5955
     *                   first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5956
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5957
     *                 first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5958
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5959
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5960
     *                   second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5961
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5962
     *                 second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5963
     * @return the value {@code 0} if, over the specified ranges, the first and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5964
     *         second array are equal and contain the same elements in the same
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5965
     *         order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5966
     *         a value less than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5967
     *         first array is lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5968
     *         a value greater than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5969
     *         first array is lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5970
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5971
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5972
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5973
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5974
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5975
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5976
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5977
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5978
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5979
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5980
    public static int compare(byte[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5981
                              byte[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5982
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5983
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5984
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5985
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5986
        int bLength = bToIndex - bFromIndex;
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5987
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5988
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5989
                                       Math.min(aLength, bLength));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5990
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5991
            return Byte.compare(a[aFromIndex + i], b[bFromIndex + i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5992
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5993
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5994
        return aLength - bLength;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5995
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5996
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5997
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5998
     * Compares two {@code byte} arrays lexicographically, numerically treating
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5999
     * elements as unsigned.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6000
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6001
     * <p>If the two arrays share a common prefix then the lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6002
     * comparison is the result of comparing two elements, as if by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6003
     * {@link Byte#compareUnsigned(byte, byte)}, at an index within the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6004
     * respective arrays that is the prefix length.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6005
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6006
     * comparison is the result of comparing the two array lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6007
     * (See {@link #mismatch(byte[], byte[])} for the definition of a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6008
     * and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6009
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6010
     * <p>A {@code null} array reference is considered lexicographically less
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6011
     * than a non-{@code null} array reference.  Two {@code null} array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6012
     * references are considered equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6013
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6014
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6015
     * <p>This method behaves as if (for non-{@code null} array references):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6016
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6017
     *     int i = Arrays.mismatch(a, b);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6018
     *     if (i >= 0 && i < Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6019
     *         return Byte.compareUnsigned(a[i], b[i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6020
     *     return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6021
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6022
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6023
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6024
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6025
     * @return the value {@code 0} if the first and second array are
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6026
     *         equal and contain the same elements in the same order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6027
     *         a value less than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6028
     *         lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6029
     *         a value greater than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6030
     *         lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6031
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6032
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6033
    public static int compareUnsigned(byte[] a, byte[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6034
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6035
            return 0;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6036
        if (a == null || b == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6037
            return a == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6038
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6039
        int i = ArraysSupport.mismatch(a, b,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6040
                                       Math.min(a.length, b.length));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6041
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6042
            return Byte.compareUnsigned(a[i], b[i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6043
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6044
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6045
        return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6046
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6047
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6048
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6049
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6050
     * Compares two {@code byte} arrays lexicographically over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6051
     * ranges, numerically treating elements as unsigned.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6052
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6053
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6054
     * then the lexicographic comparison is the result of comparing two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6055
     * elements, as if by {@link Byte#compareUnsigned(byte, byte)}, at a
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6056
     * relative index within the respective arrays that is the length of the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6057
     * prefix.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6058
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6059
     * comparison is the result of comparing the two range lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6060
     * (See {@link #mismatch(byte[], int, int, byte[], int, int)} for the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6061
     * definition of a common and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6062
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6063
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6064
     * <p>This method behaves as if:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6065
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6066
     *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6067
     *                             b, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6068
     *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6069
     *         return Byte.compareUnsigned(a[aFromIndex + i], b[bFromIndex + i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6070
     *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6071
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6072
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6073
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6074
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6075
     *                   first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6076
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6077
     *                 first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6078
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6079
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6080
     *                   second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6081
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6082
     *                 second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6083
     * @return the value {@code 0} if, over the specified ranges, the first and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6084
     *         second array are equal and contain the same elements in the same
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6085
     *         order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6086
     *         a value less than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6087
     *         first array is lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6088
     *         a value greater than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6089
     *         first array is lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6090
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6091
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6092
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6093
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6094
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6095
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6096
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6097
     *         if either array is null
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6098
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6099
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6100
    public static int compareUnsigned(byte[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6101
                                      byte[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6102
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6103
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6104
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6105
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6106
        int bLength = bToIndex - bFromIndex;
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6107
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6108
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6109
                                       Math.min(aLength, bLength));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6110
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6111
            return Byte.compareUnsigned(a[aFromIndex + i], b[bFromIndex + i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6112
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6113
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6114
        return aLength - bLength;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6115
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6116
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6117
    // Compare short
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6118
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6119
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6120
     * Compares two {@code short} arrays lexicographically.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6121
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6122
     * <p>If the two arrays share a common prefix then the lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6123
     * comparison is the result of comparing two elements, as if by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6124
     * {@link Short#compare(short, short)}, at an index within the respective
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6125
     * arrays that is the prefix length.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6126
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6127
     * comparison is the result of comparing the two array lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6128
     * (See {@link #mismatch(short[], short[])} for the definition of a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6129
     * and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6130
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6131
     * <p>A {@code null} array reference is considered lexicographically less
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6132
     * than a non-{@code null} array reference.  Two {@code null} array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6133
     * references are considered equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6134
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6135
     * <p>The comparison is consistent with {@link #equals(short[], short[]) equals},
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6136
     * more specifically the following holds for arrays {@code a} and {@code b}:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6137
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6138
     *     Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6139
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6140
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6141
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6142
     * <p>This method behaves as if (for non-{@code null} array references):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6143
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6144
     *     int i = Arrays.mismatch(a, b);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6145
     *     if (i >= 0 && i < Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6146
     *         return Short.compare(a[i], b[i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6147
     *     return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6148
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6149
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6150
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6151
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6152
     * @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
  6153
     *         contain the same elements in the same order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6154
     *         a value less than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6155
     *         lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6156
     *         a value greater than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6157
     *         lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6158
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6159
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6160
    public static int compare(short[] a, short[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6161
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6162
            return 0;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6163
        if (a == null || b == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6164
            return a == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6165
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6166
        int i = ArraysSupport.mismatch(a, b,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6167
                                       Math.min(a.length, b.length));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6168
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6169
            return Short.compare(a[i], b[i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6170
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6171
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6172
        return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6173
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6174
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6175
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6176
     * Compares two {@code short} arrays lexicographically over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6177
     * ranges.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6178
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6179
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6180
     * then the lexicographic comparison is the result of comparing two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6181
     * elements, as if by {@link Short#compare(short, short)}, at a relative
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6182
     * index within the respective arrays that is the length of the prefix.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6183
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6184
     * comparison is the result of comparing the two range lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6185
     * (See {@link #mismatch(short[], int, int, short[], int, int)} for the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6186
     * definition of a common and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6187
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6188
     * <p>The comparison is consistent with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6189
     * {@link #equals(short[], int, int, short[], int, int) equals}, more
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6190
     * specifically the following holds for arrays {@code a} and {@code b} with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6191
     * specified ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6192
     * [{@code bFromIndex}, {@code btoIndex}) respectively:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6193
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6194
     *     Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6195
     *         (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6196
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6197
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6198
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6199
     * <p>This method behaves as if:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6200
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6201
     *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6202
     *                             b, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6203
     *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6204
     *         return Short.compare(a[aFromIndex + i], b[bFromIndex + i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6205
     *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6206
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6207
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6208
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6209
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6210
     *                   first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6211
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6212
     *                 first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6213
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6214
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6215
     *                   second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6216
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6217
     *                 second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6218
     * @return the value {@code 0} if, over the specified ranges, the first and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6219
     *         second array are equal and contain the same elements in the same
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6220
     *         order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6221
     *         a value less than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6222
     *         first array is lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6223
     *         a value greater than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6224
     *         first array is lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6225
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6226
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6227
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6228
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6229
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6230
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6231
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6232
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6233
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6234
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6235
    public static int compare(short[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6236
                              short[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6237
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6238
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6239
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6240
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6241
        int bLength = bToIndex - bFromIndex;
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6242
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6243
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6244
                                       Math.min(aLength, bLength));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6245
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6246
            return Short.compare(a[aFromIndex + i], b[bFromIndex + i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6247
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6248
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6249
        return aLength - bLength;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6250
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6251
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6252
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6253
     * Compares two {@code short} arrays lexicographically, numerically treating
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6254
     * elements as unsigned.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6255
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6256
     * <p>If the two arrays share a common prefix then the lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6257
     * comparison is the result of comparing two elements, as if by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6258
     * {@link Short#compareUnsigned(short, short)}, at an index within the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6259
     * respective arrays that is the prefix length.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6260
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6261
     * comparison is the result of comparing the two array lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6262
     * (See {@link #mismatch(short[], short[])} for the definition of a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6263
     * and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6264
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6265
     * <p>A {@code null} array reference is considered lexicographically less
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6266
     * than a non-{@code null} array reference.  Two {@code null} array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6267
     * references are considered equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6268
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6269
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6270
     * <p>This method behaves as if (for non-{@code null} array references):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6271
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6272
     *     int i = Arrays.mismatch(a, b);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6273
     *     if (i >= 0 && i < Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6274
     *         return Short.compareUnsigned(a[i], b[i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6275
     *     return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6276
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6277
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6278
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6279
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6280
     * @return the value {@code 0} if the first and second array are
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6281
     *         equal and contain the same elements in the same order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6282
     *         a value less than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6283
     *         lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6284
     *         a value greater than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6285
     *         lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6286
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6287
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6288
    public static int compareUnsigned(short[] a, short[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6289
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6290
            return 0;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6291
        if (a == null || b == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6292
            return a == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6293
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6294
        int i = ArraysSupport.mismatch(a, b,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6295
                                       Math.min(a.length, b.length));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6296
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6297
            return Short.compareUnsigned(a[i], b[i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6298
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6299
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6300
        return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6301
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6302
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6303
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6304
     * Compares two {@code short} arrays lexicographically over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6305
     * ranges, numerically treating elements as unsigned.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6306
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6307
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6308
     * then the lexicographic comparison is the result of comparing two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6309
     * elements, as if by {@link Short#compareUnsigned(short, short)}, at a
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6310
     * relative index within the respective arrays that is the length of the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6311
     * prefix.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6312
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6313
     * comparison is the result of comparing the two range lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6314
     * (See {@link #mismatch(short[], int, int, short[], int, int)} for the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6315
     * definition of a common and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6316
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6317
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6318
     * <p>This method behaves as if:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6319
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6320
     *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6321
     *                             b, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6322
     *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6323
     *         return Short.compareUnsigned(a[aFromIndex + i], b[bFromIndex + i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6324
     *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6325
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6326
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6327
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6328
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6329
     *                   first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6330
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6331
     *                 first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6332
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6333
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6334
     *                   second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6335
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6336
     *                 second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6337
     * @return the value {@code 0} if, over the specified ranges, the first and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6338
     *         second array are equal and contain the same elements in the same
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6339
     *         order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6340
     *         a value less than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6341
     *         first array is lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6342
     *         a value greater than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6343
     *         first array is lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6344
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6345
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6346
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6347
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6348
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6349
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6350
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6351
     *         if either array is null
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6352
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6353
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6354
    public static int compareUnsigned(short[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6355
                                      short[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6356
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6357
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6358
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6359
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6360
        int bLength = bToIndex - bFromIndex;
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6361
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6362
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6363
                                       Math.min(aLength, bLength));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6364
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6365
            return Short.compareUnsigned(a[aFromIndex + i], b[bFromIndex + i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6366
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6367
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6368
        return aLength - bLength;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6369
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6370
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6371
    // Compare char
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6372
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6373
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6374
     * Compares two {@code char} arrays lexicographically.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6375
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6376
     * <p>If the two arrays share a common prefix then the lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6377
     * comparison is the result of comparing two elements, as if by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6378
     * {@link Character#compare(char, char)}, at an index within the respective
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6379
     * arrays that is the prefix length.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6380
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6381
     * comparison is the result of comparing the two array lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6382
     * (See {@link #mismatch(char[], char[])} for the definition of a common and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6383
     * proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6384
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6385
     * <p>A {@code null} array reference is considered lexicographically less
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6386
     * than a non-{@code null} array reference.  Two {@code null} array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6387
     * references are considered equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6388
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6389
     * <p>The comparison is consistent with {@link #equals(char[], char[]) equals},
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6390
     * more specifically the following holds for arrays {@code a} and {@code b}:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6391
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6392
     *     Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6393
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6394
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6395
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6396
     * <p>This method behaves as if (for non-{@code null} array references):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6397
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6398
     *     int i = Arrays.mismatch(a, b);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6399
     *     if (i >= 0 && i < Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6400
     *         return Character.compare(a[i], b[i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6401
     *     return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6402
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6403
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6404
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6405
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6406
     * @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
  6407
     *         contain the same elements in the same order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6408
     *         a value less than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6409
     *         lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6410
     *         a value greater than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6411
     *         lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6412
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6413
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6414
    public static int compare(char[] a, char[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6415
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6416
            return 0;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6417
        if (a == null || b == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6418
            return a == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6419
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6420
        int i = ArraysSupport.mismatch(a, b,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6421
                                       Math.min(a.length, b.length));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6422
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6423
            return Character.compare(a[i], b[i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6424
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6425
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6426
        return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6427
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6428
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6429
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6430
     * Compares two {@code char} arrays lexicographically over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6431
     * ranges.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6432
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6433
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6434
     * then the lexicographic comparison is the result of comparing two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6435
     * elements, as if by {@link Character#compare(char, char)}, at a relative
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6436
     * index within the respective arrays that is the length of the prefix.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6437
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6438
     * comparison is the result of comparing the two range lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6439
     * (See {@link #mismatch(char[], int, int, char[], int, int)} for the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6440
     * definition of a common and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6441
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6442
     * <p>The comparison is consistent with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6443
     * {@link #equals(char[], int, int, char[], int, int) equals}, more
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6444
     * specifically the following holds for arrays {@code a} and {@code b} with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6445
     * specified ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6446
     * [{@code bFromIndex}, {@code btoIndex}) respectively:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6447
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6448
     *     Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6449
     *         (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6450
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6451
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6452
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6453
     * <p>This method behaves as if:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6454
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6455
     *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6456
     *                             b, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6457
     *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6458
     *         return Character.compare(a[aFromIndex + i], b[bFromIndex + i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6459
     *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6460
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6461
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6462
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6463
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6464
     *                   first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6465
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6466
     *                 first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6467
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6468
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6469
     *                   second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6470
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6471
     *                 second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6472
     * @return the value {@code 0} if, over the specified ranges, the first and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6473
     *         second array are equal and contain the same elements in the same
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6474
     *         order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6475
     *         a value less than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6476
     *         first array is lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6477
     *         a value greater than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6478
     *         first array is lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6479
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6480
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6481
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6482
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6483
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6484
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6485
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6486
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6487
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6488
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6489
    public static int compare(char[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6490
                              char[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6491
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6492
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6493
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6494
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6495
        int bLength = bToIndex - bFromIndex;
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6496
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6497
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6498
                                       Math.min(aLength, bLength));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6499
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6500
            return Character.compare(a[aFromIndex + i], b[bFromIndex + i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6501
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6502
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6503
        return aLength - bLength;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6504
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6505
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6506
    // Compare int
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6507
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6508
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6509
     * Compares two {@code int} arrays lexicographically.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6510
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6511
     * <p>If the two arrays share a common prefix then the lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6512
     * comparison is the result of comparing two elements, as if by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6513
     * {@link Integer#compare(int, int)}, at an index within the respective
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6514
     * arrays that is the prefix length.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6515
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6516
     * comparison is the result of comparing the two array lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6517
     * (See {@link #mismatch(int[], int[])} for the definition of a common and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6518
     * proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6519
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6520
     * <p>A {@code null} array reference is considered lexicographically less
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6521
     * than a non-{@code null} array reference.  Two {@code null} array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6522
     * references are considered equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6523
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6524
     * <p>The comparison is consistent with {@link #equals(int[], int[]) equals},
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6525
     * more specifically the following holds for arrays {@code a} and {@code b}:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6526
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6527
     *     Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6528
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6529
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6530
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6531
     * <p>This method behaves as if (for non-{@code null} array references):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6532
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6533
     *     int i = Arrays.mismatch(a, b);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6534
     *     if (i >= 0 && i < Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6535
     *         return Integer.compare(a[i], b[i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6536
     *     return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6537
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6538
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6539
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6540
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6541
     * @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
  6542
     *         contain the same elements in the same order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6543
     *         a value less than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6544
     *         lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6545
     *         a value greater than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6546
     *         lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6547
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6548
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6549
    public static int compare(int[] a, int[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6550
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6551
            return 0;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6552
        if (a == null || b == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6553
            return a == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6554
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6555
        int i = ArraysSupport.mismatch(a, b,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6556
                                       Math.min(a.length, b.length));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6557
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6558
            return Integer.compare(a[i], b[i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6559
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6560
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6561
        return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6562
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6563
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6564
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6565
     * Compares two {@code int} arrays lexicographically over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6566
     * ranges.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6567
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6568
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6569
     * then the lexicographic comparison is the result of comparing two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6570
     * elements, as if by {@link Integer#compare(int, int)}, at a relative index
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6571
     * within the respective arrays that is the length of the prefix.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6572
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6573
     * comparison is the result of comparing the two range lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6574
     * (See {@link #mismatch(int[], int, int, int[], int, int)} for the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6575
     * definition of a common and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6576
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6577
     * <p>The comparison is consistent with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6578
     * {@link #equals(int[], int, int, int[], int, int) equals}, more
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6579
     * specifically the following holds for arrays {@code a} and {@code b} with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6580
     * specified ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6581
     * [{@code bFromIndex}, {@code btoIndex}) respectively:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6582
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6583
     *     Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6584
     *         (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6585
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6586
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6587
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6588
     * <p>This method behaves as if:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6589
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6590
     *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6591
     *                             b, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6592
     *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6593
     *         return Integer.compare(a[aFromIndex + i], b[bFromIndex + i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6594
     *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6595
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6596
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6597
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6598
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6599
     *                   first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6600
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6601
     *                 first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6602
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6603
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6604
     *                   second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6605
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6606
     *                 second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6607
     * @return the value {@code 0} if, over the specified ranges, the first and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6608
     *         second array are equal and contain the same elements in the same
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6609
     *         order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6610
     *         a value less than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6611
     *         first array is lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6612
     *         a value greater than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6613
     *         first array is lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6614
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6615
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6616
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6617
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6618
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6619
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6620
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6621
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6622
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6623
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6624
    public static int compare(int[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6625
                              int[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6626
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6627
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6628
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6629
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6630
        int bLength = bToIndex - bFromIndex;
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6631
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6632
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6633
                                       Math.min(aLength, bLength));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6634
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6635
            return Integer.compare(a[aFromIndex + i], b[bFromIndex + i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6636
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6637
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6638
        return aLength - bLength;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6639
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6640
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6641
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6642
     * Compares two {@code int} arrays lexicographically, numerically treating
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6643
     * elements as unsigned.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6644
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6645
     * <p>If the two arrays share a common prefix then the lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6646
     * comparison is the result of comparing two elements, as if by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6647
     * {@link Integer#compareUnsigned(int, int)}, at an index within the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6648
     * respective arrays that is the prefix length.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6649
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6650
     * comparison is the result of comparing the two array lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6651
     * (See {@link #mismatch(int[], int[])} for the definition of a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6652
     * and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6653
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6654
     * <p>A {@code null} array reference is considered lexicographically less
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6655
     * than a non-{@code null} array reference.  Two {@code null} array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6656
     * references are considered equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6657
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6658
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6659
     * <p>This method behaves as if (for non-{@code null} array references):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6660
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6661
     *     int i = Arrays.mismatch(a, b);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6662
     *     if (i >= 0 && i < Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6663
     *         return Integer.compareUnsigned(a[i], b[i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6664
     *     return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6665
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6666
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6667
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6668
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6669
     * @return the value {@code 0} if the first and second array are
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6670
     *         equal and contain the same elements in the same order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6671
     *         a value less than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6672
     *         lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6673
     *         a value greater than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6674
     *         lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6675
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6676
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6677
    public static int compareUnsigned(int[] a, int[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6678
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6679
            return 0;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6680
        if (a == null || b == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6681
            return a == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6682
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6683
        int i = ArraysSupport.mismatch(a, b,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6684
                                       Math.min(a.length, b.length));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6685
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6686
            return Integer.compareUnsigned(a[i], b[i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6687
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6688
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6689
        return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6690
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6691
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6692
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6693
     * Compares two {@code int} arrays lexicographically over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6694
     * ranges, numerically treating elements as unsigned.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6695
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6696
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6697
     * then the lexicographic comparison is the result of comparing two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6698
     * elements, as if by {@link Integer#compareUnsigned(int, int)}, at a
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6699
     * relative index within the respective arrays that is the length of the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6700
     * prefix.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6701
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6702
     * comparison is the result of comparing the two range lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6703
     * (See {@link #mismatch(int[], int, int, int[], int, int)} for the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6704
     * definition of a common and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6705
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6706
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6707
     * <p>This method behaves as if:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6708
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6709
     *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6710
     *                             b, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6711
     *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6712
     *         return Integer.compareUnsigned(a[aFromIndex + i], b[bFromIndex + i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6713
     *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6714
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6715
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6716
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6717
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6718
     *                   first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6719
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6720
     *                 first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6721
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6722
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6723
     *                   second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6724
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6725
     *                 second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6726
     * @return the value {@code 0} if, over the specified ranges, the first and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6727
     *         second array are equal and contain the same elements in the same
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6728
     *         order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6729
     *         a value less than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6730
     *         first array is lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6731
     *         a value greater than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6732
     *         first array is lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6733
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6734
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6735
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6736
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6737
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6738
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6739
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6740
     *         if either array is null
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6741
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6742
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6743
    public static int compareUnsigned(int[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6744
                                      int[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6745
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6746
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6747
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6748
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6749
        int bLength = bToIndex - bFromIndex;
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6750
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6751
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6752
                                       Math.min(aLength, bLength));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6753
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6754
            return Integer.compareUnsigned(a[aFromIndex + i], b[bFromIndex + i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6755
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6756
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6757
        return aLength - bLength;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6758
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6759
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6760
    // Compare long
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6761
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6762
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6763
     * Compares two {@code long} arrays lexicographically.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6764
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6765
     * <p>If the two arrays share a common prefix then the lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6766
     * comparison is the result of comparing two elements, as if by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6767
     * {@link Long#compare(long, long)}, at an index within the respective
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6768
     * arrays that is the prefix length.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6769
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6770
     * comparison is the result of comparing the two array lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6771
     * (See {@link #mismatch(long[], long[])} for the definition of a common and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6772
     * proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6773
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6774
     * <p>A {@code null} array reference is considered lexicographically less
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6775
     * than a non-{@code null} array reference.  Two {@code null} array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6776
     * references are considered equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6777
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6778
     * <p>The comparison is consistent with {@link #equals(long[], long[]) equals},
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6779
     * more specifically the following holds for arrays {@code a} and {@code b}:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6780
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6781
     *     Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6782
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6783
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6784
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6785
     * <p>This method behaves as if (for non-{@code null} array references):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6786
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6787
     *     int i = Arrays.mismatch(a, b);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6788
     *     if (i >= 0 && i < Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6789
     *         return Long.compare(a[i], b[i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6790
     *     return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6791
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6792
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6793
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6794
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6795
     * @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
  6796
     *         contain the same elements in the same order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6797
     *         a value less than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6798
     *         lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6799
     *         a value greater than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6800
     *         lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6801
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6802
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6803
    public static int compare(long[] a, long[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6804
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6805
            return 0;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6806
        if (a == null || b == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6807
            return a == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6808
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6809
        int i = ArraysSupport.mismatch(a, b,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6810
                                       Math.min(a.length, b.length));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6811
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6812
            return Long.compare(a[i], b[i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6813
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6814
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6815
        return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6816
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6817
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6818
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6819
     * Compares two {@code long} arrays lexicographically over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6820
     * ranges.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6821
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6822
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6823
     * then the lexicographic comparison is the result of comparing two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6824
     * elements, as if by {@link Long#compare(long, long)}, at a relative index
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6825
     * within the respective arrays that is the length of the prefix.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6826
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6827
     * comparison is the result of comparing the two range lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6828
     * (See {@link #mismatch(long[], int, int, long[], int, int)} for the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6829
     * definition of a common and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6830
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6831
     * <p>The comparison is consistent with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6832
     * {@link #equals(long[], int, int, long[], int, int) equals}, more
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6833
     * specifically the following holds for arrays {@code a} and {@code b} with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6834
     * specified ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6835
     * [{@code bFromIndex}, {@code btoIndex}) respectively:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6836
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6837
     *     Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6838
     *         (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6839
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6840
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6841
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6842
     * <p>This method behaves as if:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6843
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6844
     *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6845
     *                             b, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6846
     *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6847
     *         return Long.compare(a[aFromIndex + i], b[bFromIndex + i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6848
     *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6849
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6850
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6851
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6852
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6853
     *                   first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6854
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6855
     *                 first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6856
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6857
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6858
     *                   second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6859
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6860
     *                 second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6861
     * @return the value {@code 0} if, over the specified ranges, the first and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6862
     *         second array are equal and contain the same elements in the same
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6863
     *         order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6864
     *         a value less than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6865
     *         first array is lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6866
     *         a value greater than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6867
     *         first array is lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6868
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6869
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6870
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6871
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6872
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6873
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6874
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6875
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6876
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6877
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6878
    public static int compare(long[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6879
                              long[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6880
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6881
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6882
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6883
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6884
        int bLength = bToIndex - bFromIndex;
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6885
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6886
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6887
                                       Math.min(aLength, bLength));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6888
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6889
            return Long.compare(a[aFromIndex + i], b[bFromIndex + i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6890
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6891
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6892
        return aLength - bLength;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6893
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6894
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6895
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6896
     * Compares two {@code long} arrays lexicographically, numerically treating
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6897
     * elements as unsigned.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6898
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6899
     * <p>If the two arrays share a common prefix then the lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6900
     * comparison is the result of comparing two elements, as if by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6901
     * {@link Long#compareUnsigned(long, long)}, at an index within the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6902
     * respective arrays that is the prefix length.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6903
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6904
     * comparison is the result of comparing the two array lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6905
     * (See {@link #mismatch(long[], long[])} for the definition of a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6906
     * and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6907
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6908
     * <p>A {@code null} array reference is considered lexicographically less
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6909
     * than a non-{@code null} array reference.  Two {@code null} array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6910
     * references are considered equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6911
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6912
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6913
     * <p>This method behaves as if (for non-{@code null} array references):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6914
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6915
     *     int i = Arrays.mismatch(a, b);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6916
     *     if (i >= 0 && i < Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6917
     *         return Long.compareUnsigned(a[i], b[i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6918
     *     return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6919
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6920
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6921
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6922
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6923
     * @return the value {@code 0} if the first and second array are
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6924
     *         equal and contain the same elements in the same order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6925
     *         a value less than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6926
     *         lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6927
     *         a value greater than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6928
     *         lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6929
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6930
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6931
    public static int compareUnsigned(long[] a, long[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6932
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6933
            return 0;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6934
        if (a == null || b == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6935
            return a == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6936
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6937
        int i = ArraysSupport.mismatch(a, b,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6938
                                       Math.min(a.length, b.length));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6939
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6940
            return Long.compareUnsigned(a[i], b[i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6941
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6942
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6943
        return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6944
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6945
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6946
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6947
     * Compares two {@code long} arrays lexicographically over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6948
     * ranges, numerically treating elements as unsigned.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6949
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6950
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6951
     * then the lexicographic comparison is the result of comparing two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6952
     * elements, as if by {@link Long#compareUnsigned(long, long)}, at a
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6953
     * relative index within the respective arrays that is the length of the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6954
     * prefix.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6955
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6956
     * comparison is the result of comparing the two range lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6957
     * (See {@link #mismatch(long[], int, int, long[], int, int)} for the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6958
     * definition of a common and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6959
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6960
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6961
     * <p>This method behaves as if:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6962
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6963
     *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6964
     *                             b, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6965
     *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6966
     *         return Long.compareUnsigned(a[aFromIndex + i], b[bFromIndex + i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6967
     *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6968
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6969
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6970
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6971
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6972
     *                   first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6973
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6974
     *                 first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6975
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6976
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6977
     *                   second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6978
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6979
     *                 second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6980
     * @return the value {@code 0} if, over the specified ranges, the first and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6981
     *         second array are equal and contain the same elements in the same
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6982
     *         order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6983
     *         a value less than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6984
     *         first array is lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6985
     *         a value greater than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6986
     *         first array is lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6987
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6988
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6989
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6990
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6991
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6992
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6993
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6994
     *         if either array is null
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6995
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6996
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6997
    public static int compareUnsigned(long[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6998
                                      long[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6999
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7000
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7001
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7002
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7003
        int bLength = bToIndex - bFromIndex;
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7004
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7005
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7006
                                       Math.min(aLength, bLength));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7007
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7008
            return Long.compareUnsigned(a[aFromIndex + i], b[bFromIndex + i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7009
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7010
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7011
        return aLength - bLength;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7012
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7013
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7014
    // Compare float
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7015
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7016
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7017
     * Compares two {@code float} arrays lexicographically.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7018
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7019
     * <p>If the two arrays share a common prefix then the lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7020
     * comparison is the result of comparing two elements, as if by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7021
     * {@link Float#compare(float, float)}, at an index within the respective
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7022
     * arrays that is the prefix length.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7023
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7024
     * comparison is the result of comparing the two array lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7025
     * (See {@link #mismatch(float[], float[])} for the definition of a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7026
     * and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7027
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7028
     * <p>A {@code null} array reference is considered lexicographically less
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7029
     * than a non-{@code null} array reference.  Two {@code null} array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7030
     * references are considered equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7031
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7032
     * <p>The comparison is consistent with {@link #equals(float[], float[]) equals},
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7033
     * more specifically the following holds for arrays {@code a} and {@code b}:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7034
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7035
     *     Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7036
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7037
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7038
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7039
     * <p>This method behaves as if (for non-{@code null} array references):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7040
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7041
     *     int i = Arrays.mismatch(a, b);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7042
     *     if (i >= 0 && i < Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7043
     *         return Float.compare(a[i], b[i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7044
     *     return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7045
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7046
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7047
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7048
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7049
     * @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
  7050
     *         contain the same elements in the same order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7051
     *         a value less than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7052
     *         lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7053
     *         a value greater than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7054
     *         lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7055
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7056
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7057
    public static int compare(float[] a, float[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7058
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7059
            return 0;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7060
        if (a == null || b == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7061
            return a == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7062
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7063
        int i = ArraysSupport.mismatch(a, b,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7064
                                       Math.min(a.length, b.length));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7065
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7066
            return Float.compare(a[i], b[i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7067
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7068
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7069
        return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7070
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7071
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7072
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7073
     * Compares two {@code float} arrays lexicographically over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7074
     * ranges.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7075
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7076
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7077
     * then the lexicographic comparison is the result of comparing two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7078
     * elements, as if by {@link Float#compare(float, float)}, at a relative
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7079
     * index within the respective arrays that is the length of the prefix.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7080
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7081
     * comparison is the result of comparing the two range lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7082
     * (See {@link #mismatch(float[], int, int, float[], int, int)} for the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7083
     * definition of a common and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7084
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7085
     * <p>The comparison is consistent with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7086
     * {@link #equals(float[], int, int, float[], int, int) equals}, more
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7087
     * specifically the following holds for arrays {@code a} and {@code b} with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7088
     * specified ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7089
     * [{@code bFromIndex}, {@code btoIndex}) respectively:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7090
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7091
     *     Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7092
     *         (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7093
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7094
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7095
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7096
     * <p>This method behaves as if:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7097
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7098
     *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7099
     *                             b, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7100
     *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7101
     *         return Float.compare(a[aFromIndex + i], b[bFromIndex + i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7102
     *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7103
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7104
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7105
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7106
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7107
     *                   first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7108
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7109
     *                 first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7110
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7111
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7112
     *                   second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7113
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7114
     *                 second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7115
     * @return the value {@code 0} if, over the specified ranges, the first and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7116
     *         second array are equal and contain the same elements in the same
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7117
     *         order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7118
     *         a value less than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7119
     *         first array is lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7120
     *         a value greater than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7121
     *         first array is lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7122
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7123
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7124
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7125
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7126
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7127
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7128
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7129
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7130
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7131
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7132
    public static int compare(float[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7133
                              float[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7134
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7135
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7136
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7137
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7138
        int bLength = bToIndex - bFromIndex;
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7139
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7140
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7141
                                       Math.min(aLength, bLength));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7142
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7143
            return Float.compare(a[aFromIndex + i], b[bFromIndex + i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7144
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7145
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7146
        return aLength - bLength;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7147
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7148
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7149
    // Compare double
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7150
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7151
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7152
     * Compares two {@code double} arrays lexicographically.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7153
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7154
     * <p>If the two arrays share a common prefix then the lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7155
     * comparison is the result of comparing two elements, as if by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7156
     * {@link Double#compare(double, double)}, at an index within the respective
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7157
     * arrays that is the prefix length.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7158
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7159
     * comparison is the result of comparing the two array lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7160
     * (See {@link #mismatch(double[], double[])} for the definition of a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7161
     * and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7162
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7163
     * <p>A {@code null} array reference is considered lexicographically less
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7164
     * than a non-{@code null} array reference.  Two {@code null} array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7165
     * references are considered equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7166
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7167
     * <p>The comparison is consistent with {@link #equals(double[], double[]) equals},
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7168
     * more specifically the following holds for arrays {@code a} and {@code b}:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7169
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7170
     *     Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7171
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7172
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7173
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7174
     * <p>This method behaves as if (for non-{@code null} array references):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7175
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7176
     *     int i = Arrays.mismatch(a, b);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7177
     *     if (i >= 0 && i < Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7178
     *         return Double.compare(a[i], b[i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7179
     *     return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7180
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7181
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7182
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7183
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7184
     * @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
  7185
     *         contain the same elements in the same order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7186
     *         a value less than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7187
     *         lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7188
     *         a value greater than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7189
     *         lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7190
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7191
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7192
    public static int compare(double[] a, double[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7193
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7194
            return 0;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7195
        if (a == null || b == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7196
            return a == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7197
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7198
        int i = ArraysSupport.mismatch(a, b,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7199
                                       Math.min(a.length, b.length));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7200
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7201
            return Double.compare(a[i], b[i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7202
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7203
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7204
        return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7205
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7206
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7207
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7208
     * Compares two {@code double} arrays lexicographically over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7209
     * ranges.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7210
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7211
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7212
     * then the lexicographic comparison is the result of comparing two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7213
     * elements, as if by {@link Double#compare(double, double)}, at a relative
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7214
     * index within the respective arrays that is the length of the prefix.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7215
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7216
     * comparison is the result of comparing the two range lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7217
     * (See {@link #mismatch(double[], int, int, double[], int, int)} for the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7218
     * definition of a common and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7219
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7220
     * <p>The comparison is consistent with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7221
     * {@link #equals(double[], int, int, double[], int, int) equals}, more
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7222
     * specifically the following holds for arrays {@code a} and {@code b} with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7223
     * specified ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7224
     * [{@code bFromIndex}, {@code btoIndex}) respectively:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7225
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7226
     *     Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7227
     *         (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7228
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7229
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7230
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7231
     * <p>This method behaves as if:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7232
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7233
     *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7234
     *                             b, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7235
     *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7236
     *         return Double.compare(a[aFromIndex + i], b[bFromIndex + i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7237
     *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7238
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7239
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7240
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7241
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7242
     *                   first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7243
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7244
     *                 first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7245
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7246
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7247
     *                   second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7248
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7249
     *                 second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7250
     * @return the value {@code 0} if, over the specified ranges, the first and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7251
     *         second array are equal and contain the same elements in the same
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7252
     *         order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7253
     *         a value less than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7254
     *         first array is lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7255
     *         a value greater than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7256
     *         first array is lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7257
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7258
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7259
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7260
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7261
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7262
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7263
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7264
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7265
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7266
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7267
    public static int compare(double[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7268
                              double[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7269
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7270
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7271
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7272
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7273
        int bLength = bToIndex - bFromIndex;
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7274
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7275
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7276
                                       Math.min(aLength, bLength));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7277
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7278
            return Double.compare(a[aFromIndex + i], b[bFromIndex + i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7279
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7280
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7281
        return aLength - bLength;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7282
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7283
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7284
    // Compare objects
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7285
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7286
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7287
     * Compares two {@code Object} arrays, within comparable elements,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7288
     * lexicographically.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7289
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7290
     * <p>If the two arrays share a common prefix then the lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7291
     * comparison is the result of comparing two elements of type {@code T} at
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7292
     * an index {@code i} within the respective arrays that is the prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7293
     * length, as if by:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7294
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7295
     *     Comparator.nullsFirst(Comparator.<T>naturalOrder()).
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7296
     *         compare(a[i], b[i])
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7297
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7298
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7299
     * comparison is the result of comparing the two array lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7300
     * (See {@link #mismatch(Object[], Object[])} for the definition of a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7301
     * and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7302
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7303
     * <p>A {@code null} array reference is considered lexicographically less
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7304
     * than a non-{@code null} array reference.  Two {@code null} array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7305
     * references are considered equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7306
     * A {@code null} array element is considered lexicographically than a
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7307
     * non-{@code null} array element.  Two {@code null} array elements are
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7308
     * considered equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7309
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7310
     * <p>The comparison is consistent with {@link #equals(Object[], Object[]) equals},
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7311
     * more specifically the following holds for arrays {@code a} and {@code b}:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7312
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7313
     *     Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7314
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7315
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7316
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7317
     * <p>This method behaves as if (for non-{@code null} array references
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7318
     * and elements):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7319
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7320
     *     int i = Arrays.mismatch(a, b);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7321
     *     if (i >= 0 && i < Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7322
     *         return a[i].compareTo(b[i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7323
     *     return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7324
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7325
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7326
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7327
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7328
     * @param <T> the type of comparable array elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7329
     * @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
  7330
     *         contain the same elements in the same order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7331
     *         a value less than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7332
     *         lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7333
     *         a value greater than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7334
     *         lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7335
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7336
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7337
    public static <T extends Comparable<? super T>> int compare(T[] a, T[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7338
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7339
            return 0;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7340
        // A null array is less than a non-null array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7341
        if (a == null || b == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7342
            return a == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7343
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7344
        int length = Math.min(a.length, b.length);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7345
        for (int i = 0; i < length; i++) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7346
            T oa = a[i];
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7347
            T ob = b[i];
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7348
            if (oa != ob) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7349
                // A null element is less than a non-null element
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7350
                if (oa == null || ob == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7351
                    return oa == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7352
                int v = oa.compareTo(ob);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7353
                if (v != 0) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7354
                    return v;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7355
                }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7356
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7357
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7358
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7359
        return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7360
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7361
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7362
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7363
     * Compares two {@code Object} arrays lexicographically over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7364
     * ranges.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7365
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7366
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7367
     * then the lexicographic comparison is the result of comparing two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7368
     * elements of type {@code T} at a relative index {@code i} within the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7369
     * respective arrays that is the prefix length, as if by:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7370
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7371
     *     Comparator.nullsFirst(Comparator.<T>naturalOrder()).
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7372
     *         compare(a[aFromIndex + i, b[bFromIndex + i])
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7373
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7374
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7375
     * comparison is the result of comparing the two range lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7376
     * (See {@link #mismatch(Object[], int, int, Object[], int, int)} for the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7377
     * definition of a common and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7378
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7379
     * <p>The comparison is consistent with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7380
     * {@link #equals(Object[], int, int, Object[], int, int) equals}, more
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7381
     * specifically the following holds for arrays {@code a} and {@code b} with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7382
     * specified ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7383
     * [{@code bFromIndex}, {@code btoIndex}) respectively:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7384
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7385
     *     Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7386
     *         (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7387
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7388
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7389
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7390
     * <p>This method behaves as if (for non-{@code null} array elements):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7391
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7392
     *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7393
     *                             b, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7394
     *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7395
     *         return a[aFromIndex + i].compareTo(b[bFromIndex + i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7396
     *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7397
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7398
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7399
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7400
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7401
     *                   first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7402
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7403
     *                 first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7404
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7405
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7406
     *                   second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7407
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7408
     *                 second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7409
     * @param <T> the type of comparable array elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7410
     * @return the value {@code 0} if, over the specified ranges, the first and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7411
     *         second array are equal and contain the same elements in the same
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7412
     *         order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7413
     *         a value less than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7414
     *         first array is lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7415
     *         a value greater than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7416
     *         first array is lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7417
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7418
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7419
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7420
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7421
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7422
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7423
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7424
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7425
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7426
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7427
    public static <T extends Comparable<? super T>> int compare(
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7428
            T[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7429
            T[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7430
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7431
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7432
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7433
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7434
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7435
        int length = Math.min(aLength, bLength);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7436
        for (int i = 0; i < length; i++) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7437
            T oa = a[aFromIndex++];
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7438
            T ob = b[bFromIndex++];
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7439
            if (oa != ob) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7440
                if (oa == null || ob == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7441
                    return oa == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7442
                int v = oa.compareTo(ob);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7443
                if (v != 0) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7444
                    return v;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7445
                }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7446
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7447
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7448
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7449
        return aLength - bLength;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7450
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7451
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7452
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7453
     * Compares two {@code Object} arrays lexicographically using a specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7454
     * comparator.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7455
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7456
     * <p>If the two arrays share a common prefix then the lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7457
     * comparison is the result of comparing with the specified comparator two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7458
     * elements at an index within the respective arrays that is the prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7459
     * length.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7460
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7461
     * comparison is the result of comparing the two array lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7462
     * (See {@link #mismatch(Object[], Object[])} for the definition of a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7463
     * and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7464
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7465
     * <p>A {@code null} array reference is considered lexicographically less
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7466
     * than a non-{@code null} array reference.  Two {@code null} array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7467
     * references are considered equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7468
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7469
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7470
     * <p>This method behaves as if (for non-{@code null} array references):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7471
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7472
     *     int i = Arrays.mismatch(a, b, cmp);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7473
     *     if (i >= 0 && i < Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7474
     *         return cmp.compare(a[i], b[i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7475
     *     return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7476
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7477
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7478
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7479
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7480
     * @param cmp the comparator to compare array elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7481
     * @param <T> the type of array elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7482
     * @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
  7483
     *         contain the same elements in the same order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7484
     *         a value less than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7485
     *         lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7486
     *         a value greater than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7487
     *         lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7488
     * @throws NullPointerException if the comparator is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7489
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7490
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7491
    public static <T> int compare(T[] a, T[] b,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7492
                                  Comparator<? super T> cmp) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7493
        Objects.requireNonNull(cmp);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7494
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7495
            return 0;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7496
        if (a == null || b == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7497
            return a == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7498
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7499
        int length = Math.min(a.length, b.length);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7500
        for (int i = 0; i < length; i++) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7501
            T oa = a[i];
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7502
            T ob = b[i];
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7503
            if (oa != ob) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7504
                // Null-value comparison is deferred to the comparator
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7505
                int v = cmp.compare(oa, ob);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7506
                if (v != 0) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7507
                    return v;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7508
                }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7509
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7510
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7511
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7512
        return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7513
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7514
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7515
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7516
     * Compares two {@code Object} arrays lexicographically over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7517
     * ranges.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7518
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7519
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7520
     * then the lexicographic comparison is the result of comparing with the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7521
     * specified comparator two elements at a relative index within the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7522
     * respective arrays that is the prefix length.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7523
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7524
     * comparison is the result of comparing the two range lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7525
     * (See {@link #mismatch(Object[], int, int, Object[], int, int)} for the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7526
     * definition of a common and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7527
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7528
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7529
     * <p>This method behaves as if (for non-{@code null} array elements):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7530
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7531
     *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7532
     *                             b, bFromIndex, bToIndex, cmp);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7533
     *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7534
     *         return cmp.compare(a[aFromIndex + i], b[bFromIndex + i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7535
     *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7536
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7537
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7538
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7539
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7540
     *                   first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7541
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7542
     *                 first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7543
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7544
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7545
     *                   second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7546
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7547
     *                 second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7548
     * @param cmp the comparator to compare array elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7549
     * @param <T> the type of array elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7550
     * @return the value {@code 0} if, over the specified ranges, the first and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7551
     *         second array are equal and contain the same elements in the same
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7552
     *         order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7553
     *         a value less than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7554
     *         first array is lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7555
     *         a value greater than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7556
     *         first array is lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7557
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7558
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7559
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7560
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7561
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7562
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7563
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7564
     *         if either array or the comparator is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7565
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7566
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7567
    public static <T> int compare(
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7568
            T[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7569
            T[] b, int bFromIndex, int bToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7570
            Comparator<? super T> cmp) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7571
        Objects.requireNonNull(cmp);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7572
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7573
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7574
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7575
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7576
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7577
        int length = Math.min(aLength, bLength);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7578
        for (int i = 0; i < length; i++) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7579
            T oa = a[aFromIndex++];
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7580
            T ob = b[bFromIndex++];
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7581
            if (oa != ob) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7582
                // Null-value comparison is deferred to the comparator
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7583
                int v = cmp.compare(oa, ob);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7584
                if (v != 0) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7585
                    return v;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7586
                }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7587
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7588
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7589
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7590
        return aLength - bLength;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7591
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7592
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7593
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7594
    // Mismatch methods
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7595
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7596
    // Mismatch boolean
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7597
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7598
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7599
     * Finds and returns the index of the first mismatch between two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7600
     * {@code boolean} arrays, otherwise return -1 if no mismatch is found.  The
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7601
     * 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
  7602
     * of the smaller array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7603
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7604
     * <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
  7605
     * length of the common prefix and it follows that there is a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7606
     * between the two elements at that index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7607
     * 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
  7608
     * 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
  7609
     * valid for the larger array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7610
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7611
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7612
     * <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
  7613
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7614
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7615
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7616
     *     pl < Math.min(a.length, b.length) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7617
     *     Arrays.equals(a, 0, pl, b, 0, pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7618
     *     a[pl] != b[pl]
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7619
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7620
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7621
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7622
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7623
     * <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
  7624
     * prefix if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7625
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7626
     *     a.length != b.length &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7627
     *     Arrays.equals(a, 0, Math.min(a.length, b.length),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7628
     *                   b, 0, Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7629
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7630
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7631
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7632
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7633
     * @return the index of the first mismatch between the two arrays,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7634
     *         otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7635
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7636
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7637
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7638
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7639
    public static int mismatch(boolean[] a, boolean[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7640
        int length = Math.min(a.length, b.length); // Check null array refs
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7641
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7642
            return -1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7643
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7644
        int i = ArraysSupport.mismatch(a, b, length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7645
        return (i < 0 && a.length != b.length) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7646
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7647
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7648
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7649
     * Finds and returns the relative index of the first mismatch between two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7650
     * {@code boolean} arrays over the specified ranges, otherwise return -1 if
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7651
     * 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
  7652
     * to the length (inclusive) of the smaller range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7653
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7654
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7655
     * then the returned relative index is the length of the common prefix and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7656
     * it follows that there is a mismatch between the two elements at that
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7657
     * relative index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7658
     * 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
  7659
     * then the returned relative index is the length of the smaller range and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7660
     * 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
  7661
     * larger range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7662
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7663
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7664
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7665
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7666
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7667
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7668
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7669
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7670
     *     pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7671
     *     Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7672
     *     a[aFromIndex + pl] != b[bFromIndex + pl]
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7673
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7674
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7675
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7676
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7677
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7678
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7679
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7680
     * if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7681
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7682
     *     (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7683
     *     Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7684
     *                   b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7685
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7686
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7687
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7688
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7689
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7690
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7691
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7692
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7693
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7694
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7695
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7696
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7697
     * @return the relative index of the first mismatch between the two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7698
     *         over the specified ranges, otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7699
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7700
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7701
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7702
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7703
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7704
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7705
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7706
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7707
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7708
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7709
    public static int mismatch(boolean[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7710
                               boolean[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7711
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7712
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7713
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7714
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7715
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7716
        int length = Math.min(aLength, bLength);
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7717
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7718
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7719
                                       length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7720
        return (i < 0 && aLength != bLength) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7721
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7722
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7723
    // Mismatch byte
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7724
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7725
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7726
     * Finds and returns the index of the first mismatch between two {@code byte}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7727
     * arrays, otherwise return -1 if no mismatch is found.  The index will be
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7728
     * 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
  7729
     * array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7730
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7731
     * <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
  7732
     * length of the common prefix and it follows that there is a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7733
     * between the two elements at that index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7734
     * 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
  7735
     * 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
  7736
     * valid for the larger array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7737
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7738
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7739
     * <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
  7740
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7741
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7742
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7743
     *     pl < Math.min(a.length, b.length) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7744
     *     Arrays.equals(a, 0, pl, b, 0, pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7745
     *     a[pl] != b[pl]
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7746
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7747
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7748
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7749
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7750
     * <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
  7751
     * prefix if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7752
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7753
     *     a.length != b.length &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7754
     *     Arrays.equals(a, 0, Math.min(a.length, b.length),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7755
     *                   b, 0, Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7756
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7757
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7758
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7759
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7760
     * @return the index of the first mismatch between the two arrays,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7761
     *         otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7762
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7763
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7764
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7765
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7766
    public static int mismatch(byte[] a, byte[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7767
        int length = Math.min(a.length, b.length); // Check null array refs
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7768
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7769
            return -1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7770
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7771
        int i = ArraysSupport.mismatch(a, b, length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7772
        return (i < 0 && a.length != b.length) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7773
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7774
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7775
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7776
     * Finds and returns the relative index of the first mismatch between two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7777
     * {@code byte} arrays over the specified ranges, otherwise return -1 if no
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7778
     * 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
  7779
     * the length (inclusive) of the smaller range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7780
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7781
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7782
     * then the returned relative index is the length of the common prefix and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7783
     * it follows that there is a mismatch between the two elements at that
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7784
     * relative index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7785
     * 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
  7786
     * then the returned relative index is the length of the smaller range and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7787
     * 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
  7788
     * larger range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7789
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7790
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7791
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7792
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7793
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7794
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7795
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7796
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7797
     *     pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7798
     *     Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7799
     *     a[aFromIndex + pl] != b[bFromIndex + pl]
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7800
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7801
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7802
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7803
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7804
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7805
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7806
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7807
     * if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7808
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7809
     *     (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7810
     *     Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7811
     *                   b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7812
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7813
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7814
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7815
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7816
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7817
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7818
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7819
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7820
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7821
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7822
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7823
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7824
     * @return the relative index of the first mismatch between the two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7825
     *         over the specified ranges, otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7826
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7827
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7828
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7829
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7830
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7831
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7832
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7833
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7834
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7835
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7836
    public static int mismatch(byte[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7837
                               byte[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7838
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7839
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7840
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7841
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7842
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7843
        int length = Math.min(aLength, bLength);
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7844
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7845
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7846
                                       length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7847
        return (i < 0 && aLength != bLength) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7848
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7849
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7850
    // Mismatch char
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7851
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7852
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7853
     * Finds and returns the index of the first mismatch between two {@code char}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7854
     * arrays, otherwise return -1 if no mismatch is found.  The index will be
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7855
     * 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
  7856
     * array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7857
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7858
     * <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
  7859
     * length of the common prefix and it follows that there is a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7860
     * between the two elements at that index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7861
     * 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
  7862
     * 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
  7863
     * valid for the larger array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7864
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7865
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7866
     * <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
  7867
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7868
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7869
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7870
     *     pl < Math.min(a.length, b.length) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7871
     *     Arrays.equals(a, 0, pl, b, 0, pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7872
     *     a[pl] != b[pl]
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7873
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7874
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7875
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7876
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7877
     * <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
  7878
     * prefix if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7879
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7880
     *     a.length != b.length &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7881
     *     Arrays.equals(a, 0, Math.min(a.length, b.length),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7882
     *                   b, 0, Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7883
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7884
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7885
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7886
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7887
     * @return the index of the first mismatch between the two arrays,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7888
     *         otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7889
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7890
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7891
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7892
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7893
    public static int mismatch(char[] a, char[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7894
        int length = Math.min(a.length, b.length); // Check null array refs
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7895
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7896
            return -1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7897
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7898
        int i = ArraysSupport.mismatch(a, b, length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7899
        return (i < 0 && a.length != b.length) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7900
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7901
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7902
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7903
     * Finds and returns the relative index of the first mismatch between two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7904
     * {@code char} arrays over the specified ranges, otherwise return -1 if no
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7905
     * 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
  7906
     * the length (inclusive) of the smaller range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7907
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7908
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7909
     * then the returned relative index is the length of the common prefix and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7910
     * it follows that there is a mismatch between the two elements at that
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7911
     * relative index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7912
     * 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
  7913
     * then the returned relative index is the length of the smaller range and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7914
     * 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
  7915
     * larger range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7916
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7917
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7918
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7919
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7920
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7921
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7922
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7923
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7924
     *     pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7925
     *     Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7926
     *     a[aFromIndex + pl] != b[bFromIndex + pl]
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7927
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7928
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7929
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7930
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7931
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7932
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7933
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7934
     * if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7935
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7936
     *     (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7937
     *     Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7938
     *                   b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7939
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7940
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7941
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7942
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7943
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7944
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7945
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7946
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7947
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7948
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7949
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7950
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7951
     * @return the relative index of the first mismatch between the two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7952
     *         over the specified ranges, otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7953
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7954
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7955
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7956
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7957
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7958
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7959
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7960
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7961
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7962
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7963
    public static int mismatch(char[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7964
                               char[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7965
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7966
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7967
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7968
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7969
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7970
        int length = Math.min(aLength, bLength);
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7971
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7972
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7973
                                       length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7974
        return (i < 0 && aLength != bLength) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7975
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7976
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7977
    // Mismatch short
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7978
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7979
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7980
     * Finds and returns the index of the first mismatch between two {@code short}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7981
     * arrays, otherwise return -1 if no mismatch is found.  The index will be
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7982
     * 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
  7983
     * array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7984
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7985
     * <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
  7986
     * length of the common prefix and it follows that there is a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7987
     * between the two elements at that index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7988
     * 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
  7989
     * 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
  7990
     * valid for the larger array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7991
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7992
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7993
     * <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
  7994
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7995
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7996
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7997
     *     pl < Math.min(a.length, b.length) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7998
     *     Arrays.equals(a, 0, pl, b, 0, pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7999
     *     a[pl] != b[pl]
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8000
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8001
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8002
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8003
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8004
     * <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
  8005
     * prefix if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8006
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8007
     *     a.length != b.length &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8008
     *     Arrays.equals(a, 0, Math.min(a.length, b.length),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8009
     *                   b, 0, Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8010
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8011
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8012
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8013
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8014
     * @return the index of the first mismatch between the two arrays,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8015
     *         otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8016
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8017
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8018
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8019
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8020
    public static int mismatch(short[] a, short[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8021
        int length = Math.min(a.length, b.length); // Check null array refs
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8022
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8023
            return -1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8024
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8025
        int i = ArraysSupport.mismatch(a, b, length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8026
        return (i < 0 && a.length != b.length) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8027
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8028
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8029
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8030
     * Finds and returns the relative index of the first mismatch between two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8031
     * {@code short} arrays over the specified ranges, otherwise return -1 if no
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8032
     * 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
  8033
     * the length (inclusive) of the smaller range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8034
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8035
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8036
     * then the returned relative index is the length of the common prefix and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8037
     * it follows that there is a mismatch between the two elements at that
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8038
     * relative index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8039
     * 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
  8040
     * then the returned relative index is the length of the smaller range and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8041
     * 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
  8042
     * larger range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8043
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8044
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8045
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8046
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8047
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8048
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8049
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8050
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8051
     *     pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8052
     *     Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8053
     *     a[aFromIndex + pl] != b[bFromIndex + pl]
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8054
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8055
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8056
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8057
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8058
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8059
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8060
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8061
     * if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8062
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8063
     *     (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8064
     *     Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8065
     *                   b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8066
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8067
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8068
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8069
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8070
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8071
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8072
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8073
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8074
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8075
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8076
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8077
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8078
     * @return the relative index of the first mismatch between the two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8079
     *         over the specified ranges, otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8080
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8081
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8082
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8083
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8084
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8085
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8086
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8087
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8088
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8089
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8090
    public static int mismatch(short[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8091
                               short[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8092
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8093
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8094
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8095
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8096
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8097
        int length = Math.min(aLength, bLength);
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8098
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8099
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8100
                                       length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8101
        return (i < 0 && aLength != bLength) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8102
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8103
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8104
    // Mismatch int
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8105
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8106
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8107
     * Finds and returns the index of the first mismatch between two {@code int}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8108
     * arrays, otherwise return -1 if no mismatch is found.  The index will be
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8109
     * 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
  8110
     * array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8111
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8112
     * <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
  8113
     * length of the common prefix and it follows that there is a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8114
     * between the two elements at that index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8115
     * 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
  8116
     * 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
  8117
     * valid for the larger array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8118
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8119
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8120
     * <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
  8121
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8122
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8123
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8124
     *     pl < Math.min(a.length, b.length) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8125
     *     Arrays.equals(a, 0, pl, b, 0, pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8126
     *     a[pl] != b[pl]
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8127
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8128
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8129
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8130
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8131
     * <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
  8132
     * prefix if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8133
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8134
     *     a.length != b.length &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8135
     *     Arrays.equals(a, 0, Math.min(a.length, b.length),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8136
     *                   b, 0, Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8137
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8138
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8139
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8140
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8141
     * @return the index of the first mismatch between the two arrays,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8142
     *         otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8143
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8144
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8145
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8146
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8147
    public static int mismatch(int[] a, int[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8148
        int length = Math.min(a.length, b.length); // Check null array refs
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8149
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8150
            return -1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8151
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8152
        int i = ArraysSupport.mismatch(a, b, length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8153
        return (i < 0 && a.length != b.length) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8154
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8155
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8156
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8157
     * Finds and returns the relative index of the first mismatch between two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8158
     * {@code int} arrays over the specified ranges, otherwise return -1 if no
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8159
     * 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
  8160
     * the length (inclusive) of the smaller range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8161
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8162
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8163
     * then the returned relative index is the length of the common prefix and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8164
     * it follows that there is a mismatch between the two elements at that
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8165
     * relative index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8166
     * 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
  8167
     * then the returned relative index is the length of the smaller range and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8168
     * 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
  8169
     * larger range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8170
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8171
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8172
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8173
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8174
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8175
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8176
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8177
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8178
     *     pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8179
     *     Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8180
     *     a[aFromIndex + pl] != b[bFromIndex + pl]
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8181
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8182
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8183
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8184
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8185
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8186
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8187
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8188
     * if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8189
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8190
     *     (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8191
     *     Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8192
     *                   b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8193
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8194
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8195
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8196
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8197
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8198
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8199
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8200
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8201
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8202
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8203
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8204
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8205
     * @return the relative index of the first mismatch between the two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8206
     *         over the specified ranges, otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8207
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8208
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8209
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8210
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8211
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8212
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8213
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8214
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8215
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8216
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8217
    public static int mismatch(int[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8218
                               int[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8219
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8220
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8221
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8222
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8223
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8224
        int length = Math.min(aLength, bLength);
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8225
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8226
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8227
                                       length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8228
        return (i < 0 && aLength != bLength) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8229
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8230
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8231
    // Mismatch long
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8232
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8233
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8234
     * Finds and returns the index of the first mismatch between two {@code long}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8235
     * arrays, otherwise return -1 if no mismatch is found.  The index will be
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8236
     * 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
  8237
     * array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8238
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8239
     * <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
  8240
     * length of the common prefix and it follows that there is a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8241
     * between the two elements at that index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8242
     * 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
  8243
     * 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
  8244
     * valid for the larger array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8245
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8246
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8247
     * <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
  8248
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8249
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8250
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8251
     *     pl < Math.min(a.length, b.length) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8252
     *     Arrays.equals(a, 0, pl, b, 0, pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8253
     *     a[pl] != b[pl]
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8254
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8255
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8256
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8257
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8258
     * <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
  8259
     * prefix if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8260
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8261
     *     a.length != b.length &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8262
     *     Arrays.equals(a, 0, Math.min(a.length, b.length),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8263
     *                   b, 0, Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8264
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8265
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8266
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8267
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8268
     * @return the index of the first mismatch between the two arrays,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8269
     *         otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8270
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8271
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8272
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8273
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8274
    public static int mismatch(long[] a, long[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8275
        int length = Math.min(a.length, b.length); // Check null array refs
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8276
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8277
            return -1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8278
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8279
        int i = ArraysSupport.mismatch(a, b, length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8280
        return (i < 0 && a.length != b.length) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8281
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8282
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8283
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8284
     * Finds and returns the relative index of the first mismatch between two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8285
     * {@code long} arrays over the specified ranges, otherwise return -1 if no
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8286
     * 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
  8287
     * the length (inclusive) of the smaller range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8288
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8289
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8290
     * then the returned relative index is the length of the common prefix and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8291
     * it follows that there is a mismatch between the two elements at that
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8292
     * relative index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8293
     * 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
  8294
     * then the returned relative index is the length of the smaller range and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8295
     * 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
  8296
     * larger range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8297
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8298
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8299
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8300
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8301
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8302
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8303
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8304
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8305
     *     pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8306
     *     Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8307
     *     a[aFromIndex + pl] != b[bFromIndex + pl]
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8308
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8309
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8310
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8311
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8312
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8313
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8314
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8315
     * if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8316
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8317
     *     (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8318
     *     Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8319
     *                   b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8320
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8321
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8322
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8323
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8324
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8325
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8326
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8327
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8328
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8329
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8330
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8331
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8332
     * @return the relative index of the first mismatch between the two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8333
     *         over the specified ranges, otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8334
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8335
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8336
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8337
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8338
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8339
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8340
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8341
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8342
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8343
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8344
    public static int mismatch(long[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8345
                               long[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8346
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8347
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8348
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8349
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8350
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8351
        int length = Math.min(aLength, bLength);
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8352
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8353
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8354
                                       length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8355
        return (i < 0 && aLength != bLength) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8356
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8357
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8358
    // Mismatch float
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8359
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8360
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8361
     * Finds and returns the index of the first mismatch between two {@code float}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8362
     * arrays, otherwise return -1 if no mismatch is found.  The index will be
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8363
     * 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
  8364
     * array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8365
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8366
     * <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
  8367
     * length of the common prefix and it follows that there is a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8368
     * between the two elements at that index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8369
     * 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
  8370
     * 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
  8371
     * valid for the larger array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8372
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8373
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8374
     * <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
  8375
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8376
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8377
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8378
     *     pl < Math.min(a.length, b.length) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8379
     *     Arrays.equals(a, 0, pl, b, 0, pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8380
     *     Float.compare(a[pl], b[pl]) != 0
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8381
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8382
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8383
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8384
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8385
     * <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
  8386
     * prefix if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8387
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8388
     *     a.length != b.length &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8389
     *     Arrays.equals(a, 0, Math.min(a.length, b.length),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8390
     *                   b, 0, Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8391
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8392
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8393
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8394
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8395
     * @return the index of the first mismatch between the two arrays,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8396
     *         otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8397
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8398
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8399
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8400
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8401
    public static int mismatch(float[] a, float[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8402
        int length = Math.min(a.length, b.length); // Check null array refs
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8403
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8404
            return -1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8405
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8406
        int i = ArraysSupport.mismatch(a, b, length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8407
        return (i < 0 && a.length != b.length) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8408
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8409
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8410
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8411
     * Finds and returns the relative index of the first mismatch between two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8412
     * {@code float} arrays over the specified ranges, otherwise return -1 if no
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8413
     * 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
  8414
     * the length (inclusive) of the smaller range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8415
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8416
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8417
     * then the returned relative index is the length of the common prefix and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8418
     * it follows that there is a mismatch between the two elements at that
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8419
     * relative index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8420
     * 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
  8421
     * then the returned relative index is the length of the smaller range and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8422
     * 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
  8423
     * larger range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8424
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8425
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8426
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8427
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8428
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8429
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8430
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8431
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8432
     *     pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8433
     *     Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8434
     *     Float.compare(a[aFromIndex + pl], b[bFromIndex + pl]) != 0
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8435
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8436
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8437
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8438
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8439
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8440
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8441
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8442
     * if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8443
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8444
     *     (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8445
     *     Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8446
     *                   b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8447
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8448
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8449
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8450
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8451
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8452
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8453
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8454
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8455
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8456
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8457
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8458
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8459
     * @return the relative index of the first mismatch between the two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8460
     *         over the specified ranges, otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8461
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8462
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8463
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8464
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8465
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8466
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8467
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8468
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8469
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8470
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8471
    public static int mismatch(float[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8472
                               float[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8473
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8474
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8475
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8476
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8477
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8478
        int length = Math.min(aLength, bLength);
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8479
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8480
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8481
                                       length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8482
        return (i < 0 && aLength != bLength) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8483
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8484
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8485
    // Mismatch double
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8486
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8487
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8488
     * Finds and returns the index of the first mismatch between two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8489
     * {@code double} arrays, otherwise return -1 if no mismatch is found.  The
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8490
     * 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
  8491
     * of the smaller array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8492
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8493
     * <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
  8494
     * length of the common prefix and it follows that there is a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8495
     * between the two elements at that index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8496
     * 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
  8497
     * 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
  8498
     * valid for the larger array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8499
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8500
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8501
     * <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
  8502
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8503
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8504
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8505
     *     pl < Math.min(a.length, b.length) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8506
     *     Arrays.equals(a, 0, pl, b, 0, pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8507
     *     Double.compare(a[pl], b[pl]) != 0
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8508
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8509
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8510
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8511
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8512
     * <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
  8513
     * prefix if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8514
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8515
     *     a.length != b.length &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8516
     *     Arrays.equals(a, 0, Math.min(a.length, b.length),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8517
     *                   b, 0, Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8518
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8519
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8520
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8521
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8522
     * @return the index of the first mismatch between the two arrays,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8523
     *         otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8524
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8525
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8526
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8527
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8528
    public static int mismatch(double[] a, double[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8529
        int length = Math.min(a.length, b.length); // Check null array refs
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8530
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8531
            return -1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8532
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8533
        int i = ArraysSupport.mismatch(a, b, length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8534
        return (i < 0 && a.length != b.length) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8535
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8536
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8537
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8538
     * Finds and returns the relative index of the first mismatch between two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8539
     * {@code double} arrays over the specified ranges, otherwise return -1 if
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8540
     * 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
  8541
     * to the length (inclusive) of the smaller range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8542
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8543
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8544
     * then the returned relative index is the length of the common prefix and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8545
     * it follows that there is a mismatch between the two elements at that
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8546
     * relative index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8547
     * 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
  8548
     * then the returned relative index is the length of the smaller range and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8549
     * 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
  8550
     * larger range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8551
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8552
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8553
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8554
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8555
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8556
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8557
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8558
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8559
     *     pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8560
     *     Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8561
     *     Double.compare(a[aFromIndex + pl], b[bFromIndex + pl]) != 0
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8562
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8563
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8564
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8565
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8566
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8567
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8568
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8569
     * if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8570
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8571
     *     (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8572
     *     Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8573
     *                   b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8574
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8575
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8576
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8577
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8578
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8579
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8580
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8581
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8582
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8583
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8584
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8585
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8586
     * @return the relative index of the first mismatch between the two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8587
     *         over the specified ranges, otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8588
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8589
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8590
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8591
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8592
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8593
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8594
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8595
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8596
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8597
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8598
    public static int mismatch(double[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8599
                               double[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8600
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8601
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8602
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8603
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8604
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8605
        int length = Math.min(aLength, bLength);
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8606
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8607
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8608
                                       length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8609
        return (i < 0 && aLength != bLength) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8610
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8611
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8612
    // Mismatch objects
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8613
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8614
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8615
     * Finds and returns the index of the first mismatch between two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8616
     * {@code Object} arrays, otherwise return -1 if no mismatch is found.  The
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8617
     * 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
  8618
     * of the smaller array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8619
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8620
     * <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
  8621
     * length of the common prefix and it follows that there is a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8622
     * between the two elements at that index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8623
     * 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
  8624
     * 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
  8625
     * valid for the larger array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8626
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8627
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8628
     * <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
  8629
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8630
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8631
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8632
     *     pl < Math.min(a.length, b.length) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8633
     *     Arrays.equals(a, 0, pl, b, 0, pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8634
     *     !Objects.equals(a[pl], b[pl])
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8635
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8636
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8637
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8638
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8639
     * <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
  8640
     * prefix if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8641
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8642
     *     a.length != b.length &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8643
     *     Arrays.equals(a, 0, Math.min(a.length, b.length),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8644
     *                   b, 0, Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8645
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8646
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8647
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8648
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8649
     * @return the index of the first mismatch between the two arrays,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8650
     *         otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8651
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8652
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8653
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8654
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8655
    public static int mismatch(Object[] a, Object[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8656
        int length = Math.min(a.length, b.length); // Check null array refs
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8657
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8658
            return -1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8659
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8660
        for (int i = 0; i < length; i++) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8661
            if (!Objects.equals(a[i], b[i]))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8662
                return i;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8663
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8664
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8665
        return a.length != b.length ? length : -1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8666
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8667
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8668
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8669
     * Finds and returns the relative index of the first mismatch between two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8670
     * {@code Object} arrays over the specified ranges, otherwise return -1 if
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8671
     * 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
  8672
     * to the length (inclusive) of the smaller range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8673
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8674
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8675
     * then the returned relative index is the length of the common prefix and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8676
     * it follows that there is a mismatch between the two elements at that
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8677
     * relative index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8678
     * 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
  8679
     * then the returned relative index is the length of the smaller range and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8680
     * 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
  8681
     * larger range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8682
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8683
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8684
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8685
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8686
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8687
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8688
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8689
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8690
     *     pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8691
     *     Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8692
     *     !Objects.equals(a[aFromIndex + pl], b[bFromIndex + pl])
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8693
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8694
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8695
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8696
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8697
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8698
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8699
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8700
     * if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8701
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8702
     *     (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8703
     *     Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8704
     *                   b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8705
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8706
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8707
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8708
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8709
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8710
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8711
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8712
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8713
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8714
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8715
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8716
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8717
     * @return the relative index of the first mismatch between the two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8718
     *         over the specified ranges, otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8719
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8720
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8721
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8722
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8723
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8724
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8725
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8726
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8727
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8728
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8729
    public static int mismatch(
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8730
            Object[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8731
            Object[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8732
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8733
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8734
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8735
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8736
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8737
        int length = Math.min(aLength, bLength);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8738
        for (int i = 0; i < length; i++) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8739
            if (!Objects.equals(a[aFromIndex++], b[bFromIndex++]))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8740
                return i;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8741
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8742
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8743
        return aLength != bLength ? length : -1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8744
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8745
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8746
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8747
     * Finds and returns the index of the first mismatch between two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8748
     * {@code Object} arrays, otherwise return -1 if no mismatch is found.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8749
     * 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
  8750
     * (inclusive) of the smaller array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8751
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8752
     * <p>The specified comparator is used to determine if two array elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8753
     * from the each array are not equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8754
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8755
     * <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
  8756
     * length of the common prefix and it follows that there is a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8757
     * between the two elements at that index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8758
     * 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
  8759
     * 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
  8760
     * valid for the larger array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8761
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8762
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8763
     * <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
  8764
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8765
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8766
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8767
     *     pl < Math.min(a.length, b.length) &&
33843
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  8768
     *     Arrays.equals(a, 0, pl, b, 0, pl, cmp)
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8769
     *     cmp.compare(a[pl], b[pl]) != 0
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8770
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8771
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8772
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8773
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8774
     * <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
  8775
     * prefix if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8776
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8777
     *     a.length != b.length &&
33843
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  8778
     *     Arrays.equals(a, 0, Math.min(a.length, b.length),
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  8779
     *                   b, 0, Math.min(a.length, b.length),
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  8780
     *                   cmp)
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8781
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8782
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8783
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8784
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8785
     * @param cmp the comparator to compare array elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8786
     * @param <T> the type of array elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8787
     * @return the index of the first mismatch between the two arrays,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8788
     *         otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8789
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8790
     *         if either array or the comparator is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8791
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8792
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8793
    public static <T> int mismatch(T[] a, T[] b, Comparator<? super T> cmp) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8794
        Objects.requireNonNull(cmp);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8795
        int length = Math.min(a.length, b.length); // Check null array refs
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8796
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8797
            return -1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8798
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8799
        for (int i = 0; i < length; i++) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8800
            T oa = a[i];
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8801
            T ob = b[i];
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8802
            if (oa != ob) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8803
                // Null-value comparison is deferred to the comparator
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8804
                int v = cmp.compare(oa, ob);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8805
                if (v != 0) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8806
                    return i;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8807
                }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8808
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8809
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8810
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8811
        return a.length != b.length ? length : -1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8812
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8813
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8814
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8815
     * Finds and returns the relative index of the first mismatch between two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8816
     * {@code Object} arrays over the specified ranges, otherwise return -1 if
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8817
     * 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
  8818
     * to the length (inclusive) of the smaller range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8819
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8820
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8821
     * then the returned relative index is the length of the common prefix and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8822
     * it follows that there is a mismatch between the two elements at that
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8823
     * relative index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8824
     * 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
  8825
     * then the returned relative index is the length of the smaller range and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8826
     * 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
  8827
     * larger range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8828
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8829
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8830
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8831
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8832
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8833
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8834
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8835
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8836
     *     pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&
33843
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  8837
     *     Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl, cmp) &&
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8838
     *     cmp.compare(a[aFromIndex + pl], b[bFromIndex + pl]) != 0
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8839
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8840
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8841
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8842
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8843
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8844
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8845
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8846
     * if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8847
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8848
     *     (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&
33843
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  8849
     *     Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  8850
     *                   b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  8851
     *                   cmp)
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8852
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8853
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8854
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8855
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8856
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8857
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8858
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8859
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8860
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8861
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8862
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8863
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8864
     * @param cmp the comparator to compare array elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8865
     * @param <T> the type of array elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8866
     * @return the relative index of the first mismatch between the two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8867
     *         over the specified ranges, otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8868
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8869
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8870
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8871
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8872
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8873
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8874
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8875
     *         if either array or the comparator is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8876
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8877
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8878
    public static <T> int mismatch(
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8879
            T[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8880
            T[] b, int bFromIndex, int bToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8881
            Comparator<? super T> cmp) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8882
        Objects.requireNonNull(cmp);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8883
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8884
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8885
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8886
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8887
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8888
        int length = Math.min(aLength, bLength);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8889
        for (int i = 0; i < length; i++) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8890
            T oa = a[aFromIndex++];
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8891
            T ob = b[bFromIndex++];
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8892
            if (oa != ob) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8893
                // Null-value comparison is deferred to the comparator
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8894
                int v = cmp.compare(oa, ob);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8895
                if (v != 0) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8896
                    return i;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8897
                }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8898
            }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8899
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8900
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8901
        return aLength != bLength ? length : -1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8902
    }
44743
f0bbd698c486 8177789: fix collections framework links to point to java.util package doc
smarks
parents: 37685
diff changeset
  8903
}