src/java.base/share/classes/java/util/Arrays.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 44743 jdk/src/java.base/share/classes/java/util/Arrays.java@f0bbd698c486
child 48356 29e165bdf669
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
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;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
    29
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    30
import java.lang.reflect.Array;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    31
import java.util.concurrent.ForkJoinPool;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    32
import java.util.function.BinaryOperator;
23354
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
    33
import java.util.function.Consumer;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    34
import java.util.function.DoubleBinaryOperator;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    35
import java.util.function.IntBinaryOperator;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    36
import java.util.function.IntFunction;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    37
import java.util.function.IntToDoubleFunction;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    38
import java.util.function.IntToLongFunction;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    39
import java.util.function.IntUnaryOperator;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    40
import java.util.function.LongBinaryOperator;
23354
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
    41
import java.util.function.UnaryOperator;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    42
import java.util.stream.DoubleStream;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    43
import java.util.stream.IntStream;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    44
import java.util.stream.LongStream;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    45
import java.util.stream.Stream;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
    46
import java.util.stream.StreamSupport;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * 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
    50
 * sorting and searching). This class also contains a static factory
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * that allows arrays to be viewed as lists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    53
 * <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
    54
 * if the specified array reference is null, except where noted.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <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
    57
 * brief descriptions of the <i>implementations</i>. Such descriptions should
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * 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
    59
 * <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
    60
 * 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
    61
 * 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
    62
 * a MergeSort, but it does have to be <i>stable</i>.)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * <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
    65
 * <a href="{@docRoot}/java/util/package-summary.html#CollectionsFramework">
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    68
 * @author Josh Bloch
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    69
 * @author Neal Gafter
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    70
 * @author John Rose
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    71
 * @since  1.2
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 */
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    73
public class Arrays {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
14925
72729557c226 8003981: Support Parallel Array Sorting - JEP 103
chegar
parents: 13795
diff changeset
    75
    /**
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    76
     * The minimum array length below which a parallel sorting
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    77
     * algorithm will not further partition the sorting task. Using
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    78
     * smaller sizes typically results in memory contention across
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    79
     * tasks that makes parallel speedups unlikely.
14925
72729557c226 8003981: Support Parallel Array Sorting - JEP 103
chegar
parents: 13795
diff changeset
    80
     */
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    81
    private static final int MIN_ARRAY_SORT_GRAN = 1 << 13;
14925
72729557c226 8003981: Support Parallel Array Sorting - JEP 103
chegar
parents: 13795
diff changeset
    82
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    // Suppresses default constructor, ensuring non-instantiability.
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
    84
    private Arrays() {}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    86
    /**
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    87
     * A comparator that implements the natural ordering of a group of
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    88
     * mutually comparable elements. May be used when a supplied
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    89
     * comparator is null. To simplify code-sharing within underlying
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    90
     * implementations, the compare method only declares type Object
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    91
     * for its second argument.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    92
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    93
     * Arrays class implementor's note: It is an empirical matter
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    94
     * whether ComparableTimSort offers any performance benefit over
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    95
     * TimSort used with this comparator.  If not, you are better off
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    96
     * deleting or bypassing ComparableTimSort.  There is currently no
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    97
     * empirical case for separating them for parallel sorting, so all
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    98
     * public Object parallelSort methods use the same comparator
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
    99
     * based implementation.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   100
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   101
    static final class NaturalOrder implements Comparator<Object> {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   102
        @SuppressWarnings("unchecked")
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   103
        public int compare(Object first, Object second) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   104
            return ((Comparable<Object>)first).compareTo(second);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   105
        }
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   106
        static final NaturalOrder INSTANCE = new NaturalOrder();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   107
    }
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   108
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
     * Checks that {@code fromIndex} and {@code toIndex} are in
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   111
     * the range and throws an exception if they aren't.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   112
     */
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
   113
    static void rangeCheck(int arrayLength, int fromIndex, int toIndex) {
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   114
        if (fromIndex > toIndex) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   115
            throw new IllegalArgumentException(
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   116
                    "fromIndex(" + fromIndex + ") > toIndex(" + toIndex + ")");
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   117
        }
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   118
        if (fromIndex < 0) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   119
            throw new ArrayIndexOutOfBoundsException(fromIndex);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   120
        }
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   121
        if (toIndex > arrayLength) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   122
            throw new ArrayIndexOutOfBoundsException(toIndex);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   123
        }
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
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   126
    /*
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   127
     * Sorting methods. Note that all public "sort" methods take the
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   128
     * same form: Performing argument checks if necessary, and then
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   129
     * expanding arguments into those required for the internal
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   130
     * implementation methods residing in other package-private
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   131
     * classes (except for legacyMergeSort, included in this class).
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   132
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    /**
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   135
     * 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
   136
     *
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   137
     * <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
   138
     * 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
   139
     * 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
   140
     * 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
   141
     * faster than traditional (one-pivot) Quicksort implementations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @param a the array to be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    public static void sort(int[] a) {
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   146
        DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    /**
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   150
     * Sorts the specified range of the array into ascending order. The range
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   151
     * to be sorted extends from the index {@code fromIndex}, inclusive, to
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   152
     * 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
   153
     * the range to be sorted is empty.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   155
     * <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
   156
     * 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
   157
     * 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
   158
     * 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
   159
     * faster than traditional (one-pivot) Quicksort implementations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @param a the array to be sorted
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   162
     * @param fromIndex the index of the first element, inclusive, to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   163
     * @param toIndex the index of the last element, exclusive, to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   164
     *
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   165
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   166
     * @throws ArrayIndexOutOfBoundsException
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   167
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    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
   170
        rangeCheck(a.length, fromIndex, toIndex);
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   171
        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /**
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   175
     * 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
   176
     *
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   177
     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   178
     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   179
     * offers O(n log(n)) performance on many data sets that cause other
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   180
     * quicksorts to degrade to quadratic performance, and is typically
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   181
     * faster than traditional (one-pivot) Quicksort implementations.
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   182
     *
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   183
     * @param a the array to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   184
     */
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   185
    public static void sort(long[] a) {
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   186
        DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   187
    }
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
     * Sorts the specified range of the array into ascending order. The range
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   191
     * to be sorted extends from the index {@code fromIndex}, inclusive, to
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   192
     * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   193
     * the range to be sorted is empty.
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   194
     *
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   195
     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   196
     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   197
     * offers O(n log(n)) performance on many data sets that cause other
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   198
     * quicksorts to degrade to quadratic performance, and is typically
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   199
     * faster than traditional (one-pivot) Quicksort implementations.
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   200
     *
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   201
     * @param a the array to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   202
     * @param fromIndex the index of the first element, inclusive, to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   203
     * @param toIndex the index of the last element, exclusive, to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   204
     *
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   205
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   206
     * @throws ArrayIndexOutOfBoundsException
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   207
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   208
     */
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   209
    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
   210
        rangeCheck(a.length, fromIndex, toIndex);
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   211
        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   212
    }
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
     * Sorts the specified array into ascending numerical order.
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   216
     *
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   217
     * <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
   218
     * 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
   219
     * 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
   220
     * 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
   221
     * faster than traditional (one-pivot) Quicksort implementations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * @param a the array to be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    public static void sort(short[] a) {
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   226
        DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    /**
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   230
     * Sorts the specified range of the array into ascending order. The range
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   231
     * to be sorted extends from the index {@code fromIndex}, inclusive, to
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   232
     * 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
   233
     * the range to be sorted is empty.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   235
     * <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
   236
     * 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
   237
     * 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
   238
     * 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
   239
     * faster than traditional (one-pivot) Quicksort implementations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @param a the array to be sorted
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   242
     * @param fromIndex the index of the first element, inclusive, to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   243
     * @param toIndex the index of the last element, exclusive, to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   244
     *
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   245
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   246
     * @throws ArrayIndexOutOfBoundsException
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   247
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    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
   250
        rangeCheck(a.length, fromIndex, toIndex);
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   251
        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    /**
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   255
     * 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
   256
     *
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   257
     * <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
   258
     * 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
   259
     * 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
   260
     * 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
   261
     * faster than traditional (one-pivot) Quicksort implementations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @param a the array to be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    public static void sort(char[] a) {
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   266
        DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    /**
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   270
     * Sorts the specified range of the array into ascending order. The range
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   271
     * to be sorted extends from the index {@code fromIndex}, inclusive, to
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   272
     * 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
   273
     * the range to be sorted is empty.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   275
     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   276
     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   277
     * offers O(n log(n)) performance on many data sets that cause other
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   278
     * quicksorts to degrade to quadratic performance, and is typically
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   279
     * faster than traditional (one-pivot) Quicksort implementations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * @param a the array to be sorted
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   282
     * @param fromIndex the index of the first element, inclusive, to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   283
     * @param toIndex the index of the last element, exclusive, to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   284
     *
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   285
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   286
     * @throws ArrayIndexOutOfBoundsException
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   287
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    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
   290
        rangeCheck(a.length, fromIndex, toIndex);
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   291
        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    /**
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   295
     * 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
   296
     *
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   297
     * <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
   298
     * 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
   299
     * 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
   300
     * 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
   301
     * faster than traditional (one-pivot) Quicksort implementations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * @param a the array to be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    public static void sort(byte[] a) {
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   306
        DualPivotQuicksort.sort(a, 0, a.length - 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    /**
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   310
     * Sorts the specified range of the array into ascending order. The range
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   311
     * to be sorted extends from the index {@code fromIndex}, inclusive, to
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   312
     * 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
   313
     * the range to be sorted is empty.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     *
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   315
     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   316
     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   317
     * offers O(n log(n)) performance on many data sets that cause other
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   318
     * quicksorts to degrade to quadratic performance, and is typically
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   319
     * faster than traditional (one-pivot) Quicksort implementations.
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   320
     *
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   321
     * @param a the array to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   322
     * @param fromIndex the index of the first element, inclusive, to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   323
     * @param toIndex the index of the last element, exclusive, to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   324
     *
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   325
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   326
     * @throws ArrayIndexOutOfBoundsException
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   327
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   328
     */
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   329
    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
   330
        rangeCheck(a.length, fromIndex, toIndex);
d229d56fd918 6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents: 5506
diff changeset
   331
        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1);
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   332
    }
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
     * Sorts the specified array into ascending numerical order.
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   336
     *
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   337
     * <p>The {@code <} relation does not provide a total order on all float
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   338
     * 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
   339
     * value compares neither less than, greater than, nor equal to any value,
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   340
     * even itself. This method uses the total order imposed by the method
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   341
     * {@link Float#compareTo}: {@code -0.0f} is treated as less than value
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   342
     * {@code 0.0f} and {@code Float.NaN} is considered greater than any
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   343
     * other value and all {@code Float.NaN} values are considered equal.
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   344
     *
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   345
     * <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
   346
     * 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
   347
     * 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
   348
     * 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
   349
     * faster than traditional (one-pivot) Quicksort implementations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * @param a the array to be sorted
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   352
     */
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   353
    public static void sort(float[] a) {
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   354
        DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   355
    }
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
     * Sorts the specified range of the array into ascending order. The range
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   359
     * to be sorted extends from the index {@code fromIndex}, inclusive, to
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   360
     * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   361
     * the range to be sorted is empty.
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   362
     *
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   363
     * <p>The {@code <} relation does not provide a total order on all float
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   364
     * 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
   365
     * value compares neither less than, greater than, nor equal to any value,
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   366
     * even itself. This method uses the total order imposed by the method
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   367
     * {@link Float#compareTo}: {@code -0.0f} is treated as less than value
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   368
     * {@code 0.0f} and {@code Float.NaN} is considered greater than any
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   369
     * other value and all {@code Float.NaN} values are considered equal.
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   370
     *
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   371
     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   372
     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   373
     * offers O(n log(n)) performance on many data sets that cause other
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   374
     * quicksorts to degrade to quadratic performance, and is typically
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   375
     * faster than traditional (one-pivot) Quicksort implementations.
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   376
     *
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   377
     * @param a the array to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   378
     * @param fromIndex the index of the first element, inclusive, to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   379
     * @param toIndex the index of the last element, exclusive, to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   380
     *
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   381
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   382
     * @throws ArrayIndexOutOfBoundsException
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   383
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     */
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   385
    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
   386
        rangeCheck(a.length, fromIndex, toIndex);
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   387
        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    /**
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   391
     * 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
   392
     *
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   393
     * <p>The {@code <} relation does not provide a total order on all double
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   394
     * 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
   395
     * value compares neither less than, greater than, nor equal to any value,
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   396
     * even itself. This method uses the total order imposed by the method
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   397
     * {@link Double#compareTo}: {@code -0.0d} is treated as less than value
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   398
     * {@code 0.0d} and {@code Double.NaN} is considered greater than any
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   399
     * 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
   400
     *
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   401
     * <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
   402
     * 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
   403
     * 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
   404
     * 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
   405
     * faster than traditional (one-pivot) Quicksort implementations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * @param a the array to be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    public static void sort(double[] a) {
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   410
        DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    /**
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   414
     * Sorts the specified range of the array into ascending order. The range
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   415
     * to be sorted extends from the index {@code fromIndex}, inclusive, to
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   416
     * 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
   417
     * the range to be sorted is empty.
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   418
     *
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   419
     * <p>The {@code <} relation does not provide a total order on all double
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   420
     * 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
   421
     * value compares neither less than, greater than, nor equal to any value,
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   422
     * even itself. This method uses the total order imposed by the method
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   423
     * {@link Double#compareTo}: {@code -0.0d} is treated as less than value
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   424
     * {@code 0.0d} and {@code Double.NaN} is considered greater than any
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   425
     * 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
   426
     *
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   427
     * <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
   428
     * 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
   429
     * 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
   430
     * 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
   431
     * faster than traditional (one-pivot) Quicksort implementations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * @param a the array to be sorted
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   434
     * @param fromIndex the index of the first element, inclusive, to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   435
     * @param toIndex the index of the last element, exclusive, to be sorted
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
   436
     *
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   437
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   438
     * @throws ArrayIndexOutOfBoundsException
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   439
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     */
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
   441
    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
   442
        rangeCheck(a.length, fromIndex, toIndex);
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   443
        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   444
    }
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   445
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
     * Sorts the specified array into ascending numerical order.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   448
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   449
     * @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
   450
     * 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
   451
     * 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
   452
     * sorted using the appropriate {@link Arrays#sort(byte[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   453
     * 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
   454
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   455
     * Arrays#sort(byte[]) Arrays.sort} method. The algorithm requires a
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   456
     * 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
   457
     * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   458
     * execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   459
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   460
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   461
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   462
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   463
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   464
    public static void parallelSort(byte[] a) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   465
        int n = a.length, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   466
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   467
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   468
            DualPivotQuicksort.sort(a, 0, n - 1);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   469
        else
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   470
            new ArraysParallelSortHelpers.FJByte.Sorter
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   471
                (null, a, new byte[n], 0, n, 0,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   472
                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   473
                 MIN_ARRAY_SORT_GRAN : g).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   474
    }
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
     * Sorts the specified range of the array into ascending numerical order.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   478
     * The range to be sorted extends from the index {@code fromIndex},
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   479
     * inclusive, to the index {@code toIndex}, exclusive. If
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   480
     * {@code fromIndex == toIndex}, the range to be sorted is empty.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   481
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   482
     * @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
   483
     * 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
   484
     * 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
   485
     * sorted using the appropriate {@link Arrays#sort(byte[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   486
     * 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
   487
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   488
     * Arrays#sort(byte[]) Arrays.sort} method. The algorithm requires a working
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   489
     * 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
   490
     * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   491
     * used to execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   492
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   493
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   494
     * @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
   495
     * @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
   496
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   497
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   498
     * @throws ArrayIndexOutOfBoundsException
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   499
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   500
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   501
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   502
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   503
    public static void parallelSort(byte[] a, int fromIndex, int toIndex) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   504
        rangeCheck(a.length, fromIndex, toIndex);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   505
        int n = toIndex - fromIndex, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   506
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   507
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   508
            DualPivotQuicksort.sort(a, fromIndex, toIndex - 1);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   509
        else
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   510
            new ArraysParallelSortHelpers.FJByte.Sorter
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   511
                (null, a, new byte[n], fromIndex, n, 0,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   512
                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   513
                 MIN_ARRAY_SORT_GRAN : g).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   514
    }
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   515
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
     * Sorts the specified array into ascending numerical order.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   518
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   519
     * @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
   520
     * 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
   521
     * 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
   522
     * sorted using the appropriate {@link Arrays#sort(char[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   523
     * 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
   524
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   525
     * Arrays#sort(char[]) Arrays.sort} method. The algorithm requires a
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   526
     * 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
   527
     * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   528
     * execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   529
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   530
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   531
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   532
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   533
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   534
    public static void parallelSort(char[] a) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   535
        int n = a.length, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   536
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   537
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   538
            DualPivotQuicksort.sort(a, 0, n - 1, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   539
        else
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   540
            new ArraysParallelSortHelpers.FJChar.Sorter
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   541
                (null, a, new char[n], 0, n, 0,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   542
                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   543
                 MIN_ARRAY_SORT_GRAN : g).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   544
    }
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
     * Sorts the specified range of the array into ascending numerical order.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   548
     * The range to be sorted extends from the index {@code fromIndex},
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   549
     * inclusive, to the index {@code toIndex}, exclusive. If
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   550
     * {@code fromIndex == toIndex}, the range to be sorted is empty.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   551
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   552
      @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
   553
     * 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
   554
     * 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
   555
     * sorted using the appropriate {@link Arrays#sort(char[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   556
     * 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
   557
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   558
     * Arrays#sort(char[]) Arrays.sort} method. The algorithm requires a working
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   559
     * 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
   560
     * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   561
     * used to execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   562
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   563
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   564
     * @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
   565
     * @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
   566
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   567
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   568
     * @throws ArrayIndexOutOfBoundsException
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   569
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   570
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   571
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   572
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   573
    public static void parallelSort(char[] a, int fromIndex, int toIndex) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   574
        rangeCheck(a.length, fromIndex, toIndex);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   575
        int n = toIndex - fromIndex, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   576
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   577
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   578
            DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   579
        else
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   580
            new ArraysParallelSortHelpers.FJChar.Sorter
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   581
                (null, a, new char[n], fromIndex, n, 0,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   582
                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   583
                 MIN_ARRAY_SORT_GRAN : g).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   584
    }
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   585
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
     * Sorts the specified array into ascending numerical order.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   588
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   589
     * @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
   590
     * 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
   591
     * 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
   592
     * sorted using the appropriate {@link Arrays#sort(short[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   593
     * 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
   594
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   595
     * Arrays#sort(short[]) Arrays.sort} method. The algorithm requires a
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   596
     * 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
   597
     * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   598
     * execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   599
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   600
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   601
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   602
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   603
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   604
    public static void parallelSort(short[] a) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   605
        int n = a.length, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   606
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   607
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   608
            DualPivotQuicksort.sort(a, 0, n - 1, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   609
        else
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   610
            new ArraysParallelSortHelpers.FJShort.Sorter
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   611
                (null, a, new short[n], 0, n, 0,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   612
                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   613
                 MIN_ARRAY_SORT_GRAN : g).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   614
    }
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
     * Sorts the specified range of the array into ascending numerical order.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   618
     * The range to be sorted extends from the index {@code fromIndex},
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   619
     * inclusive, to the index {@code toIndex}, exclusive. If
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   620
     * {@code fromIndex == toIndex}, the range to be sorted is empty.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   621
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   622
     * @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
   623
     * 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
   624
     * 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
   625
     * sorted using the appropriate {@link Arrays#sort(short[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   626
     * 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
   627
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   628
     * Arrays#sort(short[]) Arrays.sort} method. The algorithm requires a working
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   629
     * 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
   630
     * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   631
     * used to execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   632
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   633
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   634
     * @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
   635
     * @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
   636
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   637
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   638
     * @throws ArrayIndexOutOfBoundsException
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   639
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   640
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   641
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   642
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   643
    public static void parallelSort(short[] a, int fromIndex, int toIndex) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   644
        rangeCheck(a.length, fromIndex, toIndex);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   645
        int n = toIndex - fromIndex, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   646
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   647
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   648
            DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   649
        else
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   650
            new ArraysParallelSortHelpers.FJShort.Sorter
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   651
                (null, a, new short[n], fromIndex, n, 0,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   652
                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   653
                 MIN_ARRAY_SORT_GRAN : g).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   654
    }
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   655
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
     * Sorts the specified array into ascending numerical order.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   658
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   659
     * @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
   660
     * 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
   661
     * 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
   662
     * sorted using the appropriate {@link Arrays#sort(int[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   663
     * 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
   664
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   665
     * Arrays#sort(int[]) Arrays.sort} method. The algorithm requires a
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   666
     * 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
   667
     * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   668
     * execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   669
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   670
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   671
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   672
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   673
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   674
    public static void parallelSort(int[] a) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   675
        int n = a.length, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   676
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   677
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   678
            DualPivotQuicksort.sort(a, 0, n - 1, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   679
        else
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   680
            new ArraysParallelSortHelpers.FJInt.Sorter
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   681
                (null, a, new int[n], 0, n, 0,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   682
                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   683
                 MIN_ARRAY_SORT_GRAN : g).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   684
    }
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
     * Sorts the specified range of the array into ascending numerical order.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   688
     * The range to be sorted extends from the index {@code fromIndex},
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   689
     * inclusive, to the index {@code toIndex}, exclusive. If
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   690
     * {@code fromIndex == toIndex}, the range to be sorted is empty.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   691
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   692
     * @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
   693
     * 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
   694
     * 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
   695
     * sorted using the appropriate {@link Arrays#sort(int[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   696
     * 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
   697
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   698
     * Arrays#sort(int[]) Arrays.sort} method. The algorithm requires a working
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   699
     * 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
   700
     * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   701
     * used to execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   702
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   703
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   704
     * @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
   705
     * @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
   706
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   707
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   708
     * @throws ArrayIndexOutOfBoundsException
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   709
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   710
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   711
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   712
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   713
    public static void parallelSort(int[] a, int fromIndex, int toIndex) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   714
        rangeCheck(a.length, fromIndex, toIndex);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   715
        int n = toIndex - fromIndex, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   716
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   717
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   718
            DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   719
        else
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   720
            new ArraysParallelSortHelpers.FJInt.Sorter
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   721
                (null, a, new int[n], fromIndex, n, 0,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   722
                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   723
                 MIN_ARRAY_SORT_GRAN : g).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   724
    }
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   725
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
     * Sorts the specified array into ascending numerical order.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   728
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   729
     * @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
   730
     * 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
   731
     * 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
   732
     * sorted using the appropriate {@link Arrays#sort(long[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   733
     * 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
   734
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   735
     * Arrays#sort(long[]) Arrays.sort} method. The algorithm requires a
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   736
     * 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
   737
     * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   738
     * execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   739
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   740
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   741
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   742
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   743
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   744
    public static void parallelSort(long[] a) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   745
        int n = a.length, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   746
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   747
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   748
            DualPivotQuicksort.sort(a, 0, n - 1, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   749
        else
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   750
            new ArraysParallelSortHelpers.FJLong.Sorter
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   751
                (null, a, new long[n], 0, n, 0,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   752
                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   753
                 MIN_ARRAY_SORT_GRAN : g).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   754
    }
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
     * Sorts the specified range of the array into ascending numerical order.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   758
     * The range to be sorted extends from the index {@code fromIndex},
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   759
     * inclusive, to the index {@code toIndex}, exclusive. If
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   760
     * {@code fromIndex == toIndex}, the range to be sorted is empty.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   761
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   762
     * @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
   763
     * 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
   764
     * 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
   765
     * sorted using the appropriate {@link Arrays#sort(long[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   766
     * 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
   767
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   768
     * Arrays#sort(long[]) Arrays.sort} method. The algorithm requires a working
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   769
     * 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
   770
     * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   771
     * used to execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   772
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   773
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   774
     * @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
   775
     * @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
   776
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   777
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   778
     * @throws ArrayIndexOutOfBoundsException
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   779
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   780
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   781
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   782
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   783
    public static void parallelSort(long[] a, int fromIndex, int toIndex) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   784
        rangeCheck(a.length, fromIndex, toIndex);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   785
        int n = toIndex - fromIndex, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   786
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   787
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   788
            DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   789
        else
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   790
            new ArraysParallelSortHelpers.FJLong.Sorter
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   791
                (null, a, new long[n], fromIndex, n, 0,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   792
                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   793
                 MIN_ARRAY_SORT_GRAN : g).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   794
    }
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   795
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
     * Sorts the specified array into ascending numerical order.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   798
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   799
     * <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
   800
     * 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
   801
     * 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
   802
     * 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
   803
     * {@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
   804
     * {@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
   805
     * other value and all {@code Float.NaN} values are considered equal.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   806
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   807
     * @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
   808
     * 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
   809
     * 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
   810
     * sorted using the appropriate {@link Arrays#sort(float[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   811
     * 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
   812
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   813
     * Arrays#sort(float[]) Arrays.sort} method. The algorithm requires a
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   814
     * 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
   815
     * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   816
     * execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   817
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   818
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   819
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   820
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   821
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   822
    public static void parallelSort(float[] a) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   823
        int n = a.length, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   824
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   825
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   826
            DualPivotQuicksort.sort(a, 0, n - 1, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   827
        else
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   828
            new ArraysParallelSortHelpers.FJFloat.Sorter
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   829
                (null, a, new float[n], 0, n, 0,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   830
                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   831
                 MIN_ARRAY_SORT_GRAN : g).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   832
    }
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
     * Sorts the specified range of the array into ascending numerical order.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   836
     * The range to be sorted extends from the index {@code fromIndex},
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   837
     * inclusive, to the index {@code toIndex}, exclusive. If
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   838
     * {@code fromIndex == toIndex}, the range to be sorted is empty.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   839
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   840
     * <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
   841
     * 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
   842
     * 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
   843
     * 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
   844
     * {@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
   845
     * {@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
   846
     * other value and all {@code Float.NaN} values are considered equal.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   847
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   848
     * @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
   849
     * 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
   850
     * 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
   851
     * sorted using the appropriate {@link Arrays#sort(float[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   852
     * 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
   853
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   854
     * Arrays#sort(float[]) Arrays.sort} method. The algorithm requires a working
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   855
     * 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
   856
     * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   857
     * used to execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   858
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   859
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   860
     * @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
   861
     * @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
   862
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   863
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   864
     * @throws ArrayIndexOutOfBoundsException
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   865
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   866
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   867
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   868
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   869
    public static void parallelSort(float[] a, int fromIndex, int toIndex) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   870
        rangeCheck(a.length, fromIndex, toIndex);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   871
        int n = toIndex - fromIndex, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   872
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   873
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   874
            DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   875
        else
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   876
            new ArraysParallelSortHelpers.FJFloat.Sorter
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   877
                (null, a, new float[n], fromIndex, n, 0,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   878
                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   879
                 MIN_ARRAY_SORT_GRAN : g).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   880
    }
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   881
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
     * Sorts the specified array into ascending numerical order.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   884
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   885
     * <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
   886
     * 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
   887
     * 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
   888
     * 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
   889
     * {@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
   890
     * {@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
   891
     * other value and all {@code Double.NaN} values are considered equal.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   892
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   893
     * @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
   894
     * 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
   895
     * 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
   896
     * sorted using the appropriate {@link Arrays#sort(double[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   897
     * 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
   898
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   899
     * Arrays#sort(double[]) Arrays.sort} method. The algorithm requires a
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   900
     * 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
   901
     * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   902
     * execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   903
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   904
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   905
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   906
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   907
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   908
    public static void parallelSort(double[] a) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   909
        int n = a.length, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   910
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   911
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   912
            DualPivotQuicksort.sort(a, 0, n - 1, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   913
        else
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   914
            new ArraysParallelSortHelpers.FJDouble.Sorter
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   915
                (null, a, new double[n], 0, n, 0,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   916
                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   917
                 MIN_ARRAY_SORT_GRAN : g).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   918
    }
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
     * Sorts the specified range of the array into ascending numerical order.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   922
     * The range to be sorted extends from the index {@code fromIndex},
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   923
     * inclusive, to the index {@code toIndex}, exclusive. If
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   924
     * {@code fromIndex == toIndex}, the range to be sorted is empty.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   925
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   926
     * <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
   927
     * 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
   928
     * 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
   929
     * 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
   930
     * {@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
   931
     * {@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
   932
     * other value and all {@code Double.NaN} values are considered equal.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   933
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   934
     * @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
   935
     * 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
   936
     * 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
   937
     * sorted using the appropriate {@link Arrays#sort(double[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   938
     * 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
   939
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   940
     * Arrays#sort(double[]) Arrays.sort} method. The algorithm requires a working
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   941
     * 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
   942
     * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   943
     * used to execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   944
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   945
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   946
     * @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
   947
     * @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
   948
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   949
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   950
     * @throws ArrayIndexOutOfBoundsException
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   951
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   952
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   953
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   954
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   955
    public static void parallelSort(double[] a, int fromIndex, int toIndex) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   956
        rangeCheck(a.length, fromIndex, toIndex);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   957
        int n = toIndex - fromIndex, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   958
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   959
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   960
            DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   961
        else
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   962
            new ArraysParallelSortHelpers.FJDouble.Sorter
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   963
                (null, a, new double[n], fromIndex, n, 0,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   964
                 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   965
                 MIN_ARRAY_SORT_GRAN : g).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   966
    }
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
     * Sorts the specified array of objects into ascending order, according
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   970
     * to the {@linkplain Comparable natural ordering} of its elements.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   971
     * All elements in the array must implement the {@link Comparable}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   972
     * interface.  Furthermore, all elements in the array must be
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   973
     * <i>mutually comparable</i> (that is, {@code e1.compareTo(e2)} must
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   974
     * not throw a {@code ClassCastException} for any elements {@code e1}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   975
     * and {@code e2} in the array).
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   976
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   977
     * <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
   978
     * not be reordered as a result of the sort.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   979
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   980
     * @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
   981
     * 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
   982
     * 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
   983
     * sorted using the appropriate {@link Arrays#sort(Object[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   984
     * 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
   985
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   986
     * Arrays#sort(Object[]) Arrays.sort} method. The algorithm requires a
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   987
     * 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
   988
     * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   989
     * execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   990
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
   991
     * @param <T> the class of the objects to be sorted
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   992
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   993
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   994
     * @throws ClassCastException if the array contains elements that are not
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   995
     *         <i>mutually comparable</i> (for example, strings and integers)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   996
     * @throws IllegalArgumentException (optional) if the natural
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   997
     *         ordering of the array elements is found to violate the
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   998
     *         {@link Comparable} contract
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
   999
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1000
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1001
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1002
    @SuppressWarnings("unchecked")
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1003
    public static <T extends Comparable<? super T>> void parallelSort(T[] a) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1004
        int n = a.length, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1005
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1006
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1007
            TimSort.sort(a, 0, n, NaturalOrder.INSTANCE, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1008
        else
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 21278
diff changeset
  1009
            new ArraysParallelSortHelpers.FJObject.Sorter<>
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1010
                (null, a,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1011
                 (T[])Array.newInstance(a.getClass().getComponentType(), n),
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1012
                 0, n, 0, ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1013
                 MIN_ARRAY_SORT_GRAN : g, NaturalOrder.INSTANCE).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1014
    }
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
     * Sorts the specified range of the specified array of objects into
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1018
     * ascending order, according to the
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1019
     * {@linkplain Comparable natural ordering} of its
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1020
     * elements.  The range to be sorted extends from index
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1021
     * {@code fromIndex}, inclusive, to index {@code toIndex}, exclusive.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1022
     * (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
  1023
     * elements in this range must implement the {@link Comparable}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1024
     * interface.  Furthermore, all elements in this range must be <i>mutually
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1025
     * 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
  1026
     * {@code ClassCastException} for any elements {@code e1} and
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1027
     * {@code e2} in the array).
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1028
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1029
     * <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
  1030
     * not be reordered as a result of the sort.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1031
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1032
     * @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
  1033
     * 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
  1034
     * 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
  1035
     * sorted using the appropriate {@link Arrays#sort(Object[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1036
     * 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
  1037
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1038
     * Arrays#sort(Object[]) Arrays.sort} method. The algorithm requires a working
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1039
     * 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
  1040
     * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1041
     * used to execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1042
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  1043
     * @param <T> the class of the objects to be sorted
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1044
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1045
     * @param fromIndex the index of the first element (inclusive) to be
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1046
     *        sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1047
     * @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
  1048
     * @throws IllegalArgumentException if {@code fromIndex > toIndex} or
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1049
     *         (optional) if the natural ordering of the array elements is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1050
     *         found to violate the {@link Comparable} contract
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1051
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1052
     *         {@code toIndex > a.length}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1053
     * @throws ClassCastException if the array contains elements that are
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1054
     *         not <i>mutually comparable</i> (for example, strings and
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1055
     *         integers).
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1056
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1057
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1058
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1059
    @SuppressWarnings("unchecked")
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1060
    public static <T extends Comparable<? super T>>
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1061
    void parallelSort(T[] a, int fromIndex, int toIndex) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1062
        rangeCheck(a.length, fromIndex, toIndex);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1063
        int n = toIndex - fromIndex, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1064
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1065
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1066
            TimSort.sort(a, fromIndex, toIndex, NaturalOrder.INSTANCE, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1067
        else
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 21278
diff changeset
  1068
            new ArraysParallelSortHelpers.FJObject.Sorter<>
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1069
                (null, a,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1070
                 (T[])Array.newInstance(a.getClass().getComponentType(), n),
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1071
                 fromIndex, n, 0, ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1072
                 MIN_ARRAY_SORT_GRAN : g, NaturalOrder.INSTANCE).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1073
    }
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
     * 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
  1077
     * the specified comparator.  All elements in the array must be
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1078
     * <i>mutually comparable</i> by the specified comparator (that is,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1079
     * {@code c.compare(e1, e2)} must not throw a {@code ClassCastException}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1080
     * for any elements {@code e1} and {@code e2} in the array).
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1081
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1082
     * <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
  1083
     * not be reordered as a result of the sort.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1084
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1085
     * @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
  1086
     * 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
  1087
     * 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
  1088
     * sorted using the appropriate {@link Arrays#sort(Object[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1089
     * 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
  1090
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1091
     * Arrays#sort(Object[]) Arrays.sort} method. The algorithm requires a
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1092
     * 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
  1093
     * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1094
     * execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1095
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  1096
     * @param <T> the class of the objects to be sorted
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1097
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1098
     * @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
  1099
     *        {@code null} value indicates that the elements'
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1100
     *        {@linkplain Comparable natural ordering} should be used.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1101
     * @throws ClassCastException if the array contains elements that are
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1102
     *         not <i>mutually comparable</i> using the specified comparator
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1103
     * @throws IllegalArgumentException (optional) if the comparator is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1104
     *         found to violate the {@link java.util.Comparator} contract
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1105
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1106
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1107
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1108
    @SuppressWarnings("unchecked")
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1109
    public static <T> void parallelSort(T[] a, Comparator<? super T> cmp) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1110
        if (cmp == null)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1111
            cmp = NaturalOrder.INSTANCE;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1112
        int n = a.length, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1113
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1114
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1115
            TimSort.sort(a, 0, n, cmp, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1116
        else
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 21278
diff changeset
  1117
            new ArraysParallelSortHelpers.FJObject.Sorter<>
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1118
                (null, a,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1119
                 (T[])Array.newInstance(a.getClass().getComponentType(), n),
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1120
                 0, n, 0, ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1121
                 MIN_ARRAY_SORT_GRAN : g, cmp).invoke();
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1122
    }
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
     * Sorts the specified range of the specified array of objects according
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1126
     * 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
  1127
     * sorted extends from index {@code fromIndex}, inclusive, to index
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1128
     * {@code toIndex}, exclusive.  (If {@code fromIndex==toIndex}, the
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1129
     * 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
  1130
     * <i>mutually comparable</i> by the specified comparator (that is,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1131
     * {@code c.compare(e1, e2)} must not throw a {@code ClassCastException}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1132
     * for any elements {@code e1} and {@code e2} in the range).
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1133
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1134
     * <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
  1135
     * not be reordered as a result of the sort.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1136
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1137
     * @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
  1138
     * 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
  1139
     * 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
  1140
     * sorted using the appropriate {@link Arrays#sort(Object[]) Arrays.sort}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1141
     * 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
  1142
     * granularity, then it is sorted using the appropriate {@link
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1143
     * Arrays#sort(Object[]) Arrays.sort} method. The algorithm requires a working
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1144
     * 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
  1145
     * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1146
     * used to execute any parallel tasks.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1147
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  1148
     * @param <T> the class of the objects to be sorted
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1149
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1150
     * @param fromIndex the index of the first element (inclusive) to be
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1151
     *        sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1152
     * @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
  1153
     * @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
  1154
     *        {@code null} value indicates that the elements'
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1155
     *        {@linkplain Comparable natural ordering} should be used.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1156
     * @throws IllegalArgumentException if {@code fromIndex > toIndex} or
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1157
     *         (optional) if the natural ordering of the array elements is
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1158
     *         found to violate the {@link Comparable} contract
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1159
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1160
     *         {@code toIndex > a.length}
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1161
     * @throws ClassCastException if the array contains elements that are
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1162
     *         not <i>mutually comparable</i> (for example, strings and
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1163
     *         integers).
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1164
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1165
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1166
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1167
    @SuppressWarnings("unchecked")
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1168
    public static <T> void parallelSort(T[] a, int fromIndex, int toIndex,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1169
                                        Comparator<? super T> cmp) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1170
        rangeCheck(a.length, fromIndex, toIndex);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1171
        if (cmp == null)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1172
            cmp = NaturalOrder.INSTANCE;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1173
        int n = toIndex - fromIndex, p, g;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1174
        if (n <= MIN_ARRAY_SORT_GRAN ||
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1175
            (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1176
            TimSort.sort(a, fromIndex, toIndex, cmp, null, 0, 0);
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1177
        else
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 21278
diff changeset
  1178
            new ArraysParallelSortHelpers.FJObject.Sorter<>
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1179
                (null, a,
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1180
                 (T[])Array.newInstance(a.getClass().getComponentType(), n),
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1181
                 fromIndex, n, 0, ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1182
                 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
  1183
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
4233
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
  1185
    /*
9b594a48d0f4 6899694: Dual-pivot quicksort improvements
alanb
parents: 4170
diff changeset
  1186
     * Sorting of complex type arrays.
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
  1187
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1189
    /**
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1190
     * 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
  1191
     * 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
  1192
     * 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
  1193
     * 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
  1194
     */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1195
    static final class LegacyMergeSort {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1196
        private static final boolean userRequested =
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1197
            java.security.AccessController.doPrivileged(
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1198
                new sun.security.action.GetBooleanAction(
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1199
                    "java.util.Arrays.useLegacyMergeSort")).booleanValue();
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1200
    }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1201
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
    /**
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1203
     * 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
  1204
     * 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
  1205
     * 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
  1206
     * 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
  1207
     * <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
  1208
     * 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
  1209
     * and {@code e2} in the array).
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1210
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1211
     * <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
  1212
     * not be reordered as a result of the sort.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
     *
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1214
     * <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
  1215
     * 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
  1216
     * 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
  1217
     * 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
  1218
     * 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
  1219
     * implementation requires approximately n comparisons.  Temporary
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1220
     * 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
  1221
     * 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
  1222
     * arrays.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
     *
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1224
     * <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
  1225
     * 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
  1226
     * 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
  1227
     * 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
  1228
     * 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
  1229
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1230
     * <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
  1231
     * (<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
  1232
     * 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
  1233
     * 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
  1234
     * 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
  1235
     * January 1993.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
     * @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
  1238
     * @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
  1239
     *         <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
  1240
     * @throws IllegalArgumentException (optional) if the natural
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1241
     *         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
  1242
     *         {@link Comparable} contract
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
    public static void sort(Object[] a) {
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1245
        if (LegacyMergeSort.userRequested)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1246
            legacyMergeSort(a);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1247
        else
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1248
            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
  1249
    }
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
    /** To be removed in a future release. */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1252
    private static void legacyMergeSort(Object[] a) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1253
        Object[] aux = a.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
        mergeSort(aux, a, 0, a.length, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
     * Sorts the specified range of the specified array of objects into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
     * ascending order, according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
     * {@linkplain Comparable natural ordering} of its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
     * 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
  1262
     * {@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
  1263
     * (If {@code fromIndex==toIndex}, the range to be sorted is empty.)  All
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
     * elements in this range must implement the {@link Comparable}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
     * 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
  1266
     * 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
  1267
     * {@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
  1268
     * {@code e2} in the array).
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1269
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1270
     * <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
  1271
     * not be reordered as a result of the sort.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
     *
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1273
     * <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
  1274
     * 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
  1275
     * 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
  1276
     * 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
  1277
     * 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
  1278
     * implementation requires approximately n comparisons.  Temporary
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1279
     * 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
  1280
     * 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
  1281
     * arrays.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
     *
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1283
     * <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
  1284
     * 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
  1285
     * 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
  1286
     * 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
  1287
     * 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
  1288
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1289
     * <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
  1290
     * (<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
  1291
     * 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
  1292
     * 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
  1293
     * 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
  1294
     * January 1993.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
     * @param a the array to be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
     *        sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
     * @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
  1300
     * @throws IllegalArgumentException if {@code fromIndex > toIndex} or
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1301
     *         (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
  1302
     *         found to violate the {@link Comparable} contract
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1303
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1304
     *         {@code toIndex > a.length}
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1305
     * @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
  1306
     *         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
  1307
     *         integers).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
    public static void sort(Object[] a, int fromIndex, int toIndex) {
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1310
        rangeCheck(a.length, fromIndex, toIndex);
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1311
        if (LegacyMergeSort.userRequested)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1312
            legacyMergeSort(a, fromIndex, toIndex);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1313
        else
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 17195
diff changeset
  1314
            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
  1315
    }
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
    /** To be removed in a future release. */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1318
    private static void legacyMergeSort(Object[] a,
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1319
                                        int fromIndex, int toIndex) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
        Object[] aux = copyOfRange(a, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
        mergeSort(aux, a, fromIndex, toIndex, -fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
     * 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
  1326
     * used in preference to mergesort.
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1327
     * To be removed in a future release.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
    private static final int INSERTIONSORT_THRESHOLD = 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
     * Src is the source array that starts at index 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
     * Dest is the (possibly larger) array destination with a possible offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
     * low is the index in dest to start sorting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
     * high is the end index in dest to end sorting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
     * 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
  1337
     * To be removed in a future release.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
     */
13795
73850c397272 7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents: 12448
diff changeset
  1339
    @SuppressWarnings({"unchecked", "rawtypes"})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
    private static void mergeSort(Object[] src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
                                  Object[] dest,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
                                  int low,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
                                  int high,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
                                  int off) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
        int length = high - low;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
        // Insertion sort on smallest arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
        if (length < INSERTIONSORT_THRESHOLD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
            for (int i=low; i<high; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
                for (int j=i; j>low &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
                         ((Comparable) dest[j-1]).compareTo(dest[j])>0; j--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
                    swap(dest, j, j-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
        // Recursively sort halves of dest into src
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
        int destLow  = low;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
        int destHigh = high;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
        low  += off;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
        high += off;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
        int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
        mergeSort(dest, src, low, mid, -off);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
        mergeSort(dest, src, mid, high, -off);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
        // If list is already sorted, just copy from src to dest.  This is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
        // optimization that results in faster sorts for nearly ordered lists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
        if (((Comparable)src[mid-1]).compareTo(src[mid]) <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
            System.arraycopy(src, low, dest, destLow, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
        // Merge sorted halves (now in src) into dest
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
        for(int i = destLow, p = low, q = mid; i < destHigh; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
            if (q >= high || p < mid && ((Comparable)src[p]).compareTo(src[q])<=0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
                dest[i] = src[p++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
                dest[i] = src[q++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
        }
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
     * Swaps x[a] with x[b].
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
    private static void swap(Object[] x, int a, int b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
        Object t = x[a];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
        x[a] = x[b];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
        x[b] = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
     * Sorts the specified array of objects according to the order induced by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
     * the specified comparator.  All elements in the array must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
     * <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
  1394
     * {@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
  1395
     * 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
  1396
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1397
     * <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
  1398
     * not be reordered as a result of the sort.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
     *
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1400
     * <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
  1401
     * 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
  1402
     * 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
  1403
     * 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
  1404
     * 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
  1405
     * implementation requires approximately n comparisons.  Temporary
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1406
     * 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
  1407
     * 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
  1408
     * arrays.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
     *
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1410
     * <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
  1411
     * 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
  1412
     * 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
  1413
     * 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
  1414
     * 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
  1415
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1416
     * <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
  1417
     * (<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
  1418
     * 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
  1419
     * 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
  1420
     * 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
  1421
     * January 1993.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  1423
     * @param <T> the class of the objects to be sorted
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
     * @param a the array to be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
     * @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
  1426
     *        {@code null} value indicates that the elements'
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
     *        {@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
  1428
     * @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
  1429
     *         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
  1430
     * @throws IllegalArgumentException (optional) if the comparator is
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1431
     *         found to violate the {@link Comparator} contract
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
    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
  1434
        if (c == null) {
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1435
            sort(a);
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1436
        } else {
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1437
            if (LegacyMergeSort.userRequested)
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1438
                legacyMergeSort(a, c);
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1439
            else
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1440
                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
  1441
        }
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1442
    }
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
    /** To be removed in a future release. */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1445
    private static <T> void legacyMergeSort(T[] a, Comparator<? super T> c) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1446
        T[] aux = a.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
        if (c==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
            mergeSort(aux, a, 0, a.length, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
            mergeSort(aux, a, 0, a.length, 0, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
     * Sorts the specified range of the specified array of objects according
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
     * 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
  1456
     * 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
  1457
     * {@code toIndex}, exclusive.  (If {@code fromIndex==toIndex}, the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
     * range to be sorted is empty.)  All elements in the range must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
     * <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
  1460
     * {@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
  1461
     * 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
  1462
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1463
     * <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
  1464
     * not be reordered as a result of the sort.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
     *
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1466
     * <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
  1467
     * 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
  1468
     * 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
  1469
     * 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
  1470
     * 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
  1471
     * implementation requires approximately n comparisons.  Temporary
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1472
     * 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
  1473
     * 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
  1474
     * arrays.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
     *
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1476
     * <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
  1477
     * 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
  1478
     * 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
  1479
     * 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
  1480
     * 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
  1481
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1482
     * <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
  1483
     * (<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
  1484
     * 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
  1485
     * 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
  1486
     * 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
  1487
     * January 1993.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  1489
     * @param <T> the class of the objects to be sorted
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
     * @param a the array to be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
     *        sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
     * @param toIndex the index of the last element (exclusive) to be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
     * @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
  1495
     *        {@code null} value indicates that the elements'
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
     *        {@linkplain Comparable natural ordering} should be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
     * @throws ClassCastException if the array contains elements that are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
     *         <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
  1499
     * @throws IllegalArgumentException if {@code fromIndex > toIndex} or
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1500
     *         (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
  1501
     *         {@link Comparator} contract
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1502
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1503
     *         {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
    public static <T> void sort(T[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
                                Comparator<? super T> c) {
22285
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1507
        if (c == null) {
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1508
            sort(a, fromIndex, toIndex);
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1509
        } else {
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1510
            rangeCheck(a.length, fromIndex, toIndex);
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1511
            if (LegacyMergeSort.userRequested)
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1512
                legacyMergeSort(a, fromIndex, toIndex, c);
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1513
            else
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  1514
                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
  1515
        }
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1516
    }
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
    /** To be removed in a future release. */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 715
diff changeset
  1519
    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
  1520
                                            Comparator<? super T> c) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1521
        T[] aux = copyOfRange(a, fromIndex, toIndex);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
        if (c==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
            mergeSort(aux, a, fromIndex, toIndex, -fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
            mergeSort(aux, a, fromIndex, toIndex, -fromIndex, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
     * Src is the source array that starts at index 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
     * Dest is the (possibly larger) array destination with a possible offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
     * low is the index in dest to start sorting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
     * high is the end index in dest to end sorting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
     * 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
  1534
     * To be removed in a future release.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
     */
13795
73850c397272 7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents: 12448
diff changeset
  1536
    @SuppressWarnings({"rawtypes", "unchecked"})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
    private static void mergeSort(Object[] src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
                                  Object[] dest,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
                                  int low, int high, int off,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
                                  Comparator c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
        int length = high - low;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
        // Insertion sort on smallest arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
        if (length < INSERTIONSORT_THRESHOLD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
            for (int i=low; i<high; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
                for (int j=i; j>low && c.compare(dest[j-1], dest[j])>0; j--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
                    swap(dest, j, j-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
        // Recursively sort halves of dest into src
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
        int destLow  = low;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
        int destHigh = high;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
        low  += off;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
        high += off;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
        int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
        mergeSort(dest, src, low, mid, -off, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
        mergeSort(dest, src, mid, high, -off, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
        // If list is already sorted, just copy from src to dest.  This is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
        // optimization that results in faster sorts for nearly ordered lists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
        if (c.compare(src[mid-1], src[mid]) <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
           System.arraycopy(src, low, dest, destLow, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
           return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
        // Merge sorted halves (now in src) into dest
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
        for(int i = destLow, p = low, q = mid; i < destHigh; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
            if (q >= high || p < mid && c.compare(src[p], src[q]) <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
                dest[i] = src[p++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
                dest[i] = src[q++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
18555
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1576
    // Parallel prefix
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1577
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
     * 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
  1580
     * 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
  1581
     * 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
  1582
     * 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
  1583
     * Parallel prefix computation is usually more efficient than
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1584
     * sequential loops for large arrays.
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1585
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  1586
     * @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
  1587
     * @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
  1588
     * @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
  1589
     * cumulation
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1590
     * @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
  1591
     * @since 1.8
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1592
     */
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1593
    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
  1594
        Objects.requireNonNull(op);
18555
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1595
        if (array.length > 0)
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1596
            new ArrayPrefixHelpers.CumulateTask<>
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1597
                    (null, op, array, 0, array.length).invoke();
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1598
    }
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
     * Performs {@link #parallelPrefix(Object[], BinaryOperator)}
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1602
     * for the given subrange of the array.
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1603
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  1604
     * @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
  1605
     * @param array the array
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1606
     * @param fromIndex the index of the first element, inclusive
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1607
     * @param toIndex the index of the last element, exclusive
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1608
     * @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
  1609
     * cumulation
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1610
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1611
     * @throws ArrayIndexOutOfBoundsException
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1612
     *     if {@code fromIndex < 0} or {@code toIndex > array.length}
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1613
     * @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
  1614
     * @since 1.8
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1615
     */
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1616
    public static <T> void parallelPrefix(T[] array, int fromIndex,
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1617
                                          int toIndex, BinaryOperator<T> op) {
20492
11418b1f61ff 8025067: Unconditionally throw NPE if null op provided to Arrays.parallelPrefix
mduigou
parents: 18829
diff changeset
  1618
        Objects.requireNonNull(op);
18555
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1619
        rangeCheck(array.length, fromIndex, toIndex);
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1620
        if (fromIndex < toIndex)
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1621
            new ArrayPrefixHelpers.CumulateTask<>
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1622
                    (null, op, array, fromIndex, toIndex).invoke();
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1623
    }
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
     * 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
  1627
     * 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
  1628
     * 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
  1629
     * 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
  1630
     * Parallel prefix computation is usually more efficient than
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1631
     * sequential loops for large arrays.
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1632
     *
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1633
     * @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
  1634
     * @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
  1635
     * cumulation
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1636
     * @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
  1637
     * @since 1.8
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1638
     */
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1639
    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
  1640
        Objects.requireNonNull(op);
18555
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1641
        if (array.length > 0)
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1642
            new ArrayPrefixHelpers.LongCumulateTask
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1643
                    (null, op, array, 0, array.length).invoke();
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1644
    }
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
     * Performs {@link #parallelPrefix(long[], LongBinaryOperator)}
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1648
     * for the given subrange of the array.
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1649
     *
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1650
     * @param array the array
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1651
     * @param fromIndex the index of the first element, inclusive
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1652
     * @param toIndex the index of the last element, exclusive
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1653
     * @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
  1654
     * cumulation
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1655
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1656
     * @throws ArrayIndexOutOfBoundsException
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1657
     *     if {@code fromIndex < 0} or {@code toIndex > array.length}
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1658
     * @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
  1659
     * @since 1.8
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1660
     */
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1661
    public static void parallelPrefix(long[] array, int fromIndex,
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1662
                                      int toIndex, LongBinaryOperator op) {
20492
11418b1f61ff 8025067: Unconditionally throw NPE if null op provided to Arrays.parallelPrefix
mduigou
parents: 18829
diff changeset
  1663
        Objects.requireNonNull(op);
18555
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1664
        rangeCheck(array.length, fromIndex, toIndex);
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1665
        if (fromIndex < toIndex)
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1666
            new ArrayPrefixHelpers.LongCumulateTask
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1667
                    (null, op, array, fromIndex, toIndex).invoke();
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1668
    }
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
     * 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
  1672
     * 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
  1673
     * 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
  1674
     * 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
  1675
     * Parallel prefix computation is usually more efficient than
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1676
     * sequential loops for large arrays.
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1677
     *
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1678
     * <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
  1679
     * 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
  1680
     * obtained if the operation was performed sequentially.
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1681
     *
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1682
     * @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
  1683
     * @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
  1684
     * @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
  1685
     * @since 1.8
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1686
     */
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1687
    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
  1688
        Objects.requireNonNull(op);
18555
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1689
        if (array.length > 0)
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1690
            new ArrayPrefixHelpers.DoubleCumulateTask
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1691
                    (null, op, array, 0, array.length).invoke();
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1692
    }
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
     * Performs {@link #parallelPrefix(double[], DoubleBinaryOperator)}
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1696
     * for the given subrange of the array.
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1697
     *
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1698
     * @param array the array
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1699
     * @param fromIndex the index of the first element, inclusive
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1700
     * @param toIndex the index of the last element, exclusive
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1701
     * @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
  1702
     * cumulation
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1703
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1704
     * @throws ArrayIndexOutOfBoundsException
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1705
     *     if {@code fromIndex < 0} or {@code toIndex > array.length}
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1706
     * @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
  1707
     * @since 1.8
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1708
     */
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1709
    public static void parallelPrefix(double[] array, int fromIndex,
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1710
                                      int toIndex, DoubleBinaryOperator op) {
20492
11418b1f61ff 8025067: Unconditionally throw NPE if null op provided to Arrays.parallelPrefix
mduigou
parents: 18829
diff changeset
  1711
        Objects.requireNonNull(op);
18555
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1712
        rangeCheck(array.length, fromIndex, toIndex);
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1713
        if (fromIndex < toIndex)
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1714
            new ArrayPrefixHelpers.DoubleCumulateTask
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1715
                    (null, op, array, fromIndex, toIndex).invoke();
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1716
    }
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
     * 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
  1720
     * 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
  1721
     * 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
  1722
     * 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
  1723
     * Parallel prefix computation is usually more efficient than
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1724
     * sequential loops for large arrays.
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1725
     *
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1726
     * @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
  1727
     * @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
  1728
     * cumulation
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1729
     * @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
  1730
     * @since 1.8
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1731
     */
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1732
    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
  1733
        Objects.requireNonNull(op);
18555
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1734
        if (array.length > 0)
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1735
            new ArrayPrefixHelpers.IntCumulateTask
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1736
                    (null, op, array, 0, array.length).invoke();
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1737
    }
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
     * Performs {@link #parallelPrefix(int[], IntBinaryOperator)}
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1741
     * for the given subrange of the array.
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1742
     *
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1743
     * @param array the array
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1744
     * @param fromIndex the index of the first element, inclusive
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1745
     * @param toIndex the index of the last element, exclusive
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1746
     * @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
  1747
     * cumulation
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1748
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1749
     * @throws ArrayIndexOutOfBoundsException
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1750
     *     if {@code fromIndex < 0} or {@code toIndex > array.length}
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1751
     * @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
  1752
     * @since 1.8
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1753
     */
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1754
    public static void parallelPrefix(int[] array, int fromIndex,
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1755
                                      int toIndex, IntBinaryOperator op) {
20492
11418b1f61ff 8025067: Unconditionally throw NPE if null op provided to Arrays.parallelPrefix
mduigou
parents: 18829
diff changeset
  1756
        Objects.requireNonNull(op);
18555
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1757
        rangeCheck(array.length, fromIndex, toIndex);
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1758
        if (fromIndex < toIndex)
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1759
            new ArrayPrefixHelpers.IntCumulateTask
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1760
                    (null, op, array, fromIndex, toIndex).invoke();
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1761
    }
d69e2a644e38 8012647: Add Arrays.parallelPrefix (prefix sum, scan, cumulative sum)
chegar
parents: 17712
diff changeset
  1762
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
    // Searching
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
     * Searches the specified array of longs for the specified value using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
     * binary search algorithm.  The array must be sorted (as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
     * by the {@link #sort(long[])} method) prior to making this call.  If it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
     * is not sorted, the results are undefined.  If the array contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
     * @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
  1776
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
     *         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
  1779
     *         element greater than the key, or {@code a.length} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
     *         elements in the array are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
    public static int binarySearch(long[] a, long key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
        return binarySearch0(a, 0, a.length, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
     * Searches a range of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
     * the specified array of longs for the specified value using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
     * binary search algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
     * The range must be sorted (as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
     * by the {@link #sort(long[], int, int)} method)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
     * prior to making this call.  If it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
     * is not sorted, the results are undefined.  If the range contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
     *          searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
     * @param toIndex the index of the last element (exclusive) to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
     * @return index of the search key, if it is contained in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
     *         within the specified range;
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  1806
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
     *         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
  1810
     *         or {@code toIndex} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
     *         elements in the range are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
     *         if {@code fromIndex > toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
     * @throws ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
     *         if {@code fromIndex < 0 or toIndex > a.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
    public static int binarySearch(long[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
                                   long key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
        return binarySearch0(a, fromIndex, toIndex, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
    // Like public version, but without range checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
    private static int binarySearch0(long[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
                                     long key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
        int low = fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
        int high = toIndex - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
            int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
            long midVal = a[mid];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
            if (midVal < key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
                low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
            else if (midVal > key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
                high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
                return mid; // key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
        return -(low + 1);  // key not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
     * Searches the specified array of ints for the specified value using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
     * binary search algorithm.  The array must be sorted (as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
     * by the {@link #sort(int[])} method) prior to making this call.  If it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
     * is not sorted, the results are undefined.  If the array contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
     * @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
  1857
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
     *         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
  1860
     *         element greater than the key, or {@code a.length} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
     *         elements in the array are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
    public static int binarySearch(int[] a, int key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
        return binarySearch0(a, 0, a.length, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
     * Searches a range of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
     * the specified array of ints for the specified value using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
     * binary search algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
     * The range must be sorted (as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
     * by the {@link #sort(int[], int, int)} method)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
     * prior to making this call.  If it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
     * is not sorted, the results are undefined.  If the range contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
     *          searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
     * @param toIndex the index of the last element (exclusive) to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
     * @return index of the search key, if it is contained in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
     *         within the specified range;
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  1887
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
     *         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
  1891
     *         or {@code toIndex} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
     *         elements in the range are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
     *         if {@code fromIndex > toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
     * @throws ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
     *         if {@code fromIndex < 0 or toIndex > a.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
    public static int binarySearch(int[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
                                   int key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
        return binarySearch0(a, fromIndex, toIndex, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
    // Like public version, but without range checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
    private static int binarySearch0(int[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
                                     int key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
        int low = fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
        int high = toIndex - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
            int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
            int midVal = a[mid];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
            if (midVal < key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
                low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
            else if (midVal > key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
                high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
                return mid; // key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
        return -(low + 1);  // key not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
     * Searches the specified array of shorts for the specified value using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
     * the binary search algorithm.  The array must be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
     * (as by the {@link #sort(short[])} method) prior to making this call.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
     * it is not sorted, the results are undefined.  If the array contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
     * @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
  1938
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
     *         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
  1941
     *         element greater than the key, or {@code a.length} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
     *         elements in the array are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
    public static int binarySearch(short[] a, short key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
        return binarySearch0(a, 0, a.length, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
     * Searches a range of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
     * the specified array of shorts for the specified value using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
     * the binary search algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
     * The range must be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
     * (as by the {@link #sort(short[], int, int)} method)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
     * prior to making this call.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
     * it is not sorted, the results are undefined.  If the range contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
     *          searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
     * @param toIndex the index of the last element (exclusive) to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
     * @return index of the search key, if it is contained in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
     *         within the specified range;
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  1968
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
     *         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
  1972
     *         or {@code toIndex} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
     *         elements in the range are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
     *         if {@code fromIndex > toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
     * @throws ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
     *         if {@code fromIndex < 0 or toIndex > a.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
    public static int binarySearch(short[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
                                   short key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
        return binarySearch0(a, fromIndex, toIndex, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
    // Like public version, but without range checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
    private static int binarySearch0(short[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
                                     short key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
        int low = fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
        int high = toIndex - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
            int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
            short midVal = a[mid];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
            if (midVal < key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
                low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
            else if (midVal > key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
                high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
                return mid; // key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
        return -(low + 1);  // key not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
     * Searches the specified array of chars for the specified value using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
     * binary search algorithm.  The array must be sorted (as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
     * by the {@link #sort(char[])} method) prior to making this call.  If it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
     * is not sorted, the results are undefined.  If the array contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
     * @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
  2019
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
     *         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
  2022
     *         element greater than the key, or {@code a.length} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
     *         elements in the array are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
    public static int binarySearch(char[] a, char key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
        return binarySearch0(a, 0, a.length, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
     * Searches a range of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
     * the specified array of chars for the specified value using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
     * binary search algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
     * The range must be sorted (as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
     * by the {@link #sort(char[], int, int)} method)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
     * prior to making this call.  If it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
     * is not sorted, the results are undefined.  If the range contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
     *          searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
     * @param toIndex the index of the last element (exclusive) to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
     * @return index of the search key, if it is contained in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
     *         within the specified range;
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2049
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
     *         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
  2053
     *         or {@code toIndex} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
     *         elements in the range are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
     *         if {@code fromIndex > toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
     * @throws ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
     *         if {@code fromIndex < 0 or toIndex > a.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
    public static int binarySearch(char[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
                                   char key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
        return binarySearch0(a, fromIndex, toIndex, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
    // Like public version, but without range checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
    private static int binarySearch0(char[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
                                     char key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
        int low = fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
        int high = toIndex - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
            int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
            char midVal = a[mid];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
            if (midVal < key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
                low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
            else if (midVal > key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
                high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
                return mid; // key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
        return -(low + 1);  // key not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
     * Searches the specified array of bytes for the specified value using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
     * binary search algorithm.  The array must be sorted (as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
     * by the {@link #sort(byte[])} method) prior to making this call.  If it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
     * is not sorted, the results are undefined.  If the array contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
     * @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
  2100
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
     *         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
  2103
     *         element greater than the key, or {@code a.length} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
     *         elements in the array are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
    public static int binarySearch(byte[] a, byte key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
        return binarySearch0(a, 0, a.length, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
     * Searches a range of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
     * the specified array of bytes for the specified value using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
     * binary search algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
     * The range must be sorted (as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
     * by the {@link #sort(byte[], int, int)} method)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
     * prior to making this call.  If it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
     * is not sorted, the results are undefined.  If the range contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
     *          searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
     * @param toIndex the index of the last element (exclusive) to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
     * @return index of the search key, if it is contained in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
     *         within the specified range;
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2130
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
     *         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
  2134
     *         or {@code toIndex} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
     *         elements in the range are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
     *         if {@code fromIndex > toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
     * @throws ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
     *         if {@code fromIndex < 0 or toIndex > a.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
    public static int binarySearch(byte[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
                                   byte key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
        return binarySearch0(a, fromIndex, toIndex, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
    // Like public version, but without range checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
    private static int binarySearch0(byte[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
                                     byte key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
        int low = fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
        int high = toIndex - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
            int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
            byte midVal = a[mid];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
            if (midVal < key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
                low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
            else if (midVal > key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
                high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
                return mid; // key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
        return -(low + 1);  // key not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
     * Searches the specified array of doubles for the specified value using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
     * the binary search algorithm.  The array must be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
     * (as by the {@link #sort(double[])} method) prior to making this call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
     * If it is not sorted, the results are undefined.  If the array contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
     * one will be found.  This method considers all NaN values to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
     * equivalent and equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
     * @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
  2182
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
     *         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
  2185
     *         element greater than the key, or {@code a.length} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
     *         elements in the array are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
    public static int binarySearch(double[] a, double key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
        return binarySearch0(a, 0, a.length, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
     * Searches a range of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
     * the specified array of doubles for the specified value using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
     * the binary search algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
     * The range must be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
     * (as by the {@link #sort(double[], int, int)} method)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
     * prior to making this call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
     * If it is not sorted, the results are undefined.  If the range contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
     * multiple elements with the specified value, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
     * one will be found.  This method considers all NaN values to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
     * equivalent and equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
     *          searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
     * @param toIndex the index of the last element (exclusive) to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
     * @return index of the search key, if it is contained in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
     *         within the specified range;
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2213
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
     *         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
  2217
     *         or {@code toIndex} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
     *         elements in the range are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
     *         if {@code fromIndex > toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
     * @throws ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
     *         if {@code fromIndex < 0 or toIndex > a.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
    public static int binarySearch(double[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
                                   double key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
        return binarySearch0(a, fromIndex, toIndex, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
    // Like public version, but without range checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
    private static int binarySearch0(double[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
                                     double key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
        int low = fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
        int high = toIndex - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
            int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
            double midVal = a[mid];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
            if (midVal < key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
                low = mid + 1;  // Neither val is NaN, thisVal is smaller
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
            else if (midVal > key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
                high = mid - 1; // Neither val is NaN, thisVal is larger
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
                long midBits = Double.doubleToLongBits(midVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
                long keyBits = Double.doubleToLongBits(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
                if (midBits == keyBits)     // Values are equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
                    return mid;             // Key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
                else if (midBits < keyBits) // (-0.0, 0.0) or (!NaN, NaN)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
                    low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
                else                        // (0.0, -0.0) or (NaN, !NaN)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
                    high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
        return -(low + 1);  // key not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
     * 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
  2263
     * 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
  2264
     * (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
  2265
     * it is not sorted, the results are undefined. If the array contains
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
     * 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
  2267
     * one will be found. This method considers all NaN values to be
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
     * equivalent and equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
     * @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
  2273
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
     *         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
  2276
     *         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
  2277
     *         elements in the array are less than the specified key. Note
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
    public static int binarySearch(float[] a, float key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
        return binarySearch0(a, 0, a.length, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
     * Searches a range of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
     * the specified array of floats for the specified value using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
     * the binary search algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
     * The range must be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
     * (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
  2291
     * prior to making this call. If
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
  2292
     * it is not sorted, the results are undefined. If the range contains
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
     * 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
  2294
     * one will be found. This method considers all NaN values to be
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
     * equivalent and equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
     *          searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
     * @param toIndex the index of the last element (exclusive) to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
     * @return index of the search key, if it is contained in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
     *         within the specified range;
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2304
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
     *         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
  2308
     *         or {@code toIndex} if all
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
  2309
     *         elements in the range are less than the specified key. Note
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
     *         if {@code fromIndex > toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
     * @throws ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
     *         if {@code fromIndex < 0 or toIndex > a.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
    public static int binarySearch(float[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
                                   float key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
        return binarySearch0(a, fromIndex, toIndex, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
    // Like public version, but without range checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
    private static int binarySearch0(float[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
                                     float key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
        int low = fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
        int high = toIndex - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
            int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
            float midVal = a[mid];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
            if (midVal < key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
                low = mid + 1;  // Neither val is NaN, thisVal is smaller
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
            else if (midVal > key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
                high = mid - 1; // Neither val is NaN, thisVal is larger
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
                int midBits = Float.floatToIntBits(midVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
                int keyBits = Float.floatToIntBits(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
                if (midBits == keyBits)     // Values are equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
                    return mid;             // Key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
                else if (midBits < keyBits) // (-0.0, 0.0) or (!NaN, NaN)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
                    low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
                else                        // (0.0, -0.0) or (NaN, !NaN)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
                    high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
        return -(low + 1);  // key not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
     * 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
  2354
     * search algorithm. The array must be sorted into ascending order
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
     * according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
     * {@linkplain Comparable natural ordering}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
     * of its elements (as by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
     * {@link #sort(Object[])} method) prior to making this call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
     * If it is not sorted, the results are undefined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
     * (If the array contains elements that are not mutually comparable (for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
     * example, strings and integers), it <i>cannot</i> be sorted according
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
     * to the natural ordering of its elements, hence results are undefined.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
     * If the array contains multiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
     * elements equal to the specified object, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
     * @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
  2370
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
     *         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
  2373
     *         element greater than the key, or {@code a.length} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
     *         elements in the array are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
     * @throws ClassCastException if the search key is not comparable to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
     *         elements of the array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
    public static int binarySearch(Object[] a, Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
        return binarySearch0(a, 0, a.length, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
     * Searches a range of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
     * the specified array for the specified object using the binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
     * search algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
     * The range must be sorted into ascending order
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
     * according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
     * {@linkplain Comparable natural ordering}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
     * of its elements (as by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
     * {@link #sort(Object[], int, int)} method) prior to making this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
     * call.  If it is not sorted, the results are undefined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
     * (If the range contains elements that are not mutually comparable (for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
     * example, strings and integers), it <i>cannot</i> be sorted according
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
     * to the natural ordering of its elements, hence results are undefined.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
     * If the range contains multiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
     * elements equal to the specified object, there is no guarantee which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
     * one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
     *          searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
     * @param toIndex the index of the last element (exclusive) to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
     * @return index of the search key, if it is contained in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
     *         within the specified range;
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2408
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
     *         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
  2412
     *         or {@code toIndex} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
     *         elements in the range are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
     * @throws ClassCastException if the search key is not comparable to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
     *         elements of the array within the specified range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2418
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
     *         if {@code fromIndex > toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2420
     * @throws ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
     *         if {@code fromIndex < 0 or toIndex > a.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
    public static int binarySearch(Object[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
                                   Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
        return binarySearch0(a, fromIndex, toIndex, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2430
    // Like public version, but without range checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
    private static int binarySearch0(Object[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
                                     Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2433
        int low = fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
        int high = toIndex - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
            int mid = (low + high) >>> 1;
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  2438
            @SuppressWarnings("rawtypes")
14925
72729557c226 8003981: Support Parallel Array Sorting - JEP 103
chegar
parents: 13795
diff changeset
  2439
            Comparable midVal = (Comparable)a[mid];
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  2440
            @SuppressWarnings("unchecked")
14925
72729557c226 8003981: Support Parallel Array Sorting - JEP 103
chegar
parents: 13795
diff changeset
  2441
            int cmp = midVal.compareTo(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2442
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2443
            if (cmp < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2444
                low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2445
            else if (cmp > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2446
                high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2447
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2448
                return mid; // key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2449
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2450
        return -(low + 1);  // key not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2451
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2452
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2453
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2454
     * Searches the specified array for the specified object using the binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2455
     * search algorithm.  The array must be sorted into ascending order
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2456
     * according to the specified comparator (as by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2457
     * {@link #sort(Object[], Comparator) sort(T[], Comparator)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2458
     * method) prior to making this call.  If it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2459
     * not sorted, the results are undefined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2460
     * If the array contains multiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2461
     * elements equal to the specified object, there is no guarantee which one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2462
     * will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2463
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  2464
     * @param <T> the class of the objects in the array
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2465
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2466
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2467
     * @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
  2468
     *        {@code null} value indicates that the elements'
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2469
     *        {@linkplain Comparable natural ordering} should be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2470
     * @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
  2471
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2472
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2473
     *         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
  2474
     *         element greater than the key, or {@code a.length} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2475
     *         elements in the array are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2476
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2477
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2478
     * @throws ClassCastException if the array contains elements that are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2479
     *         <i>mutually comparable</i> using the specified comparator,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2480
     *         or the search key is not comparable to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2481
     *         elements of the array using this comparator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2482
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2483
    public static <T> int binarySearch(T[] a, T key, Comparator<? super T> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2484
        return binarySearch0(a, 0, a.length, key, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2485
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2486
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2487
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2488
     * Searches a range of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2489
     * the specified array for the specified object using the binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2490
     * search algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2491
     * The range must be sorted into ascending order
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2492
     * according to the specified comparator (as by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2493
     * {@link #sort(Object[], int, int, Comparator)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2494
     * sort(T[], int, int, Comparator)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2495
     * method) prior to making this call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2496
     * If it is not sorted, the results are undefined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2497
     * If the range contains multiple elements equal to the specified object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2498
     * there is no guarantee which one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2499
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  2500
     * @param <T> the class of the objects in the array
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2501
     * @param a the array to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2502
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2503
     *          searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2504
     * @param toIndex the index of the last element (exclusive) to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2505
     * @param key the value to be searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2506
     * @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
  2507
     *        {@code null} value indicates that the elements'
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2508
     *        {@linkplain Comparable natural ordering} should be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2509
     * @return index of the search key, if it is contained in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2510
     *         within the specified range;
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2511
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2512
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2513
     *         key would be inserted into the array: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2514
     *         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
  2515
     *         or {@code toIndex} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2516
     *         elements in the range are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2517
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2518
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2519
     * @throws ClassCastException if the range contains elements that are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2520
     *         <i>mutually comparable</i> using the specified comparator,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2521
     *         or the search key is not comparable to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2522
     *         elements in the range using this comparator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2523
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2524
     *         if {@code fromIndex > toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2525
     * @throws ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2526
     *         if {@code fromIndex < 0 or toIndex > a.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2527
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2528
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2529
    public static <T> int binarySearch(T[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2530
                                       T key, Comparator<? super T> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2531
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2532
        return binarySearch0(a, fromIndex, toIndex, key, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2533
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2534
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2535
    // Like public version, but without range checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2536
    private static <T> int binarySearch0(T[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2537
                                         T key, Comparator<? super T> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2538
        if (c == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2539
            return binarySearch0(a, fromIndex, toIndex, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2540
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2541
        int low = fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2542
        int high = toIndex - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2543
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2544
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2545
            int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2546
            T midVal = a[mid];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2547
            int cmp = c.compare(midVal, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2548
            if (cmp < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2549
                low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2550
            else if (cmp > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2551
                high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2552
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2553
                return mid; // key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2554
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2555
        return -(low + 1);  // key not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2556
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2557
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2558
    // Equality Testing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2559
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2560
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2561
     * Returns {@code true} if the two specified arrays of longs are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2562
     * <i>equal</i> to one another.  Two arrays are considered equal if both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2563
     * arrays contain the same number of elements, and all corresponding pairs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2564
     * of elements in the two arrays are equal.  In other words, two arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2565
     * 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
  2566
     * two array references are considered equal if both are {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2567
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2568
     * @param a one array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2569
     * @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
  2570
     * @return {@code true} if the two arrays are equal
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2571
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2572
    public static boolean equals(long[] a, long[] a2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2573
        if (a==a2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2574
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2575
        if (a==null || a2==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2576
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2577
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2578
        int length = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2579
        if (a2.length != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2580
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2581
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2582
        return ArraysSupport.mismatch(a, a2, length) < 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2583
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2584
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2585
    /**
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2586
     * Returns true if the two specified arrays of longs, over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2587
     * ranges, are <i>equal</i> to one another.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2588
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2589
     * <p>Two arrays are considered equal if the number of elements covered by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2590
     * each range is the same, and all corresponding pairs of elements over the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2591
     * specified ranges in the two arrays are equal.  In other words, two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2592
     * are equal if they contain, over the specified ranges, the same elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2593
     * in the same order.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2594
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2595
     * @param a the first array to be tested for equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2596
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2597
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2598
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2599
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2600
     * @param b the second array to be tested fro equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2601
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2602
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2603
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2604
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2605
     * @return {@code true} if the two arrays, over the specified ranges, are
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2606
     *         equal
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2607
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2608
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2609
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2610
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2611
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2612
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2613
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2614
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2615
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2616
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2617
    public static boolean equals(long[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2618
                                 long[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2619
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2620
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2621
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2622
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2623
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2624
        if (aLength != bLength)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2625
            return false;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2626
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2627
        return ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2628
                                      b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2629
                                      aLength) < 0;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2630
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2631
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2632
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2633
     * Returns {@code true} if the two specified arrays of ints are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2634
     * <i>equal</i> to one another.  Two arrays are considered equal if both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2635
     * arrays contain the same number of elements, and all corresponding pairs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2636
     * of elements in the two arrays are equal.  In other words, two arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2637
     * 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
  2638
     * two array references are considered equal if both are {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2639
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2640
     * @param a one array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2641
     * @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
  2642
     * @return {@code true} if the two arrays are equal
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2643
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2644
    public static boolean equals(int[] a, int[] a2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2645
        if (a==a2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2646
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2647
        if (a==null || a2==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2648
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2649
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2650
        int length = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2651
        if (a2.length != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2652
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2653
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2654
        return ArraysSupport.mismatch(a, a2, length) < 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2655
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2656
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2657
    /**
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2658
     * Returns true if the two specified arrays of ints, over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2659
     * ranges, are <i>equal</i> to one another.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2660
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2661
     * <p>Two arrays are considered equal if the number of elements covered by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2662
     * each range is the same, and all corresponding pairs of elements over the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2663
     * specified ranges in the two arrays are equal.  In other words, two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2664
     * are equal if they contain, over the specified ranges, the same elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2665
     * in the same order.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2666
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2667
     * @param a the first array to be tested for equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2668
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2669
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2670
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2671
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2672
     * @param b the second array to be tested fro equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2673
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2674
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2675
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2676
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2677
     * @return {@code true} if the two arrays, over the specified ranges, are
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2678
     *         equal
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2679
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2680
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2681
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2682
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2683
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2684
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2685
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2686
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2687
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2688
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2689
    public static boolean equals(int[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2690
                                 int[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2691
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2692
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2693
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2694
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2695
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2696
        if (aLength != bLength)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2697
            return false;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2698
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2699
        return ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2700
                                      b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2701
                                      aLength) < 0;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2702
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2703
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2704
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2705
     * Returns {@code true} if the two specified arrays of shorts are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2706
     * <i>equal</i> to one another.  Two arrays are considered equal if both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2707
     * arrays contain the same number of elements, and all corresponding pairs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2708
     * of elements in the two arrays are equal.  In other words, two arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2709
     * 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
  2710
     * two array references are considered equal if both are {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2711
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2712
     * @param a one array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2713
     * @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
  2714
     * @return {@code true} if the two arrays are equal
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2715
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2716
    public static boolean equals(short[] a, short a2[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2717
        if (a==a2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2718
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2719
        if (a==null || a2==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2720
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2721
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2722
        int length = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2723
        if (a2.length != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2724
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2725
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2726
        return ArraysSupport.mismatch(a, a2, length) < 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2727
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2728
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2729
    /**
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2730
     * Returns true if the two specified arrays of shorts, over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2731
     * ranges, are <i>equal</i> to one another.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2732
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2733
     * <p>Two arrays are considered equal if the number of elements covered by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2734
     * each range is the same, and all corresponding pairs of elements over the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2735
     * specified ranges in the two arrays are equal.  In other words, two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2736
     * are equal if they contain, over the specified ranges, the same elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2737
     * in the same order.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2738
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2739
     * @param a the first array to be tested for equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2740
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2741
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2742
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2743
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2744
     * @param b the second array to be tested fro equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2745
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2746
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2747
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2748
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2749
     * @return {@code true} if the two arrays, over the specified ranges, are
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2750
     *         equal
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2751
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2752
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2753
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2754
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2755
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2756
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2757
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2758
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2759
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2760
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2761
    public static boolean equals(short[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2762
                                 short[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2763
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2764
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2765
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2766
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2767
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2768
        if (aLength != bLength)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2769
            return false;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2770
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2771
        return ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2772
                                      b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2773
                                      aLength) < 0;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2774
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2775
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2776
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2777
     * Returns {@code true} if the two specified arrays of chars are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2778
     * <i>equal</i> to one another.  Two arrays are considered equal if both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2779
     * arrays contain the same number of elements, and all corresponding pairs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2780
     * of elements in the two arrays are equal.  In other words, two arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2781
     * 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
  2782
     * two array references are considered equal if both are {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2783
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2784
     * @param a one array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2785
     * @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
  2786
     * @return {@code true} if the two arrays are equal
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2787
     */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 29223
diff changeset
  2788
    @HotSpotIntrinsicCandidate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2789
    public static boolean equals(char[] a, char[] a2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2790
        if (a==a2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2791
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2792
        if (a==null || a2==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2793
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2794
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2795
        int length = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2796
        if (a2.length != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2797
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2798
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2799
        return ArraysSupport.mismatch(a, a2, length) < 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2800
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2801
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2802
    /**
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2803
     * Returns true if the two specified arrays of chars, over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2804
     * ranges, are <i>equal</i> to one another.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2805
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2806
     * <p>Two arrays are considered equal if the number of elements covered by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2807
     * each range is the same, and all corresponding pairs of elements over the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2808
     * specified ranges in the two arrays are equal.  In other words, two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2809
     * are equal if they contain, over the specified ranges, the same elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2810
     * in the same order.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2811
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2812
     * @param a the first array to be tested for equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2813
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2814
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2815
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2816
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2817
     * @param b the second array to be tested fro equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2818
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2819
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2820
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2821
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2822
     * @return {@code true} if the two arrays, over the specified ranges, are
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2823
     *         equal
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2824
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2825
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2826
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2827
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2828
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2829
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2830
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2831
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2832
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2833
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2834
    public static boolean equals(char[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2835
                                 char[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2836
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2837
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2838
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2839
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2840
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2841
        if (aLength != bLength)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2842
            return false;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2843
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2844
        return ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2845
                                      b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2846
                                      aLength) < 0;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2847
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2848
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2849
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2850
     * Returns {@code true} if the two specified arrays of bytes are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2851
     * <i>equal</i> to one another.  Two arrays are considered equal if both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2852
     * arrays contain the same number of elements, and all corresponding pairs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2853
     * of elements in the two arrays are equal.  In other words, two arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2854
     * 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
  2855
     * two array references are considered equal if both are {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2856
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2857
     * @param a one array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2858
     * @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
  2859
     * @return {@code true} if the two arrays are equal
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2860
     */
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32108
diff changeset
  2861
    @HotSpotIntrinsicCandidate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2862
    public static boolean equals(byte[] a, byte[] a2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2863
        if (a==a2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2864
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2865
        if (a==null || a2==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2866
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2867
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2868
        int length = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2869
        if (a2.length != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2870
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2871
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2872
        return ArraysSupport.mismatch(a, a2, length) < 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2873
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2874
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2875
    /**
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2876
     * Returns true if the two specified arrays of bytes, over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2877
     * ranges, are <i>equal</i> to one another.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2878
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2879
     * <p>Two arrays are considered equal if the number of elements covered by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2880
     * each range is the same, and all corresponding pairs of elements over the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2881
     * specified ranges in the two arrays are equal.  In other words, two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2882
     * are equal if they contain, over the specified ranges, the same elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2883
     * in the same order.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2884
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2885
     * @param a the first array to be tested for equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2886
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2887
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2888
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2889
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2890
     * @param b the second array to be tested fro equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2891
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2892
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2893
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2894
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2895
     * @return {@code true} if the two arrays, over the specified ranges, are
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2896
     *         equal
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2897
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2898
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2899
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2900
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2901
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2902
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2903
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2904
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2905
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2906
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2907
    public static boolean equals(byte[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2908
                                 byte[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2909
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2910
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2911
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2912
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2913
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2914
        if (aLength != bLength)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2915
            return false;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2916
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2917
        return ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2918
                                      b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2919
                                      aLength) < 0;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2920
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2921
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2922
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2923
     * Returns {@code true} if the two specified arrays of booleans are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2924
     * <i>equal</i> to one another.  Two arrays are considered equal if both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2925
     * arrays contain the same number of elements, and all corresponding pairs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2926
     * of elements in the two arrays are equal.  In other words, two arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2927
     * 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
  2928
     * two array references are considered equal if both are {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2929
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2930
     * @param a one array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2931
     * @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
  2932
     * @return {@code true} if the two arrays are equal
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2933
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2934
    public static boolean equals(boolean[] a, boolean[] a2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2935
        if (a==a2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2936
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2937
        if (a==null || a2==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2938
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2939
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2940
        int length = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2941
        if (a2.length != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2942
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2943
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2944
        return ArraysSupport.mismatch(a, a2, length) < 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2945
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2946
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2947
    /**
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2948
     * Returns true if the two specified arrays of booleans, over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2949
     * ranges, are <i>equal</i> to one another.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2950
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2951
     * <p>Two arrays are considered equal if the number of elements covered by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2952
     * each range is the same, and all corresponding pairs of elements over the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2953
     * specified ranges in the two arrays are equal.  In other words, two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2954
     * are equal if they contain, over the specified ranges, the same elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2955
     * in the same order.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2956
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2957
     * @param a the first array to be tested for equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2958
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2959
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2960
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2961
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2962
     * @param b the second array to be tested fro equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2963
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2964
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2965
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2966
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2967
     * @return {@code true} if the two arrays, over the specified ranges, are
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2968
     *         equal
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2969
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2970
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2971
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2972
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2973
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2974
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2975
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2976
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2977
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2978
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2979
    public static boolean equals(boolean[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2980
                                 boolean[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2981
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2982
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2983
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2984
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2985
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2986
        if (aLength != bLength)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2987
            return false;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2988
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2989
        return ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2990
                                      b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  2991
                                      aLength) < 0;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2992
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2993
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  2994
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  2995
     * Returns {@code true} if the two specified arrays of doubles are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2996
     * <i>equal</i> to one another.  Two arrays are considered equal if both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2997
     * arrays contain the same number of elements, and all corresponding pairs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2998
     * of elements in the two arrays are equal.  In other words, two arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2999
     * 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
  3000
     * 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
  3001
     *
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3002
     * 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
  3003
     * <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
  3004
     * (Unlike the {@code ==} operator, this method considers
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3005
     * {@code NaN} equals to itself, and 0.0d unequal to -0.0d.)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3006
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3007
     * @param a one array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3008
     * @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
  3009
     * @return {@code true} if the two arrays are equal
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3010
     * @see Double#equals(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3011
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3012
    public static boolean equals(double[] a, double[] a2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3013
        if (a==a2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3014
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3015
        if (a==null || a2==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3016
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3017
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3018
        int length = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3019
        if (a2.length != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3020
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3021
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  3022
        return ArraysSupport.mismatch(a, a2, length) < 0;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3023
    }
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
     * Returns true if the two specified arrays of doubles, over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3027
     * ranges, are <i>equal</i> to one another.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3028
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3029
     * <p>Two arrays are considered equal if the number of elements covered by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3030
     * each range is the same, and all corresponding pairs of elements over the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3031
     * specified ranges in the two arrays are equal.  In other words, two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3032
     * are equal if they contain, over the specified ranges, the same elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3033
     * in the same order.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3034
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3035
     * <p>Two doubles {@code d1} and {@code d2} are considered equal if:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3036
     * <pre>    {@code new Double(d1).equals(new Double(d2))}</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3037
     * (Unlike the {@code ==} operator, this method considers
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3038
     * {@code NaN} equals to itself, and 0.0d unequal to -0.0d.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3039
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3040
     * @param a the first array to be tested for equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3041
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3042
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3043
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3044
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3045
     * @param b the second array to be tested fro equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3046
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3047
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3048
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3049
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3050
     * @return {@code true} if the two arrays, over the specified ranges, are
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3051
     *         equal
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3052
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3053
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3054
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3055
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3056
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3057
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3058
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3059
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3060
     * @see Double#equals(Object)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3061
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3062
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3063
    public static boolean equals(double[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3064
                                 double[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3065
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3066
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3067
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3068
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3069
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3070
        if (aLength != bLength)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3071
            return false;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3072
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  3073
        return ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  3074
                                      b, bFromIndex, aLength) < 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3075
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3076
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3077
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3078
     * Returns {@code true} if the two specified arrays of floats are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3079
     * <i>equal</i> to one another.  Two arrays are considered equal if both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3080
     * arrays contain the same number of elements, and all corresponding pairs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3081
     * of elements in the two arrays are equal.  In other words, two arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3082
     * 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
  3083
     * 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
  3084
     *
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3085
     * 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
  3086
     * <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
  3087
     * (Unlike the {@code ==} operator, this method considers
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3088
     * {@code NaN} equals to itself, and 0.0f unequal to -0.0f.)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3089
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3090
     * @param a one array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3091
     * @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
  3092
     * @return {@code true} if the two arrays are equal
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3093
     * @see Float#equals(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3094
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3095
    public static boolean equals(float[] a, float[] a2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3096
        if (a==a2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3097
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3098
        if (a==null || a2==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3099
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3100
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3101
        int length = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3102
        if (a2.length != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3103
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3104
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  3105
        return ArraysSupport.mismatch(a, a2, length) < 0;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3106
    }
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
     * Returns true if the two specified arrays of floats, over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3110
     * ranges, are <i>equal</i> to one another.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3111
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3112
     * <p>Two arrays are considered equal if the number of elements covered by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3113
     * each range is the same, and all corresponding pairs of elements over the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3114
     * specified ranges in the two arrays are equal.  In other words, two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3115
     * are equal if they contain, over the specified ranges, the same elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3116
     * in the same order.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3117
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3118
     * <p>Two floats {@code f1} and {@code f2} are considered equal if:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3119
     * <pre>    {@code new Float(f1).equals(new Float(f2))}</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3120
     * (Unlike the {@code ==} operator, this method considers
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3121
     * {@code NaN} equals to itself, and 0.0f unequal to -0.0f.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3122
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3123
     * @param a the first array to be tested for equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3124
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3125
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3126
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3127
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3128
     * @param b the second array to be tested fro equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3129
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3130
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3131
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3132
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3133
     * @return {@code true} if the two arrays, over the specified ranges, are
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3134
     *         equal
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3135
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3136
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3137
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3138
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3139
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3140
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3141
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3142
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3143
     * @see Float#equals(Object)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3144
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3145
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3146
    public static boolean equals(float[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3147
                                 float[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3148
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3149
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3150
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3151
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3152
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3153
        if (aLength != bLength)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3154
            return false;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3155
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  3156
        return ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  3157
                                      b, bFromIndex, aLength) < 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3159
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3160
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3161
     * Returns {@code true} if the two specified arrays of Objects are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3162
     * <i>equal</i> to one another.  The two arrays are considered equal if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3163
     * 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
  3164
     * 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
  3165
     * 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
  3166
     * {@code Objects.equals(e1, e2)}.
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3167
     * In other words, the two arrays are equal if
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3168
     * 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
  3169
     * references are considered equal if both are {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3170
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3171
     * @param a one array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3172
     * @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
  3173
     * @return {@code true} if the two arrays are equal
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3174
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3175
    public static boolean equals(Object[] a, Object[] a2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3176
        if (a==a2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3177
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3178
        if (a==null || a2==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3179
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3180
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3181
        int length = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3182
        if (a2.length != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3183
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3184
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3185
        for (int i=0; i<length; i++) {
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3186
            if (!Objects.equals(a[i], a2[i]))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3187
                return false;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3188
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3189
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3190
        return true;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3191
    }
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
     * Returns true if the two specified arrays of Objects, over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3195
     * ranges, are <i>equal</i> to one another.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3196
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3197
     * <p>Two arrays are considered equal if the number of elements covered by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3198
     * each range is the same, and all corresponding pairs of elements over the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3199
     * specified ranges in the two arrays are equal.  In other words, two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3200
     * are equal if they contain, over the specified ranges, the same elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3201
     * in the same order.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3202
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3203
     * <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
  3204
     * {@code Objects.equals(e1, e2)}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3205
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3206
     * @param a the first array to be tested for equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3207
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3208
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3209
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3210
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3211
     * @param b the second array to be tested fro equality
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3212
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3213
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3214
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3215
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3216
     * @return {@code true} if the two arrays, over the specified ranges, are
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3217
     *         equal
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3218
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3219
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3220
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3221
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3222
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3223
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3224
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3225
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3226
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3227
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3228
    public static boolean equals(Object[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3229
                                 Object[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3230
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3231
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3232
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3233
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3234
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3235
        if (aLength != bLength)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3236
            return false;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3237
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3238
        for (int i = 0; i < aLength; i++) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  3239
            if (!Objects.equals(a[aFromIndex++], b[bFromIndex++]))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3240
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3241
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3242
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3243
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3245
33843
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3246
    /**
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3247
     * Returns {@code true} if the two specified arrays of Objects are
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3248
     * <i>equal</i> to one another.
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3249
     *
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3250
     * <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
  3251
     * of elements, and all corresponding pairs of elements in the two arrays
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3252
     * 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
  3253
     * same elements in the same order.  Also, two array references are
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3254
     * considered equal if both are {@code null}.
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3255
     *
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3256
     * <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
  3257
     * given the specified comparator, {@code cmp.compare(e1, e2) == 0}.
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3258
     *
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3259
     * @param a one array to be tested for equality
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3260
     * @param a2 the other array to be tested for equality
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3261
     * @param cmp the comparator to compare array elements
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3262
     * @param <T> the type of array elements
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3263
     * @return {@code true} if the two arrays are equal
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3264
     * @throws NullPointerException if the comparator is {@code null}
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3265
     * @since 9
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3266
     */
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3267
    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
  3268
        Objects.requireNonNull(cmp);
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3269
        if (a==a2)
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3270
            return true;
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3271
        if (a==null || a2==null)
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3272
            return false;
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3273
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3274
        int length = a.length;
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3275
        if (a2.length != length)
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3276
            return false;
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3277
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3278
        for (int i=0; i<length; i++) {
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3279
            if (cmp.compare(a[i], a2[i]) != 0)
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3280
                return false;
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3281
        }
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
        return true;
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3284
    }
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3285
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
     * Returns true if the two specified arrays of Objects, over the specified
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3288
     * ranges, are <i>equal</i> to one another.
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3289
     *
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3290
     * <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
  3291
     * 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
  3292
     * 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
  3293
     * are equal if they contain, over the specified ranges, the same elements
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3294
     * in the same order.
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3295
     *
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3296
     * <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
  3297
     * given the specified comparator, {@code cmp.compare(e1, e2) == 0}.
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3298
     *
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3299
     * @param a the first array to be tested for equality
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3300
     * @param aFromIndex the index (inclusive) of the first element in the
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3301
     *                   first array to be tested
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3302
     * @param aToIndex the index (exclusive) of the last element in the
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3303
     *                 first array to be tested
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3304
     * @param b the second array to be tested fro equality
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3305
     * @param bFromIndex the index (inclusive) of the first element in the
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3306
     *                   second array to be tested
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3307
     * @param bToIndex the index (exclusive) of the last element in the
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3308
     *                 second array to be tested
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3309
     * @param cmp the comparator to compare array elements
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3310
     * @param <T> the type of array elements
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3311
     * @return {@code true} if the two arrays, over the specified ranges, are
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3312
     *         equal
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3313
     * @throws IllegalArgumentException
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3314
     *         if {@code aFromIndex > aToIndex} or
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3315
     *         if {@code bFromIndex > bToIndex}
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3316
     * @throws ArrayIndexOutOfBoundsException
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3317
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3318
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3319
     * @throws NullPointerException
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3320
     *         if either array or the comparator is {@code null}
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3321
     * @since 9
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3322
     */
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3323
    public static <T> boolean equals(T[] a, int aFromIndex, int aToIndex,
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3324
                                     T[] b, int bFromIndex, int bToIndex,
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3325
                                     Comparator<? super T> cmp) {
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3326
        Objects.requireNonNull(cmp);
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3327
        rangeCheck(a.length, aFromIndex, aToIndex);
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3328
        rangeCheck(b.length, bFromIndex, bToIndex);
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3329
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3330
        int aLength = aToIndex - aFromIndex;
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3331
        int bLength = bToIndex - bFromIndex;
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3332
        if (aLength != bLength)
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3333
            return false;
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3334
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3335
        for (int i = 0; i < aLength; i++) {
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3336
            if (cmp.compare(a[aFromIndex++], b[bFromIndex++]) != 0)
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3337
                return false;
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3338
        }
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
        return true;
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3341
    }
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  3342
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3343
    // Filling
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3344
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3345
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3346
     * Assigns the specified long value to each element of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3347
     * of longs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3348
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3349
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3350
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3351
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3352
    public static void fill(long[] a, long val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3353
        for (int i = 0, len = a.length; i < len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3354
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3355
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3356
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3357
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3358
     * Assigns the specified long value to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3359
     * 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
  3360
     * 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
  3361
     * {@code toIndex}, exclusive.  (If {@code fromIndex==toIndex}, the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3362
     * range to be filled is empty.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3363
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3364
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3365
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3366
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3367
     * @param toIndex the index of the last element (exclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3368
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3369
     * @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
  3370
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3371
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3372
     *         {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3373
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3374
    public static void fill(long[] a, int fromIndex, int toIndex, long val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3375
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3376
        for (int i = fromIndex; i < toIndex; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3377
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3379
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3380
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3381
     * Assigns the specified int value to each element of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3382
     * of ints.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3383
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3384
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3385
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3386
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3387
    public static void fill(int[] a, int val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3388
        for (int i = 0, len = a.length; i < len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3389
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3390
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3391
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3392
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3393
     * Assigns the specified int value to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3394
     * 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
  3395
     * 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
  3396
     * {@code toIndex}, exclusive.  (If {@code fromIndex==toIndex}, the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3397
     * range to be filled is empty.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3398
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3399
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3400
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3401
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3402
     * @param toIndex the index of the last element (exclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3403
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3404
     * @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
  3405
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3406
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3407
     *         {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3408
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3409
    public static void fill(int[] a, int fromIndex, int toIndex, int val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3410
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3411
        for (int i = fromIndex; i < toIndex; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3412
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3413
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3414
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3415
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3416
     * Assigns the specified short value to each element of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3417
     * of shorts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3418
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3419
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3420
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3421
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3422
    public static void fill(short[] a, short val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3423
        for (int i = 0, len = a.length; i < len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3424
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3425
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3426
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3427
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3428
     * Assigns the specified short value to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3429
     * 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
  3430
     * 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
  3431
     * {@code toIndex}, exclusive.  (If {@code fromIndex==toIndex}, the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3432
     * range to be filled is empty.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3433
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3434
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3435
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3436
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3437
     * @param toIndex the index of the last element (exclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3438
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3439
     * @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
  3440
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3441
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3442
     *         {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3443
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3444
    public static void fill(short[] a, int fromIndex, int toIndex, short val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3445
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3446
        for (int i = fromIndex; i < toIndex; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3447
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3448
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3449
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3450
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3451
     * Assigns the specified char value to each element of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3452
     * of chars.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3453
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3454
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3455
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3456
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3457
    public static void fill(char[] a, char val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3458
        for (int i = 0, len = a.length; i < len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3459
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3460
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3461
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3462
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3463
     * Assigns the specified char value to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3464
     * 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
  3465
     * 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
  3466
     * {@code toIndex}, exclusive.  (If {@code fromIndex==toIndex}, the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3467
     * range to be filled is empty.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3468
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3469
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3470
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3471
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3472
     * @param toIndex the index of the last element (exclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3473
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3474
     * @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
  3475
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3476
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3477
     *         {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3478
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3479
    public static void fill(char[] a, int fromIndex, int toIndex, char val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3480
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3481
        for (int i = fromIndex; i < toIndex; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3482
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3483
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3484
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3485
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3486
     * Assigns the specified byte value to each element of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3487
     * of bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3488
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3489
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3490
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3491
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3492
    public static void fill(byte[] a, byte val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3493
        for (int i = 0, len = a.length; i < len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3494
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3495
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3496
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3497
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3498
     * Assigns the specified byte value to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3499
     * 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
  3500
     * 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
  3501
     * {@code toIndex}, exclusive.  (If {@code fromIndex==toIndex}, the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3502
     * range to be filled is empty.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3503
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3504
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3505
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3506
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3507
     * @param toIndex the index of the last element (exclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3508
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3509
     * @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
  3510
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3511
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3512
     *         {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3513
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3514
    public static void fill(byte[] a, int fromIndex, int toIndex, byte val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3515
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3516
        for (int i = fromIndex; i < toIndex; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3517
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3518
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3519
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3520
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3521
     * Assigns the specified boolean value to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3522
     * array of booleans.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3523
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3524
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3525
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3526
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3527
    public static void fill(boolean[] a, boolean val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3528
        for (int i = 0, len = a.length; i < len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3529
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3530
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3531
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3532
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3533
     * Assigns the specified boolean value to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3534
     * 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
  3535
     * 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
  3536
     * {@code toIndex}, exclusive.  (If {@code fromIndex==toIndex}, the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3537
     * range to be filled is empty.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3538
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3539
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3540
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3541
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3542
     * @param toIndex the index of the last element (exclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3543
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3544
     * @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
  3545
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3546
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3547
     *         {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3548
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3549
    public static void fill(boolean[] a, int fromIndex, int toIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3550
                            boolean val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3551
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3552
        for (int i = fromIndex; i < toIndex; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3553
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3554
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3555
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3556
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3557
     * Assigns the specified double value to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3558
     * array of doubles.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3559
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3560
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3561
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3562
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3563
    public static void fill(double[] a, double val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3564
        for (int i = 0, len = a.length; i < len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3565
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3566
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3567
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3568
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3569
     * Assigns the specified double value to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3570
     * 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
  3571
     * 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
  3572
     * {@code toIndex}, exclusive.  (If {@code fromIndex==toIndex}, the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3573
     * range to be filled is empty.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3574
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3575
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3576
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3577
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3578
     * @param toIndex the index of the last element (exclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3579
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3580
     * @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
  3581
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3582
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3583
     *         {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3584
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3585
    public static void fill(double[] a, int fromIndex, int toIndex,double val){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3586
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3587
        for (int i = fromIndex; i < toIndex; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3588
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3589
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3590
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3591
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3592
     * Assigns the specified float value to each element of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3593
     * of floats.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3594
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3595
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3596
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3597
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3598
    public static void fill(float[] a, float val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3599
        for (int i = 0, len = a.length; i < len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3600
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3601
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3602
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3603
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3604
     * Assigns the specified float value to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3605
     * 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
  3606
     * 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
  3607
     * {@code toIndex}, exclusive.  (If {@code fromIndex==toIndex}, the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3608
     * range to be filled is empty.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3609
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3610
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3611
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3612
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3613
     * @param toIndex the index of the last element (exclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3614
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3615
     * @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
  3616
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3617
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3618
     *         {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3619
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3620
    public static void fill(float[] a, int fromIndex, int toIndex, float val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3621
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3622
        for (int i = fromIndex; i < toIndex; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3623
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3624
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3625
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3626
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3627
     * Assigns the specified Object reference to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3628
     * array of Objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3629
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3630
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3631
     * @param val the value to be stored in all elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3632
     * @throws ArrayStoreException if the specified value is not of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3633
     *         runtime type that can be stored in the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3634
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3635
    public static void fill(Object[] a, Object val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3636
        for (int i = 0, len = a.length; i < len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3637
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3638
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3639
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3640
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3641
     * Assigns the specified Object reference to each element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3642
     * 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
  3643
     * 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
  3644
     * {@code toIndex}, exclusive.  (If {@code fromIndex==toIndex}, the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3645
     * range to be filled is empty.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3646
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3647
     * @param a the array to be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3648
     * @param fromIndex the index of the first element (inclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3649
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3650
     * @param toIndex the index of the last element (exclusive) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3651
     *        filled with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3652
     * @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
  3653
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3654
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3655
     *         {@code toIndex > a.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3656
     * @throws ArrayStoreException if the specified value is not of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3657
     *         runtime type that can be stored in the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3658
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3659
    public static void fill(Object[] a, int fromIndex, int toIndex, Object val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3660
        rangeCheck(a.length, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3661
        for (int i = fromIndex; i < toIndex; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3662
            a[i] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3663
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3664
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
  3665
    // Cloning
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3666
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3667
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3668
     * Copies the specified array, truncating or padding with nulls (if necessary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3669
     * so the copy has the specified length.  For all indices that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3670
     * valid in both the original array and the copy, the two arrays will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3671
     * 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
  3672
     * copy but not the original, the copy will contain {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3673
     * Such indices will exist if and only if the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3674
     * is greater than that of the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3675
     * The resulting array is of exactly the same class as the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3676
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  3677
     * @param <T> the class of the objects in the array
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3678
     * @param original the array to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3679
     * @param newLength the length of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3680
     * @return a copy of the original array, truncated or padded with nulls
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3681
     *     to obtain the specified length
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3682
     * @throws NegativeArraySizeException if {@code newLength} is negative
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3683
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3684
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3685
     */
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  3686
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3687
    public static <T> T[] copyOf(T[] original, int newLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3688
        return (T[]) copyOf(original, newLength, original.getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3689
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3690
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3691
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3692
     * Copies the specified array, truncating or padding with nulls (if necessary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3693
     * so the copy has the specified length.  For all indices that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3694
     * valid in both the original array and the copy, the two arrays will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3695
     * 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
  3696
     * copy but not the original, the copy will contain {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3697
     * Such indices will exist if and only if the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3698
     * 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
  3699
     * The resulting array is of the class {@code newType}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3700
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  3701
     * @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
  3702
     * @param <T> the class of the objects in the returned array
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3703
     * @param original the array to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3704
     * @param newLength the length of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3705
     * @param newType the class of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3706
     * @return a copy of the original array, truncated or padded with nulls
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3707
     *     to obtain the specified length
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3708
     * @throws NegativeArraySizeException if {@code newLength} is negative
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3709
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3710
     * @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
  3711
     *     {@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
  3712
     *     an array of class {@code newType}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3713
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3714
     */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 29223
diff changeset
  3715
    @HotSpotIntrinsicCandidate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3716
    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
  3717
        @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3718
        T[] copy = ((Object)newType == (Object)Object[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3719
            ? (T[]) new Object[newLength]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3720
            : (T[]) Array.newInstance(newType.getComponentType(), newLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3721
        System.arraycopy(original, 0, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3722
                         Math.min(original.length, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3723
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3724
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3725
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3726
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3727
     * Copies the specified array, truncating or padding with zeros (if necessary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3728
     * so the copy has the specified length.  For all indices that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3729
     * valid in both the original array and the copy, the two arrays will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3730
     * 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
  3731
     * copy but not the original, the copy will contain {@code (byte)0}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3732
     * Such indices will exist if and only if the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3733
     * is greater than that of the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3734
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3735
     * @param original the array to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3736
     * @param newLength the length of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3737
     * @return a copy of the original array, truncated or padded with zeros
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3738
     *     to obtain the specified length
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3739
     * @throws NegativeArraySizeException if {@code newLength} is negative
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3740
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3741
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3742
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3743
    public static byte[] copyOf(byte[] original, int newLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3744
        byte[] copy = new byte[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3745
        System.arraycopy(original, 0, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3746
                         Math.min(original.length, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3747
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3748
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3749
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3750
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3751
     * Copies the specified array, truncating or padding with zeros (if necessary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3752
     * so the copy has the specified length.  For all indices that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3753
     * valid in both the original array and the copy, the two arrays will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3754
     * 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
  3755
     * copy but not the original, the copy will contain {@code (short)0}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3756
     * Such indices will exist if and only if the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3757
     * is greater than that of the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3758
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3759
     * @param original the array to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3760
     * @param newLength the length of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3761
     * @return a copy of the original array, truncated or padded with zeros
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3762
     *     to obtain the specified length
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3763
     * @throws NegativeArraySizeException if {@code newLength} is negative
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3764
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3765
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3766
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3767
    public static short[] copyOf(short[] original, int newLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3768
        short[] copy = new short[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3769
        System.arraycopy(original, 0, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3770
                         Math.min(original.length, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3771
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3772
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3773
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3774
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3775
     * Copies the specified array, truncating or padding with zeros (if necessary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3776
     * so the copy has the specified length.  For all indices that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3777
     * valid in both the original array and the copy, the two arrays will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3778
     * 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
  3779
     * copy but not the original, the copy will contain {@code 0}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3780
     * Such indices will exist if and only if the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3781
     * is greater than that of the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3782
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3783
     * @param original the array to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3784
     * @param newLength the length of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3785
     * @return a copy of the original array, truncated or padded with zeros
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3786
     *     to obtain the specified length
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3787
     * @throws NegativeArraySizeException if {@code newLength} is negative
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3788
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3789
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3790
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3791
    public static int[] copyOf(int[] original, int newLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3792
        int[] copy = new int[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3793
        System.arraycopy(original, 0, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3794
                         Math.min(original.length, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3795
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3796
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3797
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3798
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3799
     * Copies the specified array, truncating or padding with zeros (if necessary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3800
     * so the copy has the specified length.  For all indices that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3801
     * valid in both the original array and the copy, the two arrays will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3802
     * 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
  3803
     * copy but not the original, the copy will contain {@code 0L}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3804
     * Such indices will exist if and only if the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3805
     * is greater than that of the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3806
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3807
     * @param original the array to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3808
     * @param newLength the length of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3809
     * @return a copy of the original array, truncated or padded with zeros
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3810
     *     to obtain the specified length
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3811
     * @throws NegativeArraySizeException if {@code newLength} is negative
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3812
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3813
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3814
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3815
    public static long[] copyOf(long[] original, int newLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3816
        long[] copy = new long[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3817
        System.arraycopy(original, 0, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3818
                         Math.min(original.length, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3819
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3820
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3821
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3822
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3823
     * Copies the specified array, truncating or padding with null characters (if necessary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3824
     * so the copy has the specified length.  For all indices that are valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3825
     * in both the original array and the copy, the two arrays will contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3826
     * 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
  3827
     * the original, the copy will contain {@code '\\u000'}.  Such indices
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3828
     * will exist if and only if the specified length is greater than that of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3829
     * the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3830
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3831
     * @param original the array to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3832
     * @param newLength the length of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3833
     * @return a copy of the original array, truncated or padded with null characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3834
     *     to obtain the specified length
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3835
     * @throws NegativeArraySizeException if {@code newLength} is negative
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3836
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3837
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3838
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3839
    public static char[] copyOf(char[] original, int newLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3840
        char[] copy = new char[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3841
        System.arraycopy(original, 0, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3842
                         Math.min(original.length, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3843
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3844
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3845
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3846
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3847
     * Copies the specified array, truncating or padding with zeros (if necessary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3848
     * so the copy has the specified length.  For all indices that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3849
     * valid in both the original array and the copy, the two arrays will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3850
     * 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
  3851
     * copy but not the original, the copy will contain {@code 0f}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3852
     * Such indices will exist if and only if the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3853
     * is greater than that of the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3854
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3855
     * @param original the array to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3856
     * @param newLength the length of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3857
     * @return a copy of the original array, truncated or padded with zeros
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3858
     *     to obtain the specified length
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3859
     * @throws NegativeArraySizeException if {@code newLength} is negative
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3860
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3861
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3862
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3863
    public static float[] copyOf(float[] original, int newLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3864
        float[] copy = new float[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3865
        System.arraycopy(original, 0, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3866
                         Math.min(original.length, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3867
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3868
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3869
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3870
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3871
     * Copies the specified array, truncating or padding with zeros (if necessary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3872
     * so the copy has the specified length.  For all indices that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3873
     * valid in both the original array and the copy, the two arrays will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3874
     * 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
  3875
     * copy but not the original, the copy will contain {@code 0d}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3876
     * Such indices will exist if and only if the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3877
     * is greater than that of the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3878
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3879
     * @param original the array to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3880
     * @param newLength the length of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3881
     * @return a copy of the original array, truncated or padded with zeros
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3882
     *     to obtain the specified length
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3883
     * @throws NegativeArraySizeException if {@code newLength} is negative
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3884
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3885
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3886
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3887
    public static double[] copyOf(double[] original, int newLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3888
        double[] copy = new double[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3889
        System.arraycopy(original, 0, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3890
                         Math.min(original.length, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3891
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3892
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3893
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3894
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3895
     * Copies the specified array, truncating or padding with {@code false} (if necessary)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3896
     * so the copy has the specified length.  For all indices that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3897
     * valid in both the original array and the copy, the two arrays will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3898
     * 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
  3899
     * copy but not the original, the copy will contain {@code false}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3900
     * Such indices will exist if and only if the specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3901
     * is greater than that of the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3902
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3903
     * @param original the array to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3904
     * @param newLength the length of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3905
     * @return a copy of the original array, truncated or padded with false elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3906
     *     to obtain the specified length
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3907
     * @throws NegativeArraySizeException if {@code newLength} is negative
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3908
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3909
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3910
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3911
    public static boolean[] copyOf(boolean[] original, int newLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3912
        boolean[] copy = new boolean[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3913
        System.arraycopy(original, 0, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3914
                         Math.min(original.length, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3915
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3916
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3917
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3918
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3919
     * 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
  3920
     * 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
  3921
     * 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
  3922
     * {@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
  3923
     * (unless {@code from == original.length} or {@code from == to}).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3924
     * Values from subsequent elements in the original array are placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3925
     * 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
  3926
     * ({@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
  3927
     * 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
  3928
     * {@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
  3929
     * 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
  3930
     * of the returned array will be {@code to - from}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3931
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3932
     * The resulting array is of exactly the same class as the original array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3933
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  3934
     * @param <T> the class of the objects in the array
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3935
     * @param original the array from which a range is to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3936
     * @param from the initial index of the range to be copied, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3937
     * @param to the final index of the range to be copied, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3938
     *     (This index may lie outside the array.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3939
     * @return a new array containing the specified range from the original array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3940
     *     truncated or padded with nulls to obtain the required length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3941
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3942
     *     or {@code from > original.length}
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3943
     * @throws IllegalArgumentException if {@code from > to}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3944
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3945
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3946
     */
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  3947
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3948
    public static <T> T[] copyOfRange(T[] original, int from, int to) {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  3949
        return copyOfRange(original, from, to, (Class<? extends T[]>) original.getClass());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3950
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3951
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3952
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3953
     * 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
  3954
     * 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
  3955
     * 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
  3956
     * {@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
  3957
     * (unless {@code from == original.length} or {@code from == to}).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3958
     * Values from subsequent elements in the original array are placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3959
     * 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
  3960
     * ({@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
  3961
     * 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
  3962
     * {@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
  3963
     * 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
  3964
     * 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
  3965
     * The resulting array is of the class {@code newType}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3966
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  3967
     * @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
  3968
     * @param <T> the class of the objects in the returned array
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3969
     * @param original the array from which a range is to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3970
     * @param from the initial index of the range to be copied, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3971
     * @param to the final index of the range to be copied, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3972
     *     (This index may lie outside the array.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3973
     * @param newType the class of the copy to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3974
     * @return a new array containing the specified range from the original array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3975
     *     truncated or padded with nulls to obtain the required length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3976
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3977
     *     or {@code from > original.length}
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3978
     * @throws IllegalArgumentException if {@code from > to}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  3979
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3980
     * @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
  3981
     *     {@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
  3982
     *     an array of class {@code newType}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3983
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3984
     */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 29223
diff changeset
  3985
    @HotSpotIntrinsicCandidate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3986
    public static <T,U> T[] copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3987
        int newLength = to - from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3988
        if (newLength < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3989
            throw new IllegalArgumentException(from + " > " + to);
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  3990
        @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3991
        T[] copy = ((Object)newType == (Object)Object[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3992
            ? (T[]) new Object[newLength]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3993
            : (T[]) Array.newInstance(newType.getComponentType(), newLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3994
        System.arraycopy(original, from, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3995
                         Math.min(original.length - from, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3996
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3997
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3998
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3999
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4000
     * 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
  4001
     * 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
  4002
     * 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
  4003
     * {@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
  4004
     * (unless {@code from == original.length} or {@code from == to}).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4005
     * Values from subsequent elements in the original array are placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4006
     * 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
  4007
     * ({@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
  4008
     * 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
  4009
     * {@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
  4010
     * 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
  4011
     * of the returned array will be {@code to - from}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4012
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4013
     * @param original the array from which a range is to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4014
     * @param from the initial index of the range to be copied, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4015
     * @param to the final index of the range to be copied, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4016
     *     (This index may lie outside the array.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4017
     * @return a new array containing the specified range from the original array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4018
     *     truncated or padded with zeros to obtain the required length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4019
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4020
     *     or {@code from > original.length}
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4021
     * @throws IllegalArgumentException if {@code from > to}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4022
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4023
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4024
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4025
    public static byte[] copyOfRange(byte[] original, int from, int to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4026
        int newLength = to - from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4027
        if (newLength < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4028
            throw new IllegalArgumentException(from + " > " + to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4029
        byte[] copy = new byte[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4030
        System.arraycopy(original, from, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4031
                         Math.min(original.length - from, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4032
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4033
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4034
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4035
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4036
     * 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
  4037
     * 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
  4038
     * 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
  4039
     * {@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
  4040
     * (unless {@code from == original.length} or {@code from == to}).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4041
     * Values from subsequent elements in the original array are placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4042
     * 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
  4043
     * ({@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
  4044
     * 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
  4045
     * {@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
  4046
     * 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
  4047
     * of the returned array will be {@code to - from}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4048
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4049
     * @param original the array from which a range is to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4050
     * @param from the initial index of the range to be copied, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4051
     * @param to the final index of the range to be copied, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4052
     *     (This index may lie outside the array.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4053
     * @return a new array containing the specified range from the original array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4054
     *     truncated or padded with zeros to obtain the required length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4055
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4056
     *     or {@code from > original.length}
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4057
     * @throws IllegalArgumentException if {@code from > to}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4058
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4059
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4060
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4061
    public static short[] copyOfRange(short[] original, int from, int to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4062
        int newLength = to - from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4063
        if (newLength < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4064
            throw new IllegalArgumentException(from + " > " + to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4065
        short[] copy = new short[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4066
        System.arraycopy(original, from, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4067
                         Math.min(original.length - from, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4068
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4069
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4070
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4071
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4072
     * 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
  4073
     * 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
  4074
     * 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
  4075
     * {@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
  4076
     * (unless {@code from == original.length} or {@code from == to}).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4077
     * Values from subsequent elements in the original array are placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4078
     * 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
  4079
     * ({@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
  4080
     * 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
  4081
     * {@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
  4082
     * 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
  4083
     * of the returned array will be {@code to - from}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4084
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4085
     * @param original the array from which a range is to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4086
     * @param from the initial index of the range to be copied, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4087
     * @param to the final index of the range to be copied, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4088
     *     (This index may lie outside the array.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4089
     * @return a new array containing the specified range from the original array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4090
     *     truncated or padded with zeros to obtain the required length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4091
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4092
     *     or {@code from > original.length}
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4093
     * @throws IllegalArgumentException if {@code from > to}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4094
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4095
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4096
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4097
    public static int[] copyOfRange(int[] original, int from, int to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4098
        int newLength = to - from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4099
        if (newLength < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4100
            throw new IllegalArgumentException(from + " > " + to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4101
        int[] copy = new int[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4102
        System.arraycopy(original, from, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4103
                         Math.min(original.length - from, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4104
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4106
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4108
     * 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
  4109
     * 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
  4110
     * 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
  4111
     * {@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
  4112
     * (unless {@code from == original.length} or {@code from == to}).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4113
     * Values from subsequent elements in the original array are placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4114
     * 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
  4115
     * ({@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
  4116
     * 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
  4117
     * {@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
  4118
     * 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
  4119
     * of the returned array will be {@code to - from}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4120
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4121
     * @param original the array from which a range is to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4122
     * @param from the initial index of the range to be copied, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4123
     * @param to the final index of the range to be copied, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4124
     *     (This index may lie outside the array.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4125
     * @return a new array containing the specified range from the original array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4126
     *     truncated or padded with zeros to obtain the required length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4127
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4128
     *     or {@code from > original.length}
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4129
     * @throws IllegalArgumentException if {@code from > to}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4130
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4131
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4133
    public static long[] copyOfRange(long[] original, int from, int to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4134
        int newLength = to - from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4135
        if (newLength < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4136
            throw new IllegalArgumentException(from + " > " + to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4137
        long[] copy = new long[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4138
        System.arraycopy(original, from, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4139
                         Math.min(original.length - from, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4140
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4142
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4144
     * 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
  4145
     * 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
  4146
     * 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
  4147
     * {@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
  4148
     * (unless {@code from == original.length} or {@code from == to}).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4149
     * Values from subsequent elements in the original array are placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4150
     * 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
  4151
     * ({@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
  4152
     * 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
  4153
     * {@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
  4154
     * 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
  4155
     * of the returned array will be {@code to - from}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4156
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4157
     * @param original the array from which a range is to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4158
     * @param from the initial index of the range to be copied, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4159
     * @param to the final index of the range to be copied, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4160
     *     (This index may lie outside the array.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4161
     * @return a new array containing the specified range from the original array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4162
     *     truncated or padded with null characters to obtain the required length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4163
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4164
     *     or {@code from > original.length}
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4165
     * @throws IllegalArgumentException if {@code from > to}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4166
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4167
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4169
    public static char[] copyOfRange(char[] original, int from, int to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4170
        int newLength = to - from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4171
        if (newLength < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4172
            throw new IllegalArgumentException(from + " > " + to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4173
        char[] copy = new char[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4174
        System.arraycopy(original, from, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4175
                         Math.min(original.length - from, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4176
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4178
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4179
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4180
     * 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
  4181
     * 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
  4182
     * 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
  4183
     * {@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
  4184
     * (unless {@code from == original.length} or {@code from == to}).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4185
     * Values from subsequent elements in the original array are placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4186
     * 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
  4187
     * ({@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
  4188
     * 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
  4189
     * {@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
  4190
     * 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
  4191
     * of the returned array will be {@code to - from}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4192
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4193
     * @param original the array from which a range is to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4194
     * @param from the initial index of the range to be copied, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4195
     * @param to the final index of the range to be copied, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4196
     *     (This index may lie outside the array.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4197
     * @return a new array containing the specified range from the original array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4198
     *     truncated or padded with zeros to obtain the required length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4199
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4200
     *     or {@code from > original.length}
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4201
     * @throws IllegalArgumentException if {@code from > to}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4202
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4203
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4204
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4205
    public static float[] copyOfRange(float[] original, int from, int to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4206
        int newLength = to - from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4207
        if (newLength < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4208
            throw new IllegalArgumentException(from + " > " + to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4209
        float[] copy = new float[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4210
        System.arraycopy(original, from, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4211
                         Math.min(original.length - from, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4212
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4214
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4215
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4216
     * 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
  4217
     * 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
  4218
     * 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
  4219
     * {@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
  4220
     * (unless {@code from == original.length} or {@code from == to}).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4221
     * Values from subsequent elements in the original array are placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4222
     * 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
  4223
     * ({@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
  4224
     * 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
  4225
     * {@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
  4226
     * 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
  4227
     * of the returned array will be {@code to - from}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4228
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4229
     * @param original the array from which a range is to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4230
     * @param from the initial index of the range to be copied, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4231
     * @param to the final index of the range to be copied, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4232
     *     (This index may lie outside the array.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4233
     * @return a new array containing the specified range from the original array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4234
     *     truncated or padded with zeros to obtain the required length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4235
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4236
     *     or {@code from > original.length}
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4237
     * @throws IllegalArgumentException if {@code from > to}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4238
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4239
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4240
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4241
    public static double[] copyOfRange(double[] original, int from, int to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4242
        int newLength = to - from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4243
        if (newLength < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4244
            throw new IllegalArgumentException(from + " > " + to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4245
        double[] copy = new double[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4246
        System.arraycopy(original, from, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4247
                         Math.min(original.length - from, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4248
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4250
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4251
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4252
     * 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
  4253
     * 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
  4254
     * 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
  4255
     * {@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
  4256
     * (unless {@code from == original.length} or {@code from == to}).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4257
     * Values from subsequent elements in the original array are placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4258
     * 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
  4259
     * ({@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
  4260
     * 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
  4261
     * {@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
  4262
     * 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
  4263
     * of the returned array will be {@code to - from}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4264
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4265
     * @param original the array from which a range is to be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4266
     * @param from the initial index of the range to be copied, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4267
     * @param to the final index of the range to be copied, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4268
     *     (This index may lie outside the array.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4269
     * @return a new array containing the specified range from the original array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4270
     *     truncated or padded with false elements to obtain the required length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4271
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4272
     *     or {@code from > original.length}
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4273
     * @throws IllegalArgumentException if {@code from > to}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4274
     * @throws NullPointerException if {@code original} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4275
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4276
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4277
    public static boolean[] copyOfRange(boolean[] original, int from, int to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4278
        int newLength = to - from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4279
        if (newLength < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4280
            throw new IllegalArgumentException(from + " > " + to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4281
        boolean[] copy = new boolean[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4282
        System.arraycopy(original, from, copy, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4283
                         Math.min(original.length - from, newLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4284
        return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4286
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4287
    // Misc
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4288
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4289
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4290
     * Returns a fixed-size list backed by the specified array.  (Changes to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4291
     * the returned list "write through" to the array.)  This method acts
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4292
     * as bridge between array-based and collection-based APIs, in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4293
     * combination with {@link Collection#toArray}.  The returned list is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4294
     * serializable and implements {@link RandomAccess}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4295
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4296
     * <p>This method also provides a convenient way to create a fixed-size
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4297
     * list initialized to contain several elements:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4298
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4299
     *     List&lt;String&gt; stooges = Arrays.asList("Larry", "Moe", "Curly");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4300
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4301
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  4302
     * @param <T> the class of the objects in the array
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4303
     * @param a the array by which the list will be backed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4304
     * @return a list view of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4305
     */
8151
88b01a6d5f51 7006578: Project Coin: Retrofit JDK libraries with @SafeVarargs
darcy
parents: 7816
diff changeset
  4306
    @SafeVarargs
13795
73850c397272 7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents: 12448
diff changeset
  4307
    @SuppressWarnings("varargs")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4308
    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
  4309
        return new ArrayList<>(a);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4311
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4312
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4313
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4314
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4315
    private static class ArrayList<E> extends AbstractList<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4316
        implements RandomAccess, java.io.Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4317
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4318
        private static final long serialVersionUID = -2764017481108945198L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4319
        private final E[] a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4320
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4321
        ArrayList(E[] array) {
14925
72729557c226 8003981: Support Parallel Array Sorting - JEP 103
chegar
parents: 13795
diff changeset
  4322
            a = Objects.requireNonNull(array);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4323
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4324
14925
72729557c226 8003981: Support Parallel Array Sorting - JEP 103
chegar
parents: 13795
diff changeset
  4325
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4326
        public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4327
            return a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4329
14925
72729557c226 8003981: Support Parallel Array Sorting - JEP 103
chegar
parents: 13795
diff changeset
  4330
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4331
        public Object[] toArray() {
31540
6efd719b3330 6260652: (coll) Arrays.asList(x).toArray().getClass() should be Object[].class
martin
parents: 29223
diff changeset
  4332
            return Arrays.copyOf(a, a.length, Object[].class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4334
14925
72729557c226 8003981: Support Parallel Array Sorting - JEP 103
chegar
parents: 13795
diff changeset
  4335
        @Override
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  4336
        @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4337
        public <T> T[] toArray(T[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4338
            int size = size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4339
            if (a.length < size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4340
                return Arrays.copyOf(this.a, size,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4341
                                     (Class<? extends T[]>) a.getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4342
            System.arraycopy(this.a, 0, a, 0, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4343
            if (a.length > size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4344
                a[size] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4345
            return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4346
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4347
14925
72729557c226 8003981: Support Parallel Array Sorting - JEP 103
chegar
parents: 13795
diff changeset
  4348
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4349
        public E get(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4350
            return a[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4351
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4352
14925
72729557c226 8003981: Support Parallel Array Sorting - JEP 103
chegar
parents: 13795
diff changeset
  4353
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4354
        public E set(int index, E element) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4355
            E oldValue = a[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4356
            a[index] = element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4357
            return oldValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4358
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4359
14925
72729557c226 8003981: Support Parallel Array Sorting - JEP 103
chegar
parents: 13795
diff changeset
  4360
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4361
        public int indexOf(Object o) {
23354
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4362
            E[] a = this.a;
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4363
            if (o == null) {
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4364
                for (int i = 0; i < a.length; i++)
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4365
                    if (a[i] == null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4366
                        return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4367
            } else {
23354
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4368
                for (int i = 0; i < a.length; i++)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4369
                    if (o.equals(a[i]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4370
                        return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4371
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4372
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4373
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4374
14925
72729557c226 8003981: Support Parallel Array Sorting - JEP 103
chegar
parents: 13795
diff changeset
  4375
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4376
        public boolean contains(Object o) {
26451
f86e59f18322 8056951: pico-optimize contains(Object) methods
martin
parents: 25859
diff changeset
  4377
            return indexOf(o) >= 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4378
        }
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4379
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4380
        @Override
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4381
        public Spliterator<E> spliterator() {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4382
            return Spliterators.spliterator(a, Spliterator.ORDERED);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  4383
        }
23354
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4384
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4385
        @Override
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4386
        public void forEach(Consumer<? super E> action) {
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4387
            Objects.requireNonNull(action);
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4388
            for (E e : a) {
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4389
                action.accept(e);
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4390
            }
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
        @Override
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4394
        public void replaceAll(UnaryOperator<E> operator) {
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4395
            Objects.requireNonNull(operator);
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4396
            E[] a = this.a;
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4397
            for (int i = 0; i < a.length; i++) {
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4398
                a[i] = operator.apply(a[i]);
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4399
            }
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
        @Override
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4403
        public void sort(Comparator<? super E> c) {
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4404
            Arrays.sort(a, c);
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 22954
diff changeset
  4405
        }
37685
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4406
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4407
        @Override
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4408
        public Iterator<E> iterator() {
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4409
            return new ArrayItr<>(a);
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4410
        }
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
    private static class ArrayItr<E> implements Iterator<E> {
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4414
        private int cursor;
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4415
        private final E[] a;
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4416
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4417
        ArrayItr(E[] a) {
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4418
            this.a = a;
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4419
        }
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
        @Override
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4422
        public boolean hasNext() {
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4423
            return cursor < a.length;
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4424
        }
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
        @Override
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4427
        public E next() {
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4428
            int i = cursor;
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4429
            if (i >= a.length) {
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4430
                throw new NoSuchElementException();
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4431
            }
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4432
            cursor = i + 1;
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4433
            return a[i];
6045bfdd65a5 8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents: 35255
diff changeset
  4434
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4435
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4436
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4437
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4438
     * 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
  4439
     * 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
  4440
     * 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
  4441
     * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4442
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4443
     * <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
  4444
     * obtained by invoking the {@link List#hashCode() hashCode}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4445
     * 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
  4446
     * 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
  4447
     * If {@code a} is {@code null}, this method returns 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4448
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4449
     * @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
  4450
     * @return a content-based hash code for {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4451
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4452
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4453
    public static int hashCode(long a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4454
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4455
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4456
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4457
        int result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4458
        for (long element : a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4459
            int elementHash = (int)(element ^ (element >>> 32));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4460
            result = 31 * result + elementHash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4461
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4462
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4463
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4464
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4465
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4466
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4467
     * 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
  4468
     * 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
  4469
     * 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
  4470
     * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4471
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4472
     * <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
  4473
     * obtained by invoking the {@link List#hashCode() hashCode}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4474
     * 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
  4475
     * 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
  4476
     * If {@code a} is {@code null}, this method returns 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4477
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4478
     * @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
  4479
     * @return a content-based hash code for {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4480
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4481
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4482
    public static int hashCode(int a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4483
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4484
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4485
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4486
        int result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4487
        for (int element : a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4488
            result = 31 * result + element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4489
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4490
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4491
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4492
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4493
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4494
     * 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
  4495
     * 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
  4496
     * 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
  4497
     * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4498
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4499
     * <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
  4500
     * obtained by invoking the {@link List#hashCode() hashCode}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4501
     * 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
  4502
     * 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
  4503
     * If {@code a} is {@code null}, this method returns 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4504
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4505
     * @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
  4506
     * @return a content-based hash code for {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4507
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4508
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4509
    public static int hashCode(short a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4510
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4511
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4512
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4513
        int result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4514
        for (short element : a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4515
            result = 31 * result + element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4516
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4517
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4518
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4519
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4520
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4521
     * 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
  4522
     * 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
  4523
     * 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
  4524
     * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4525
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4526
     * <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
  4527
     * obtained by invoking the {@link List#hashCode() hashCode}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4528
     * 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
  4529
     * 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
  4530
     * If {@code a} is {@code null}, this method returns 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4531
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4532
     * @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
  4533
     * @return a content-based hash code for {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4534
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4535
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4536
    public static int hashCode(char a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4537
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4538
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4539
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4540
        int result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4541
        for (char element : a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4542
            result = 31 * result + element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4543
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4544
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4545
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4546
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4547
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4548
     * 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
  4549
     * 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
  4550
     * 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
  4551
     * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4552
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4553
     * <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
  4554
     * obtained by invoking the {@link List#hashCode() hashCode}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4555
     * 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
  4556
     * 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
  4557
     * If {@code a} is {@code null}, this method returns 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4558
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4559
     * @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
  4560
     * @return a content-based hash code for {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4561
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4562
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4563
    public static int hashCode(byte a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4564
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4565
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4566
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4567
        int result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4568
        for (byte element : a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4569
            result = 31 * result + element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4570
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4571
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4572
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4573
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4574
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4575
     * 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
  4576
     * 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
  4577
     * 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
  4578
     * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4579
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4580
     * <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
  4581
     * obtained by invoking the {@link List#hashCode() hashCode}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4582
     * 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
  4583
     * 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
  4584
     * If {@code a} is {@code null}, this method returns 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4585
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4586
     * @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
  4587
     * @return a content-based hash code for {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4588
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4589
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4590
    public static int hashCode(boolean a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4591
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4592
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4593
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4594
        int result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4595
        for (boolean element : a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4596
            result = 31 * result + (element ? 1231 : 1237);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4597
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4598
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4599
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4600
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4601
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4602
     * 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
  4603
     * 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
  4604
     * 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
  4605
     * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4606
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4607
     * <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
  4608
     * obtained by invoking the {@link List#hashCode() hashCode}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4609
     * 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
  4610
     * 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
  4611
     * If {@code a} is {@code null}, this method returns 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4612
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4613
     * @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
  4614
     * @return a content-based hash code for {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4615
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4616
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4617
    public static int hashCode(float a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4618
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4619
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4620
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4621
        int result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4622
        for (float element : a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4623
            result = 31 * result + Float.floatToIntBits(element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4624
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4625
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4626
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4627
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4628
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4629
     * 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
  4630
     * 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
  4631
     * 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
  4632
     * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4633
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4634
     * <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
  4635
     * obtained by invoking the {@link List#hashCode() hashCode}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4636
     * 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
  4637
     * 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
  4638
     * If {@code a} is {@code null}, this method returns 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4639
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4640
     * @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
  4641
     * @return a content-based hash code for {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4642
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4643
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4644
    public static int hashCode(double a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4645
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4646
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4647
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4648
        int result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4649
        for (double element : a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4650
            long bits = Double.doubleToLongBits(element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4651
            result = 31 * result + (int)(bits ^ (bits >>> 32));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4652
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4653
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4654
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4655
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4656
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4657
     * Returns a hash code based on the contents of the specified array.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4658
     * the array contains other arrays as elements, the hash code is based on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4659
     * their identities rather than their contents.  It is therefore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4660
     * acceptable to invoke this method on an array that contains itself as an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4661
     * element,  either directly or indirectly through one or more levels of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4662
     * arrays.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4663
     *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4664
     * <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
  4665
     * {@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
  4666
     * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4667
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4668
     * <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
  4669
     * 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
  4670
     * is {@code null}, in which case {@code 0} is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4671
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4672
     * @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
  4673
     * @return a content-based hash code for {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4674
     * @see #deepHashCode(Object[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4675
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4676
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4677
    public static int hashCode(Object a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4678
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4679
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4680
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4681
        int result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4682
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4683
        for (Object element : a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4684
            result = 31 * result + (element == null ? 0 : element.hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4685
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4686
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4687
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4688
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4689
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4690
     * Returns a hash code based on the "deep contents" of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4691
     * array.  If the array contains other arrays as elements, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4692
     * hash code is based on their contents and so on, ad infinitum.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4693
     * It is therefore unacceptable to invoke this method on an array that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4694
     * contains itself as an element, either directly or indirectly through
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4695
     * one or more levels of arrays.  The behavior of such an invocation is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4696
     * undefined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4697
     *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4698
     * <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
  4699
     * {@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
  4700
     * {@code Arrays.deepHashCode(a) == Arrays.deepHashCode(b)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4701
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4702
     * <p>The computation of the value returned by this method is similar to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4703
     * 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
  4704
     * 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
  4705
     * 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
  4706
     * 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
  4707
     * 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
  4708
     * 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
  4709
     * {@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
  4710
     * of a reference type.  If {@code a} is {@code null}, this method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4711
     * returns 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4712
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4713
     * @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
  4714
     * @return a deep-content-based hash code for {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4715
     * @see #hashCode(Object[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4716
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4717
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4718
    public static int deepHashCode(Object a[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4719
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4720
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4721
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4722
        int result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4723
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4724
        for (Object element : a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4725
            int elementHash = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4726
            if (element instanceof Object[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4727
                elementHash = deepHashCode((Object[]) element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4728
            else if (element instanceof byte[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4729
                elementHash = hashCode((byte[]) element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4730
            else if (element instanceof short[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4731
                elementHash = hashCode((short[]) element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4732
            else if (element instanceof int[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4733
                elementHash = hashCode((int[]) element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4734
            else if (element instanceof long[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4735
                elementHash = hashCode((long[]) element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4736
            else if (element instanceof char[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4737
                elementHash = hashCode((char[]) element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4738
            else if (element instanceof float[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4739
                elementHash = hashCode((float[]) element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4740
            else if (element instanceof double[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4741
                elementHash = hashCode((double[]) element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4742
            else if (element instanceof boolean[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4743
                elementHash = hashCode((boolean[]) element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4744
            else if (element != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4745
                elementHash = element.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4746
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4747
            result = 31 * result + elementHash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4748
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4749
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4750
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4751
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4752
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4753
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4754
     * Returns {@code true} if the two specified arrays are <i>deeply
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4755
     * equal</i> to one another.  Unlike the {@link #equals(Object[],Object[])}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4756
     * method, this method is appropriate for use with nested arrays of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4757
     * arbitrary depth.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4758
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4759
     * <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
  4760
     * are {@code null}, or if they refer to arrays that contain the same
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4761
     * number of elements and all corresponding pairs of elements in the two
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4762
     * arrays are deeply equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4763
     *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4764
     * <p>Two possibly {@code null} elements {@code e1} and {@code e2} are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4765
     * deeply equal if any of the following conditions hold:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4766
     * <ul>
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4767
     *    <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
  4768
     *         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
  4769
     *    <li> {@code e1} and {@code e2} are arrays of the same primitive
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4770
     *         type, and the appropriate overloading of
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4771
     *         {@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
  4772
     *    <li> {@code e1 == e2}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4773
     *    <li> {@code e1.equals(e2)} would return true.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4774
     * </ul>
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4775
     * Note that this definition permits {@code null} elements at any depth.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4776
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4777
     * <p>If either of the specified arrays contain themselves as elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4778
     * either directly or indirectly through one or more levels of arrays,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4779
     * the behavior of this method is undefined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4780
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4781
     * @param a1 one array to be tested for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4782
     * @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
  4783
     * @return {@code true} if the two arrays are equal
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4784
     * @see #equals(Object[],Object[])
4165
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4785
     * @see Objects#deepEquals(Object, Object)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4786
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4787
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4788
    public static boolean deepEquals(Object[] a1, Object[] a2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4789
        if (a1 == a2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4790
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4791
        if (a1 == null || a2==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4792
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4793
        int length = a1.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4794
        if (a2.length != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4795
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4796
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4797
        for (int i = 0; i < length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4798
            Object e1 = a1[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4799
            Object e2 = a2[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4800
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4801
            if (e1 == e2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4802
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4803
            if (e1 == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4804
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4805
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4806
            // 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
  4807
            boolean eq = deepEquals0(e1, e2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4808
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4809
            if (!eq)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4810
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4811
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4812
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4813
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4814
4165
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4815
    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
  4816
        assert e1 != null;
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4817
        boolean eq;
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4818
        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
  4819
            eq = deepEquals ((Object[]) e1, (Object[]) e2);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4820
        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
  4821
            eq = equals((byte[]) e1, (byte[]) e2);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4822
        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
  4823
            eq = equals((short[]) e1, (short[]) e2);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4824
        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
  4825
            eq = equals((int[]) e1, (int[]) e2);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4826
        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
  4827
            eq = equals((long[]) e1, (long[]) e2);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4828
        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
  4829
            eq = equals((char[]) e1, (char[]) e2);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4830
        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
  4831
            eq = equals((float[]) e1, (float[]) e2);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4832
        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
  4833
            eq = equals((double[]) e1, (double[]) e2);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4834
        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
  4835
            eq = equals((boolean[]) e1, (boolean[]) e2);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4836
        else
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4837
            eq = e1.equals(e2);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4838
        return eq;
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4839
    }
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 3420
diff changeset
  4840
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4841
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4842
     * Returns a string representation of the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4843
     * 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
  4844
     * 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
  4845
     * separated by the characters {@code ", "} (a comma followed by a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4846
     * 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
  4847
     * {@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
  4848
     * is {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4849
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4850
     * @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
  4851
     * @return a string representation of {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4852
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4853
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4854
    public static String toString(long[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4855
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4856
            return "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4857
        int iMax = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4858
        if (iMax == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4859
            return "[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4860
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4861
        StringBuilder b = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4862
        b.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4863
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4864
            b.append(a[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4865
            if (i == iMax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4866
                return b.append(']').toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4867
            b.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4868
        }
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
     * Returns a string representation of the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4873
     * 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
  4874
     * 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
  4875
     * separated by the characters {@code ", "} (a comma followed by a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4876
     * 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
  4877
     * {@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
  4878
     * {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4879
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4880
     * @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
  4881
     * @return a string representation of {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4882
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4883
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4884
    public static String toString(int[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4885
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4886
            return "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4887
        int iMax = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4888
        if (iMax == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4889
            return "[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4890
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4891
        StringBuilder b = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4892
        b.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4893
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4894
            b.append(a[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4895
            if (i == iMax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4896
                return b.append(']').toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4897
            b.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4898
        }
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
     * Returns a string representation of the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4903
     * 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
  4904
     * 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
  4905
     * separated by the characters {@code ", "} (a comma followed by a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4906
     * 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
  4907
     * {@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
  4908
     * is {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4909
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4910
     * @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
  4911
     * @return a string representation of {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4912
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4913
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4914
    public static String toString(short[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4915
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4916
            return "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4917
        int iMax = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4918
        if (iMax == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4919
            return "[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4920
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4921
        StringBuilder b = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4922
        b.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4923
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4924
            b.append(a[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4925
            if (i == iMax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4926
                return b.append(']').toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4927
            b.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4928
        }
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
     * Returns a string representation of the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4933
     * 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
  4934
     * 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
  4935
     * separated by the characters {@code ", "} (a comma followed by a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4936
     * 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
  4937
     * {@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
  4938
     * is {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4939
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4940
     * @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
  4941
     * @return a string representation of {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4942
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4943
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4944
    public static String toString(char[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4945
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4946
            return "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4947
        int iMax = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4948
        if (iMax == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4949
            return "[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4950
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4951
        StringBuilder b = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4952
        b.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4953
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4954
            b.append(a[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4955
            if (i == iMax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4956
                return b.append(']').toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4957
            b.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4958
        }
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
     * Returns a string representation of the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4963
     * 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
  4964
     * enclosed in square brackets ({@code "[]"}).  Adjacent elements
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  4965
     * are separated by the characters {@code ", "} (a comma followed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4966
     * 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
  4967
     * {@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
  4968
     * {@code a} is {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4969
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4970
     * @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
  4971
     * @return a string representation of {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4972
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4973
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4974
    public static String toString(byte[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4975
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4976
            return "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4977
        int iMax = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4978
        if (iMax == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4979
            return "[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4980
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4981
        StringBuilder b = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4982
        b.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4983
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4984
            b.append(a[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4985
            if (i == iMax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4986
                return b.append(']').toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4987
            b.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4988
        }
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
     * Returns a string representation of the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4993
     * 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
  4994
     * 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
  4995
     * separated by the characters {@code ", "} (a comma followed by a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4996
     * 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
  4997
     * {@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
  4998
     * {@code a} is {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4999
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5000
     * @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
  5001
     * @return a string representation of {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5002
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5003
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5004
    public static String toString(boolean[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5005
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5006
            return "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5007
        int iMax = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5008
        if (iMax == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5009
            return "[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5010
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5011
        StringBuilder b = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5012
        b.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5013
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5014
            b.append(a[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5015
            if (i == iMax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5016
                return b.append(']').toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5017
            b.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5018
        }
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
     * Returns a string representation of the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5023
     * 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
  5024
     * 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
  5025
     * separated by the characters {@code ", "} (a comma followed by a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5026
     * 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
  5027
     * {@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
  5028
     * is {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5029
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5030
     * @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
  5031
     * @return a string representation of {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5032
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5033
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5034
    public static String toString(float[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5035
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5036
            return "null";
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
  5037
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5038
        int iMax = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5039
        if (iMax == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5040
            return "[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5041
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5042
        StringBuilder b = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5043
        b.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5044
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5045
            b.append(a[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5046
            if (i == iMax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5047
                return b.append(']').toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5048
            b.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5049
        }
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
     * Returns a string representation of the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5054
     * 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
  5055
     * 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
  5056
     * separated by the characters {@code ", "} (a comma followed by a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5057
     * 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
  5058
     * {@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
  5059
     * is {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5060
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5061
     * @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
  5062
     * @return a string representation of {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5063
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5064
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5065
    public static String toString(double[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5066
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5067
            return "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5068
        int iMax = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5069
        if (iMax == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5070
            return "[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5071
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5072
        StringBuilder b = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5073
        b.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5074
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5075
            b.append(a[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5076
            if (i == iMax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5077
                return b.append(']').toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5078
            b.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5079
        }
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
     * Returns a string representation of the contents of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5084
     * If the array contains other arrays as elements, they are converted to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5085
     * 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
  5086
     * {@code Object}, which describes their <i>identities</i> rather than
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5087
     * their contents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5088
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5089
     * <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
  5090
     * 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
  5091
     * is {@code null}, in which case {@code "null"} is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5092
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5093
     * @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
  5094
     * @return a string representation of {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5095
     * @see #deepToString(Object[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5096
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5097
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5098
    public static String toString(Object[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5099
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5100
            return "null";
4170
a94a6faf44e6 6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents: 4165
diff changeset
  5101
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5102
        int iMax = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5103
        if (iMax == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5104
            return "[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5105
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5106
        StringBuilder b = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5107
        b.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5108
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5109
            b.append(String.valueOf(a[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5110
            if (i == iMax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5111
                return b.append(']').toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5112
            b.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5113
        }
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
     * Returns a string representation of the "deep contents" of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5118
     * array.  If the array contains other arrays as elements, the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5119
     * representation contains their contents and so on.  This method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5120
     * designed for converting multidimensional arrays to strings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5121
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5122
     * <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
  5123
     * elements, enclosed in square brackets ({@code "[]"}).  Adjacent
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  5124
     * elements are separated by the characters {@code ", "} (a comma
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5125
     * 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
  5126
     * {@code String.valueOf(Object)}, unless they are themselves
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5127
     * arrays.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5128
     *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  5129
     * <p>If an element {@code e} is an array of a primitive type, it is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5130
     * 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
  5131
     * {@code Arrays.toString(e)}.  If an element {@code e} is an array of a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5132
     * reference type, it is converted to a string as by invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5133
     * this method recursively.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5134
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5135
     * <p>To avoid infinite recursion, if the specified array contains itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5136
     * as an element, or contains an indirect reference to itself through one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5137
     * 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
  5138
     * {@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
  5139
     * to itself would be rendered as {@code "[[...]]"}.
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  5140
     *
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31678
diff changeset
  5141
     * <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
  5142
     * is {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5143
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5144
     * @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
  5145
     * @return a string representation of {@code a}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5146
     * @see #toString(Object[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5147
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5148
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5149
    public static String deepToString(Object[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5150
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5151
            return "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5152
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5153
        int bufLen = 20 * a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5154
        if (a.length != 0 && bufLen <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5155
            bufLen = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5156
        StringBuilder buf = new StringBuilder(bufLen);
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 21278
diff changeset
  5157
        deepToString(a, buf, new HashSet<>());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5158
        return buf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5160
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5161
    private static void deepToString(Object[] a, StringBuilder buf,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5162
                                     Set<Object[]> dejaVu) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5163
        if (a == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5164
            buf.append("null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5165
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5167
        int iMax = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5168
        if (iMax == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5169
            buf.append("[]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5170
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5172
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5173
        dejaVu.add(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5174
        buf.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5175
        for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5176
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5177
            Object element = a[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5178
            if (element == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5179
                buf.append("null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5180
            } else {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  5181
                Class<?> eClass = element.getClass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5182
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5183
                if (eClass.isArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5184
                    if (eClass == byte[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5185
                        buf.append(toString((byte[]) element));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5186
                    else if (eClass == short[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5187
                        buf.append(toString((short[]) element));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5188
                    else if (eClass == int[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5189
                        buf.append(toString((int[]) element));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5190
                    else if (eClass == long[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5191
                        buf.append(toString((long[]) element));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5192
                    else if (eClass == char[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5193
                        buf.append(toString((char[]) element));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5194
                    else if (eClass == float[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5195
                        buf.append(toString((float[]) element));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5196
                    else if (eClass == double[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5197
                        buf.append(toString((double[]) element));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5198
                    else if (eClass == boolean[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5199
                        buf.append(toString((boolean[]) element));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5200
                    else { // element is an array of object references
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5201
                        if (dejaVu.contains(element))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5202
                            buf.append("[...]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5203
                        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5204
                            deepToString((Object[])element, buf, dejaVu);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5205
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5206
                } else {  // element is non-null and not an array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5207
                    buf.append(element.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5208
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5209
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5210
            if (i == iMax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5211
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5212
            buf.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5214
        buf.append(']');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5215
        dejaVu.remove(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5216
    }
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5217
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5218
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5219
    /**
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5220
     * Set all elements of the specified array, using the provided
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5221
     * generator function to compute each element.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5222
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5223
     * <p>If the generator function throws an exception, it is relayed to
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5224
     * the caller and the array is left in an indeterminate state.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5225
     *
29223
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5226
     * @apiNote
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5227
     * 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
  5228
     * each element, can be written as follows:
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5229
     * <pre>{@code
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5230
     * IntStream.range(startInclusive, endExclusive)
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5231
     *          .forEach(i -> array[i] = generator.apply(i));
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5232
     * }</pre>
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5233
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5234
     * @param <T> type of elements of the array
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5235
     * @param array array to be initialized
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5236
     * @param generator a function accepting an index and producing the desired
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5237
     *        value for that position
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5238
     * @throws NullPointerException if the generator is null
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5239
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5240
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5241
    public static <T> void setAll(T[] array, IntFunction<? extends T> generator) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5242
        Objects.requireNonNull(generator);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5243
        for (int i = 0; i < array.length; i++)
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5244
            array[i] = generator.apply(i);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5245
    }
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
     * Set all elements of the specified array, in parallel, using the
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5249
     * provided generator function to compute each element.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5250
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5251
     * <p>If the generator function throws an exception, an unchecked exception
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5252
     * is thrown from {@code parallelSetAll} and the array is left in an
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5253
     * indeterminate state.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5254
     *
29223
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5255
     * @apiNote
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5256
     * 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
  5257
     * to compute each element, can be written as follows:
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5258
     * <pre>{@code
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5259
     * IntStream.range(startInclusive, endExclusive)
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5260
     *          .parallel()
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5261
     *          .forEach(i -> array[i] = generator.apply(i));
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5262
     * }</pre>
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5263
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5264
     * @param <T> type of elements of the array
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5265
     * @param array array to be initialized
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5266
     * @param generator a function accepting an index and producing the desired
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5267
     *        value for that position
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5268
     * @throws NullPointerException if the generator is null
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5269
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5270
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5271
    public static <T> void parallelSetAll(T[] array, IntFunction<? extends T> generator) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5272
        Objects.requireNonNull(generator);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5273
        IntStream.range(0, array.length).parallel().forEach(i -> { array[i] = generator.apply(i); });
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5274
    }
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
     * Set all elements of the specified array, using the provided
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5278
     * generator function to compute each element.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5279
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5280
     * <p>If the generator function throws an exception, it is relayed to
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5281
     * the caller and the array is left in an indeterminate state.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5282
     *
29223
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5283
     * @apiNote
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5284
     * 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
  5285
     * each element, can be written as follows:
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5286
     * <pre>{@code
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5287
     * IntStream.range(startInclusive, endExclusive)
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5288
     *          .forEach(i -> array[i] = generator.applyAsInt(i));
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5289
     * }</pre>
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5290
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5291
     * @param array array to be initialized
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5292
     * @param generator a function accepting an index and producing the desired
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5293
     *        value for that position
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5294
     * @throws NullPointerException if the generator is null
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5295
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5296
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5297
    public static void setAll(int[] array, IntUnaryOperator generator) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5298
        Objects.requireNonNull(generator);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5299
        for (int i = 0; i < array.length; i++)
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5300
            array[i] = generator.applyAsInt(i);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5301
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5302
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5303
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5304
     * Set all elements of the specified array, in parallel, using the
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5305
     * provided generator function to compute each element.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5306
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5307
     * <p>If the generator function throws an exception, an unchecked exception
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5308
     * is thrown from {@code parallelSetAll} and the array is left in an
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5309
     * indeterminate state.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5310
     *
29223
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5311
     * @apiNote
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5312
     * 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
  5313
     * to compute each element, can be written as follows:
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5314
     * <pre>{@code
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5315
     * IntStream.range(startInclusive, endExclusive)
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5316
     *          .parallel()
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5317
     *          .forEach(i -> array[i] = generator.applyAsInt(i));
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5318
     * }</pre>
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5319
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5320
     * @param array array to be initialized
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5321
     * @param generator a function accepting an index and producing the desired
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5322
     * value for that position
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5323
     * @throws NullPointerException if the generator is null
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5324
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5325
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5326
    public static void parallelSetAll(int[] array, IntUnaryOperator generator) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5327
        Objects.requireNonNull(generator);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5328
        IntStream.range(0, array.length).parallel().forEach(i -> { array[i] = generator.applyAsInt(i); });
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5329
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5330
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5331
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5332
     * Set all elements of the specified array, using the provided
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5333
     * generator function to compute each element.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5334
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5335
     * <p>If the generator function throws an exception, it is relayed to
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5336
     * the caller and the array is left in an indeterminate state.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5337
     *
29223
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5338
     * @apiNote
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5339
     * Setting a subrange of an array, using a generator function to compute
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5340
     * each element, can be written as follows:
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5341
     * <pre>{@code
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5342
     * IntStream.range(startInclusive, endExclusive)
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5343
     *          .forEach(i -> array[i] = generator.applyAsLong(i));
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5344
     * }</pre>
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5345
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5346
     * @param array array to be initialized
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5347
     * @param generator a function accepting an index and producing the desired
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5348
     *        value for that position
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5349
     * @throws NullPointerException if the generator is null
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5350
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5351
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5352
    public static void setAll(long[] array, IntToLongFunction generator) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5353
        Objects.requireNonNull(generator);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5354
        for (int i = 0; i < array.length; i++)
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5355
            array[i] = generator.applyAsLong(i);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5356
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5357
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5358
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5359
     * Set all elements of the specified array, in parallel, using the
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5360
     * provided generator function to compute each element.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5361
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5362
     * <p>If the generator function throws an exception, an unchecked exception
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5363
     * is thrown from {@code parallelSetAll} and the array is left in an
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5364
     * indeterminate state.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5365
     *
29223
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5366
     * @apiNote
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5367
     * 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
  5368
     * to compute each element, can be written as follows:
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5369
     * <pre>{@code
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5370
     * IntStream.range(startInclusive, endExclusive)
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5371
     *          .parallel()
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5372
     *          .forEach(i -> array[i] = generator.applyAsLong(i));
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5373
     * }</pre>
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5374
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5375
     * @param array array to be initialized
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5376
     * @param generator a function accepting an index and producing the desired
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5377
     *        value for that position
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5378
     * @throws NullPointerException if the generator is null
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5379
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5380
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5381
    public static void parallelSetAll(long[] array, IntToLongFunction generator) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5382
        Objects.requireNonNull(generator);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5383
        IntStream.range(0, array.length).parallel().forEach(i -> { array[i] = generator.applyAsLong(i); });
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5384
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5385
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5386
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5387
     * Set all elements of the specified array, using the provided
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5388
     * generator function to compute each element.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5389
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5390
     * <p>If the generator function throws an exception, it is relayed to
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5391
     * the caller and the array is left in an indeterminate state.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5392
     *
29223
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5393
     * @apiNote
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5394
     * Setting a subrange of an array, using a generator function to compute
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5395
     * each element, can be written as follows:
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5396
     * <pre>{@code
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5397
     * IntStream.range(startInclusive, endExclusive)
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5398
     *          .forEach(i -> array[i] = generator.applyAsDouble(i));
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5399
     * }</pre>
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5400
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5401
     * @param array array to be initialized
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5402
     * @param generator a function accepting an index and producing the desired
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5403
     *        value for that position
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5404
     * @throws NullPointerException if the generator is null
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5405
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5406
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5407
    public static void setAll(double[] array, IntToDoubleFunction generator) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5408
        Objects.requireNonNull(generator);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5409
        for (int i = 0; i < array.length; i++)
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5410
            array[i] = generator.applyAsDouble(i);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5411
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5412
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5413
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5414
     * Set all elements of the specified array, in parallel, using the
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5415
     * provided generator function to compute each element.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5416
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5417
     * <p>If the generator function throws an exception, an unchecked exception
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5418
     * is thrown from {@code parallelSetAll} and the array is left in an
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5419
     * indeterminate state.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5420
     *
29223
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5421
     * @apiNote
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5422
     * 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
  5423
     * to compute each element, can be written as follows:
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5424
     * <pre>{@code
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5425
     * IntStream.range(startInclusive, endExclusive)
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5426
     *          .parallel()
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5427
     *          .forEach(i -> array[i] = generator.applyAsDouble(i));
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5428
     * }</pre>
a9f00e8ae58b 8074043: java.util.Arrays setAll and parallelSetAll subrange note
chegar
parents: 28059
diff changeset
  5429
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5430
     * @param array array to be initialized
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5431
     * @param generator a function accepting an index and producing the desired
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5432
     *        value for that position
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5433
     * @throws NullPointerException if the generator is null
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5434
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5435
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5436
    public static void parallelSetAll(double[] array, IntToDoubleFunction generator) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5437
        Objects.requireNonNull(generator);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5438
        IntStream.range(0, array.length).parallel().forEach(i -> { array[i] = generator.applyAsDouble(i); });
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5439
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5440
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5441
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5442
     * Returns a {@link Spliterator} covering all of the specified array.
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5443
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5444
     * <p>The spliterator reports {@link Spliterator#SIZED},
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5445
     * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5446
     * {@link Spliterator#IMMUTABLE}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5447
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5448
     * @param <T> type of elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5449
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5450
     * @return a spliterator for the array elements
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5451
     * @since 1.8
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5452
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5453
    public static <T> Spliterator<T> spliterator(T[] array) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5454
        return Spliterators.spliterator(array,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5455
                                        Spliterator.ORDERED | Spliterator.IMMUTABLE);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5456
    }
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
    /**
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5459
     * Returns a {@link Spliterator} covering the specified range of the
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5460
     * specified array.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5461
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5462
     * <p>The spliterator reports {@link Spliterator#SIZED},
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5463
     * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5464
     * {@link Spliterator#IMMUTABLE}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5465
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5466
     * @param <T> type of elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5467
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5468
     * @param startInclusive the first index to cover, inclusive
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5469
     * @param endExclusive index immediately past the last index to cover
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5470
     * @return a spliterator for the array elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5471
     * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5472
     *         negative, {@code endExclusive} is less than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5473
     *         {@code startInclusive}, or {@code endExclusive} is greater than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5474
     *         the array size
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5475
     * @since 1.8
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5476
     */
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5477
    public static <T> Spliterator<T> spliterator(T[] array, int startInclusive, int endExclusive) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5478
        return Spliterators.spliterator(array, startInclusive, endExclusive,
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5479
                                        Spliterator.ORDERED | Spliterator.IMMUTABLE);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5480
    }
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
    /**
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5483
     * Returns a {@link Spliterator.OfInt} covering all of the specified array.
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5484
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5485
     * <p>The spliterator reports {@link Spliterator#SIZED},
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5486
     * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5487
     * {@link Spliterator#IMMUTABLE}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5488
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5489
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5490
     * @return a spliterator for the array elements
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5491
     * @since 1.8
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5492
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5493
    public static Spliterator.OfInt spliterator(int[] array) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5494
        return Spliterators.spliterator(array,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5495
                                        Spliterator.ORDERED | Spliterator.IMMUTABLE);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5496
    }
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
    /**
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5499
     * Returns a {@link Spliterator.OfInt} covering the specified range of the
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5500
     * specified array.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5501
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5502
     * <p>The spliterator reports {@link Spliterator#SIZED},
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5503
     * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5504
     * {@link Spliterator#IMMUTABLE}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5505
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5506
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5507
     * @param startInclusive the first index to cover, inclusive
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5508
     * @param endExclusive index immediately past the last index to cover
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5509
     * @return a spliterator for the array elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5510
     * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5511
     *         negative, {@code endExclusive} is less than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5512
     *         {@code startInclusive}, or {@code endExclusive} is greater than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5513
     *         the array size
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5514
     * @since 1.8
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5515
     */
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5516
    public static Spliterator.OfInt spliterator(int[] array, int startInclusive, int endExclusive) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5517
        return Spliterators.spliterator(array, startInclusive, endExclusive,
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5518
                                        Spliterator.ORDERED | Spliterator.IMMUTABLE);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5519
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5520
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5521
    /**
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5522
     * Returns a {@link Spliterator.OfLong} covering all of the specified array.
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5523
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5524
     * <p>The spliterator reports {@link Spliterator#SIZED},
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5525
     * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5526
     * {@link Spliterator#IMMUTABLE}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5527
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5528
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5529
     * @return the spliterator for the array elements
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5530
     * @since 1.8
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5531
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5532
    public static Spliterator.OfLong spliterator(long[] array) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5533
        return Spliterators.spliterator(array,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5534
                                        Spliterator.ORDERED | Spliterator.IMMUTABLE);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5535
    }
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
    /**
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5538
     * Returns a {@link Spliterator.OfLong} covering the specified range of the
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5539
     * specified array.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5540
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5541
     * <p>The spliterator reports {@link Spliterator#SIZED},
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5542
     * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5543
     * {@link Spliterator#IMMUTABLE}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5544
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5545
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5546
     * @param startInclusive the first index to cover, inclusive
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5547
     * @param endExclusive index immediately past the last index to cover
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5548
     * @return a spliterator for the array elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5549
     * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5550
     *         negative, {@code endExclusive} is less than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5551
     *         {@code startInclusive}, or {@code endExclusive} is greater than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5552
     *         the array size
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5553
     * @since 1.8
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5554
     */
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5555
    public static Spliterator.OfLong spliterator(long[] array, int startInclusive, int endExclusive) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5556
        return Spliterators.spliterator(array, startInclusive, endExclusive,
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5557
                                        Spliterator.ORDERED | Spliterator.IMMUTABLE);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5558
    }
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5559
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5560
    /**
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5561
     * Returns a {@link Spliterator.OfDouble} covering all of the specified
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5562
     * array.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5563
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5564
     * <p>The spliterator reports {@link Spliterator#SIZED},
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5565
     * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5566
     * {@link Spliterator#IMMUTABLE}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5567
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5568
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5569
     * @return a spliterator for the array elements
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5570
     * @since 1.8
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5571
     */
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5572
    public static Spliterator.OfDouble spliterator(double[] array) {
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5573
        return Spliterators.spliterator(array,
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5574
                                        Spliterator.ORDERED | Spliterator.IMMUTABLE);
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5575
    }
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
    /**
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5578
     * Returns a {@link Spliterator.OfDouble} covering the specified range of
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5579
     * the specified array.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5580
     *
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5581
     * <p>The spliterator reports {@link Spliterator#SIZED},
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5582
     * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5583
     * {@link Spliterator#IMMUTABLE}.
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5584
     *
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5585
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5586
     * @param startInclusive the first index to cover, inclusive
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5587
     * @param endExclusive index immediately past the last index to cover
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5588
     * @return a spliterator for the array elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5589
     * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5590
     *         negative, {@code endExclusive} is less than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5591
     *         {@code startInclusive}, or {@code endExclusive} is greater than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5592
     *         the array size
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5593
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5594
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5595
    public static Spliterator.OfDouble spliterator(double[] array, int startInclusive, int endExclusive) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5596
        return Spliterators.spliterator(array, startInclusive, endExclusive,
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5597
                                        Spliterator.ORDERED | Spliterator.IMMUTABLE);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5598
    }
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
     * Returns a sequential {@link Stream} with the specified array as its
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5602
     * source.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5603
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5604
     * @param <T> The type of the array elements
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5605
     * @param array The array, assumed to be unmodified during use
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5606
     * @return a {@code Stream} for the array
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5607
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5608
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5609
    public static <T> Stream<T> stream(T[] array) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5610
        return stream(array, 0, array.length);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5611
    }
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
     * Returns a sequential {@link Stream} with the specified range of the
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5615
     * specified array as its source.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5616
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5617
     * @param <T> the type of the array elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5618
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5619
     * @param startInclusive the first index to cover, inclusive
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5620
     * @param endExclusive index immediately past the last index to cover
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5621
     * @return a {@code Stream} for the array range
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5622
     * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5623
     *         negative, {@code endExclusive} is less than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5624
     *         {@code startInclusive}, or {@code endExclusive} is greater than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5625
     *         the array size
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5626
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5627
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5628
    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
  5629
        return StreamSupport.stream(spliterator(array, startInclusive, endExclusive), false);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5630
    }
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
     * Returns a sequential {@link IntStream} with the specified array as its
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5634
     * source.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5635
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5636
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5637
     * @return an {@code IntStream} for the array
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5638
     * @since 1.8
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5639
     */
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5640
    public static IntStream stream(int[] array) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5641
        return stream(array, 0, array.length);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5642
    }
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
     * Returns a sequential {@link IntStream} with the specified range of the
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5646
     * specified array as its source.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5647
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5648
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5649
     * @param startInclusive the first index to cover, inclusive
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5650
     * @param endExclusive index immediately past the last index to cover
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5651
     * @return an {@code IntStream} for the array range
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5652
     * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5653
     *         negative, {@code endExclusive} is less than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5654
     *         {@code startInclusive}, or {@code endExclusive} is greater than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5655
     *         the array size
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5656
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5657
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5658
    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
  5659
        return StreamSupport.intStream(spliterator(array, startInclusive, endExclusive), false);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5660
    }
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
     * Returns a sequential {@link LongStream} with the specified array as its
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5664
     * source.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5665
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5666
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5667
     * @return a {@code LongStream} for the array
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5668
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5669
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5670
    public static LongStream stream(long[] array) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5671
        return stream(array, 0, array.length);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5672
    }
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
     * Returns a sequential {@link LongStream} with the specified range of the
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5676
     * specified array as its source.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5677
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5678
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5679
     * @param startInclusive the first index to cover, inclusive
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5680
     * @param endExclusive index immediately past the last index to cover
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5681
     * @return a {@code LongStream} for the array range
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5682
     * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5683
     *         negative, {@code endExclusive} is less than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5684
     *         {@code startInclusive}, or {@code endExclusive} is greater than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5685
     *         the array size
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5686
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5687
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5688
    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
  5689
        return StreamSupport.longStream(spliterator(array, startInclusive, endExclusive), false);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5690
    }
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
     * Returns a sequential {@link DoubleStream} with the specified array as its
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5694
     * source.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5695
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5696
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5697
     * @return a {@code DoubleStream} for the array
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5698
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5699
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5700
    public static DoubleStream stream(double[] array) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5701
        return stream(array, 0, array.length);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5702
    }
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
     * Returns a sequential {@link DoubleStream} with the specified range of the
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5706
     * specified array as its source.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5707
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5708
     * @param array the array, assumed to be unmodified during use
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5709
     * @param startInclusive the first index to cover, inclusive
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5710
     * @param endExclusive index immediately past the last index to cover
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5711
     * @return a {@code DoubleStream} for the array range
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5712
     * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5713
     *         negative, {@code endExclusive} is less than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5714
     *         {@code startInclusive}, or {@code endExclusive} is greater than
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5715
     *         the array size
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5716
     * @since 1.8
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5717
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 16929
diff changeset
  5718
    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
  5719
        return StreamSupport.doubleStream(spliterator(array, startInclusive, endExclusive), false);
16929
c984ae5655cb 8010096: Initial java.util.Spliterator putback
briangoetz
parents: 14925
diff changeset
  5720
    }
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5721
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5722
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5723
    // Comparison methods
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5724
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5725
    // Compare boolean
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5726
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5727
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5728
     * Compares two {@code boolean} arrays lexicographically.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5729
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5730
     * <p>If the two arrays share a common prefix then the lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5731
     * comparison is the result of comparing two elements, as if by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5732
     * {@link Boolean#compare(boolean, boolean)}, at an index within the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5733
     * respective arrays that is the prefix length.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5734
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5735
     * comparison is the result of comparing the two array lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5736
     * (See {@link #mismatch(boolean[], boolean[])} for the definition of a
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5737
     * common and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5738
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5739
     * <p>A {@code null} array reference is considered lexicographically less
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5740
     * than a non-{@code null} array reference.  Two {@code null} array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5741
     * references are considered equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5742
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5743
     * <p>The comparison is consistent with {@link #equals(boolean[], boolean[]) equals},
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5744
     * more specifically the following holds for arrays {@code a} and {@code b}:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5745
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5746
     *     Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5747
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5748
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5749
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5750
     * <p>This method behaves as if (for non-{@code null} array references):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5751
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5752
     *     int i = Arrays.mismatch(a, b);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5753
     *     if (i >= 0 && i < Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5754
     *         return Boolean.compare(a[i], b[i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5755
     *     return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5756
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5757
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5758
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5759
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5760
     * @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
  5761
     *         contain the same elements in the same order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5762
     *         a value less than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5763
     *         lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5764
     *         a value greater than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5765
     *         lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5766
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5767
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5768
    public static int compare(boolean[] a, boolean[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5769
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5770
            return 0;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5771
        if (a == null || b == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5772
            return a == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5773
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5774
        int i = ArraysSupport.mismatch(a, b,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5775
                                       Math.min(a.length, b.length));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5776
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5777
            return Boolean.compare(a[i], b[i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5778
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5779
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5780
        return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5781
    }
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
     * Compares two {@code boolean} arrays lexicographically over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5785
     * ranges.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5786
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5787
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5788
     * then the lexicographic comparison is the result of comparing two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5789
     * elements, as if by {@link Boolean#compare(boolean, boolean)}, at a
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5790
     * relative index within the respective arrays that is the length of the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5791
     * prefix.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5792
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5793
     * comparison is the result of comparing the two range lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5794
     * (See {@link #mismatch(boolean[], int, int, boolean[], int, int)} for the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5795
     * definition of a common and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5796
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5797
     * <p>The comparison is consistent with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5798
     * {@link #equals(boolean[], int, int, boolean[], int, int) equals}, more
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5799
     * specifically the following holds for arrays {@code a} and {@code b} with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5800
     * specified ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5801
     * [{@code bFromIndex}, {@code btoIndex}) respectively:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5802
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5803
     *     Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5804
     *         (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5805
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5806
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5807
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5808
     * <p>This method behaves as if:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5809
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5810
     *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5811
     *                             b, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5812
     *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5813
     *         return Boolean.compare(a[aFromIndex + i], b[bFromIndex + i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5814
     *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5815
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5816
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5817
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5818
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5819
     *                   first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5820
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5821
     *                 first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5822
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5823
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5824
     *                   second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5825
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5826
     *                 second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5827
     * @return the value {@code 0} if, over the specified ranges, the first and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5828
     *         second array are equal and contain the same elements in the same
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5829
     *         order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5830
     *         a value less than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5831
     *         first array is lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5832
     *         a value greater than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5833
     *         first array is lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5834
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5835
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5836
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5837
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5838
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5839
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5840
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5841
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5842
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5843
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5844
    public static int compare(boolean[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5845
                              boolean[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5846
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5847
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5848
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5849
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5850
        int bLength = bToIndex - bFromIndex;
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5851
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5852
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5853
                                       Math.min(aLength, bLength));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5854
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5855
            return Boolean.compare(a[aFromIndex + i], b[bFromIndex + i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5856
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5857
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5858
        return aLength - bLength;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5859
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5860
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5861
    // Compare byte
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5862
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5863
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5864
     * Compares two {@code byte} arrays lexicographically.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5865
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5866
     * <p>If the two arrays share a common prefix then the lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5867
     * comparison is the result of comparing two elements, as if by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5868
     * {@link Byte#compare(byte, byte)}, at an index within the respective
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5869
     * arrays that is the prefix length.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5870
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5871
     * comparison is the result of comparing the two array lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5872
     * (See {@link #mismatch(byte[], byte[])} for the definition of a common and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5873
     * proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5874
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5875
     * <p>A {@code null} array reference is considered lexicographically less
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5876
     * than a non-{@code null} array reference.  Two {@code null} array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5877
     * references are considered equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5878
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5879
     * <p>The comparison is consistent with {@link #equals(byte[], byte[]) equals},
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5880
     * more specifically the following holds for arrays {@code a} and {@code b}:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5881
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5882
     *     Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5883
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5884
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5885
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5886
     * <p>This method behaves as if (for non-{@code null} array references):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5887
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5888
     *     int i = Arrays.mismatch(a, b);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5889
     *     if (i >= 0 && i < Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5890
     *         return Byte.compare(a[i], b[i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5891
     *     return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5892
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5893
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5894
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5895
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5896
     * @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
  5897
     *         contain the same elements in the same order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5898
     *         a value less than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5899
     *         lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5900
     *         a value greater than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5901
     *         lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5902
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5903
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5904
    public static int compare(byte[] a, byte[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5905
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5906
            return 0;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5907
        if (a == null || b == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5908
            return a == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5909
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5910
        int i = ArraysSupport.mismatch(a, b,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5911
                                       Math.min(a.length, b.length));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5912
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5913
            return Byte.compare(a[i], b[i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5914
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5915
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5916
        return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5917
    }
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
     * Compares two {@code byte} arrays lexicographically over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5921
     * ranges.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5922
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5923
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5924
     * then the lexicographic comparison is the result of comparing two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5925
     * elements, as if by {@link Byte#compare(byte, byte)}, at a relative index
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5926
     * within the respective arrays that is the length of the prefix.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5927
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5928
     * comparison is the result of comparing the two range lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5929
     * (See {@link #mismatch(byte[], int, int, byte[], int, int)} for the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5930
     * definition of a common and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5931
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5932
     * <p>The comparison is consistent with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5933
     * {@link #equals(byte[], int, int, byte[], int, int) equals}, more
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5934
     * specifically the following holds for arrays {@code a} and {@code b} with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5935
     * specified ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5936
     * [{@code bFromIndex}, {@code btoIndex}) respectively:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5937
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5938
     *     Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5939
     *         (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5940
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5941
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5942
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5943
     * <p>This method behaves as if:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5944
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5945
     *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5946
     *                             b, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5947
     *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5948
     *         return Byte.compare(a[aFromIndex + i], b[bFromIndex + i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5949
     *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5950
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5951
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5952
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5953
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5954
     *                   first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5955
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5956
     *                 first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5957
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5958
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5959
     *                   second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5960
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5961
     *                 second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5962
     * @return the value {@code 0} if, over the specified ranges, the first and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5963
     *         second array are equal and contain the same elements in the same
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5964
     *         order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5965
     *         a value less than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5966
     *         first array is lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5967
     *         a value greater than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5968
     *         first array is lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5969
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5970
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5971
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5972
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5973
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5974
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5975
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5976
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5977
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5978
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5979
    public static int compare(byte[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5980
                              byte[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5981
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5982
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5983
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5984
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5985
        int bLength = bToIndex - bFromIndex;
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5986
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5987
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5988
                                       Math.min(aLength, bLength));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5989
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  5990
            return Byte.compare(a[aFromIndex + i], b[bFromIndex + i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5991
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5992
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5993
        return aLength - bLength;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5994
    }
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
     * Compares two {@code byte} arrays lexicographically, numerically treating
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5998
     * elements as unsigned.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  5999
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6000
     * <p>If the two arrays share a common prefix then the lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6001
     * comparison is the result of comparing two elements, as if by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6002
     * {@link Byte#compareUnsigned(byte, byte)}, at an index within the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6003
     * respective arrays that is the prefix length.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6004
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6005
     * comparison is the result of comparing the two array lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6006
     * (See {@link #mismatch(byte[], byte[])} for the definition of a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6007
     * and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6008
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6009
     * <p>A {@code null} array reference is considered lexicographically less
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6010
     * than a non-{@code null} array reference.  Two {@code null} array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6011
     * references are considered equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6012
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6013
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6014
     * <p>This method behaves as if (for non-{@code null} array references):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6015
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6016
     *     int i = Arrays.mismatch(a, b);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6017
     *     if (i >= 0 && i < Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6018
     *         return Byte.compareUnsigned(a[i], b[i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6019
     *     return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6020
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6021
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6022
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6023
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6024
     * @return the value {@code 0} if the first and second array are
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6025
     *         equal and contain the same elements in the same order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6026
     *         a value less than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6027
     *         lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6028
     *         a value greater than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6029
     *         lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6030
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6031
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6032
    public static int compareUnsigned(byte[] a, byte[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6033
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6034
            return 0;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6035
        if (a == null || b == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6036
            return a == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6037
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6038
        int i = ArraysSupport.mismatch(a, b,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6039
                                       Math.min(a.length, b.length));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6040
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6041
            return Byte.compareUnsigned(a[i], b[i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6042
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6043
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6044
        return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6045
    }
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
     * Compares two {@code byte} arrays lexicographically over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6050
     * ranges, numerically treating elements as unsigned.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6051
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6052
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6053
     * then the lexicographic comparison is the result of comparing two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6054
     * elements, as if by {@link Byte#compareUnsigned(byte, byte)}, at a
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6055
     * relative index within the respective arrays that is the length of the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6056
     * prefix.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6057
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6058
     * comparison is the result of comparing the two range lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6059
     * (See {@link #mismatch(byte[], int, int, byte[], int, int)} for the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6060
     * definition of a common and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6061
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6062
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6063
     * <p>This method behaves as if:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6064
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6065
     *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6066
     *                             b, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6067
     *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6068
     *         return Byte.compareUnsigned(a[aFromIndex + i], b[bFromIndex + i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6069
     *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6070
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6071
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6072
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6073
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6074
     *                   first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6075
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6076
     *                 first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6077
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6078
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6079
     *                   second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6080
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6081
     *                 second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6082
     * @return the value {@code 0} if, over the specified ranges, the first and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6083
     *         second array are equal and contain the same elements in the same
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6084
     *         order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6085
     *         a value less than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6086
     *         first array is lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6087
     *         a value greater than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6088
     *         first array is lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6089
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6090
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6091
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6092
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6093
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6094
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6095
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6096
     *         if either array is null
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6097
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6098
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6099
    public static int compareUnsigned(byte[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6100
                                      byte[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6101
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6102
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6103
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6104
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6105
        int bLength = bToIndex - bFromIndex;
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6106
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6107
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6108
                                       Math.min(aLength, bLength));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6109
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6110
            return Byte.compareUnsigned(a[aFromIndex + i], b[bFromIndex + i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6111
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6112
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6113
        return aLength - bLength;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6114
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6115
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6116
    // Compare short
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6117
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6118
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6119
     * Compares two {@code short} arrays lexicographically.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6120
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6121
     * <p>If the two arrays share a common prefix then the lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6122
     * comparison is the result of comparing two elements, as if by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6123
     * {@link Short#compare(short, short)}, at an index within the respective
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6124
     * arrays that is the prefix length.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6125
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6126
     * comparison is the result of comparing the two array lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6127
     * (See {@link #mismatch(short[], short[])} for the definition of a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6128
     * and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6129
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6130
     * <p>A {@code null} array reference is considered lexicographically less
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6131
     * than a non-{@code null} array reference.  Two {@code null} array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6132
     * references are considered equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6133
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6134
     * <p>The comparison is consistent with {@link #equals(short[], short[]) equals},
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6135
     * more specifically the following holds for arrays {@code a} and {@code b}:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6136
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6137
     *     Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6138
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6139
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6140
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6141
     * <p>This method behaves as if (for non-{@code null} array references):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6142
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6143
     *     int i = Arrays.mismatch(a, b);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6144
     *     if (i >= 0 && i < Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6145
     *         return Short.compare(a[i], b[i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6146
     *     return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6147
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6148
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6149
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6150
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6151
     * @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
  6152
     *         contain the same elements in the same order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6153
     *         a value less than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6154
     *         lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6155
     *         a value greater than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6156
     *         lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6157
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6158
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6159
    public static int compare(short[] a, short[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6160
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6161
            return 0;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6162
        if (a == null || b == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6163
            return a == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6164
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6165
        int i = ArraysSupport.mismatch(a, b,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6166
                                       Math.min(a.length, b.length));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6167
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6168
            return Short.compare(a[i], b[i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6169
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6170
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6171
        return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6172
    }
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
     * Compares two {@code short} arrays lexicographically over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6176
     * ranges.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6177
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6178
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6179
     * then the lexicographic comparison is the result of comparing two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6180
     * elements, as if by {@link Short#compare(short, short)}, at a relative
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6181
     * index within the respective arrays that is the length of the prefix.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6182
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6183
     * comparison is the result of comparing the two range lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6184
     * (See {@link #mismatch(short[], int, int, short[], int, int)} for the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6185
     * definition of a common and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6186
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6187
     * <p>The comparison is consistent with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6188
     * {@link #equals(short[], int, int, short[], int, int) equals}, more
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6189
     * specifically the following holds for arrays {@code a} and {@code b} with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6190
     * specified ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6191
     * [{@code bFromIndex}, {@code btoIndex}) respectively:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6192
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6193
     *     Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6194
     *         (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6195
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6196
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6197
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6198
     * <p>This method behaves as if:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6199
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6200
     *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6201
     *                             b, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6202
     *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6203
     *         return Short.compare(a[aFromIndex + i], b[bFromIndex + i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6204
     *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6205
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6206
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6207
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6208
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6209
     *                   first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6210
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6211
     *                 first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6212
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6213
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6214
     *                   second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6215
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6216
     *                 second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6217
     * @return the value {@code 0} if, over the specified ranges, the first and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6218
     *         second array are equal and contain the same elements in the same
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6219
     *         order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6220
     *         a value less than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6221
     *         first array is lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6222
     *         a value greater than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6223
     *         first array is lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6224
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6225
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6226
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6227
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6228
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6229
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6230
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6231
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6232
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6233
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6234
    public static int compare(short[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6235
                              short[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6236
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6237
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6238
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6239
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6240
        int bLength = bToIndex - bFromIndex;
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6241
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6242
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6243
                                       Math.min(aLength, bLength));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6244
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6245
            return Short.compare(a[aFromIndex + i], b[bFromIndex + i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6246
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6247
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6248
        return aLength - bLength;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6249
    }
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
     * Compares two {@code short} arrays lexicographically, numerically treating
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6253
     * elements as unsigned.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6254
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6255
     * <p>If the two arrays share a common prefix then the lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6256
     * comparison is the result of comparing two elements, as if by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6257
     * {@link Short#compareUnsigned(short, short)}, at an index within the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6258
     * respective arrays that is the prefix length.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6259
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6260
     * comparison is the result of comparing the two array lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6261
     * (See {@link #mismatch(short[], short[])} for the definition of a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6262
     * and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6263
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6264
     * <p>A {@code null} array reference is considered lexicographically less
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6265
     * than a non-{@code null} array reference.  Two {@code null} array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6266
     * references are considered equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6267
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6268
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6269
     * <p>This method behaves as if (for non-{@code null} array references):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6270
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6271
     *     int i = Arrays.mismatch(a, b);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6272
     *     if (i >= 0 && i < Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6273
     *         return Short.compareUnsigned(a[i], b[i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6274
     *     return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6275
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6276
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6277
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6278
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6279
     * @return the value {@code 0} if the first and second array are
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6280
     *         equal and contain the same elements in the same order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6281
     *         a value less than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6282
     *         lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6283
     *         a value greater than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6284
     *         lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6285
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6286
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6287
    public static int compareUnsigned(short[] a, short[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6288
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6289
            return 0;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6290
        if (a == null || b == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6291
            return a == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6292
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6293
        int i = ArraysSupport.mismatch(a, b,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6294
                                       Math.min(a.length, b.length));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6295
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6296
            return Short.compareUnsigned(a[i], b[i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6297
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6298
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6299
        return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6300
    }
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
     * Compares two {@code short} arrays lexicographically over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6304
     * ranges, numerically treating elements as unsigned.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6305
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6306
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6307
     * then the lexicographic comparison is the result of comparing two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6308
     * elements, as if by {@link Short#compareUnsigned(short, short)}, at a
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6309
     * relative index within the respective arrays that is the length of the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6310
     * prefix.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6311
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6312
     * comparison is the result of comparing the two range lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6313
     * (See {@link #mismatch(short[], int, int, short[], int, int)} for the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6314
     * definition of a common and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6315
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6316
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6317
     * <p>This method behaves as if:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6318
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6319
     *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6320
     *                             b, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6321
     *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6322
     *         return Short.compareUnsigned(a[aFromIndex + i], b[bFromIndex + i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6323
     *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6324
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6325
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6326
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6327
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6328
     *                   first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6329
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6330
     *                 first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6331
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6332
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6333
     *                   second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6334
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6335
     *                 second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6336
     * @return the value {@code 0} if, over the specified ranges, the first and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6337
     *         second array are equal and contain the same elements in the same
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6338
     *         order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6339
     *         a value less than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6340
     *         first array is lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6341
     *         a value greater than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6342
     *         first array is lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6343
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6344
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6345
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6346
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6347
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6348
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6349
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6350
     *         if either array is null
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6351
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6352
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6353
    public static int compareUnsigned(short[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6354
                                      short[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6355
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6356
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6357
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6358
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6359
        int bLength = bToIndex - bFromIndex;
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6360
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6361
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6362
                                       Math.min(aLength, bLength));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6363
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6364
            return Short.compareUnsigned(a[aFromIndex + i], b[bFromIndex + i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6365
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6366
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6367
        return aLength - bLength;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6368
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6369
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6370
    // Compare char
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6371
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6372
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6373
     * Compares two {@code char} arrays lexicographically.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6374
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6375
     * <p>If the two arrays share a common prefix then the lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6376
     * comparison is the result of comparing two elements, as if by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6377
     * {@link Character#compare(char, char)}, at an index within the respective
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6378
     * arrays that is the prefix length.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6379
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6380
     * comparison is the result of comparing the two array lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6381
     * (See {@link #mismatch(char[], char[])} for the definition of a common and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6382
     * proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6383
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6384
     * <p>A {@code null} array reference is considered lexicographically less
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6385
     * than a non-{@code null} array reference.  Two {@code null} array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6386
     * references are considered equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6387
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6388
     * <p>The comparison is consistent with {@link #equals(char[], char[]) equals},
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6389
     * more specifically the following holds for arrays {@code a} and {@code b}:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6390
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6391
     *     Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6392
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6393
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6394
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6395
     * <p>This method behaves as if (for non-{@code null} array references):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6396
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6397
     *     int i = Arrays.mismatch(a, b);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6398
     *     if (i >= 0 && i < Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6399
     *         return Character.compare(a[i], b[i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6400
     *     return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6401
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6402
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6403
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6404
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6405
     * @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
  6406
     *         contain the same elements in the same order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6407
     *         a value less than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6408
     *         lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6409
     *         a value greater than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6410
     *         lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6411
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6412
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6413
    public static int compare(char[] a, char[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6414
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6415
            return 0;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6416
        if (a == null || b == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6417
            return a == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6418
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6419
        int i = ArraysSupport.mismatch(a, b,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6420
                                       Math.min(a.length, b.length));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6421
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6422
            return Character.compare(a[i], b[i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6423
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6424
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6425
        return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6426
    }
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
     * Compares two {@code char} arrays lexicographically over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6430
     * ranges.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6431
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6432
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6433
     * then the lexicographic comparison is the result of comparing two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6434
     * elements, as if by {@link Character#compare(char, char)}, at a relative
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6435
     * index within the respective arrays that is the length of the prefix.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6436
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6437
     * comparison is the result of comparing the two range lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6438
     * (See {@link #mismatch(char[], int, int, char[], int, int)} for the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6439
     * definition of a common and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6440
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6441
     * <p>The comparison is consistent with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6442
     * {@link #equals(char[], int, int, char[], int, int) equals}, more
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6443
     * specifically the following holds for arrays {@code a} and {@code b} with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6444
     * specified ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6445
     * [{@code bFromIndex}, {@code btoIndex}) respectively:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6446
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6447
     *     Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6448
     *         (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6449
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6450
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6451
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6452
     * <p>This method behaves as if:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6453
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6454
     *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6455
     *                             b, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6456
     *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6457
     *         return Character.compare(a[aFromIndex + i], b[bFromIndex + i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6458
     *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6459
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6460
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6461
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6462
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6463
     *                   first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6464
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6465
     *                 first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6466
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6467
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6468
     *                   second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6469
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6470
     *                 second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6471
     * @return the value {@code 0} if, over the specified ranges, the first and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6472
     *         second array are equal and contain the same elements in the same
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6473
     *         order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6474
     *         a value less than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6475
     *         first array is lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6476
     *         a value greater than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6477
     *         first array is lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6478
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6479
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6480
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6481
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6482
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6483
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6484
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6485
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6486
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6487
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6488
    public static int compare(char[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6489
                              char[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6490
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6491
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6492
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6493
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6494
        int bLength = bToIndex - bFromIndex;
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6495
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6496
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6497
                                       Math.min(aLength, bLength));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6498
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6499
            return Character.compare(a[aFromIndex + i], b[bFromIndex + i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6500
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6501
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6502
        return aLength - bLength;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6503
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6504
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6505
    // Compare int
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6506
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6507
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6508
     * Compares two {@code int} arrays lexicographically.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6509
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6510
     * <p>If the two arrays share a common prefix then the lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6511
     * comparison is the result of comparing two elements, as if by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6512
     * {@link Integer#compare(int, int)}, at an index within the respective
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6513
     * arrays that is the prefix length.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6514
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6515
     * comparison is the result of comparing the two array lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6516
     * (See {@link #mismatch(int[], int[])} for the definition of a common and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6517
     * proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6518
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6519
     * <p>A {@code null} array reference is considered lexicographically less
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6520
     * than a non-{@code null} array reference.  Two {@code null} array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6521
     * references are considered equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6522
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6523
     * <p>The comparison is consistent with {@link #equals(int[], int[]) equals},
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6524
     * more specifically the following holds for arrays {@code a} and {@code b}:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6525
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6526
     *     Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6527
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6528
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6529
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6530
     * <p>This method behaves as if (for non-{@code null} array references):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6531
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6532
     *     int i = Arrays.mismatch(a, b);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6533
     *     if (i >= 0 && i < Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6534
     *         return Integer.compare(a[i], b[i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6535
     *     return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6536
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6537
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6538
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6539
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6540
     * @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
  6541
     *         contain the same elements in the same order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6542
     *         a value less than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6543
     *         lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6544
     *         a value greater than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6545
     *         lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6546
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6547
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6548
    public static int compare(int[] a, int[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6549
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6550
            return 0;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6551
        if (a == null || b == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6552
            return a == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6553
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6554
        int i = ArraysSupport.mismatch(a, b,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6555
                                       Math.min(a.length, b.length));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6556
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6557
            return Integer.compare(a[i], b[i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6558
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6559
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6560
        return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6561
    }
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
     * Compares two {@code int} arrays lexicographically over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6565
     * ranges.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6566
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6567
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6568
     * then the lexicographic comparison is the result of comparing two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6569
     * elements, as if by {@link Integer#compare(int, int)}, at a relative index
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6570
     * within the respective arrays that is the length of the prefix.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6571
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6572
     * comparison is the result of comparing the two range lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6573
     * (See {@link #mismatch(int[], int, int, int[], int, int)} for the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6574
     * definition of a common and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6575
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6576
     * <p>The comparison is consistent with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6577
     * {@link #equals(int[], int, int, int[], int, int) equals}, more
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6578
     * specifically the following holds for arrays {@code a} and {@code b} with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6579
     * specified ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6580
     * [{@code bFromIndex}, {@code btoIndex}) respectively:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6581
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6582
     *     Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6583
     *         (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6584
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6585
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6586
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6587
     * <p>This method behaves as if:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6588
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6589
     *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6590
     *                             b, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6591
     *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6592
     *         return Integer.compare(a[aFromIndex + i], b[bFromIndex + i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6593
     *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6594
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6595
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6596
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6597
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6598
     *                   first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6599
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6600
     *                 first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6601
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6602
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6603
     *                   second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6604
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6605
     *                 second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6606
     * @return the value {@code 0} if, over the specified ranges, the first and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6607
     *         second array are equal and contain the same elements in the same
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6608
     *         order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6609
     *         a value less than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6610
     *         first array is lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6611
     *         a value greater than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6612
     *         first array is lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6613
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6614
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6615
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6616
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6617
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6618
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6619
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6620
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6621
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6622
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6623
    public static int compare(int[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6624
                              int[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6625
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6626
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6627
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6628
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6629
        int bLength = bToIndex - bFromIndex;
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6630
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6631
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6632
                                       Math.min(aLength, bLength));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6633
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6634
            return Integer.compare(a[aFromIndex + i], b[bFromIndex + i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6635
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6636
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6637
        return aLength - bLength;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6638
    }
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
     * Compares two {@code int} arrays lexicographically, numerically treating
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6642
     * elements as unsigned.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6643
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6644
     * <p>If the two arrays share a common prefix then the lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6645
     * comparison is the result of comparing two elements, as if by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6646
     * {@link Integer#compareUnsigned(int, int)}, at an index within the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6647
     * respective arrays that is the prefix length.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6648
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6649
     * comparison is the result of comparing the two array lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6650
     * (See {@link #mismatch(int[], int[])} for the definition of a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6651
     * and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6652
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6653
     * <p>A {@code null} array reference is considered lexicographically less
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6654
     * than a non-{@code null} array reference.  Two {@code null} array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6655
     * references are considered equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6656
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6657
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6658
     * <p>This method behaves as if (for non-{@code null} array references):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6659
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6660
     *     int i = Arrays.mismatch(a, b);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6661
     *     if (i >= 0 && i < Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6662
     *         return Integer.compareUnsigned(a[i], b[i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6663
     *     return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6664
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6665
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6666
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6667
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6668
     * @return the value {@code 0} if the first and second array are
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6669
     *         equal and contain the same elements in the same order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6670
     *         a value less than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6671
     *         lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6672
     *         a value greater than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6673
     *         lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6674
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6675
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6676
    public static int compareUnsigned(int[] a, int[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6677
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6678
            return 0;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6679
        if (a == null || b == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6680
            return a == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6681
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6682
        int i = ArraysSupport.mismatch(a, b,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6683
                                       Math.min(a.length, b.length));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6684
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6685
            return Integer.compareUnsigned(a[i], b[i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6686
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6687
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6688
        return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6689
    }
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
     * Compares two {@code int} arrays lexicographically over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6693
     * ranges, numerically treating elements as unsigned.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6694
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6695
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6696
     * then the lexicographic comparison is the result of comparing two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6697
     * elements, as if by {@link Integer#compareUnsigned(int, int)}, at a
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6698
     * relative index within the respective arrays that is the length of the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6699
     * prefix.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6700
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6701
     * comparison is the result of comparing the two range lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6702
     * (See {@link #mismatch(int[], int, int, int[], int, int)} for the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6703
     * definition of a common and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6704
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6705
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6706
     * <p>This method behaves as if:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6707
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6708
     *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6709
     *                             b, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6710
     *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6711
     *         return Integer.compareUnsigned(a[aFromIndex + i], b[bFromIndex + i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6712
     *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6713
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6714
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6715
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6716
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6717
     *                   first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6718
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6719
     *                 first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6720
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6721
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6722
     *                   second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6723
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6724
     *                 second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6725
     * @return the value {@code 0} if, over the specified ranges, the first and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6726
     *         second array are equal and contain the same elements in the same
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6727
     *         order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6728
     *         a value less than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6729
     *         first array is lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6730
     *         a value greater than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6731
     *         first array is lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6732
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6733
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6734
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6735
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6736
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6737
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6738
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6739
     *         if either array is null
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6740
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6741
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6742
    public static int compareUnsigned(int[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6743
                                      int[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6744
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6745
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6746
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6747
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6748
        int bLength = bToIndex - bFromIndex;
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6749
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6750
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6751
                                       Math.min(aLength, bLength));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6752
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6753
            return Integer.compareUnsigned(a[aFromIndex + i], b[bFromIndex + i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6754
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6755
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6756
        return aLength - bLength;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6757
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6758
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6759
    // Compare long
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6760
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6761
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6762
     * Compares two {@code long} arrays lexicographically.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6763
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6764
     * <p>If the two arrays share a common prefix then the lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6765
     * comparison is the result of comparing two elements, as if by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6766
     * {@link Long#compare(long, long)}, at an index within the respective
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6767
     * arrays that is the prefix length.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6768
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6769
     * comparison is the result of comparing the two array lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6770
     * (See {@link #mismatch(long[], long[])} for the definition of a common and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6771
     * proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6772
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6773
     * <p>A {@code null} array reference is considered lexicographically less
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6774
     * than a non-{@code null} array reference.  Two {@code null} array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6775
     * references are considered equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6776
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6777
     * <p>The comparison is consistent with {@link #equals(long[], long[]) equals},
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6778
     * more specifically the following holds for arrays {@code a} and {@code b}:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6779
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6780
     *     Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6781
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6782
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6783
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6784
     * <p>This method behaves as if (for non-{@code null} array references):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6785
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6786
     *     int i = Arrays.mismatch(a, b);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6787
     *     if (i >= 0 && i < Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6788
     *         return Long.compare(a[i], b[i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6789
     *     return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6790
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6791
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6792
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6793
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6794
     * @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
  6795
     *         contain the same elements in the same order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6796
     *         a value less than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6797
     *         lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6798
     *         a value greater than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6799
     *         lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6800
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6801
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6802
    public static int compare(long[] a, long[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6803
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6804
            return 0;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6805
        if (a == null || b == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6806
            return a == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6807
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6808
        int i = ArraysSupport.mismatch(a, b,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6809
                                       Math.min(a.length, b.length));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6810
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6811
            return Long.compare(a[i], b[i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6812
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6813
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6814
        return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6815
    }
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
     * Compares two {@code long} arrays lexicographically over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6819
     * ranges.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6820
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6821
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6822
     * then the lexicographic comparison is the result of comparing two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6823
     * elements, as if by {@link Long#compare(long, long)}, at a relative index
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6824
     * within the respective arrays that is the length of the prefix.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6825
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6826
     * comparison is the result of comparing the two range lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6827
     * (See {@link #mismatch(long[], int, int, long[], int, int)} for the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6828
     * definition of a common and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6829
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6830
     * <p>The comparison is consistent with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6831
     * {@link #equals(long[], int, int, long[], int, int) equals}, more
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6832
     * specifically the following holds for arrays {@code a} and {@code b} with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6833
     * specified ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6834
     * [{@code bFromIndex}, {@code btoIndex}) respectively:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6835
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6836
     *     Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6837
     *         (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6838
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6839
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6840
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6841
     * <p>This method behaves as if:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6842
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6843
     *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6844
     *                             b, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6845
     *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6846
     *         return Long.compare(a[aFromIndex + i], b[bFromIndex + i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6847
     *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6848
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6849
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6850
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6851
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6852
     *                   first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6853
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6854
     *                 first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6855
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6856
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6857
     *                   second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6858
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6859
     *                 second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6860
     * @return the value {@code 0} if, over the specified ranges, the first and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6861
     *         second array are equal and contain the same elements in the same
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6862
     *         order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6863
     *         a value less than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6864
     *         first array is lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6865
     *         a value greater than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6866
     *         first array is lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6867
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6868
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6869
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6870
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6871
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6872
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6873
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6874
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6875
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6876
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6877
    public static int compare(long[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6878
                              long[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6879
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6880
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6881
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6882
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6883
        int bLength = bToIndex - bFromIndex;
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6884
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6885
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6886
                                       Math.min(aLength, bLength));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6887
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6888
            return Long.compare(a[aFromIndex + i], b[bFromIndex + i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6889
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6890
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6891
        return aLength - bLength;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6892
    }
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
     * Compares two {@code long} arrays lexicographically, numerically treating
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6896
     * elements as unsigned.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6897
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6898
     * <p>If the two arrays share a common prefix then the lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6899
     * comparison is the result of comparing two elements, as if by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6900
     * {@link Long#compareUnsigned(long, long)}, at an index within the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6901
     * respective arrays that is the prefix length.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6902
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6903
     * comparison is the result of comparing the two array lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6904
     * (See {@link #mismatch(long[], long[])} for the definition of a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6905
     * and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6906
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6907
     * <p>A {@code null} array reference is considered lexicographically less
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6908
     * than a non-{@code null} array reference.  Two {@code null} array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6909
     * references are considered equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6910
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6911
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6912
     * <p>This method behaves as if (for non-{@code null} array references):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6913
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6914
     *     int i = Arrays.mismatch(a, b);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6915
     *     if (i >= 0 && i < Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6916
     *         return Long.compareUnsigned(a[i], b[i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6917
     *     return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6918
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6919
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6920
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6921
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6922
     * @return the value {@code 0} if the first and second array are
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6923
     *         equal and contain the same elements in the same order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6924
     *         a value less than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6925
     *         lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6926
     *         a value greater than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6927
     *         lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6928
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6929
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6930
    public static int compareUnsigned(long[] a, long[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6931
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6932
            return 0;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6933
        if (a == null || b == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6934
            return a == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6935
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6936
        int i = ArraysSupport.mismatch(a, b,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6937
                                       Math.min(a.length, b.length));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6938
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  6939
            return Long.compareUnsigned(a[i], b[i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6940
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6941
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6942
        return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6943
    }
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
     * Compares two {@code long} arrays lexicographically over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6947
     * ranges, numerically treating elements as unsigned.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6948
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6949
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6950
     * then the lexicographic comparison is the result of comparing two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6951
     * elements, as if by {@link Long#compareUnsigned(long, long)}, at a
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6952
     * relative index within the respective arrays that is the length of the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6953
     * prefix.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6954
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6955
     * comparison is the result of comparing the two range lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6956
     * (See {@link #mismatch(long[], int, int, long[], int, int)} for the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6957
     * definition of a common and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6958
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6959
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6960
     * <p>This method behaves as if:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6961
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6962
     *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6963
     *                             b, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6964
     *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6965
     *         return Long.compareUnsigned(a[aFromIndex + i], b[bFromIndex + i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6966
     *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6967
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6968
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6969
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6970
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6971
     *                   first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6972
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6973
     *                 first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6974
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6975
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6976
     *                   second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6977
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6978
     *                 second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6979
     * @return the value {@code 0} if, over the specified ranges, the first and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6980
     *         second array are equal and contain the same elements in the same
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6981
     *         order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6982
     *         a value less than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6983
     *         first array is lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6984
     *         a value greater than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6985
     *         first array is lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6986
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6987
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6988
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6989
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6990
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6991
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6992
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6993
     *         if either array is null
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6994
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6995
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6996
    public static int compareUnsigned(long[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6997
                                      long[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6998
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  6999
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7000
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7001
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7002
        int bLength = bToIndex - bFromIndex;
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7003
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7004
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7005
                                       Math.min(aLength, bLength));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7006
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7007
            return Long.compareUnsigned(a[aFromIndex + i], b[bFromIndex + i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7008
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7009
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7010
        return aLength - bLength;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7011
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7012
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7013
    // Compare float
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7014
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7015
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7016
     * Compares two {@code float} arrays lexicographically.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7017
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7018
     * <p>If the two arrays share a common prefix then the lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7019
     * comparison is the result of comparing two elements, as if by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7020
     * {@link Float#compare(float, float)}, at an index within the respective
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7021
     * arrays that is the prefix length.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7022
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7023
     * comparison is the result of comparing the two array lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7024
     * (See {@link #mismatch(float[], float[])} for the definition of a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7025
     * and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7026
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7027
     * <p>A {@code null} array reference is considered lexicographically less
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7028
     * than a non-{@code null} array reference.  Two {@code null} array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7029
     * references are considered equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7030
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7031
     * <p>The comparison is consistent with {@link #equals(float[], float[]) equals},
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7032
     * more specifically the following holds for arrays {@code a} and {@code b}:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7033
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7034
     *     Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7035
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7036
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7037
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7038
     * <p>This method behaves as if (for non-{@code null} array references):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7039
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7040
     *     int i = Arrays.mismatch(a, b);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7041
     *     if (i >= 0 && i < Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7042
     *         return Float.compare(a[i], b[i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7043
     *     return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7044
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7045
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7046
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7047
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7048
     * @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
  7049
     *         contain the same elements in the same order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7050
     *         a value less than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7051
     *         lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7052
     *         a value greater than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7053
     *         lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7054
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7055
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7056
    public static int compare(float[] a, float[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7057
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7058
            return 0;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7059
        if (a == null || b == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7060
            return a == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7061
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7062
        int i = ArraysSupport.mismatch(a, b,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7063
                                       Math.min(a.length, b.length));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7064
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7065
            return Float.compare(a[i], b[i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7066
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7067
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7068
        return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7069
    }
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
     * Compares two {@code float} arrays lexicographically over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7073
     * ranges.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7074
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7075
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7076
     * then the lexicographic comparison is the result of comparing two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7077
     * elements, as if by {@link Float#compare(float, float)}, at a relative
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7078
     * index within the respective arrays that is the length of the prefix.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7079
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7080
     * comparison is the result of comparing the two range lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7081
     * (See {@link #mismatch(float[], int, int, float[], int, int)} for the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7082
     * definition of a common and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7083
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7084
     * <p>The comparison is consistent with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7085
     * {@link #equals(float[], int, int, float[], int, int) equals}, more
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7086
     * specifically the following holds for arrays {@code a} and {@code b} with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7087
     * specified ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7088
     * [{@code bFromIndex}, {@code btoIndex}) respectively:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7089
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7090
     *     Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7091
     *         (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7092
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7093
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7094
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7095
     * <p>This method behaves as if:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7096
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7097
     *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7098
     *                             b, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7099
     *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7100
     *         return Float.compare(a[aFromIndex + i], b[bFromIndex + i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7101
     *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7102
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7103
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7104
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7105
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7106
     *                   first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7107
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7108
     *                 first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7109
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7110
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7111
     *                   second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7112
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7113
     *                 second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7114
     * @return the value {@code 0} if, over the specified ranges, the first and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7115
     *         second array are equal and contain the same elements in the same
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7116
     *         order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7117
     *         a value less than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7118
     *         first array is lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7119
     *         a value greater than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7120
     *         first array is lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7121
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7122
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7123
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7124
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7125
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7126
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7127
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7128
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7129
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7130
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7131
    public static int compare(float[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7132
                              float[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7133
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7134
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7135
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7136
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7137
        int bLength = bToIndex - bFromIndex;
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7138
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7139
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7140
                                       Math.min(aLength, bLength));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7141
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7142
            return Float.compare(a[aFromIndex + i], b[bFromIndex + i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7143
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7144
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7145
        return aLength - bLength;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7146
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7147
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7148
    // Compare double
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7149
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7150
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7151
     * Compares two {@code double} arrays lexicographically.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7152
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7153
     * <p>If the two arrays share a common prefix then the lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7154
     * comparison is the result of comparing two elements, as if by
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7155
     * {@link Double#compare(double, double)}, at an index within the respective
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7156
     * arrays that is the prefix length.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7157
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7158
     * comparison is the result of comparing the two array lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7159
     * (See {@link #mismatch(double[], double[])} for the definition of a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7160
     * and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7161
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7162
     * <p>A {@code null} array reference is considered lexicographically less
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7163
     * than a non-{@code null} array reference.  Two {@code null} array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7164
     * references are considered equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7165
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7166
     * <p>The comparison is consistent with {@link #equals(double[], double[]) equals},
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7167
     * more specifically the following holds for arrays {@code a} and {@code b}:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7168
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7169
     *     Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7170
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7171
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7172
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7173
     * <p>This method behaves as if (for non-{@code null} array references):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7174
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7175
     *     int i = Arrays.mismatch(a, b);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7176
     *     if (i >= 0 && i < Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7177
     *         return Double.compare(a[i], b[i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7178
     *     return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7179
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7180
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7181
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7182
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7183
     * @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
  7184
     *         contain the same elements in the same order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7185
     *         a value less than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7186
     *         lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7187
     *         a value greater than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7188
     *         lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7189
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7190
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7191
    public static int compare(double[] a, double[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7192
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7193
            return 0;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7194
        if (a == null || b == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7195
            return a == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7196
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7197
        int i = ArraysSupport.mismatch(a, b,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7198
                                       Math.min(a.length, b.length));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7199
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7200
            return Double.compare(a[i], b[i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7201
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7202
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7203
        return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7204
    }
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
     * Compares two {@code double} arrays lexicographically over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7208
     * ranges.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7209
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7210
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7211
     * then the lexicographic comparison is the result of comparing two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7212
     * elements, as if by {@link Double#compare(double, double)}, at a relative
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7213
     * index within the respective arrays that is the length of the prefix.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7214
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7215
     * comparison is the result of comparing the two range lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7216
     * (See {@link #mismatch(double[], int, int, double[], int, int)} for the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7217
     * definition of a common and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7218
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7219
     * <p>The comparison is consistent with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7220
     * {@link #equals(double[], int, int, double[], int, int) equals}, more
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7221
     * specifically the following holds for arrays {@code a} and {@code b} with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7222
     * specified ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7223
     * [{@code bFromIndex}, {@code btoIndex}) respectively:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7224
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7225
     *     Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7226
     *         (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7227
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7228
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7229
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7230
     * <p>This method behaves as if:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7231
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7232
     *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7233
     *                             b, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7234
     *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7235
     *         return Double.compare(a[aFromIndex + i], b[bFromIndex + i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7236
     *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7237
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7238
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7239
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7240
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7241
     *                   first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7242
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7243
     *                 first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7244
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7245
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7246
     *                   second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7247
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7248
     *                 second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7249
     * @return the value {@code 0} if, over the specified ranges, the first and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7250
     *         second array are equal and contain the same elements in the same
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7251
     *         order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7252
     *         a value less than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7253
     *         first array is lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7254
     *         a value greater than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7255
     *         first array is lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7256
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7257
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7258
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7259
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7260
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7261
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7262
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7263
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7264
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7265
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7266
    public static int compare(double[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7267
                              double[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7268
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7269
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7270
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7271
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7272
        int bLength = bToIndex - bFromIndex;
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7273
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7274
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7275
                                       Math.min(aLength, bLength));
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7276
        if (i >= 0) {
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7277
            return Double.compare(a[aFromIndex + i], b[bFromIndex + i]);
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7278
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7279
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7280
        return aLength - bLength;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7281
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7282
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7283
    // Compare objects
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7284
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7285
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7286
     * Compares two {@code Object} arrays, within comparable elements,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7287
     * lexicographically.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7288
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7289
     * <p>If the two arrays share a common prefix then the lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7290
     * comparison is the result of comparing two elements of type {@code T} at
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7291
     * an index {@code i} within the respective arrays that is the prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7292
     * length, as if by:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7293
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7294
     *     Comparator.nullsFirst(Comparator.<T>naturalOrder()).
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7295
     *         compare(a[i], b[i])
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7296
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7297
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7298
     * comparison is the result of comparing the two array lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7299
     * (See {@link #mismatch(Object[], Object[])} for the definition of a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7300
     * and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7301
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7302
     * <p>A {@code null} array reference is considered lexicographically less
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7303
     * than a non-{@code null} array reference.  Two {@code null} array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7304
     * references are considered equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7305
     * A {@code null} array element is considered lexicographically than a
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7306
     * non-{@code null} array element.  Two {@code null} array elements are
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7307
     * considered equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7308
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7309
     * <p>The comparison is consistent with {@link #equals(Object[], Object[]) equals},
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7310
     * more specifically the following holds for arrays {@code a} and {@code b}:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7311
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7312
     *     Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7313
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7314
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7315
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7316
     * <p>This method behaves as if (for non-{@code null} array references
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7317
     * and elements):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7318
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7319
     *     int i = Arrays.mismatch(a, b);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7320
     *     if (i >= 0 && i < Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7321
     *         return a[i].compareTo(b[i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7322
     *     return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7323
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7324
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7325
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7326
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7327
     * @param <T> the type of comparable array elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7328
     * @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
  7329
     *         contain the same elements in the same order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7330
     *         a value less than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7331
     *         lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7332
     *         a value greater than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7333
     *         lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7334
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7335
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7336
    public static <T extends Comparable<? super T>> int compare(T[] a, T[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7337
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7338
            return 0;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7339
        // A null array is less than a non-null array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7340
        if (a == null || b == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7341
            return a == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7342
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7343
        int length = Math.min(a.length, b.length);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7344
        for (int i = 0; i < length; i++) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7345
            T oa = a[i];
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7346
            T ob = b[i];
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7347
            if (oa != ob) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7348
                // A null element is less than a non-null element
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7349
                if (oa == null || ob == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7350
                    return oa == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7351
                int v = oa.compareTo(ob);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7352
                if (v != 0) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7353
                    return v;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7354
                }
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
        return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7359
    }
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
     * Compares two {@code Object} arrays lexicographically over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7363
     * ranges.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7364
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7365
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7366
     * then the lexicographic comparison is the result of comparing two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7367
     * elements of type {@code T} at a relative index {@code i} within the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7368
     * respective arrays that is the prefix length, as if by:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7369
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7370
     *     Comparator.nullsFirst(Comparator.<T>naturalOrder()).
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7371
     *         compare(a[aFromIndex + i, b[bFromIndex + i])
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7372
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7373
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7374
     * comparison is the result of comparing the two range lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7375
     * (See {@link #mismatch(Object[], int, int, Object[], int, int)} for the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7376
     * definition of a common and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7377
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7378
     * <p>The comparison is consistent with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7379
     * {@link #equals(Object[], int, int, Object[], int, int) equals}, more
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7380
     * specifically the following holds for arrays {@code a} and {@code b} with
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7381
     * specified ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7382
     * [{@code bFromIndex}, {@code btoIndex}) respectively:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7383
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7384
     *     Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7385
     *         (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7386
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7387
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7388
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7389
     * <p>This method behaves as if (for non-{@code null} array elements):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7390
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7391
     *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7392
     *                             b, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7393
     *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7394
     *         return a[aFromIndex + i].compareTo(b[bFromIndex + i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7395
     *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7396
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7397
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7398
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7399
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7400
     *                   first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7401
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7402
     *                 first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7403
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7404
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7405
     *                   second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7406
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7407
     *                 second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7408
     * @param <T> the type of comparable array elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7409
     * @return the value {@code 0} if, over the specified ranges, the first and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7410
     *         second array are equal and contain the same elements in the same
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7411
     *         order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7412
     *         a value less than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7413
     *         first array is lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7414
     *         a value greater than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7415
     *         first array is lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7416
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7417
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7418
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7419
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7420
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7421
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7422
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7423
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7424
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7425
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7426
    public static <T extends Comparable<? super T>> int compare(
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7427
            T[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7428
            T[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7429
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7430
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7431
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7432
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7433
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7434
        int length = Math.min(aLength, bLength);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7435
        for (int i = 0; i < length; i++) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7436
            T oa = a[aFromIndex++];
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7437
            T ob = b[bFromIndex++];
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7438
            if (oa != ob) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7439
                if (oa == null || ob == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7440
                    return oa == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7441
                int v = oa.compareTo(ob);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7442
                if (v != 0) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7443
                    return v;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7444
                }
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
        return aLength - bLength;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7449
    }
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
     * Compares two {@code Object} arrays lexicographically using a specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7453
     * comparator.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7454
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7455
     * <p>If the two arrays share a common prefix then the lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7456
     * comparison is the result of comparing with the specified comparator two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7457
     * elements at an index within the respective arrays that is the prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7458
     * length.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7459
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7460
     * comparison is the result of comparing the two array lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7461
     * (See {@link #mismatch(Object[], Object[])} for the definition of a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7462
     * and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7463
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7464
     * <p>A {@code null} array reference is considered lexicographically less
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7465
     * than a non-{@code null} array reference.  Two {@code null} array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7466
     * references are considered equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7467
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7468
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7469
     * <p>This method behaves as if (for non-{@code null} array references):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7470
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7471
     *     int i = Arrays.mismatch(a, b, cmp);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7472
     *     if (i >= 0 && i < Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7473
     *         return cmp.compare(a[i], b[i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7474
     *     return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7475
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7476
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7477
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7478
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7479
     * @param cmp the comparator to compare array elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7480
     * @param <T> the type of array elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7481
     * @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
  7482
     *         contain the same elements in the same order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7483
     *         a value less than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7484
     *         lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7485
     *         a value greater than {@code 0} if the first array is
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7486
     *         lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7487
     * @throws NullPointerException if the comparator is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7488
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7489
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7490
    public static <T> int compare(T[] a, T[] b,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7491
                                  Comparator<? super T> cmp) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7492
        Objects.requireNonNull(cmp);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7493
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7494
            return 0;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7495
        if (a == null || b == null)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7496
            return a == null ? -1 : 1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7497
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7498
        int length = Math.min(a.length, b.length);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7499
        for (int i = 0; i < length; i++) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7500
            T oa = a[i];
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7501
            T ob = b[i];
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7502
            if (oa != ob) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7503
                // Null-value comparison is deferred to the comparator
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7504
                int v = cmp.compare(oa, ob);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7505
                if (v != 0) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7506
                    return v;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7507
                }
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
        return a.length - b.length;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7512
    }
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
     * Compares two {@code Object} arrays lexicographically over the specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7516
     * ranges.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7517
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7518
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7519
     * then the lexicographic comparison is the result of comparing with the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7520
     * specified comparator two elements at a relative index within the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7521
     * respective arrays that is the prefix length.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7522
     * Otherwise, one array is a proper prefix of the other and, lexicographic
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7523
     * comparison is the result of comparing the two range lengths.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7524
     * (See {@link #mismatch(Object[], int, int, Object[], int, int)} for the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7525
     * definition of a common and proper prefix.)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7526
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7527
     * @apiNote
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7528
     * <p>This method behaves as if (for non-{@code null} array elements):
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7529
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7530
     *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7531
     *                             b, bFromIndex, bToIndex, cmp);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7532
     *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7533
     *         return cmp.compare(a[aFromIndex + i], b[bFromIndex + i]);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7534
     *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7535
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7536
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7537
     * @param a the first array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7538
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7539
     *                   first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7540
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7541
     *                 first array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7542
     * @param b the second array to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7543
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7544
     *                   second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7545
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7546
     *                 second array to be compared
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7547
     * @param cmp the comparator to compare array elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7548
     * @param <T> the type of array elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7549
     * @return the value {@code 0} if, over the specified ranges, the first and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7550
     *         second array are equal and contain the same elements in the same
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7551
     *         order;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7552
     *         a value less than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7553
     *         first array is lexicographically less than the second array; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7554
     *         a value greater than {@code 0} if, over the specified ranges, the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7555
     *         first array is lexicographically greater than the second array
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7556
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7557
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7558
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7559
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7560
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7561
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7562
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7563
     *         if either array or the comparator is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7564
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7565
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7566
    public static <T> int compare(
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7567
            T[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7568
            T[] b, int bFromIndex, int bToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7569
            Comparator<? super T> cmp) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7570
        Objects.requireNonNull(cmp);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7571
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7572
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7573
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7574
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7575
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7576
        int length = Math.min(aLength, bLength);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7577
        for (int i = 0; i < length; i++) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7578
            T oa = a[aFromIndex++];
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7579
            T ob = b[bFromIndex++];
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7580
            if (oa != ob) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7581
                // Null-value comparison is deferred to the comparator
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7582
                int v = cmp.compare(oa, ob);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7583
                if (v != 0) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7584
                    return v;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7585
                }
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
        return aLength - bLength;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7590
    }
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
    // Mismatch methods
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7594
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7595
    // Mismatch boolean
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7596
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7597
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7598
     * Finds and returns the index of the first mismatch between two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7599
     * {@code boolean} arrays, otherwise return -1 if no mismatch is found.  The
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7600
     * 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
  7601
     * of the smaller array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7602
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7603
     * <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
  7604
     * length of the common prefix and it follows that there is a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7605
     * between the two elements at that index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7606
     * 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
  7607
     * 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
  7608
     * valid for the larger array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7609
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7610
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7611
     * <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
  7612
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7613
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7614
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7615
     *     pl < Math.min(a.length, b.length) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7616
     *     Arrays.equals(a, 0, pl, b, 0, pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7617
     *     a[pl] != b[pl]
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7618
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7619
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7620
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7621
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7622
     * <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
  7623
     * prefix if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7624
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7625
     *     a.length != b.length &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7626
     *     Arrays.equals(a, 0, Math.min(a.length, b.length),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7627
     *                   b, 0, Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7628
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7629
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7630
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7631
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7632
     * @return the index of the first mismatch between the two arrays,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7633
     *         otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7634
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7635
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7636
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7637
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7638
    public static int mismatch(boolean[] a, boolean[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7639
        int length = Math.min(a.length, b.length); // Check null array refs
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7640
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7641
            return -1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7642
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7643
        int i = ArraysSupport.mismatch(a, b, length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7644
        return (i < 0 && a.length != b.length) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7645
    }
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
     * Finds and returns the relative index of the first mismatch between two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7649
     * {@code boolean} arrays over the specified ranges, otherwise return -1 if
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7650
     * 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
  7651
     * to the length (inclusive) of the smaller range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7652
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7653
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7654
     * then the returned relative index is the length of the common prefix and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7655
     * it follows that there is a mismatch between the two elements at that
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7656
     * relative index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7657
     * 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
  7658
     * then the returned relative index is the length of the smaller range and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7659
     * 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
  7660
     * larger range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7661
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7662
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7663
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7664
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7665
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7666
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7667
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7668
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7669
     *     pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7670
     *     Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7671
     *     a[aFromIndex + pl] != b[bFromIndex + pl]
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7672
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7673
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7674
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7675
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7676
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7677
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7678
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7679
     * if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7680
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7681
     *     (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7682
     *     Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7683
     *                   b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7684
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7685
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7686
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7687
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7688
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7689
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7690
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7691
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7692
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7693
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7694
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7695
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7696
     * @return the relative index of the first mismatch between the two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7697
     *         over the specified ranges, otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7698
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7699
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7700
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7701
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7702
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7703
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7704
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7705
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7706
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7707
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7708
    public static int mismatch(boolean[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7709
                               boolean[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7710
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7711
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7712
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7713
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7714
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7715
        int length = Math.min(aLength, bLength);
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7716
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7717
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7718
                                       length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7719
        return (i < 0 && aLength != bLength) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7720
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7721
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7722
    // Mismatch byte
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7723
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7724
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7725
     * Finds and returns the index of the first mismatch between two {@code byte}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7726
     * arrays, otherwise return -1 if no mismatch is found.  The index will be
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7727
     * 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
  7728
     * array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7729
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7730
     * <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
  7731
     * length of the common prefix and it follows that there is a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7732
     * between the two elements at that index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7733
     * 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
  7734
     * 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
  7735
     * valid for the larger array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7736
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7737
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7738
     * <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
  7739
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7740
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7741
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7742
     *     pl < Math.min(a.length, b.length) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7743
     *     Arrays.equals(a, 0, pl, b, 0, pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7744
     *     a[pl] != b[pl]
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7745
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7746
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7747
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7748
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7749
     * <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
  7750
     * prefix if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7751
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7752
     *     a.length != b.length &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7753
     *     Arrays.equals(a, 0, Math.min(a.length, b.length),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7754
     *                   b, 0, Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7755
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7756
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7757
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7758
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7759
     * @return the index of the first mismatch between the two arrays,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7760
     *         otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7761
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7762
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7763
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7764
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7765
    public static int mismatch(byte[] a, byte[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7766
        int length = Math.min(a.length, b.length); // Check null array refs
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7767
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7768
            return -1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7769
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7770
        int i = ArraysSupport.mismatch(a, b, length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7771
        return (i < 0 && a.length != b.length) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7772
    }
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
     * Finds and returns the relative index of the first mismatch between two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7776
     * {@code byte} arrays over the specified ranges, otherwise return -1 if no
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7777
     * 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
  7778
     * the length (inclusive) of the smaller range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7779
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7780
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7781
     * then the returned relative index is the length of the common prefix and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7782
     * it follows that there is a mismatch between the two elements at that
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7783
     * relative index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7784
     * 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
  7785
     * then the returned relative index is the length of the smaller range and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7786
     * 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
  7787
     * larger range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7788
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7789
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7790
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7791
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7792
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7793
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7794
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7795
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7796
     *     pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7797
     *     Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7798
     *     a[aFromIndex + pl] != b[bFromIndex + pl]
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7799
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7800
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7801
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7802
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7803
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7804
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7805
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7806
     * if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7807
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7808
     *     (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7809
     *     Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7810
     *                   b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7811
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7812
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7813
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7814
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7815
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7816
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7817
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7818
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7819
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7820
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7821
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7822
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7823
     * @return the relative index of the first mismatch between the two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7824
     *         over the specified ranges, otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7825
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7826
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7827
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7828
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7829
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7830
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7831
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7832
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7833
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7834
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7835
    public static int mismatch(byte[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7836
                               byte[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7837
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7838
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7839
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7840
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7841
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7842
        int length = Math.min(aLength, bLength);
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7843
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7844
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7845
                                       length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7846
        return (i < 0 && aLength != bLength) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7847
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7848
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7849
    // Mismatch char
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7850
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7851
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7852
     * Finds and returns the index of the first mismatch between two {@code char}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7853
     * arrays, otherwise return -1 if no mismatch is found.  The index will be
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7854
     * 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
  7855
     * array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7856
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7857
     * <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
  7858
     * length of the common prefix and it follows that there is a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7859
     * between the two elements at that index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7860
     * 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
  7861
     * 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
  7862
     * valid for the larger array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7863
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7864
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7865
     * <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
  7866
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7867
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7868
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7869
     *     pl < Math.min(a.length, b.length) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7870
     *     Arrays.equals(a, 0, pl, b, 0, pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7871
     *     a[pl] != b[pl]
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7872
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7873
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7874
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7875
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7876
     * <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
  7877
     * prefix if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7878
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7879
     *     a.length != b.length &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7880
     *     Arrays.equals(a, 0, Math.min(a.length, b.length),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7881
     *                   b, 0, Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7882
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7883
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7884
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7885
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7886
     * @return the index of the first mismatch between the two arrays,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7887
     *         otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7888
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7889
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7890
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7891
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7892
    public static int mismatch(char[] a, char[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7893
        int length = Math.min(a.length, b.length); // Check null array refs
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7894
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7895
            return -1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7896
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7897
        int i = ArraysSupport.mismatch(a, b, length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7898
        return (i < 0 && a.length != b.length) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7899
    }
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
     * Finds and returns the relative index of the first mismatch between two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7903
     * {@code char} arrays over the specified ranges, otherwise return -1 if no
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7904
     * 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
  7905
     * the length (inclusive) of the smaller range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7906
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7907
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7908
     * then the returned relative index is the length of the common prefix and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7909
     * it follows that there is a mismatch between the two elements at that
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7910
     * relative index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7911
     * 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
  7912
     * then the returned relative index is the length of the smaller range and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7913
     * 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
  7914
     * larger range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7915
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7916
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7917
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7918
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7919
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7920
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7921
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7922
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7923
     *     pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7924
     *     Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7925
     *     a[aFromIndex + pl] != b[bFromIndex + pl]
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7926
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7927
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7928
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7929
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7930
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7931
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7932
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7933
     * if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7934
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7935
     *     (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7936
     *     Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7937
     *                   b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7938
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7939
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7940
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7941
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7942
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7943
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7944
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7945
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7946
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7947
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7948
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7949
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7950
     * @return the relative index of the first mismatch between the two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7951
     *         over the specified ranges, otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7952
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7953
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7954
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7955
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7956
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7957
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7958
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7959
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7960
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7961
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7962
    public static int mismatch(char[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7963
                               char[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7964
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7965
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7966
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7967
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7968
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7969
        int length = Math.min(aLength, bLength);
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7970
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7971
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7972
                                       length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  7973
        return (i < 0 && aLength != bLength) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7974
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7975
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7976
    // Mismatch short
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7977
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7978
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7979
     * Finds and returns the index of the first mismatch between two {@code short}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7980
     * arrays, otherwise return -1 if no mismatch is found.  The index will be
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7981
     * 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
  7982
     * array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7983
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7984
     * <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
  7985
     * length of the common prefix and it follows that there is a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7986
     * between the two elements at that index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7987
     * 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
  7988
     * 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
  7989
     * valid for the larger array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7990
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7991
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7992
     * <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
  7993
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7994
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7995
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7996
     *     pl < Math.min(a.length, b.length) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7997
     *     Arrays.equals(a, 0, pl, b, 0, pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7998
     *     a[pl] != b[pl]
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  7999
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8000
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8001
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8002
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8003
     * <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
  8004
     * prefix if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8005
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8006
     *     a.length != b.length &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8007
     *     Arrays.equals(a, 0, Math.min(a.length, b.length),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8008
     *                   b, 0, Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8009
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8010
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8011
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8012
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8013
     * @return the index of the first mismatch between the two arrays,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8014
     *         otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8015
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8016
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8017
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8018
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8019
    public static int mismatch(short[] a, short[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8020
        int length = Math.min(a.length, b.length); // Check null array refs
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8021
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8022
            return -1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8023
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8024
        int i = ArraysSupport.mismatch(a, b, length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8025
        return (i < 0 && a.length != b.length) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8026
    }
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
     * Finds and returns the relative index of the first mismatch between two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8030
     * {@code short} arrays over the specified ranges, otherwise return -1 if no
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8031
     * 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
  8032
     * the length (inclusive) of the smaller range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8033
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8034
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8035
     * then the returned relative index is the length of the common prefix and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8036
     * it follows that there is a mismatch between the two elements at that
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8037
     * relative index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8038
     * 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
  8039
     * then the returned relative index is the length of the smaller range and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8040
     * 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
  8041
     * larger range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8042
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8043
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8044
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8045
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8046
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8047
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8048
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8049
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8050
     *     pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8051
     *     Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8052
     *     a[aFromIndex + pl] != b[bFromIndex + pl]
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8053
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8054
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8055
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8056
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8057
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8058
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8059
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8060
     * if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8061
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8062
     *     (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8063
     *     Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8064
     *                   b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8065
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8066
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8067
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8068
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8069
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8070
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8071
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8072
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8073
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8074
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8075
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8076
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8077
     * @return the relative index of the first mismatch between the two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8078
     *         over the specified ranges, otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8079
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8080
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8081
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8082
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8083
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8084
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8085
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8086
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8087
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8088
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8089
    public static int mismatch(short[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8090
                               short[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8091
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8092
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8093
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8094
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8095
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8096
        int length = Math.min(aLength, bLength);
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8097
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8098
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8099
                                       length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8100
        return (i < 0 && aLength != bLength) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8101
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8102
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8103
    // Mismatch int
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8104
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8105
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8106
     * Finds and returns the index of the first mismatch between two {@code int}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8107
     * arrays, otherwise return -1 if no mismatch is found.  The index will be
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8108
     * 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
  8109
     * array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8110
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8111
     * <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
  8112
     * length of the common prefix and it follows that there is a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8113
     * between the two elements at that index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8114
     * 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
  8115
     * 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
  8116
     * valid for the larger array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8117
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8118
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8119
     * <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
  8120
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8121
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8122
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8123
     *     pl < Math.min(a.length, b.length) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8124
     *     Arrays.equals(a, 0, pl, b, 0, pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8125
     *     a[pl] != b[pl]
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8126
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8127
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8128
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8129
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8130
     * <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
  8131
     * prefix if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8132
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8133
     *     a.length != b.length &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8134
     *     Arrays.equals(a, 0, Math.min(a.length, b.length),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8135
     *                   b, 0, Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8136
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8137
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8138
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8139
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8140
     * @return the index of the first mismatch between the two arrays,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8141
     *         otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8142
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8143
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8144
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8145
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8146
    public static int mismatch(int[] a, int[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8147
        int length = Math.min(a.length, b.length); // Check null array refs
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8148
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8149
            return -1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8150
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8151
        int i = ArraysSupport.mismatch(a, b, length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8152
        return (i < 0 && a.length != b.length) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8153
    }
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
     * Finds and returns the relative index of the first mismatch between two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8157
     * {@code int} arrays over the specified ranges, otherwise return -1 if no
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8158
     * 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
  8159
     * the length (inclusive) of the smaller range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8160
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8161
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8162
     * then the returned relative index is the length of the common prefix and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8163
     * it follows that there is a mismatch between the two elements at that
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8164
     * relative index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8165
     * 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
  8166
     * then the returned relative index is the length of the smaller range and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8167
     * 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
  8168
     * larger range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8169
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8170
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8171
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8172
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8173
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8174
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8175
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8176
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8177
     *     pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8178
     *     Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8179
     *     a[aFromIndex + pl] != b[bFromIndex + pl]
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8180
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8181
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8182
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8183
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8184
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8185
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8186
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8187
     * if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8188
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8189
     *     (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8190
     *     Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8191
     *                   b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8192
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8193
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8194
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8195
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8196
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8197
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8198
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8199
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8200
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8201
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8202
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8203
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8204
     * @return the relative index of the first mismatch between the two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8205
     *         over the specified ranges, otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8206
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8207
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8208
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8209
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8210
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8211
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8212
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8213
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8214
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8215
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8216
    public static int mismatch(int[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8217
                               int[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8218
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8219
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8220
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8221
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8222
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8223
        int length = Math.min(aLength, bLength);
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8224
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8225
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8226
                                       length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8227
        return (i < 0 && aLength != bLength) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8228
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8229
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8230
    // Mismatch long
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8231
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8232
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8233
     * Finds and returns the index of the first mismatch between two {@code long}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8234
     * arrays, otherwise return -1 if no mismatch is found.  The index will be
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8235
     * 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
  8236
     * array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8237
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8238
     * <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
  8239
     * length of the common prefix and it follows that there is a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8240
     * between the two elements at that index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8241
     * 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
  8242
     * 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
  8243
     * valid for the larger array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8244
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8245
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8246
     * <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
  8247
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8248
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8249
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8250
     *     pl < Math.min(a.length, b.length) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8251
     *     Arrays.equals(a, 0, pl, b, 0, pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8252
     *     a[pl] != b[pl]
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8253
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8254
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8255
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8256
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8257
     * <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
  8258
     * prefix if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8259
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8260
     *     a.length != b.length &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8261
     *     Arrays.equals(a, 0, Math.min(a.length, b.length),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8262
     *                   b, 0, Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8263
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8264
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8265
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8266
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8267
     * @return the index of the first mismatch between the two arrays,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8268
     *         otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8269
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8270
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8271
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8272
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8273
    public static int mismatch(long[] a, long[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8274
        int length = Math.min(a.length, b.length); // Check null array refs
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8275
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8276
            return -1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8277
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8278
        int i = ArraysSupport.mismatch(a, b, length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8279
        return (i < 0 && a.length != b.length) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8280
    }
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
     * Finds and returns the relative index of the first mismatch between two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8284
     * {@code long} arrays over the specified ranges, otherwise return -1 if no
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8285
     * 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
  8286
     * the length (inclusive) of the smaller range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8287
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8288
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8289
     * then the returned relative index is the length of the common prefix and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8290
     * it follows that there is a mismatch between the two elements at that
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8291
     * relative index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8292
     * 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
  8293
     * then the returned relative index is the length of the smaller range and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8294
     * 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
  8295
     * larger range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8296
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8297
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8298
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8299
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8300
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8301
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8302
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8303
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8304
     *     pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8305
     *     Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8306
     *     a[aFromIndex + pl] != b[bFromIndex + pl]
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8307
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8308
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8309
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8310
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8311
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8312
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8313
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8314
     * if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8315
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8316
     *     (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8317
     *     Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8318
     *                   b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8319
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8320
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8321
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8322
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8323
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8324
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8325
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8326
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8327
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8328
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8329
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8330
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8331
     * @return the relative index of the first mismatch between the two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8332
     *         over the specified ranges, otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8333
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8334
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8335
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8336
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8337
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8338
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8339
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8340
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8341
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8342
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8343
    public static int mismatch(long[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8344
                               long[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8345
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8346
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8347
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8348
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8349
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8350
        int length = Math.min(aLength, bLength);
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8351
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8352
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8353
                                       length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8354
        return (i < 0 && aLength != bLength) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8355
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8356
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8357
    // Mismatch float
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8358
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8359
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8360
     * Finds and returns the index of the first mismatch between two {@code float}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8361
     * arrays, otherwise return -1 if no mismatch is found.  The index will be
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8362
     * 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
  8363
     * array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8364
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8365
     * <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
  8366
     * length of the common prefix and it follows that there is a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8367
     * between the two elements at that index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8368
     * 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
  8369
     * 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
  8370
     * valid for the larger array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8371
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8372
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8373
     * <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
  8374
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8375
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8376
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8377
     *     pl < Math.min(a.length, b.length) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8378
     *     Arrays.equals(a, 0, pl, b, 0, pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8379
     *     Float.compare(a[pl], b[pl]) != 0
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8380
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8381
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8382
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8383
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8384
     * <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
  8385
     * prefix if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8386
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8387
     *     a.length != b.length &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8388
     *     Arrays.equals(a, 0, Math.min(a.length, b.length),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8389
     *                   b, 0, Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8390
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8391
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8392
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8393
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8394
     * @return the index of the first mismatch between the two arrays,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8395
     *         otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8396
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8397
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8398
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8399
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8400
    public static int mismatch(float[] a, float[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8401
        int length = Math.min(a.length, b.length); // Check null array refs
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8402
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8403
            return -1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8404
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8405
        int i = ArraysSupport.mismatch(a, b, length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8406
        return (i < 0 && a.length != b.length) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8407
    }
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
     * Finds and returns the relative index of the first mismatch between two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8411
     * {@code float} arrays over the specified ranges, otherwise return -1 if no
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8412
     * 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
  8413
     * the length (inclusive) of the smaller range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8414
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8415
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8416
     * then the returned relative index is the length of the common prefix and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8417
     * it follows that there is a mismatch between the two elements at that
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8418
     * relative index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8419
     * 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
  8420
     * then the returned relative index is the length of the smaller range and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8421
     * 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
  8422
     * larger range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8423
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8424
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8425
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8426
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8427
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8428
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8429
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8430
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8431
     *     pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8432
     *     Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8433
     *     Float.compare(a[aFromIndex + pl], b[bFromIndex + pl]) != 0
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8434
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8435
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8436
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8437
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8438
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8439
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8440
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8441
     * if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8442
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8443
     *     (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8444
     *     Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8445
     *                   b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8446
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8447
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8448
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8449
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8450
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8451
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8452
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8453
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8454
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8455
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8456
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8457
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8458
     * @return the relative index of the first mismatch between the two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8459
     *         over the specified ranges, otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8460
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8461
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8462
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8463
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8464
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8465
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8466
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8467
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8468
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8469
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8470
    public static int mismatch(float[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8471
                               float[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8472
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8473
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8474
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8475
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8476
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8477
        int length = Math.min(aLength, bLength);
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8478
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8479
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8480
                                       length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8481
        return (i < 0 && aLength != bLength) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8482
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8483
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8484
    // Mismatch double
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8485
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8486
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8487
     * Finds and returns the index of the first mismatch between two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8488
     * {@code double} arrays, otherwise return -1 if no mismatch is found.  The
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8489
     * 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
  8490
     * of the smaller array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8491
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8492
     * <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
  8493
     * length of the common prefix and it follows that there is a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8494
     * between the two elements at that index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8495
     * 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
  8496
     * 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
  8497
     * valid for the larger array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8498
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8499
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8500
     * <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
  8501
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8502
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8503
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8504
     *     pl < Math.min(a.length, b.length) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8505
     *     Arrays.equals(a, 0, pl, b, 0, pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8506
     *     Double.compare(a[pl], b[pl]) != 0
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8507
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8508
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8509
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8510
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8511
     * <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
  8512
     * prefix if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8513
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8514
     *     a.length != b.length &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8515
     *     Arrays.equals(a, 0, Math.min(a.length, b.length),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8516
     *                   b, 0, Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8517
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8518
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8519
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8520
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8521
     * @return the index of the first mismatch between the two arrays,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8522
     *         otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8523
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8524
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8525
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8526
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8527
    public static int mismatch(double[] a, double[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8528
        int length = Math.min(a.length, b.length); // Check null array refs
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8529
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8530
            return -1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8531
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8532
        int i = ArraysSupport.mismatch(a, b, length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8533
        return (i < 0 && a.length != b.length) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8534
    }
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
     * Finds and returns the relative index of the first mismatch between two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8538
     * {@code double} arrays over the specified ranges, otherwise return -1 if
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8539
     * 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
  8540
     * to the length (inclusive) of the smaller range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8541
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8542
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8543
     * then the returned relative index is the length of the common prefix and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8544
     * it follows that there is a mismatch between the two elements at that
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8545
     * relative index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8546
     * 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
  8547
     * then the returned relative index is the length of the smaller range and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8548
     * 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
  8549
     * larger range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8550
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8551
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8552
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8553
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8554
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8555
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8556
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8557
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8558
     *     pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8559
     *     Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8560
     *     Double.compare(a[aFromIndex + pl], b[bFromIndex + pl]) != 0
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8561
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8562
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8563
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8564
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8565
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8566
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8567
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8568
     * if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8569
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8570
     *     (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8571
     *     Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8572
     *                   b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8573
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8574
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8575
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8576
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8577
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8578
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8579
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8580
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8581
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8582
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8583
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8584
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8585
     * @return the relative index of the first mismatch between the two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8586
     *         over the specified ranges, otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8587
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8588
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8589
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8590
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8591
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8592
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8593
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8594
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8595
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8596
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8597
    public static int mismatch(double[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8598
                               double[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8599
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8600
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8601
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8602
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8603
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8604
        int length = Math.min(aLength, bLength);
35255
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8605
        int i = ArraysSupport.mismatch(a, aFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8606
                                       b, bFromIndex,
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8607
                                       length);
6f0bf592d149 8136924: Vectorized support for array equals/compare/mismatch using Unsafe
psandoz
parents: 33843
diff changeset
  8608
        return (i < 0 && aLength != bLength) ? length : i;
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8609
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8610
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8611
    // Mismatch objects
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8612
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8613
    /**
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8614
     * Finds and returns the index of the first mismatch between two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8615
     * {@code Object} arrays, otherwise return -1 if no mismatch is found.  The
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8616
     * 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
  8617
     * of the smaller array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8618
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8619
     * <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
  8620
     * length of the common prefix and it follows that there is a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8621
     * between the two elements at that index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8622
     * 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
  8623
     * 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
  8624
     * valid for the larger array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8625
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8626
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8627
     * <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
  8628
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8629
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8630
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8631
     *     pl < Math.min(a.length, b.length) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8632
     *     Arrays.equals(a, 0, pl, b, 0, pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8633
     *     !Objects.equals(a[pl], b[pl])
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8634
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8635
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8636
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8637
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8638
     * <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
  8639
     * prefix if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8640
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8641
     *     a.length != b.length &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8642
     *     Arrays.equals(a, 0, Math.min(a.length, b.length),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8643
     *                   b, 0, Math.min(a.length, b.length))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8644
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8645
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8646
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8647
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8648
     * @return the index of the first mismatch between the two arrays,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8649
     *         otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8650
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8651
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8652
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8653
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8654
    public static int mismatch(Object[] a, Object[] b) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8655
        int length = Math.min(a.length, b.length); // Check null array refs
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8656
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8657
            return -1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8658
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8659
        for (int i = 0; i < length; i++) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8660
            if (!Objects.equals(a[i], b[i]))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8661
                return i;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8662
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8663
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8664
        return a.length != b.length ? length : -1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8665
    }
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
     * Finds and returns the relative index of the first mismatch between two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8669
     * {@code Object} arrays over the specified ranges, otherwise return -1 if
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8670
     * 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
  8671
     * to the length (inclusive) of the smaller range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8672
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8673
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8674
     * then the returned relative index is the length of the common prefix and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8675
     * it follows that there is a mismatch between the two elements at that
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8676
     * relative index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8677
     * 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
  8678
     * then the returned relative index is the length of the smaller range and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8679
     * 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
  8680
     * larger range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8681
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8682
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8683
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8684
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8685
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8686
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8687
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8688
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8689
     *     pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8690
     *     Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8691
     *     !Objects.equals(a[aFromIndex + pl], b[bFromIndex + pl])
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8692
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8693
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8694
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8695
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8696
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8697
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8698
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8699
     * if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8700
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8701
     *     (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8702
     *     Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8703
     *                   b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8704
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8705
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8706
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8707
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8708
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8709
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8710
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8711
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8712
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8713
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8714
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8715
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8716
     * @return the relative index of the first mismatch between the two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8717
     *         over the specified ranges, otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8718
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8719
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8720
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8721
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8722
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8723
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8724
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8725
     *         if either array is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8726
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8727
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8728
    public static int mismatch(
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8729
            Object[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8730
            Object[] b, int bFromIndex, int bToIndex) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8731
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8732
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8733
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8734
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8735
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8736
        int length = Math.min(aLength, bLength);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8737
        for (int i = 0; i < length; i++) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8738
            if (!Objects.equals(a[aFromIndex++], b[bFromIndex++]))
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8739
                return i;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8740
        }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8741
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8742
        return aLength != bLength ? length : -1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8743
    }
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
     * Finds and returns the index of the first mismatch between two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8747
     * {@code Object} arrays, otherwise return -1 if no mismatch is found.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8748
     * 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
  8749
     * (inclusive) of the smaller array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8750
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8751
     * <p>The specified comparator is used to determine if two array elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8752
     * from the each array are not equal.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8753
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8754
     * <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
  8755
     * length of the common prefix and it follows that there is a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8756
     * between the two elements at that index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8757
     * 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
  8758
     * 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
  8759
     * valid for the larger array.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8760
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8761
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8762
     * <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
  8763
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8764
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8765
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8766
     *     pl < Math.min(a.length, b.length) &&
33843
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  8767
     *     Arrays.equals(a, 0, pl, b, 0, pl, cmp)
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8768
     *     cmp.compare(a[pl], b[pl]) != 0
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8769
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8770
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8771
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8772
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8773
     * <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
  8774
     * prefix if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8775
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8776
     *     a.length != b.length &&
33843
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  8777
     *     Arrays.equals(a, 0, Math.min(a.length, b.length),
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  8778
     *                   b, 0, Math.min(a.length, b.length),
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  8779
     *                   cmp)
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8780
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8781
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8782
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8783
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8784
     * @param cmp the comparator to compare array elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8785
     * @param <T> the type of array elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8786
     * @return the index of the first mismatch between the two arrays,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8787
     *         otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8788
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8789
     *         if either array or the comparator is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8790
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8791
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8792
    public static <T> int mismatch(T[] a, T[] b, Comparator<? super T> cmp) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8793
        Objects.requireNonNull(cmp);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8794
        int length = Math.min(a.length, b.length); // Check null array refs
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8795
        if (a == b)
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8796
            return -1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8797
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8798
        for (int i = 0; i < length; i++) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8799
            T oa = a[i];
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8800
            T ob = b[i];
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8801
            if (oa != ob) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8802
                // Null-value comparison is deferred to the comparator
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8803
                int v = cmp.compare(oa, ob);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8804
                if (v != 0) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8805
                    return i;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8806
                }
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
        return a.length != b.length ? length : -1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8811
    }
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
     * Finds and returns the relative index of the first mismatch between two
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8815
     * {@code Object} arrays over the specified ranges, otherwise return -1 if
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8816
     * 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
  8817
     * to the length (inclusive) of the smaller range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8818
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8819
     * <p>If the two arrays, over the specified ranges, share a common prefix
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8820
     * then the returned relative index is the length of the common prefix and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8821
     * it follows that there is a mismatch between the two elements at that
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8822
     * relative index within the respective arrays.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8823
     * 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
  8824
     * then the returned relative index is the length of the smaller range and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8825
     * 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
  8826
     * larger range.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8827
     * Otherwise, there is no mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8828
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8829
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8830
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8831
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8832
     * prefix of length {@code pl} if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8833
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8834
     *     pl >= 0 &&
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8835
     *     pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&
33843
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  8836
     *     Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl, cmp) &&
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8837
     *     cmp.compare(a[aFromIndex + pl], b[bFromIndex + pl]) != 0
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8838
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8839
     * Note that a common prefix length of {@code 0} indicates that the first
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8840
     * elements from each array mismatch.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8841
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8842
     * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8843
     * ranges [{@code aFromIndex}, {@code atoIndex}) and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8844
     * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8845
     * if the following expression is true:
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8846
     * <pre>{@code
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8847
     *     (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&
33843
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  8848
     *     Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  8849
     *                   b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
8fdd836f35bc 8141409: Arrays.equals accepting a Comparator
psandoz
parents: 33666
diff changeset
  8850
     *                   cmp)
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8851
     * }</pre>
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8852
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8853
     * @param a the first array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8854
     * @param aFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8855
     *                   first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8856
     * @param aToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8857
     *                 first array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8858
     * @param b the second array to be tested for a mismatch
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8859
     * @param bFromIndex the index (inclusive) of the first element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8860
     *                   second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8861
     * @param bToIndex the index (exclusive) of the last element in the
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8862
     *                 second array to be tested
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8863
     * @param cmp the comparator to compare array elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8864
     * @param <T> the type of array elements
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8865
     * @return the relative index of the first mismatch between the two arrays
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8866
     *         over the specified ranges, otherwise {@code -1}.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8867
     * @throws IllegalArgumentException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8868
     *         if {@code aFromIndex > aToIndex} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8869
     *         if {@code bFromIndex > bToIndex}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8870
     * @throws ArrayIndexOutOfBoundsException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8871
     *         if {@code aFromIndex < 0 or aToIndex > a.length} or
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8872
     *         if {@code bFromIndex < 0 or bToIndex > b.length}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8873
     * @throws NullPointerException
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8874
     *         if either array or the comparator is {@code null}
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8875
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8876
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8877
    public static <T> int mismatch(
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8878
            T[] a, int aFromIndex, int aToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8879
            T[] b, int bFromIndex, int bToIndex,
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8880
            Comparator<? super T> cmp) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8881
        Objects.requireNonNull(cmp);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8882
        rangeCheck(a.length, aFromIndex, aToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8883
        rangeCheck(b.length, bFromIndex, bToIndex);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8884
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8885
        int aLength = aToIndex - aFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8886
        int bLength = bToIndex - bFromIndex;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8887
        int length = Math.min(aLength, bLength);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8888
        for (int i = 0; i < length; i++) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8889
            T oa = a[aFromIndex++];
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8890
            T ob = b[bFromIndex++];
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8891
            if (oa != ob) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8892
                // Null-value comparison is deferred to the comparator
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8893
                int v = cmp.compare(oa, ob);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8894
                if (v != 0) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8895
                    return i;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8896
                }
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
        return aLength != bLength ? length : -1;
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 32108
diff changeset
  8901
    }
44743
f0bbd698c486 8177789: fix collections framework links to point to java.util package doc
smarks
parents: 37685
diff changeset
  8902
}